[gcc r16-5489] c++: make __reference_*_from_temporary honor access [PR120529]
Marek Polacek
mpolacek@gcc.gnu.org
Fri Nov 21 14:17:09 GMT 2025
https://gcc.gnu.org/g:e82a8e51848b5a74edb530f7597861f7080662c3
commit r16-5489-ge82a8e51848b5a74edb530f7597861f7080662c3
Author: Marek Polacek <polacek@redhat.com>
Date: Thu Nov 20 13:57:43 2025 -0500
c++: make __reference_*_from_temporary honor access [PR120529]
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>
Diff:
---
gcc/cp/call.cc | 16 +++++++++----
.../g++.dg/ext/reference_xes_from_temporary1.C | 28 ++++++++++++++++++++++
2 files changed, 40 insertions(+), 4 deletions(-)
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index f80d597b3394..ea89130572b8 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -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
index 000000000000..274de05e7b7a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/reference_xes_from_temporary1.C
@@ -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));
More information about the Gcc-cvs
mailing list