llvm-project/clang/test/SemaCXX/constexpr-default-arg.cpp
yronglin 060137038a
Reapply "[Clang][CWG1815] Support lifetime extension of temporary created by aggregate initialization using a default member initializer" (#108039)
The PR reapply https://github.com/llvm/llvm-project/pull/97308. 

- Implement [CWG1815](https://wg21.link/CWG1815): Support lifetime
extension of temporary created by aggregate initialization using a
default member initializer.

- Fix crash that introduced in
https://github.com/llvm/llvm-project/pull/97308. In
`InitListChecker::FillInEmptyInitForField`, when we enter
rebuild-default-init context, we copy all the contents of the parent
context to the current context, which will cause the `MaybeODRUseExprs`
to be lost. But we don't need to copy the entire context, only the
`DelayedDefaultInitializationContext` was required, which is used to
build `SourceLocExpr`, etc.

---------

Signed-off-by: yronglin <yronglin777@gmail.com>
2024-09-12 06:29:48 +08:00

40 lines
1.1 KiB
C++

// RUN: %clang_cc1 -std=c++1y -o - -emit-llvm -verify %s
// RUN: %clang_cc1 -std=c++1y -fexperimental-new-constant-interpreter -o - -emit-llvm -verify %s
namespace default_arg_temporary {
constexpr bool equals(const float& arg = 1.0f) {
return arg == 1.0f;
}
constexpr const int &x(const int &p = 0) {
return p;
}
struct S {
constexpr S(const int &a = 0) {}
};
void test_default_arg2() {
// This piece of code used to cause an assertion failure in
// CallStackFrame::createTemporary because the same MTE is used to initilize
// both elements of the array (see PR33140).
constexpr S s[2] = {};
// This piece of code used to cause an assertion failure in
// CallStackFrame::createTemporary because multiple CXXDefaultArgExpr share
// the same MTE (see PR33140).
static_assert(equals() && equals(), "");
// Test that constant expression evaluation produces distinct lvalues for
// each call.
static_assert(&x() != &x(), "");
}
// Check that multiple CXXDefaultInitExprs don't cause an assertion failure.
struct A { int &&r = 0; };
struct B { A x, y; };
B b = {}; // expected-no-diagnostics
}