mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-17 08:36:41 +00:00

Unlike brace initialization, the parenthesized aggregate initialization in C++20 does not extend the lifetime of a temporary object bound to a reference in an aggreate. This can lead to dangling references: ``` struct A { const int& r; }; A a1(1); // well-formed, but results in a dangling reference. ``` With this patch, clang will diagnose this common dangling issues. Fixes #101957
60 lines
1.4 KiB
C++
60 lines
1.4 KiB
C++
// RUN: %clang_cc1 -verify -std=c++20 %s -fsyntax-only
|
|
|
|
namespace std {
|
|
template <class T> struct remove_reference { typedef T type; };
|
|
template <class T> struct remove_reference<T&> { typedef T type; };
|
|
template <class T> struct remove_reference<T&&> { typedef T type; };
|
|
|
|
template <class T> typename remove_reference<T>::type &&move(T &&t);
|
|
} // namespace std
|
|
|
|
// dcl.init 16.6.2.2
|
|
struct A {
|
|
int a;
|
|
int&& r;
|
|
};
|
|
|
|
int f();
|
|
int n = 10;
|
|
|
|
A a1{1, f()}; // OK, lifetime is extended for direct-list-initialization
|
|
// well-formed, but dangling reference
|
|
A a2(1, f()); // expected-warning {{temporary whose address is used as value}}
|
|
// well-formed, but dangling reference
|
|
A a4(1.0, 1); // expected-warning {{temporary whose address is used as value}}
|
|
A a5(1.0, std::move(n)); // OK
|
|
|
|
|
|
|
|
struct B {
|
|
const int& r;
|
|
};
|
|
B test(int local) {
|
|
return B(1); // expected-warning {{returning address}}
|
|
return B(local); // expected-warning {{address of stack memory}}
|
|
}
|
|
|
|
void f(B b);
|
|
void test2(int local) {
|
|
// No diagnostic on the following cases where both the aggregate object and
|
|
// temporary end at the end of the full expression.
|
|
f(B(1));
|
|
f(B(local));
|
|
}
|
|
|
|
// Test nested struct.
|
|
struct C {
|
|
B b;
|
|
};
|
|
|
|
struct D {
|
|
C c;
|
|
};
|
|
|
|
C c1(B(
|
|
1 // expected-warning {{temporary whose address is used as value}}
|
|
));
|
|
D d1(C(B(
|
|
1 // expected-warning {{temporary whose address is used as value}}
|
|
)));
|