From 611bd6e2dbf0acff3a80282675b7fcf3afe3ec84 Mon Sep 17 00:00:00 2001 From: Dan Sarginson Date: Thu, 4 Mar 2021 17:58:23 +0000 Subject: [PATCH] [Pattern Matching] Fix bug causing incorrect diagnostic Incorrect diagnostic can be emitted when using statement expressions as a pattern substatement in an inspect with a trailing `void` return type. Add a specific check for the void return type. All other cases are handled when we check for convertibility later in semantic analysis. fixes #19 --- clang/lib/Sema/SemaExpr.cpp | 13 +++++++++++-- clang/test/SemaCXX/inspect.cpp | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 33354df69575c4..62e108ef07efb4 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -16744,8 +16744,17 @@ ExprResult Sema::ActOnStmtExprResult(ExprResult ER) { // to perform implicit conversion during the copy initialization. if (getCurScope()->isPatternScope()) { InspectExpr *IE = getCurFunction()->InspectStack.back().getPointer(); - if (IE->hasExplicitResultType()) - T = IE->getType(); + if (IE->hasExplicitResultType()) { + QualType ER = IE->getType(); + + // Catch this edge case where we can't make a valid + // statement expression result from a void type. + // All other cases should be caught elsewhere when + // we explore convertibility between the pattern + // substatement and the inspect return type. + if (!ER.getTypePtr()->isVoidType()) + T = ER; + } } // FIXME: Provide a better location for the initialization. diff --git a/clang/test/SemaCXX/inspect.cpp b/clang/test/SemaCXX/inspect.cpp index 8ed814d1556f84..c200405a7d6ae3 100644 --- a/clang/test/SemaCXX/inspect.cpp +++ b/clang/test/SemaCXX/inspect.cpp @@ -17,7 +17,7 @@ void a2() { void b(int x) { inspect(x) -> void { - __ => 3; // expected-error {{cannot initialize statement expression result of type 'void' with an rvalue of type 'int'}} + __ => 3; // expected-error {{resulting expression type 'int' must match trailing result type 'void'}} }; }