* [PATCH] c++: ICE with noexcept and canonical types [PR101715] @ 2022-01-15 0:22 Marek Polacek 2022-01-15 14:24 ` Patrick Palka 2022-01-17 18:48 ` Jason Merrill 0 siblings, 2 replies; 10+ messages in thread From: Marek Polacek @ 2022-01-15 0:22 UTC (permalink / raw) To: Jason Merrill, Nathan Sidwell, GCC Patches 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. 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 -- 2.34.1 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] c++: ICE with noexcept and canonical types [PR101715] 2022-01-15 0:22 [PATCH] c++: ICE with noexcept and canonical types [PR101715] Marek Polacek @ 2022-01-15 14:24 ` Patrick Palka 2022-01-18 16:08 ` Marek Polacek 2022-01-17 18:48 ` Jason Merrill 1 sibling, 1 reply; 10+ messages in thread From: Patrick Palka @ 2022-01-15 14:24 UTC (permalink / raw) To: Marek Polacek; +Cc: Jason Merrill, Nathan Sidwell, GCC Patches On Fri, 14 Jan 2022, Marek Polacek via Gcc-patches 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. > > 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). I wonder about instead making build_cp_fntype_variant set the TYPE_CANONICAL for #3 to TYPE_CANONICAL(#2) (i.e. #1) instead of to #2? Something like: -- >8 -- gcc/cp/tree.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c index 7f7de86b4e8..b89135fa121 100644 --- a/gcc/cp/tree.c +++ b/gcc/cp/tree.c @@ -2779,8 +2779,9 @@ build_cp_fntype_variant (tree type, cp_ref_qualifier rqual, else if (TYPE_CANONICAL (type) != type || cr != raises || late) /* Build the underlying canonical type, since it is different from TYPE. */ - TYPE_CANONICAL (v) = build_cp_fntype_variant (TYPE_CANONICAL (type), - rqual, cr, false); + TYPE_CANONICAL (v) + = TYPE_CANONICAL (build_cp_fntype_variant (TYPE_CANONICAL (type), + rqual, cr, false)); else /* T is its own canonical type. */ TYPE_CANONICAL (v) = v; ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] c++: ICE with noexcept and canonical types [PR101715] 2022-01-15 14:24 ` Patrick Palka @ 2022-01-18 16:08 ` Marek Polacek 0 siblings, 0 replies; 10+ messages in thread From: Marek Polacek @ 2022-01-18 16:08 UTC (permalink / raw) To: Patrick Palka; +Cc: Jason Merrill, Nathan Sidwell, GCC Patches On Sat, Jan 15, 2022 at 09:24:05AM -0500, Patrick Palka wrote: > On Fri, 14 Jan 2022, Marek Polacek via Gcc-patches 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. > > > > 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). > > I wonder about instead making build_cp_fntype_variant set the TYPE_CANONICAL for > #3 to TYPE_CANONICAL(#2) (i.e. #1) instead of to #2? Something like: > > -- >8 -- > > gcc/cp/tree.c | 5 +++-- > 1 file changed, 3 insertions(+), 2 deletions(-) > > diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c > index 7f7de86b4e8..b89135fa121 100644 > --- a/gcc/cp/tree.c > +++ b/gcc/cp/tree.c > @@ -2779,8 +2779,9 @@ build_cp_fntype_variant (tree type, cp_ref_qualifier rqual, > else if (TYPE_CANONICAL (type) != type || cr != raises || late) > /* Build the underlying canonical type, since it is different > from TYPE. */ > - TYPE_CANONICAL (v) = build_cp_fntype_variant (TYPE_CANONICAL (type), > - rqual, cr, false); > + TYPE_CANONICAL (v) > + = TYPE_CANONICAL (build_cp_fntype_variant (TYPE_CANONICAL (type), > + rqual, cr, false)); > else > /* T is its own canonical type. */ > TYPE_CANONICAL (v) = v; Thanks for looking. I can dig that (and verified it works), but it strikes me more as a workaround for the duplicity problem. I also don't see TYPE_CANONICAL (...) = TYPE_CANONICAL (build_cp_fntype_variant (...)) anywhere in the codebase, if that means anything. Marek ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] c++: ICE with noexcept and canonical types [PR101715] 2022-01-15 0:22 [PATCH] c++: ICE with noexcept and canonical types [PR101715] Marek Polacek 2022-01-15 14:24 ` Patrick Palka @ 2022-01-17 18:48 ` Jason Merrill 2022-01-18 16:05 ` Marek Polacek 1 sibling, 1 reply; 10+ messages in thread From: Jason Merrill @ 2022-01-17 18:48 UTC (permalink / raw) To: Marek Polacek, Nathan Sidwell, GCC Patches 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 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] c++: ICE with noexcept and canonical types [PR101715] 2022-01-17 18:48 ` Jason Merrill @ 2022-01-18 16:05 ` Marek Polacek 2022-01-20 20:23 ` Jason Merrill 0 siblings, 1 reply; 10+ messages in thread From: Marek Polacek @ 2022-01-18 16:05 UTC (permalink / raw) To: Jason Merrill; +Cc: Nathan Sidwell, GCC Patches On Mon, Jan 17, 2022 at 01:48:48PM -0500, Jason Merrill wrote: > 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? In other words, I think you're asking: why did fixup_deferred_exception_variants set TYPE_CANONICAL (#2) to #1 (which then differs from TYPE_CANONICAL (#3), which is #2)? The method_type for #1 (I'll mark is as #1 here) is built with it being its own canonical type. The first call to fixup_deferred_exception_variants does not change it: in there, VARIANT is #1, the loop with 'TYPE_CANONICAL (v) == v' cannot find an existing variant that would match, so when we do v = build_cp_fntype_variant (TYPE_CANONICAL (variant), rqual, cr, false); we get #1 so TYPE_CANONICAL (variant) = v; is just TYPE_CANONICAL (#1) = #1; so no change. The second call to fixup_deferred_exception_variants: here we're working with VARIANT #2. Now we again scan the list of variants {main, #2, #1} where we find a match for #2: #1. #1's TYPE_CANONICAL is #1 as per above, so we set TYPE_CANONICAL (#2) = #1; which I think is correct. I think TYPE_CANONICAL (#3) should also be #1, not #2, which my patch attempts to do. Hope this explanation makes some sense, please ask away if it doesn't! > > 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 > Marek ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] c++: ICE with noexcept and canonical types [PR101715] 2022-01-18 16:05 ` Marek Polacek @ 2022-01-20 20:23 ` Jason Merrill 2022-01-21 1:03 ` [PATCH v2] " Marek Polacek 0 siblings, 1 reply; 10+ messages in thread From: Jason Merrill @ 2022-01-20 20:23 UTC (permalink / raw) To: Marek Polacek; +Cc: Nathan Sidwell, GCC Patches On 1/18/22 11:05, Marek Polacek wrote: > On Mon, Jan 17, 2022 at 01:48:48PM -0500, Jason Merrill wrote: >> 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? > > In other words, I think you're asking: why did fixup_deferred_exception_variants > set TYPE_CANONICAL (#2) to #1 (which then differs from TYPE_CANONICAL (#3), > which is #2)? I meant to ask why TYPE_CANONICAL (#3) got set to #2 instead of #1? And to answer my own question, it's because the check I mention is in fixup_deferred_exception_variants, and #3 doesn't go through there at all; the loop in build_cp_fntype_variant assumes no duplicate variants, which your patch fixes. > The method_type for #1 (I'll mark is as #1 here) is built with it being its own > canonical type. > > The first call to fixup_deferred_exception_variants does not change it: in > there, VARIANT is #1, the loop with 'TYPE_CANONICAL (v) == v' cannot find > an existing variant that would match, so when we do > > v = build_cp_fntype_variant (TYPE_CANONICAL (variant), > rqual, cr, false); > we get #1 so > TYPE_CANONICAL (variant) = v; > is just > TYPE_CANONICAL (#1) = #1; > so no change. > > The second call to fixup_deferred_exception_variants: here we're working with > VARIANT #2. Now we again scan the list of variants {main, #2, #1} where we > find a match for #2: #1. #1's TYPE_CANONICAL is #1 as per above, so we set > TYPE_CANONICAL (#2) = #1; > which I think is correct. > > > I think TYPE_CANONICAL (#3) should also be #1, not #2, which my patch attempts > to do. > > > Hope this explanation makes some sense, please ask away if it doesn't! > >>> 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); I think we don't two loops through the variants. It ought to work to replace the existing loop with yours; if we find v, we prune and use its TYPE_CANONICAL. Jason ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v2] c++: ICE with noexcept and canonical types [PR101715] 2022-01-20 20:23 ` Jason Merrill @ 2022-01-21 1:03 ` Marek Polacek 2022-01-21 14:27 ` Jason Merrill 0 siblings, 1 reply; 10+ messages in thread From: Marek Polacek @ 2022-01-21 1:03 UTC (permalink / raw) To: Jason Merrill; +Cc: Nathan Sidwell, GCC Patches On Thu, Jan 20, 2022 at 03:23:24PM -0500, Jason Merrill wrote: > On 1/18/22 11:05, Marek Polacek wrote: > > On Mon, Jan 17, 2022 at 01:48:48PM -0500, Jason Merrill wrote: > > > 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? > > > > In other words, I think you're asking: why did fixup_deferred_exception_variants > > set TYPE_CANONICAL (#2) to #1 (which then differs from TYPE_CANONICAL (#3), > > which is #2)? > > I meant to ask why TYPE_CANONICAL (#3) got set to #2 instead of #1? > > And to answer my own question, it's because the check I mention is in > fixup_deferred_exception_variants, and #3 doesn't go through there at all; > the loop in build_cp_fntype_variant assumes no duplicate variants, which > your patch fixes. Right, fixup_deferred_exception_variants is only called for fn decls in unparsed_noexcepts. > > The method_type for #1 (I'll mark is as #1 here) is built with it being its own > > canonical type. > > > > The first call to fixup_deferred_exception_variants does not change it: in > > there, VARIANT is #1, the loop with 'TYPE_CANONICAL (v) == v' cannot find > > an existing variant that would match, so when we do > > > > v = build_cp_fntype_variant (TYPE_CANONICAL (variant), > > rqual, cr, false); > > we get #1 so > > TYPE_CANONICAL (variant) = v; > > is just > > TYPE_CANONICAL (#1) = #1; > > so no change. > > > > The second call to fixup_deferred_exception_variants: here we're working with > > VARIANT #2. Now we again scan the list of variants {main, #2, #1} where we > > find a match for #2: #1. #1's TYPE_CANONICAL is #1 as per above, so we set > > TYPE_CANONICAL (#2) = #1; > > which I think is correct. > > > > > > I think TYPE_CANONICAL (#3) should also be #1, not #2, which my patch attempts > > to do. > > > > > > Hope this explanation makes some sense, please ask away if it doesn't! > > > > > > 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); > > I think we don't two loops through the variants. It ought to work to > replace the existing loop with yours; if we find v, we prune and use its > TYPE_CANONICAL. Ah yes, good idea; I don't actually need to wait till TYPE_RAISES_EXCEPTIONS is set on variant! The following seems to work just as well. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- 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. 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). 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.cc | 16 ++++++++++++++-- gcc/testsuite/g++.dg/cpp0x/noexcept72.C | 21 +++++++++++++++++++++ gcc/testsuite/g++.dg/cpp0x/noexcept73.C | 13 +++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) 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.cc b/gcc/cp/tree.cc index bcd44e73921..17436f0512d 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -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)); @@ -2815,12 +2816,23 @@ fixup_deferred_exception_variants (tree type, tree raises) cp_cv_quals var_quals = TYPE_QUALS (variant); cp_ref_qualifier rqual = type_memfn_rqual (variant); + /* If VARIANT would become a dup (cp_check_qualified_type-wise) + of an existing variant in the variant list of TYPE after its + exception specification has been parsed, elide it. Otherwise, + build_cp_fntype_variant could use it, leading to "canonical + types differ for identical types." */ tree v = TYPE_MAIN_VARIANT (type); for (; v; v = TYPE_NEXT_VARIANT (v)) if (TYPE_CANONICAL (v) == v + && v != variant && cp_check_qualified_type (v, variant, var_quals, rqual, cr, false)) - break; + { + /* The main variant will not match V, so PREV will never + be null. */ + TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant); + break; + } TYPE_RAISES_EXCEPTIONS (variant) = raises; if (!v) 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: d2ad748eeef0dd260f3993b8dcbffbded3240a0a -- 2.34.1 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v2] c++: ICE with noexcept and canonical types [PR101715] 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 0 siblings, 1 reply; 10+ messages in thread From: Jason Merrill @ 2022-01-21 14:27 UTC (permalink / raw) To: Marek Polacek; +Cc: Nathan Sidwell, GCC Patches On 1/20/22 20:03, Marek Polacek wrote: > On Thu, Jan 20, 2022 at 03:23:24PM -0500, Jason Merrill wrote: >> On 1/18/22 11:05, Marek Polacek wrote: >>> On Mon, Jan 17, 2022 at 01:48:48PM -0500, Jason Merrill wrote: >>>> 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? >>> >>> In other words, I think you're asking: why did fixup_deferred_exception_variants >>> set TYPE_CANONICAL (#2) to #1 (which then differs from TYPE_CANONICAL (#3), >>> which is #2)? >> >> I meant to ask why TYPE_CANONICAL (#3) got set to #2 instead of #1? >> >> And to answer my own question, it's because the check I mention is in >> fixup_deferred_exception_variants, and #3 doesn't go through there at all; >> the loop in build_cp_fntype_variant assumes no duplicate variants, which >> your patch fixes. > > Right, fixup_deferred_exception_variants is only called for fn decls in > unparsed_noexcepts. > >>> The method_type for #1 (I'll mark is as #1 here) is built with it being its own >>> canonical type. >>> >>> The first call to fixup_deferred_exception_variants does not change it: in >>> there, VARIANT is #1, the loop with 'TYPE_CANONICAL (v) == v' cannot find >>> an existing variant that would match, so when we do >>> >>> v = build_cp_fntype_variant (TYPE_CANONICAL (variant), >>> rqual, cr, false); >>> we get #1 so >>> TYPE_CANONICAL (variant) = v; >>> is just >>> TYPE_CANONICAL (#1) = #1; >>> so no change. >>> >>> The second call to fixup_deferred_exception_variants: here we're working with >>> VARIANT #2. Now we again scan the list of variants {main, #2, #1} where we >>> find a match for #2: #1. #1's TYPE_CANONICAL is #1 as per above, so we set >>> TYPE_CANONICAL (#2) = #1; >>> which I think is correct. >>> >>> >>> I think TYPE_CANONICAL (#3) should also be #1, not #2, which my patch attempts >>> to do. >>> >>> >>> Hope this explanation makes some sense, please ask away if it doesn't! >>> >>>>> 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); >> >> I think we don't two loops through the variants. It ought to work to >> replace the existing loop with yours; if we find v, we prune and use its >> TYPE_CANONICAL. > > Ah yes, good idea; I don't actually need to wait till TYPE_RAISES_EXCEPTIONS > is set on variant! The following seems to work just as well. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > -- >8 -- > 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. > > 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). > > 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.cc | 16 ++++++++++++++-- > gcc/testsuite/g++.dg/cpp0x/noexcept72.C | 21 +++++++++++++++++++++ > gcc/testsuite/g++.dg/cpp0x/noexcept73.C | 13 +++++++++++++ > 3 files changed, 48 insertions(+), 2 deletions(-) > 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.cc b/gcc/cp/tree.cc > index bcd44e73921..17436f0512d 100644 > --- a/gcc/cp/tree.cc > +++ b/gcc/cp/tree.cc > @@ -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)); > @@ -2815,12 +2816,23 @@ fixup_deferred_exception_variants (tree type, tree raises) > cp_cv_quals var_quals = TYPE_QUALS (variant); > cp_ref_qualifier rqual = type_memfn_rqual (variant); > > + /* If VARIANT would become a dup (cp_check_qualified_type-wise) > + of an existing variant in the variant list of TYPE after its > + exception specification has been parsed, elide it. Otherwise, > + build_cp_fntype_variant could use it, leading to "canonical > + types differ for identical types." */ > tree v = TYPE_MAIN_VARIANT (type); > for (; v; v = TYPE_NEXT_VARIANT (v)) > if (TYPE_CANONICAL (v) == v I think we want to drop the TYPE_CANONICAL check here, and below change TYPE_CANONICAL (variant) = v; to TYPE_CANONICAL (variant) = TYPE_CANONICAL (v); so that this also works for e.g. signatures involving typedefs. > + && v != variant I think we don't need this check since we haven't changed TYPE_RAISES_EXCEPTIONS yet. > && cp_check_qualified_type (v, variant, var_quals, > rqual, cr, false)) > - break; > + { > + /* The main variant will not match V, so PREV will never > + be null. */ > + TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant); > + break; > + } > TYPE_RAISES_EXCEPTIONS (variant) = raises; > > if (!v) > 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: d2ad748eeef0dd260f3993b8dcbffbded3240a0a ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v3] c++: ICE with noexcept and canonical types [PR101715] 2022-01-21 14:27 ` Jason Merrill @ 2022-01-21 17:42 ` Marek Polacek 2022-01-21 18:08 ` Jason Merrill 0 siblings, 1 reply; 10+ messages in thread From: Marek Polacek @ 2022-01-21 17:42 UTC (permalink / raw) To: Jason Merrill; +Cc: Nathan Sidwell, GCC Patches On Fri, Jan 21, 2022 at 09:27:17AM -0500, Jason Merrill wrote: > On 1/20/22 20:03, Marek Polacek wrote: > > @@ -2815,12 +2816,23 @@ fixup_deferred_exception_variants (tree type, tree raises) > > cp_cv_quals var_quals = TYPE_QUALS (variant); > > cp_ref_qualifier rqual = type_memfn_rqual (variant); > > + /* If VARIANT would become a dup (cp_check_qualified_type-wise) > > + of an existing variant in the variant list of TYPE after its > > + exception specification has been parsed, elide it. Otherwise, > > + build_cp_fntype_variant could use it, leading to "canonical > > + types differ for identical types." */ > > tree v = TYPE_MAIN_VARIANT (type); > > for (; v; v = TYPE_NEXT_VARIANT (v)) > > if (TYPE_CANONICAL (v) == v > > I think we want to drop the TYPE_CANONICAL check here, and below change > > TYPE_CANONICAL (variant) = v; > > to > > TYPE_CANONICAL (variant) = TYPE_CANONICAL (v); OK. I couldn't really find a way to test it; clang++ rejected my attempts with "error: exception specifications are not allowed in typedefs" so I'm not sure if I want to add such tests even though we happen to accept it currently. > so that this also works for e.g. signatures involving typedefs. > > > + && v != variant > > I think we don't need this check since we haven't changed > TYPE_RAISES_EXCEPTIONS yet. And variant will never be the main variant, because of the if (TYPE_RAISES_EXCEPTIONS (variant) == original) check. Ok, so the following should be enough: Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- 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. 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). 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.cc | 22 ++++++++++++++++------ gcc/testsuite/g++.dg/cpp0x/noexcept72.C | 21 +++++++++++++++++++++ gcc/testsuite/g++.dg/cpp0x/noexcept73.C | 13 +++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) 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.cc b/gcc/cp/tree.cc index bcd44e73921..f88006aec4f 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -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)); @@ -2815,18 +2816,27 @@ fixup_deferred_exception_variants (tree type, tree raises) cp_cv_quals var_quals = TYPE_QUALS (variant); cp_ref_qualifier rqual = type_memfn_rqual (variant); + /* If VARIANT would become a dup (cp_check_qualified_type-wise) + of an existing variant in the variant list of TYPE after its + exception specification has been parsed, elide it. Otherwise, + build_cp_fntype_variant could use it, leading to "canonical + types differ for identical types." */ tree v = TYPE_MAIN_VARIANT (type); for (; v; v = TYPE_NEXT_VARIANT (v)) - if (TYPE_CANONICAL (v) == v - && cp_check_qualified_type (v, variant, var_quals, - rqual, cr, false)) - break; + if (cp_check_qualified_type (v, variant, var_quals, + rqual, cr, false)) + { + /* The main variant will not match V, so PREV will never + be null. */ + TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant); + break; + } TYPE_RAISES_EXCEPTIONS (variant) = raises; if (!v) v = build_cp_fntype_variant (TYPE_CANONICAL (variant), rqual, cr, false); - TYPE_CANONICAL (variant) = v; + TYPE_CANONICAL (variant) = TYPE_CANONICAL (v); } 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: 45cae5b6392496028f35c5948f7fae0af81d135b -- 2.34.1 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v3] c++: ICE with noexcept and canonical types [PR101715] 2022-01-21 17:42 ` [PATCH v3] " Marek Polacek @ 2022-01-21 18:08 ` Jason Merrill 0 siblings, 0 replies; 10+ messages in thread From: Jason Merrill @ 2022-01-21 18:08 UTC (permalink / raw) To: Marek Polacek; +Cc: Nathan Sidwell, GCC Patches On 1/21/22 12:42, Marek Polacek wrote: > On Fri, Jan 21, 2022 at 09:27:17AM -0500, Jason Merrill wrote: >> On 1/20/22 20:03, Marek Polacek wrote: >>> @@ -2815,12 +2816,23 @@ fixup_deferred_exception_variants (tree type, tree raises) >>> cp_cv_quals var_quals = TYPE_QUALS (variant); >>> cp_ref_qualifier rqual = type_memfn_rqual (variant); >>> + /* If VARIANT would become a dup (cp_check_qualified_type-wise) >>> + of an existing variant in the variant list of TYPE after its >>> + exception specification has been parsed, elide it. Otherwise, >>> + build_cp_fntype_variant could use it, leading to "canonical >>> + types differ for identical types." */ >>> tree v = TYPE_MAIN_VARIANT (type); >>> for (; v; v = TYPE_NEXT_VARIANT (v)) >>> if (TYPE_CANONICAL (v) == v >> >> I think we want to drop the TYPE_CANONICAL check here, and below change >> >> TYPE_CANONICAL (variant) = v; >> >> to >> >> TYPE_CANONICAL (variant) = TYPE_CANONICAL (v); > > OK. I couldn't really find a way to test it; clang++ rejected > my attempts with "error: exception specifications are not allowed in > typedefs" so I'm not sure if I want to add such tests even though we > happen to accept it currently. > >> so that this also works for e.g. signatures involving typedefs. >> >>> + && v != variant >> >> I think we don't need this check since we haven't changed >> TYPE_RAISES_EXCEPTIONS yet. > > And variant will never be the main variant, because of the > > if (TYPE_RAISES_EXCEPTIONS (variant) == original) > > check. Ok, so the following should be enough: > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? OK, thanks. > -- >8 -- > 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. > > 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). > > 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.cc | 22 ++++++++++++++++------ > gcc/testsuite/g++.dg/cpp0x/noexcept72.C | 21 +++++++++++++++++++++ > gcc/testsuite/g++.dg/cpp0x/noexcept73.C | 13 +++++++++++++ > 3 files changed, 50 insertions(+), 6 deletions(-) > 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.cc b/gcc/cp/tree.cc > index bcd44e73921..f88006aec4f 100644 > --- a/gcc/cp/tree.cc > +++ b/gcc/cp/tree.cc > @@ -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)); > @@ -2815,18 +2816,27 @@ fixup_deferred_exception_variants (tree type, tree raises) > cp_cv_quals var_quals = TYPE_QUALS (variant); > cp_ref_qualifier rqual = type_memfn_rqual (variant); > > + /* If VARIANT would become a dup (cp_check_qualified_type-wise) > + of an existing variant in the variant list of TYPE after its > + exception specification has been parsed, elide it. Otherwise, > + build_cp_fntype_variant could use it, leading to "canonical > + types differ for identical types." */ > tree v = TYPE_MAIN_VARIANT (type); > for (; v; v = TYPE_NEXT_VARIANT (v)) > - if (TYPE_CANONICAL (v) == v > - && cp_check_qualified_type (v, variant, var_quals, > - rqual, cr, false)) > - break; > + if (cp_check_qualified_type (v, variant, var_quals, > + rqual, cr, false)) > + { > + /* The main variant will not match V, so PREV will never > + be null. */ > + TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant); > + break; > + } > TYPE_RAISES_EXCEPTIONS (variant) = raises; > > if (!v) > v = build_cp_fntype_variant (TYPE_CANONICAL (variant), > rqual, cr, false); > - TYPE_CANONICAL (variant) = v; > + TYPE_CANONICAL (variant) = TYPE_CANONICAL (v); > } > 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: 45cae5b6392496028f35c5948f7fae0af81d135b ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2022-01-21 18:08 UTC | newest] Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2022-01-15 0:22 [PATCH] c++: ICE with noexcept and canonical types [PR101715] Marek Polacek 2022-01-15 14:24 ` Patrick Palka 2022-01-18 16:08 ` Marek Polacek 2022-01-17 18:48 ` Jason Merrill 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
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).