Skip to content

Commit 3562703

Browse files
saarrazzmodem
authored andcommitted
[Concepts] Fix incorrect control flow when TryAnnotateTypeConstraint annotates an invalid template-id
TryAnnotateTypeConstraint could annotate a template-id which doesn't end up being a type-constraint, in which case control flow would incorrectly flow into ParseImplicitInt. Reenter the loop in this case. Enable relevant tests for C++20. This required disabling typo-correction during TryAnnotateTypeConstraint and changing a test case which is broken due to a separate bug (will be reported and handled separately). (cherry picked from commit 19fccc5)
1 parent a36a14b commit 3562703

File tree

7 files changed

+25
-10
lines changed

7 files changed

+25
-10
lines changed

clang/include/clang/Sema/Sema.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -6885,7 +6885,8 @@ class Sema final {
68856885
QualType ObjectType, bool EnteringContext,
68866886
bool &MemberOfUnknownSpecialization,
68876887
SourceLocation TemplateKWLoc = SourceLocation(),
6888-
AssumedTemplateKind *ATK = nullptr);
6888+
AssumedTemplateKind *ATK = nullptr,
6889+
bool Disambiguation = false);
68896890

68906891
TemplateNameKind isTemplateName(Scope *S,
68916892
CXXScopeSpec &SS,
@@ -6894,7 +6895,8 @@ class Sema final {
68946895
ParsedType ObjectType,
68956896
bool EnteringContext,
68966897
TemplateTy &Template,
6897-
bool &MemberOfUnknownSpecialization);
6898+
bool &MemberOfUnknownSpecialization,
6899+
bool Disambiguation = false);
68986900

68996901
/// Try to resolve an undeclared template name as a type template.
69006902
///

clang/lib/Parse/ParseDecl.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -3252,6 +3252,9 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
32523252
goto DoneWithDeclSpec;
32533253
if (isTypeConstraintAnnotation())
32543254
continue;
3255+
if (NextToken().is(tok::annot_template_id))
3256+
// Might have been annotated by TryAnnotateTypeConstraint.
3257+
continue;
32553258
// Eat the scope spec so the identifier is current.
32563259
ConsumeAnnotationToken();
32573260
ParsedAttributesWithRange Attrs(AttrFactory);
@@ -3405,6 +3408,9 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
34053408
goto DoneWithDeclSpec;
34063409
if (isTypeConstraintAnnotation())
34073410
continue;
3411+
if (Tok.is(tok::annot_template_id))
3412+
// Might have been annotated by TryAnnotateTypeConstraint.
3413+
continue;
34083414
ParsedAttributesWithRange Attrs(AttrFactory);
34093415
if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) {
34103416
if (!Attrs.empty()) {

clang/lib/Parse/ParseTemplate.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,8 @@ bool Parser::TryAnnotateTypeConstraint() {
710710
/*ObjectType=*/ParsedType(),
711711
/*EnteringContext=*/false,
712712
PossibleConcept,
713-
MemberOfUnknownSpecialization);
713+
MemberOfUnknownSpecialization,
714+
/*Disambiguation=*/true);
714715
if (MemberOfUnknownSpecialization || !PossibleConcept ||
715716
TNK != TNK_Concept_template) {
716717
if (SS.isNotEmpty())

clang/lib/Sema/SemaTemplate.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ TemplateNameKind Sema::isTemplateName(Scope *S,
174174
ParsedType ObjectTypePtr,
175175
bool EnteringContext,
176176
TemplateTy &TemplateResult,
177-
bool &MemberOfUnknownSpecialization) {
177+
bool &MemberOfUnknownSpecialization,
178+
bool Disambiguation) {
178179
assert(getLangOpts().CPlusPlus && "No template names in C!");
179180

180181
DeclarationName TName;
@@ -204,7 +205,7 @@ TemplateNameKind Sema::isTemplateName(Scope *S,
204205
LookupResult R(*this, TName, Name.getBeginLoc(), LookupOrdinaryName);
205206
if (LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
206207
MemberOfUnknownSpecialization, SourceLocation(),
207-
&AssumedTemplate))
208+
&AssumedTemplate, Disambiguation))
208209
return TNK_Non_template;
209210

210211
if (AssumedTemplate != AssumedTemplateKind::None) {
@@ -371,7 +372,8 @@ bool Sema::LookupTemplateName(LookupResult &Found,
371372
bool EnteringContext,
372373
bool &MemberOfUnknownSpecialization,
373374
SourceLocation TemplateKWLoc,
374-
AssumedTemplateKind *ATK) {
375+
AssumedTemplateKind *ATK,
376+
bool Disambiguation) {
375377
if (ATK)
376378
*ATK = AssumedTemplateKind::None;
377379

@@ -494,8 +496,9 @@ bool Sema::LookupTemplateName(LookupResult &Found,
494496
}
495497
}
496498

497-
if (Found.empty() && !IsDependent) {
498-
// If we did not find any names, attempt to correct any typos.
499+
if (Found.empty() && !IsDependent && !Disambiguation) {
500+
// If we did not find any names, and this is not a disambiguation, attempt
501+
// to correct any typos.
499502
DeclarationName Name = Found.getLookupName();
500503
Found.clear();
501504
// Simple filter callback that, for keywords, only accepts the C++ *_cast

clang/test/SemaCXX/invalid-member-expr.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %clang_cc1 -fsyntax-only -verify %s
22
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
33
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4+
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
45

56
class X {};
67

clang/test/SemaCXX/typo-correction.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -fspell-checking-limit 0 -verify -Wno-c++11-extensions %s
2+
// RUN: %clang_cc1 -fspell-checking-limit 0 -verify -Wno-c++11-extensions -std=c++20 %s
23

34
namespace PR21817{
45
int a(-rsing[2]); // expected-error {{undeclared identifier 'rsing'; did you mean 'using'?}}
@@ -523,8 +524,8 @@ PR18685::BitVector Map; // expected-error-re {{no type named 'BitVector' in nam
523524
namespace shadowed_template {
524525
template <typename T> class Fizbin {}; // expected-note {{'::shadowed_template::Fizbin' declared here}}
525526
class Baz {
526-
int Fizbin();
527-
Fizbin<int> qux; // expected-error {{no template named 'Fizbin'; did you mean '::shadowed_template::Fizbin'?}}
527+
int Fizbin;
528+
Fizbin<int> qux; // expected-error {{no template named 'Fizbin'; did you mean '::shadowed_template::Fizbin'?}}
528529
};
529530
}
530531

clang/test/SemaTemplate/ms-lookup-template-base-classes.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -std=c++1y -fms-compatibility -fno-spell-checking -fsyntax-only -verify %s
2+
// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -std=c++20 -fms-compatibility -fno-spell-checking -fsyntax-only -verify %s
23

34

45
template <class T>

0 commit comments

Comments
 (0)