public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Marek Polacek <polacek@redhat.com>,
	Nathan Sidwell <nathan@acm.org>,
	GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] c++: ICE with noexcept and canonical types [PR101715]
Date: Mon, 17 Jan 2022 13:48:48 -0500	[thread overview]
Message-ID: <1ef32dc7-8b16-9fa4-7c7c-649632be6768@redhat.com> (raw)
In-Reply-To: <20220115002249.366484-1-polacek@redhat.com>

On 1/14/22 19:22, Marek Polacek wrote:
> This is a "canonical types differ for identical types" ICE, which started
> with r11-4682.  It's a bit tricky to explain.  Consider:
> 
>    template <typename T> struct S {
>      S<T> bar() noexcept(T::value);  // #1
>      S<T> foo() noexcept(T::value);  // #2
>    };
> 
>    template <typename T> S<T> S<T>::foo() noexcept(T::value) {}  // #3
> 
> We ICE because #3 and #2 have the same type, but their canonical types
> differ: TYPE_CANONICAL (#3) == #2 but TYPE_CANONICAL (#2) == #1.
> 
> The member functions #1 and #2 have the same type.  However, since their
> noexcept-specifier is deferred, when parsing them, we create a variant for
> both of them, because DEFERRED_PARSE cannot be compared.  In other words,
> build_cp_fntype_variant's
> 
>    tree v = TYPE_MAIN_VARIANT (type);
>    for (; v; v = TYPE_NEXT_VARIANT (v))
>      if (cp_check_qualified_type (v, type, type_quals, rqual, raises, late))
>        return v;
> 
> will *not* find an existing variant when creating a method_type for #2, so we
> have to create a new one.
> 
> But then we perform delayed parsing and call fixup_deferred_exception_variants
> for #1 and #2.  f_d_e_v will replace TYPE_RAISES_EXCEPTIONS with the newly
> parsed noexcept-specifier.  It also sets TYPE_CANONICAL (#2) to #1.  Both
> noexcepts turned out to be the same, so now we have two equivalent variants in
> the list!  I.e.,
> 
> +-----------------+      +-----------------+      +-----------------+
> |      main       |      |      #2         |      |      #1         |
> | S S::<T379>(S*) |----->| S S::<T37c>(S*) |----->| S S::<T37a>(S*) |----->NULL
> |    -            |      |  noex(T::value) |      |  noex(T::value) |
> +-----------------+      +-----------------+      +-----------------+
> 
> Then we get to #3.  As for #1 and #2, grokdeclarator calls build_memfn_type,
> which ends up calling build_cp_fntype_variant, which will use the loop
> above to look for an existing variant.  The first one that matches
> cp_check_qualified_type will be used, so we use #2 rather than #1, and the
> TYPE_CANONICAL mismatch follows.  Hopefully that makes sense.

Why doesn't the TYPE_CANONICAL (v) == v check prevent this?

> As for the fix, I didn't think I could rewrite the method_type #2 with #1
> because the type may have escaped via decltype.  So my approach is to
> elide #2 from the list, so when looking for a matching variant, we always
> find #1 (#2 remains live though, which admittedly sounds sort of dodgy).
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/11?
> 
> 	PR c++/101715
> 
> gcc/cp/ChangeLog:
> 
> 	* tree.c (fixup_deferred_exception_variants): Remove duplicate
> 	variants after parsing the exception specifications.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/noexcept72.C: New test.
> 	* g++.dg/cpp0x/noexcept73.C: New test.
> ---
>   gcc/cp/tree.c                           | 16 +++++++++++++++-
>   gcc/testsuite/g++.dg/cpp0x/noexcept72.C | 21 +++++++++++++++++++++
>   gcc/testsuite/g++.dg/cpp0x/noexcept73.C | 13 +++++++++++++
>   3 files changed, 49 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept72.C
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept73.C
> 
> diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
> index 7f7de86b4e8..2efad49e7c1 100644
> --- a/gcc/cp/tree.c
> +++ b/gcc/cp/tree.c
> @@ -2804,8 +2804,9 @@ fixup_deferred_exception_variants (tree type, tree raises)
>   
>     /* Though sucky, this walk will process the canonical variants
>        first.  */
> +  tree prev = NULL_TREE;
>     for (tree variant = TYPE_MAIN_VARIANT (type);
> -       variant; variant = TYPE_NEXT_VARIANT (variant))
> +       variant; prev = variant, variant = TYPE_NEXT_VARIANT (variant))
>       if (TYPE_RAISES_EXCEPTIONS (variant) == original)
>         {
>   	gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type));
> @@ -2827,6 +2828,19 @@ fixup_deferred_exception_variants (tree type, tree raises)
>   	      v = build_cp_fntype_variant (TYPE_CANONICAL (variant),
>   					   rqual, cr, false);
>   	    TYPE_CANONICAL (variant) = v;
> +
> +	    /* If VARIANT became a duplicate (cp_check_qualified_type-wise)
> +	       of an existing variant in the variant list of TYPE after we
> +	       have parsed its exception specification, elide it.  Otherwise,
> +	       build_cp_fntype_variant would use it, leading to "canonical
> +	       types differ for identical types."  */
> +	    for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
> +	      if (v != variant
> +		  /* The main variant will not have TYPE_RAISES_EXCEPTIONS
> +		     so PREV should never be null.  */
> +		  && cp_check_qualified_type (v, variant, var_quals,
> +					      rqual, cr, false))
> +		TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant);
>   	  }
>   	else
>   	  TYPE_RAISES_EXCEPTIONS (variant) = raises;
> diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept72.C b/gcc/testsuite/g++.dg/cpp0x/noexcept72.C
> new file mode 100644
> index 00000000000..f1455b3b46b
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept72.C
> @@ -0,0 +1,21 @@
> +// PR c++/101715
> +// { dg-do compile { target c++11 } }
> +
> +template <typename T> struct S {
> +  S<T> bar() noexcept(T::value);  // #1
> +  S<T> foo() noexcept(T::value);  // #2
> +};
> +
> +template <typename T> S<T> S<T>::foo() noexcept(T::value) {}  // #3
> +
> +template <typename T> struct S2 {
> +  S2<T> bar1() noexcept(T::value);
> +  S2<T> bar2() noexcept(T::value);
> +  S2<T> bar3() noexcept(T::value);
> +  S2<T> bar4() noexcept(T::value);
> +  S2<T> bar5() noexcept(T::value);
> +  S2<T> baz() noexcept(T::value2);
> +  S2<T> foo() noexcept(T::value);
> +};
> +
> +template <typename T> S2<T> S2<T>::foo() noexcept(T::value) {}
> diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept73.C b/gcc/testsuite/g++.dg/cpp0x/noexcept73.C
> new file mode 100644
> index 00000000000..24524f3592a
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept73.C
> @@ -0,0 +1,13 @@
> +// PR c++/101715
> +// { dg-do compile { target c++11 } }
> +
> +template <typename T> struct S { };
> +
> +template<typename T>
> +struct A
> +{
> +    A& foo(A&&) noexcept((S<T>::value));
> +    A& assign(A&&) noexcept((S<T>::value));
> +};
> +template<typename T>
> +A<T>& A<T>::foo(A&&) noexcept((S<T>::value)) {}
> 
> base-commit: 952b7dbb418198f86d7829aaf9d7f9fc7714a8b3


  parent reply	other threads:[~2022-01-17 18:48 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-15  0:22 Marek Polacek
2022-01-15 14:24 ` Patrick Palka
2022-01-18 16:08   ` Marek Polacek
2022-01-17 18:48 ` Jason Merrill [this message]
2022-01-18 16:05   ` Marek Polacek
2022-01-20 20:23     ` Jason Merrill
2022-01-21  1:03       ` [PATCH v2] " Marek Polacek
2022-01-21 14:27         ` Jason Merrill
2022-01-21 17:42           ` [PATCH v3] " Marek Polacek
2022-01-21 18:08             ` Jason Merrill

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=1ef32dc7-8b16-9fa4-7c7c-649632be6768@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=nathan@acm.org \
    --cc=polacek@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).