diff options
author | Marek Polacek <polacek@redhat.com> | 2022-09-26 10:21:38 -0400 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2022-09-26 12:20:38 -0400 |
commit | be4b32b9ef69b86b662cb7511b48cd1048a55403 (patch) | |
tree | b931de83ee678a47a7085e09152a6bacdf93adc1 /gcc | |
parent | 099a66498bf7a40764002793eba66c881a251b76 (diff) | |
download | gcc-be4b32b9ef69b86b662cb7511b48cd1048a55403.zip gcc-be4b32b9ef69b86b662cb7511b48cd1048a55403.tar.gz gcc-be4b32b9ef69b86b662cb7511b48cd1048a55403.tar.bz2 |
c++: Instantiate less when evaluating __is_convertible
Jon reported that evaluating __is_convertible in a test led to
instantiating something ill-formed and so we failed to compile the test.
__is_convertible doesn't and shouldn't need to instantiate so much, so
let's limit it with a cp_unevaluated guard. Use a helper function to
implement both built-ins.
PR c++/106784
gcc/cp/ChangeLog:
* method.cc (is_convertible_helper): New.
(is_convertible): Use it.
(is_nothrow_convertible): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/is_convertible3.C: New test.
* g++.dg/ext/is_nothrow_convertible3.C: New test.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/method.cc | 23 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/ext/is_convertible3.C | 9 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/ext/is_nothrow_convertible3.C | 9 |
3 files changed, 33 insertions, 8 deletions
diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc index c35a59f..9f917f1 100644 --- a/gcc/cp/method.cc +++ b/gcc/cp/method.cc @@ -2236,6 +2236,19 @@ ref_xes_from_temporary (tree to, tree from, bool direct_init_p) return ref_conv_binds_directly (to, val, direct_init_p).is_false (); } +/* Worker for is_{,nothrow_}convertible. Attempt to perform an implicit + conversion from FROM to TO and return the result. */ + +static tree +is_convertible_helper (tree from, tree to) +{ + if (VOID_TYPE_P (from) && VOID_TYPE_P (to)) + return integer_one_node; + cp_unevaluated u; + tree expr = build_stub_object (from); + return perform_implicit_conversion (to, expr, tf_none); +} + /* Return true if FROM can be converted to TO using implicit conversions, or both FROM and TO are possibly cv-qualified void. NB: This doesn't implement the "Access checks are performed as if from a context unrelated @@ -2244,10 +2257,7 @@ ref_xes_from_temporary (tree to, tree from, bool direct_init_p) bool is_convertible (tree from, tree to) { - if (VOID_TYPE_P (from) && VOID_TYPE_P (to)) - return true; - tree expr = build_stub_object (from); - expr = perform_implicit_conversion (to, expr, tf_none); + tree expr = is_convertible_helper (from, to); if (expr == error_mark_node) return false; return !!expr; @@ -2258,10 +2268,7 @@ is_convertible (tree from, tree to) bool is_nothrow_convertible (tree from, tree to) { - if (VOID_TYPE_P (from) && VOID_TYPE_P (to)) - return true; - tree expr = build_stub_object (from); - expr = perform_implicit_conversion (to, expr, tf_none); + tree expr = is_convertible_helper (from, to); if (expr == NULL_TREE || expr == error_mark_node) return false; return expr_noexcept_p (expr, tf_none); diff --git a/gcc/testsuite/g++.dg/ext/is_convertible3.C b/gcc/testsuite/g++.dg/ext/is_convertible3.C new file mode 100644 index 0000000..7a986d0 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_convertible3.C @@ -0,0 +1,9 @@ +// PR c++/106784 +// { dg-do compile { target c++11 } } +// Make sure we don't reject this at runtime by trying to instantiate +// something that would be ill-formed. + +struct A; +struct B { template<class T> B(const T&) noexcept { T::nonexistent; } }; + +static_assert(__is_convertible(const A&, B), ""); diff --git a/gcc/testsuite/g++.dg/ext/is_nothrow_convertible3.C b/gcc/testsuite/g++.dg/ext/is_nothrow_convertible3.C new file mode 100644 index 0000000..05b1e1d --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_nothrow_convertible3.C @@ -0,0 +1,9 @@ +// PR c++/106784 +// { dg-do compile { target c++11 } } +// Make sure we don't reject this at runtime by trying to instantiate +// something that would be ill-formed. + +struct A; +struct B { template<class T> B(const T&) noexcept { T::nonexistent; } }; + +static_assert(__is_nothrow_convertible(const A&, B), ""); |