On 9/26/22 11:51, Patrick Palka wrote:
On Mon, 26 Sep 2022, Marek Polacek via Gcc-patches wrote:

Jon reported that evaluating __is_convertible in this test leads to
instantiating char_traits<char>::eq, which is invalid (because we
are trying to call a member function on a char) and so we fail 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.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

        PR c++/106784

gcc/cp/ChangeLog:

        * method.cc (is_convertible): Use cp_unevaluated.

I think is_nothrow_convertible would need cp_unevaluated too (or maybe we
should define is_nothrow_convertible in terms of is_convertible).

Agreed.

And the testcase can probably be minimized to something like:

   struct A;
   struct B { template<class T> B(const T&) noexcept { T::nonexistent; } };

   static_assert(__is_convertible(const A&, B));
   static_assert(__is_nothrow_convertible(const A&, B));


gcc/testsuite/ChangeLog:

        * g++.dg/ext/is_convertible3.C: New test.
---
  gcc/cp/method.cc                           |  1 +
  gcc/testsuite/g++.dg/ext/is_convertible3.C | 66 ++++++++++++++++++++++
  2 files changed, 67 insertions(+)
  create mode 100644 gcc/testsuite/g++.dg/ext/is_convertible3.C

diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
index c35a59fe56c..45f70f5d3f3 100644
--- a/gcc/cp/method.cc
+++ b/gcc/cp/method.cc
@@ -2246,6 +2246,7 @@ is_convertible (tree from, tree to)
  {
    if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
      return true;
+  cp_unevaluated u;
    tree expr = build_stub_object (from);
    expr = perform_implicit_conversion (to, expr, tf_none);
    if (expr == error_mark_node)
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 00000000000..c817dc6f146
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_convertible3.C
@@ -0,0 +1,66 @@
+// PR c++/106784
+// { dg-do compile { target c++11 } }
+// Make sure we don't reject this at runtime by trying to instantiate
+// char_traits<CharT>::eq(CharT, CharT) while evaluating __is_convertible.
+
+template<bool B>
+struct bool_constant { static constexpr bool value = B; };
+using true_type = bool_constant<true>;
+using false_type = bool_constant<false>;
+
+template<typename T> struct is_void : false_type { };
+template<> struct is_void<void> : true_type { };
+
+template<typename T> T&& declval();
+
+template<bool> struct enable_if { };
+template<> struct enable_if<true> { using type = void; };
+template<bool B> using enable_if_t = typename enable_if<B>::type;
+
+template<typename _From, typename _To>
+  struct is_convertible
+  : public bool_constant<__is_convertible(_From, _To)>
+  { };
+
+template<class CharT>
+struct char_traits
+{
+  static unsigned long length(const char* s) { eq(*s, *s); return 0; }
+
+  static void eq(CharT l, CharT r) noexcept { l.f(r); }
+};
+
+template<class CharT>
+struct basic_string_view
+{
+  using traits_type = char_traits<CharT>;
+
+  constexpr basic_string_view() = default;
+  constexpr basic_string_view(const basic_string_view&) = default;
+
+  constexpr
+  basic_string_view(const CharT* __str) noexcept
+  : _M_len{traits_type::length(__str)}
+  { }
+
+  unsigned long _M_len = 0;
+};
+
+template<class CharT>
+struct basic_string
+{
+  template<class T>
+    enable_if_t<is_convertible<const T&, basic_string_view<CharT>>::value
+                && !is_convertible<const T&, const char*>::value>
+    replace(int, T) { }
+
+  void replace(unsigned long, const char*) { }
+
+  void replace(const char* s) { replace(1, s); }
+};
+
+int main()
+{
+  basic_string<char> s;
+  s.replace("");
+}

base-commit: 2460f7cdef7ef9c971de79271afc0db73687a272
--
2.37.3




Reply via email to