public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marek Polacek <polacek@redhat.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>, Jason Merrill <jason@redhat.com>
Cc: Jonathan Wakely <jwakely@redhat.com>
Subject: [PATCH] c++: Instantiate less when evaluating __is_convertible
Date: Mon, 26 Sep 2022 11:22:58 -0400	[thread overview]
Message-ID: <20220926152258.20921-1-polacek@redhat.com> (raw)

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


             reply	other threads:[~2022-09-26 15:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-26 15:22 Marek Polacek [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220926152258.20921-1-polacek@redhat.com \
    --to=polacek@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=jwakely@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).