public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Instantiate less when evaluating __is_convertible
@ 2022-09-26 15:22 Marek Polacek
  2022-09-26 15:51 ` Patrick Palka
  2022-09-26 16:02 ` [PATCH] " Jonathan Wakely
  0 siblings, 2 replies; 7+ messages in thread
From: Marek Polacek @ 2022-09-26 15:22 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill; +Cc: Jonathan Wakely

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.

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-09-26 16:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-26 15:22 [PATCH] c++: Instantiate less when evaluating __is_convertible Marek Polacek
2022-09-26 15:51 ` Patrick Palka
2022-09-26 16:21   ` Jason Merrill
2022-09-26 16:25   ` [PATCH v2] " Marek Polacek
2022-09-26 16:41     ` Jason Merrill
2022-09-26 16:02 ` [PATCH] " Jonathan Wakely
2022-09-26 16:26   ` Marek Polacek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).