Skip to content

[Clang] Implement C++26 P2748R5 "Disallow Binding a Returned Glvalue to a Temporary" #89942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ C++2c Feature Support

- Implemented `P0609R3: Attributes for Structured Bindings <https://wg21.link/P0609R3>`_

- Implemented `P2748R5 Disallow Binding a Returned Glvalue to a Temporary <https://wg21.link/P2748R5>`_.

Resolutions to C++ Defect Reports
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -9950,6 +9950,8 @@ def warn_ret_stack_addr_ref : Warning<
def warn_ret_local_temp_addr_ref : Warning<
"returning %select{address of|reference to}0 local temporary object">,
InGroup<ReturnStackAddress>;
def err_ret_local_temp_ref : Error<
"returning reference to local temporary object">;
def warn_ret_addr_label : Warning<
"returning address of label, which is local">,
InGroup<ReturnStackAddress>;
Expand Down
13 changes: 11 additions & 2 deletions clang/lib/Sema/SemaInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8340,8 +8340,17 @@ void Sema::checkInitializerLifetime(const InitializedEntity &Entity,
<< Entity.getType()->isReferenceType() << CLE->getInitializer() << 2
<< DiagRange;
} else {
Diag(DiagLoc, diag::warn_ret_local_temp_addr_ref)
<< Entity.getType()->isReferenceType() << DiagRange;
// P2748R5: Disallow Binding a Returned Glvalue to a Temporary.
// [stmt.return]/p6: In a function whose return type is a reference,
// other than an invented function for std::is_convertible ([meta.rel]),
// a return statement that binds the returned reference to a temporary
// expression ([class.temporary]) is ill-formed.
if (getLangOpts().CPlusPlus26 && Entity.getType()->isReferenceType())
Diag(DiagLoc, diag::err_ret_local_temp_ref)
<< Entity.getType()->isReferenceType() << DiagRange;
else
Diag(DiagLoc, diag::warn_ret_local_temp_addr_ref)
<< Entity.getType()->isReferenceType() << DiagRange;
}
break;
}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CXX/drs/cwg650.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// RUN: %clang_cc1 -std=c++17 %s -triple x86_64-linux-gnu -emit-llvm -o - -fexceptions -fcxx-exceptions -pedantic-errors | llvm-cxxfilt -n | FileCheck %s --check-prefixes CHECK
// RUN: %clang_cc1 -std=c++20 %s -triple x86_64-linux-gnu -emit-llvm -o - -fexceptions -fcxx-exceptions -pedantic-errors | llvm-cxxfilt -n | FileCheck %s --check-prefixes CHECK
// RUN: %clang_cc1 -std=c++23 %s -triple x86_64-linux-gnu -emit-llvm -o - -fexceptions -fcxx-exceptions -pedantic-errors | llvm-cxxfilt -n | FileCheck %s --check-prefixes CHECK
// RUN: %clang_cc1 -std=c++2c %s -triple x86_64-linux-gnu -emit-llvm -o - -fexceptions -fcxx-exceptions -pedantic-errors | llvm-cxxfilt -n | FileCheck %s --check-prefixes CHECK
// We aren't testing this since C++26 because of P2748R5 "Disallow Binding a Returned Glvalue to a Temporary".

#if __cplusplus == 199711L
#define NOTHROW throw()
Expand Down
25 changes: 25 additions & 0 deletions clang/test/CXX/stmt.stmt/stmt.return/p6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %clang_cc1 -std=c++26 -fsyntax-only -verify %s

auto&& f1() {
return 42; // expected-error{{returning reference to local temporary object}}
}
const double& f2() {
static int x = 42;
return x; // expected-error{{returning reference to local temporary object}}
}
auto&& id(auto&& r) {
return static_cast<decltype(r)&&>(r);
}
auto&& f3() {
return id(42); // OK, but probably a bug
}

void unevaluated() {
using a = decltype ([] () -> const int & {
const int &i = 0; // expected-note {{binding reference variable 'i' here}}
return i; // expected-error{{returning reference to local temporary object}}
} ());
}

static_assert(__is_convertible(int, const int &));
static_assert(__is_nothrow_convertible(int, const int &));
28 changes: 28 additions & 0 deletions clang/test/SemaCXX/type-traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2509,6 +2509,20 @@ void is_convertible()
static_assert(__is_convertible(FloatWrapper, IntWrapper));
static_assert(__is_convertible(FloatWrapper, float));
static_assert(__is_convertible(float, FloatWrapper));
static_assert(__is_convertible(IntWrapper, IntWrapper&&));
static_assert(__is_convertible(IntWrapper, const IntWrapper&));
static_assert(__is_convertible(IntWrapper, int&&));
static_assert(__is_convertible(IntWrapper, const int&));
static_assert(__is_convertible(int, IntWrapper&&));
static_assert(__is_convertible(int, const IntWrapper&));
static_assert(__is_convertible(IntWrapper, FloatWrapper&&));
static_assert(__is_convertible(IntWrapper, const FloatWrapper&));
static_assert(__is_convertible(FloatWrapper, IntWrapper&&));
static_assert(__is_convertible(FloatWrapper, const IntWrapper&&));
static_assert(__is_convertible(FloatWrapper, float&&));
static_assert(__is_convertible(FloatWrapper, const float&));
static_assert(__is_convertible(float, FloatWrapper&&));
static_assert(__is_convertible(float, const FloatWrapper&));
}

void is_nothrow_convertible()
Expand All @@ -2521,6 +2535,20 @@ void is_nothrow_convertible()
static_assert(!__is_nothrow_convertible(FloatWrapper, IntWrapper));
static_assert(!__is_nothrow_convertible(FloatWrapper, float));
static_assert(__is_nothrow_convertible(float, FloatWrapper));
static_assert(__is_nothrow_convertible(IntWrapper, IntWrapper&&));
static_assert(__is_nothrow_convertible(IntWrapper, const IntWrapper&));
static_assert(__is_nothrow_convertible(IntWrapper, int&&));
static_assert(__is_nothrow_convertible(IntWrapper, const int&));
static_assert(!__is_nothrow_convertible(int, IntWrapper&&));
static_assert(!__is_nothrow_convertible(int, const IntWrapper&));
static_assert(!__is_nothrow_convertible(IntWrapper, FloatWrapper&&));
static_assert(!__is_nothrow_convertible(IntWrapper, const FloatWrapper&));
static_assert(!__is_nothrow_convertible(FloatWrapper, IntWrapper&&));
static_assert(!__is_nothrow_convertible(FloatWrapper, const IntWrapper&));
static_assert(!__is_nothrow_convertible(FloatWrapper, float&&));
static_assert(!__is_nothrow_convertible(FloatWrapper, const float&));
static_assert(__is_nothrow_convertible(float, FloatWrapper&&));
static_assert(__is_nothrow_convertible(float, const FloatWrapper&));
}

struct FromInt { FromInt(int); };
Expand Down
2 changes: 1 addition & 1 deletion clang/www/cxx_status.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ <h2 id="cxx26">C++2c implementation status</h2>
<tr>
<td>Disallow Binding a Returned Glvalue to a Temporary</td>
<td><a href="https://wg21.link/P2748R5">P2748R5</a></td>
<td class="none" align="center">No</td>
<td class="full" align="center">Clang 19</td>
</tr>
<tr>
<td>Clarifying rules for brace elision in aggregate initialization</td>
Expand Down
Loading