Skip to content

Commit 3abcbf2

Browse files
committed
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 <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.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.
1 parent 087e545 commit 3abcbf2

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

gcc/cp/tree.cc

+16-6
Original file line numberDiff line numberDiff line change
@@ -2804,8 +2804,9 @@ fixup_deferred_exception_variants (tree type, tree raises)
28042804

28052805
/* Though sucky, this walk will process the canonical variants
28062806
first. */
2807+
tree prev = NULL_TREE;
28072808
for (tree variant = TYPE_MAIN_VARIANT (type);
2808-
variant; variant = TYPE_NEXT_VARIANT (variant))
2809+
variant; prev = variant, variant = TYPE_NEXT_VARIANT (variant))
28092810
if (TYPE_RAISES_EXCEPTIONS (variant) == original)
28102811
{
28112812
gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type));
@@ -2815,18 +2816,27 @@ fixup_deferred_exception_variants (tree type, tree raises)
28152816
cp_cv_quals var_quals = TYPE_QUALS (variant);
28162817
cp_ref_qualifier rqual = type_memfn_rqual (variant);
28172818

2819+
/* If VARIANT would become a dup (cp_check_qualified_type-wise)
2820+
of an existing variant in the variant list of TYPE after its
2821+
exception specification has been parsed, elide it. Otherwise,
2822+
build_cp_fntype_variant could use it, leading to "canonical
2823+
types differ for identical types." */
28182824
tree v = TYPE_MAIN_VARIANT (type);
28192825
for (; v; v = TYPE_NEXT_VARIANT (v))
2820-
if (TYPE_CANONICAL (v) == v
2821-
&& cp_check_qualified_type (v, variant, var_quals,
2822-
rqual, cr, false))
2823-
break;
2826+
if (cp_check_qualified_type (v, variant, var_quals,
2827+
rqual, cr, false))
2828+
{
2829+
/* The main variant will not match V, so PREV will never
2830+
be null. */
2831+
TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant);
2832+
break;
2833+
}
28242834
TYPE_RAISES_EXCEPTIONS (variant) = raises;
28252835

28262836
if (!v)
28272837
v = build_cp_fntype_variant (TYPE_CANONICAL (variant),
28282838
rqual, cr, false);
2829-
TYPE_CANONICAL (variant) = v;
2839+
TYPE_CANONICAL (variant) = TYPE_CANONICAL (v);
28302840
}
28312841
else
28322842
TYPE_RAISES_EXCEPTIONS (variant) = raises;
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// PR c++/101715
2+
// { dg-do compile { target c++11 } }
3+
4+
template <typename T> struct S {
5+
S<T> bar() noexcept(T::value); // #1
6+
S<T> foo() noexcept(T::value); // #2
7+
};
8+
9+
template <typename T> S<T> S<T>::foo() noexcept(T::value) {} // #3
10+
11+
template <typename T> struct S2 {
12+
S2<T> bar1() noexcept(T::value);
13+
S2<T> bar2() noexcept(T::value);
14+
S2<T> bar3() noexcept(T::value);
15+
S2<T> bar4() noexcept(T::value);
16+
S2<T> bar5() noexcept(T::value);
17+
S2<T> baz() noexcept(T::value2);
18+
S2<T> foo() noexcept(T::value);
19+
};
20+
21+
template <typename T> S2<T> S2<T>::foo() noexcept(T::value) {}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// PR c++/101715
2+
// { dg-do compile { target c++11 } }
3+
4+
template <typename T> struct S { };
5+
6+
template<typename T>
7+
struct A
8+
{
9+
A& foo(A&&) noexcept((S<T>::value));
10+
A& assign(A&&) noexcept((S<T>::value));
11+
};
12+
template<typename T>
13+
A<T>& A<T>::foo(A&&) noexcept((S<T>::value)) {}

0 commit comments

Comments
 (0)