[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@gmail.com>

Reviewed By: rsmith

Differential Revision: https://reviews.llvm.org/D105446
This commit is contained in:
Matheus Izvekov 2021-07-06 00:24:43 +02:00
parent de5582be26
commit 5a1c50410c
3 changed files with 35 additions and 1 deletions

View File

@ -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.");

View File

@ -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,

View File

@ -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());
}