Skip to content

Commit 19fccc5

Browse files
committed
[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).
1 parent 2a3723e commit 19fccc5

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
@@ -6986,7 +6986,8 @@ class Sema final {
69866986
QualType ObjectType, bool EnteringContext,
69876987
bool &MemberOfUnknownSpecialization,
69886988
SourceLocation TemplateKWLoc = SourceLocation(),
6989-
AssumedTemplateKind *ATK = nullptr);
6989+
AssumedTemplateKind *ATK = nullptr,
6990+
bool Disambiguation = false);
69906991

69916992
TemplateNameKind isTemplateName(Scope *S,
69926993
CXXScopeSpec &SS,
@@ -6995,7 +6996,8 @@ class Sema final {
69956996
ParsedType ObjectType,
69966997
bool EnteringContext,
69976998
TemplateTy &Template,
6998-
bool &MemberOfUnknownSpecialization);
6999+
bool &MemberOfUnknownSpecialization,
7000+
bool Disambiguation = false);
69997001

70007002
/// Try to resolve an undeclared template name as a type template.
70017003
///

clang/lib/Parse/ParseDecl.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -3253,6 +3253,9 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
32533253
goto DoneWithDeclSpec;
32543254
if (isTypeConstraintAnnotation())
32553255
continue;
3256+
if (NextToken().is(tok::annot_template_id))
3257+
// Might have been annotated by TryAnnotateTypeConstraint.
3258+
continue;
32563259
// Eat the scope spec so the identifier is current.
32573260
ConsumeAnnotationToken();
32583261
ParsedAttributesWithRange Attrs(AttrFactory);
@@ -3406,6 +3409,9 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
34063409
goto DoneWithDeclSpec;
34073410
if (isTypeConstraintAnnotation())
34083411
continue;
3412+
if (Tok.is(tok::annot_template_id))
3413+
// Might have been annotated by TryAnnotateTypeConstraint.
3414+
continue;
34093415
ParsedAttributesWithRange Attrs(AttrFactory);
34103416
if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) {
34113417
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)