Skip to content

Commit 71afd06

Browse files
committed
c++: ICE with -Wmismatched-tags and member template [PR106259]
-Wmismatched-tags warns about the (harmless) struct/class mismatch. For, e.g., template<typename T> struct A { }; class A<int> a; it works by adding A<T> to the class2loc hash table while parsing the class-head and then, while parsing the elaborate type-specifier, we add A<int>. At the end of c_parse_file we go through the table and warn about the class-key mismatches. In this PR we crash though; we have template<typename T> struct A { template<typename U> struct W { }; }; struct A<int>::W<int> w; // #1 where while parsing A and #1 we've stashed A<T> A<T>::W<U> A<int>::W<int> into class2loc. Then in class_decl_loc_t::diag_mismatched_tags TYPE is A<int>::W<int>, and specialization_of gets us A<int>::W<U>, which is not in class2loc, so we crash on gcc_assert (cdlguide). But it's OK not to have found A<int>::W<U>, we should just look one "level" up, that is, A<T>::W<U>. It's important to handle class specializations, so e.g. template<> struct A<char> { template<typename U> class W { }; }; where W's class-key is different than in the primary template above, so we should warn depending on whether we're looking into A<char> or into a different instantiation. PR c++/106259 gcc/cp/ChangeLog: * parser.cc (class_decl_loc_t::diag_mismatched_tags): If the first lookup of SPEC didn't find anything, try to look for most_general_template. gcc/testsuite/ChangeLog: * g++.dg/warn/Wmismatched-tags-11.C: New test.
1 parent da19e37 commit 71afd06

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

gcc/cp/parser.cc

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34473,14 +34473,32 @@ class_decl_loc_t::diag_mismatched_tags (tree type_decl)
3447334473
be (and inevitably is) at index zero. */
3447434474
tree spec = specialization_of (type);
3447534475
cdlguide = class2loc.get (spec);
34476+
/* It's possible that we didn't find SPEC. Consider:
34477+
34478+
template<typename T> struct A {
34479+
template<typename U> struct W { };
34480+
};
34481+
struct A<int>::W<int> w; // #1
34482+
34483+
where while parsing A and #1 we've stashed
34484+
A<T>
34485+
A<T>::W<U>
34486+
A<int>::W<int>
34487+
into CLASS2LOC. If TYPE is A<int>::W<int>, specialization_of
34488+
will yield A<int>::W<U> which may be in CLASS2LOC if we had
34489+
an A<int> class specialization, but otherwise won't be in it.
34490+
So try to look up A<T>::W<U>. */
34491+
if (!cdlguide)
34492+
{
34493+
spec = DECL_TEMPLATE_RESULT (most_general_template (spec));
34494+
cdlguide = class2loc.get (spec);
34495+
}
34496+
/* Now we really should have found something. */
3447634497
gcc_assert (cdlguide != NULL);
3447734498
}
34478-
else
34479-
{
34480-
/* Skip declarations that consistently use the same class-key. */
34481-
if (def_class_key != none_type)
34482-
return;
34483-
}
34499+
/* Skip declarations that consistently use the same class-key. */
34500+
else if (def_class_key != none_type)
34501+
return;
3448434502

3448534503
/* Set if a definition for the class has been seen. */
3448634504
const bool def_p = cdlguide->def_p ();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// PR c++/106259
2+
// { dg-do compile }
3+
// { dg-options "-Wmismatched-tags" }
4+
5+
template<typename T> struct A {
6+
template<typename U>
7+
struct W { };
8+
};
9+
10+
template<>
11+
struct A<char> {
12+
template<typename U>
13+
class W { };
14+
};
15+
16+
void
17+
g ()
18+
{
19+
struct A<char>::W<int> w1; // { dg-warning "mismatched" }
20+
struct A<int>::W<int> w2;
21+
class A<char>::W<int> w3;
22+
class A<int>::W<int> w4; // { dg-warning "mismatched" }
23+
}

0 commit comments

Comments
 (0)