Skip to content

Commit 83c90e6

Browse files
committed
[clang] Fix consteval initializers of temporaries
When a potential immediate invocation is met, it is immediately wrapped by a `ConstantExpr`. There is also a TreeTransform that removes this `ConstantExpr` wrapper when corresponding expression evaluation context is popped. So, in case initializer was an immediate invocation, `CXXTemporaryObjectExpr` was wrapped by a `ConstantExpr`, and that caused additional unnecessary `CXXFunctionalCastExpr` to be added, which later confused the TreeTransform that rebuilds immediate invocations, so it was adding unnecessary constructor call. Fixes #60286 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D146801
1 parent c6ec6e3 commit 83c90e6

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ Bug Fixes in This Version
238238
the initializer, so it behaves consistently with other ``VarDecls`` and ends
239239
on the last token of initializer, instead of right angle bracket of
240240
the template argument list.
241+
- Fix false-positive diagnostic issued for consteval initializers of temporary
242+
objects.
243+
(`#60286 <https://github.com/llvm/llvm-project/issues/60286>`_)
241244

242245
Bug Fixes to Compiler Builtins
243246
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,9 @@ Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo,
15901590
Expr *Inner = Result.get();
15911591
if (CXXBindTemporaryExpr *BTE = dyn_cast_or_null<CXXBindTemporaryExpr>(Inner))
15921592
Inner = BTE->getSubExpr();
1593+
if (auto *CE = dyn_cast<ConstantExpr>(Inner);
1594+
CE && CE->isImmediateInvocation())
1595+
Inner = CE->getSubExpr();
15931596
if (!isa<CXXTemporaryObjectExpr>(Inner) &&
15941597
!isa<CXXScalarValueInitExpr>(Inner)) {
15951598
// If we created a CXXTemporaryObjectExpr, that node also represents the

clang/test/SemaCXX/cxx2a-consteval.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,3 +1050,18 @@ void test() {
10501050

10511051
}
10521052
}
1053+
1054+
namespace GH60286 {
1055+
1056+
struct A {
1057+
int i = 0;
1058+
1059+
consteval A() {}
1060+
A(const A&) { i = 1; }
1061+
consteval int f() { return i; }
1062+
};
1063+
1064+
constexpr auto B = A{A{}}.f();
1065+
static_assert(B == 0);
1066+
1067+
}

0 commit comments

Comments
 (0)