]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: make __reference_*_from_temporary honor access [PR120529]
authorMarek Polacek <polacek@redhat.com>
Thu, 20 Nov 2025 18:57:43 +0000 (13:57 -0500)
committerMarek Polacek <polacek@redhat.com>
Fri, 21 Nov 2025 14:12:59 +0000 (09:12 -0500)
This PR reports that our __reference_*_from_temporary ignore access
control.  The reason is that we only check if implicit_conversion
works, but not if the conversion can actually be performed, via
convert_like.

PR c++/120529

gcc/cp/ChangeLog:

* call.cc (ref_conv_binds_to_temporary): Don't ignore access control.

gcc/testsuite/ChangeLog:

* g++.dg/ext/reference_xes_from_temporary1.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/call.cc
gcc/testsuite/g++.dg/ext/reference_xes_from_temporary1.C [new file with mode: 0644]

index f80d597b33944a0d3ce9164229f1205c3cbc3160..ea89130572b8ba9c9c3b38845aa56c869084dd0a 100644 (file)
@@ -10203,11 +10203,19 @@ ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/)
   const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT;
   conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr,
                                          /*c_cast_p=*/false, flags, tf_none);
-  tristate ret (tristate::TS_UNKNOWN);
-  if (conv && !conv->bad_p)
-    ret = tristate (conv_binds_ref_to_temporary (conv));
+  if (!conv || conv->bad_p)
+    return tristate::unknown ();
 
-  return ret;
+  if (conv_binds_ref_to_temporary (conv))
+    {
+      /* Actually perform the conversion to check access control.  */
+      if (convert_like (conv, expr, tf_none) != error_mark_node)
+       return tristate (true);
+      else
+       return tristate::unknown ();
+    }
+
+  return tristate (false);
 }
 
 /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
diff --git a/gcc/testsuite/g++.dg/ext/reference_xes_from_temporary1.C b/gcc/testsuite/g++.dg/ext/reference_xes_from_temporary1.C
new file mode 100644 (file)
index 0000000..274de05
--- /dev/null
@@ -0,0 +1,28 @@
+// PR c++/120529
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+class Dst {};
+
+class Src {
+private:
+    operator Dst() const;
+};
+
+class Src2 {
+protected:
+    operator Dst() const;
+};
+
+class Src3 {
+public:
+    operator Dst() const;
+};
+
+SA (!__reference_converts_from_temporary (Dst&&, Src));
+SA (!__reference_constructs_from_temporary (Dst&&, Src));
+SA (!__reference_converts_from_temporary (Dst&&, Src2));
+SA (!__reference_constructs_from_temporary (Dst&&, Src2));
+SA (__reference_converts_from_temporary (Dst&&, Src3));
+SA (__reference_constructs_from_temporary (Dst&&, Src3));