From 3abcbf243239f9576a60f4ce7f8ee4b3fa14784b Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Fri, 14 Jan 2022 17:41:49 -0500 Subject: [PATCH] c++: ICE with noexcept and canonical types [PR101715] This is a "canonical types differ for identical types" ICE, which started with r11-4682. It's a bit tricky to explain. Consider: template struct S { S bar() noexcept(T::value); // #1 S foo() noexcept(T::value); // #2 }; template S S::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::(S*) |----->| S S::(S*) |----->| S S::(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.cc (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 bcd44e739214f..f88006aec4fb1 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 0000000000000..f1455b3b46b9b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept72.C @@ -0,0 +1,21 @@ +// PR c++/101715 +// { dg-do compile { target c++11 } } + +template struct S { + S bar() noexcept(T::value); // #1 + S foo() noexcept(T::value); // #2 +}; + +template S S::foo() noexcept(T::value) {} // #3 + +template struct S2 { + S2 bar1() noexcept(T::value); + S2 bar2() noexcept(T::value); + S2 bar3() noexcept(T::value); + S2 bar4() noexcept(T::value); + S2 bar5() noexcept(T::value); + S2 baz() noexcept(T::value2); + S2 foo() noexcept(T::value); +}; + +template S2 S2::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 0000000000000..24524f3592a72 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept73.C @@ -0,0 +1,13 @@ +// PR c++/101715 +// { dg-do compile { target c++11 } } + +template struct S { }; + +template +struct A +{ + A& foo(A&&) noexcept((S::value)); + A& assign(A&&) noexcept((S::value)); +}; +template +A& A::foo(A&&) noexcept((S::value)) {}