From: Serge Pavlov Date: Fri, 10 Jun 2016 04:39:07 +0000 (+0000) Subject: Fix recognition of shadowed template parameter X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=13f56e29a745a736abae6e82eb082c93c8f238d9;p=clang Fix recognition of shadowed template parameter Crash reported in PR28023 is caused by the fact that non-type template parameters are found by tag name lookup. In the code provided in that PR: template struct A { struct B { template friend struct V; }; }; the template parameter V is found when lookup for redeclarations of 'struct V' is made. Latter on the error about shadowing of 'V' is emitted but the semantic context of 'struct V' is already determined wrong: 'struct A' instead of translation unit. The fix moves the check for shadowing toward the beginning of the method and thus prevents from wrong context calculations. This change fixes PR28023. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@272366 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 73b5c44b59..2ea242fc95 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -929,6 +929,13 @@ Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, if (Previous.begin() != Previous.end()) PrevDecl = (*Previous.begin())->getUnderlyingDecl(); + if (PrevDecl && PrevDecl->isTemplateParameter()) { + // Maybe we will complain about the shadowed template parameter. + DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); + // Just pretend that we didn't see the previous declaration. + PrevDecl = nullptr; + } + // If there is a previous declaration with the same name, check // whether this is a valid redeclaration. ClassTemplateDecl *PrevClassTemplate @@ -1054,12 +1061,7 @@ Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, // definition, as part of error recovery? return true; } - } - } else if (PrevDecl && PrevDecl->isTemplateParameter()) { - // Maybe we will complain about the shadowed template parameter. - DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); - // Just pretend that we didn't see the previous declaration. - PrevDecl = nullptr; + } } else if (PrevDecl) { // C++ [temp]p5: // A class template shall not have the same name as any other diff --git a/test/CXX/temp/temp.res/temp.local/p6.cpp b/test/CXX/temp/temp.res/temp.local/p6.cpp index 843b45543f..e2aa0ff344 100644 --- a/test/CXX/temp/temp.res/temp.local/p6.cpp +++ b/test/CXX/temp/temp.res/temp.local/p6.cpp @@ -5,11 +5,11 @@ namespace N {} template struct X {}; // expected-error {{declaration of 'T' shadows template parameter}} -template struct Y { // expected-note 17{{declared here}} +template struct Y { // expected-note 18{{declared here}} template struct A {}; // expected-error {{declaration of 'T' shadows template parameter}} struct B { - template struct T {}; // FIXME: desired-error {{declaration of 'T' shadows template parameter}} + template struct T {}; // expected-error {{declaration of 'T' shadows template parameter}} }; struct C { template void T(); // expected-error {{declaration of 'T' shadows template parameter}} @@ -65,11 +65,11 @@ template struct Y { // expected-note 17{{declared here}} friend struct T; // expected-error {{declaration of 'T' shadows template parameter}} }; -template struct Z { // expected-note 15{{declared here}} +template struct Z { // expected-note 16{{declared here}} template struct A {}; // expected-error {{declaration of 'T' shadows template parameter}} struct B { - template struct T {}; // FIXME: desired-error {{declaration of 'T' shadows template parameter}} + template struct T {}; // expected-error {{declaration of 'T' shadows template parameter}} }; struct C { template void T(); // expected-error {{declaration of 'T' shadows template parameter}} @@ -129,7 +129,8 @@ void f(int T) {} // expected-error {{declaration of 'T' shadows template paramet // FIXME: These are ill-formed: a template-parameter shall not have the same name as the template name. namespace A { - template struct T {}; + template struct T {}; // expected-error{{declaration of 'T' shadows template parameter}} + // expected-note@-1{{template parameter is declared here}} } namespace B { template void T() {} @@ -137,3 +138,13 @@ namespace B { namespace C { template int T; } + +namespace PR28023 { +template // expected-note{{template parameter is declared here}} +struct A { + struct B { + template friend struct V; // expected-error{{declaration of 'V' shadows template parameter}} + }; +}; +A<0>::B a; +}