On Fri, Sep 23, 2022 at 11:54:53AM -0400, Jason Merrill wrote: > On 9/23/22 10:34, Marek Polacek wrote: > > On Thu, Sep 22, 2022 at 06:14:44PM -0400, Jason Merrill wrote: > > > On 9/22/22 09:39, Marek Polacek wrote: > > > > To improve compile times, the C++ library could use compiler built-ins > > > > rather than implementing std::is_convertible (and _nothrow) as class > > > > templates. This patch adds the built-ins. We already have > > > > __is_constructible and __is_assignable, and the nothrow forms of those. > > > > > > > > Microsoft (and clang, for compatibility) also provide an alias called > > > > __is_convertible_to. I did not add it, but it would be trivial to do > > > > so. > > > > > > > > I noticed that our __is_assignable doesn't implement the "Access checks > > > > are performed as if from a context unrelated to either type" > > > > requirement, > > > > therefore std::is_assignable / __is_assignable give two different > > > > results > > > > here: > > > > > > > > class S { > > > > operator int(); > > > > friend void g(); // #1 > > > > }; > > > > > > > > void > > > > g () > > > > { > > > > // #1 doesn't matter > > > > static_assert(std::is_assignable<int&, S>::value, ""); > > > > static_assert(__is_assignable(int&, S), ""); > > > > } > > > > > > > > This is not a problem if __is_assignable is not meant to be used by > > > > the users. > > > > > > That's fine, it's not. > > Okay then. libstdc++ needs to make sure then that it's handled right. > > > > > > This patch doesn't make libstdc++ use the new built-ins, but I had to > > > > rename a class otherwise its name would clash with the new built-in. > > > > > > Sigh, that's going to be a hassle when comparing compiler versions on > > > preprocessed code. > > > > Yeah, I guess :/. Kind of like __integer_pack / __make_integer_seq. > > > > > > --- a/gcc/cp/constraint.cc > > > > +++ b/gcc/cp/constraint.cc > > > > @@ -3697,6 +3697,12 @@ diagnose_trait_expr (tree expr, tree args) > > > > case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS: > > > > inform (loc, " %qT does not have unique object > > > > representations", t1); > > > > break; > > > > + case CPTK_IS_CONVERTIBLE: > > > > + inform (loc, " %qT is not convertible from %qE", t2, t1); > > > > + break; > > > > + case CPTK_IS_NOTHROW_CONVERTIBLE: > > > > + inform (loc, " %qT is not %<nothrow%> convertible from %qE", > > > > t2, t1); > > > > > > It's odd that the existing diagnostics quote "nothrow", which is not a > > > keyword. I wonder why these library traits didn't use "noexcept"? > > > > Eh, yeah, only "throw" is. The quotes were deliberately added in > > <https://gcc.gnu.org/pipermail/gcc-patches/2019-May/522333.html>. Should > > I prepare a separate patch to use "%<noexcept%>" rather than "%<nothrow%>"? > > OTOH, the traits have "nothrow" in their names, so maybe just go back to > > "nothrow"? > > The latter, I think. Or possibly "no-throw". I guess -Wformat-diag wants > "nothrow" quoted because of the attribute of that name.
OK, let me see. > > > > --- a/gcc/cp/method.cc > > > > +++ b/gcc/cp/method.cc > > > > @@ -2236,6 +2236,37 @@ ref_xes_from_temporary (tree to, tree from, bool > > > > direct_init_p) > > > > return ref_conv_binds_directly (to, val, direct_init_p).is_false > > > > (); > > > > } > > > > +/* 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 > > > > + to either type" restriction. */ > > > > + > > > > +bool > > > > +is_convertible (tree from, tree to) > > > > > > You didn't want to add conversion to is*_xible? > > > > No, it didn't look like a good fit. It does things we don't need, and > > also has if VOID_TYPE_P -> return error_mark_node; which would be wrong > > for __is_convertible. > > > > I realized I'm not testing passing an incomplete type to the built-in, > > but since that is UB, I reckon we don't need to test it (we issue > > "error: invalid use of incomplete type"). > > But your patch does test that, in the existing call to check_trait_type from > finish_trait_expr? Yes, it eventually checks complete_type_or_else. > The patch is OK. Thanks, Marek