]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: C++26 returning reference to temporary
authorJason Merrill <jason@redhat.com>
Fri, 29 Mar 2024 01:33:57 +0000 (21:33 -0400)
committerJason Merrill <jason@redhat.com>
Mon, 1 Apr 2024 16:02:50 +0000 (12:02 -0400)
P2748R5 makes it ill-formed to return a reference to temporary in C++26;
implementing this is a simple matter of changing the existing warning to a
permerror.

For most of the tests I just changed dg-warning to dg-message to accept
both; I test the specific diagnostic type in Wreturn-local-addr-5.C.

gcc/cp/ChangeLog:

* typeck.cc (maybe_warn_about_returning_address_of_local):
Permerror in C++26.

gcc/testsuite/ChangeLog:

* g++.dg/conversion/pr16333.C: Change dg-warning to dg-message.
* g++.dg/cpp0x/constexpr-48324.C
* g++.dg/other/pr94326.C
* g++.dg/warn/Wreturn-local-addr-2.C
* g++.old-deja/g++.jason/warning8.C: Likewise.
* g++.dg/cpp1y/auto-fn6.C: Check that others don't complain.
* g++.dg/warn/Wreturn-local-addr-5.C: Expect error in C++26.

gcc/cp/typeck.cc
gcc/testsuite/g++.dg/conversion/pr16333.C
gcc/testsuite/g++.dg/cpp0x/constexpr-48324.C
gcc/testsuite/g++.dg/cpp1y/auto-fn6.C
gcc/testsuite/g++.dg/other/pr94326.C
gcc/testsuite/g++.dg/warn/Wreturn-local-addr-2.C
gcc/testsuite/g++.dg/warn/Wreturn-local-addr-5.C
gcc/testsuite/g++.old-deja/g++.jason/warning8.C

index f5a0a2273be25f50b73ed3ec46914ce4ea2b7d7e..88ed38e4f30c5b4e77210ed7df6b3b2c24548897 100644 (file)
@@ -10626,8 +10626,10 @@ maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
       || TREE_CODE (whats_returned) == TARGET_EXPR)
     {
       if (TYPE_REF_P (valtype))
-       warning_at (loc, OPT_Wreturn_local_addr,
-                   "returning reference to temporary");
+       /* P2748 made this an error in C++26.  */
+       emit_diagnostic (cxx_dialect >= cxx26 ? DK_PERMERROR : DK_WARNING,
+                        loc, OPT_Wreturn_local_addr,
+                        "returning reference to temporary");
       else if (TYPE_PTR_P (valtype))
        warning_at (loc, OPT_Wreturn_local_addr,
                    "returning pointer to temporary");
index a00bc5c167c4ed6250e4160b2614e742a271b8ee..d0049304790320059f2dab48293f1357856b2685 100644 (file)
@@ -6,5 +6,5 @@ struct X {
 
 int a[3];
 X foo1 () { return a; }
-const X &foo2 () { return a; } // { dg-warning "returning reference to temporary" }
+const X &foo2 () { return a; } // { dg-message "returning reference to temporary" }
 X &foo3 () { return a; } // { dg-error "cannot bind non-const lvalue ref" }
index 37ed0e1d163c6b9db2865b759e847f6839cd4c68..f53fd4ddbd3a17f8da49a19d9381938f028ec3e5 100644 (file)
@@ -7,7 +7,7 @@ struct S {
 };
 
 constexpr const int& to_ref(int i) {
-  return S(i).val; // { dg-warning "reference to temporary" }
+  return S(i).val; // { dg-message "reference to temporary" }
 }
 
 constexpr int ary[to_ref(98)] = { }; // { dg-error "25:size of array .ary. is not an integral" }
index 17ca6f22c50e361a82326ec924900b5390aa5fe6..7fada180841301834b94b28f7c3b9752796a656a 100644 (file)
@@ -8,11 +8,11 @@ char& g(char);
 double&& g(double);
 
 template <class T> auto&& f(T t)
-{ return g(t); }               // { dg-warning "reference to temporary" }
+{ return g(t); }               // { dg-message "reference to temporary" }
 
 int main()
 {
-  ST<decltype(f(1)),int&&>();
-  ST<decltype(f('\0')),char&>();
-  ST<decltype(f(1.0)),double&&>();
+  ST<decltype(f(1)),int&&>();  // { dg-message "required from here" }
+  ST<decltype(f('\0')),char&>(); // { dg-bogus "required from here" }
+  ST<decltype(f(1.0)),double&&>(); // { dg-bogus "required from here" }
 }
index 4069c038d529cb1ee9429e495f9bd7bd2cae3d2c..5df72a648fd9da78ee68bc0c624dc69db3c004b0 100644 (file)
@@ -3,7 +3,7 @@
 // { dg-options "-fcompare-debug" }
 
 template <typename = int> struct A {
-  const int &foo() { return 0; }       // { dg-warning "returning reference to temporary" }
+  const int &foo() { return 0; }       // { dg-message "returning reference to temporary" }
   template <typename _Kt> void bar(_Kt) { foo(); }
 };
 struct B {
index c483601e6b23e866e21afb7dd2462f7122091e4a..a9f984c153d773fa4f1db9f31a1b4abc57540d3b 100644 (file)
@@ -3,9 +3,9 @@
 struct Base2 { int m_foo; };
 struct Derived2 : public Base2 {};
 
-const Base2& f8() { return Derived2(); } // { dg-warning "reference to temporary" }
+const Base2& f8() { return Derived2(); } // { dg-message "reference to temporary" }
 
 struct foo { };
 struct bar { foo base; };
 
-const foo& f9() { return bar().base; } // { dg-warning "reference to temporary" }
+const foo& f9() { return bar().base; } // { dg-message "reference to temporary" }
index 76096279a641ee1fb806d5105884ab200c940ad2..69f1e682363905c5024c39281b1e5376c278c019 100644 (file)
@@ -5,4 +5,5 @@
 
 int&& f() { int i = 0; return std::move(i); } // { dg-warning "reference to local variable" }
 int&& g() { int i = 0; return std::forward<int>(i); } // { dg-warning "reference to local variable" }
-int&& h() { long l = 0; return std::forward<int>(l); } // { dg-warning "reference to temporary" }
+int&& h() { long l = 0; return std::forward<int>(l); } // { dg-warning "reference to temporary" "" { target { ! c++26 } } }
+// { dg-error "reference to temporary" "" { target c++26 } .-1 }
index b5ff3f3de60452489f9430fa03074ef6797d8657..23830716ab46f1456b35bed10a909a969fd7d1f6 100644 (file)
@@ -13,5 +13,5 @@ struct B {
 const B& f ()
 {
   A a;
-  return a;                    // { dg-warning "" } returning reference to temporary
+  return a;                    // { dg-message "" } returning reference to temporary
 }