[clang] 5a1c504 - [clang] fix constexpr code generation for user conversions.

Matheus Izvekov via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 8 11:23:26 PDT 2021


Author: Matheus Izvekov
Date: 2021-07-08T20:23:19+02:00
New Revision: 5a1c50410ccc1973a1a0a4acca0c01677c28e9b6

URL: https://github.com/llvm/llvm-project/commit/5a1c50410ccc1973a1a0a4acca0c01677c28e9b6
DIFF: https://github.com/llvm/llvm-project/commit/5a1c50410ccc1973a1a0a4acca0c01677c28e9b6.diff

LOG: [clang] fix constexpr code generation for user conversions.

When building the member call to a user conversion function during an
implicit cast, the expression was not being checked for immediate
invocation, so we were never adding the ConstantExpr node to AST.

This would cause the call to the user conversion operator to be emitted
even if it was constantexpr evaluated, and this would even trip an
assert when said user conversion was declared consteval:
`Assertion failed: !cast<FunctionDecl>(GD.getDecl())->isConsteval() && "consteval function should never be emitted", file clang\lib\CodeGen\CodeGenModule.cpp, line 3530`

Fixes PR48855.

Signed-off-by: Matheus Izvekov <mizvekov at gmail.com>

Reviewed By: rsmith

Differential Revision: https://reviews.llvm.org/D105446

Added: 
    

Modified: 
    clang/lib/AST/Expr.cpp
    clang/lib/Sema/SemaExprCXX.cpp
    clang/test/CodeGenCXX/cxx2a-consteval.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 03dc65eeb6b03..842b9c0a8a0e2 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -1913,6 +1913,7 @@ Expr *CastExpr::getSubExprAsWritten() {
       SubExpr =
         skipImplicitTemporary(cast<CXXConstructExpr>(SubExpr->IgnoreImplicit())->getArg(0));
     else if (E->getCastKind() == CK_UserDefinedConversion) {
+      SubExpr = SubExpr->IgnoreImplicit();
       assert((isa<CXXMemberCallExpr>(SubExpr) ||
               isa<BlockExpr>(SubExpr)) &&
              "Unexpected SubExpr for CK_UserDefinedConversion.");

diff  --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index b0b6b3dca5f6a..2e9e9a4a88dff 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -7786,7 +7786,7 @@ ExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl,
                         Method->getType()->castAs<FunctionProtoType>()))
     return ExprError();
 
-  return CE;
+  return CheckForImmediateInvocation(CE, CE->getMethodDecl());
 }
 
 ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand,

diff  --git a/clang/test/CodeGenCXX/cxx2a-consteval.cpp b/clang/test/CodeGenCXX/cxx2a-consteval.cpp
index e5bd99c6ca537..7edddde693ca2 100644
--- a/clang/test/CodeGenCXX/cxx2a-consteval.cpp
+++ b/clang/test/CodeGenCXX/cxx2a-consteval.cpp
@@ -210,3 +210,36 @@ long test_AggCtor() {
   AggCtor C(i);
   return C.a + C.b;
 }
+
+struct UserConv {
+  consteval operator int() const noexcept { return 42; }
+};
+
+// EVAL-FN-LABEL: @_Z13test_UserConvv(
+// EVAL-FN-NEXT:  entry:
+// EVAL-FN-NEXT:    ret i32 42
+//
+int test_UserConv() {
+  return UserConv();
+}
+
+int test_UserConvOverload_helper(int a) { return a; }
+
+// EVAL-FN-LABEL: @_Z21test_UserConvOverloadv(
+// EVAL-FN-NEXT:  entry:
+// EVAL-FN-NEXT:    %call = call i32 @_Z28test_UserConvOverload_helperi(i32 42)
+// EVAL-FN-NEXT:    ret i32 %call
+//
+int test_UserConvOverload() {
+  return test_UserConvOverload_helper(UserConv());
+}
+
+consteval int test_UserConvOverload_helper_ceval(int a) { return a; }
+
+// EVAL-FN-LABEL: @_Z27test_UserConvOverload_cevalv(
+// EVAL-FN-NEXT:  entry:
+// EVAL-FN-NEXT:    ret i32 42
+//
+int test_UserConvOverload_ceval() {
+  return test_UserConvOverload_helper_ceval(UserConv());
+}


        


More information about the cfe-commits mailing list