Skip to content

Commit 07f3388

Browse files
committed
Revert "[clang] Implement instantiation context note for checking template parameters (#126088)"
This reverts commit a24523a. This is causing significant compile-time regressions for C++ code, see: #126088 (comment)
1 parent c579ec6 commit 07f3388

File tree

97 files changed

+575
-648
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+575
-648
lines changed

clang/docs/ReleaseNotes.rst

-10
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,6 @@ Bug Fixes to C++ Support
296296
- Clang now uses the parameter location for abbreviated function templates in ``extern "C"``. (#GH46386)
297297
- Clang now correctly parses ``if constexpr`` expressions in immediate function context. (#GH123524)
298298

299-
Improvements to C++ diagnostics
300-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
301-
302-
- Clang now more consistently adds a note pointing to the relevant template
303-
parameter. Some diagnostics are reworded to better take advantage of this.
304-
- Template Template Parameter diagnostics now stop referring to template
305-
parameters as template arguments, in some circumstances, better hiding
306-
from the users template template parameter partial ordering arcana.
307-
308-
309299
Bug Fixes to AST Handling
310300
^^^^^^^^^^^^^^^^^^^^^^^^^
311301
- Fixed type checking when a statement expression ends in an l-value of atomic type. (#GH106576)

clang/include/clang/Basic/DiagnosticSemaKinds.td

+18-9
Original file line numberDiff line numberDiff line change
@@ -5210,11 +5210,16 @@ def err_template_unnamed_class : Error<
52105210
def err_template_param_list_different_arity : Error<
52115211
"%select{too few|too many}0 template parameters in template "
52125212
"%select{|template parameter }1redeclaration">;
5213+
def note_template_param_list_different_arity : Note<
5214+
"%select{too few|too many}0 template parameters in template template "
5215+
"argument">;
52135216
def note_template_prev_declaration : Note<
52145217
"previous template %select{declaration|template parameter}0 is here">;
52155218
def err_template_param_different_kind : Error<
52165219
"template parameter has a different kind in template "
52175220
"%select{|template parameter }0redeclaration">;
5221+
def note_template_param_different_kind : Note<
5222+
"template parameter has a different kind in template argument">;
52185223

52195224
def err_invalid_decl_specifier_in_nontype_parm : Error<
52205225
"invalid declaration specifier in template non-type parameter">;
@@ -5223,6 +5228,8 @@ def err_template_nontype_parm_different_type : Error<
52235228
"template non-type parameter has a different type %0 in template "
52245229
"%select{|template parameter }1redeclaration">;
52255230

5231+
def note_template_nontype_parm_different_type : Note<
5232+
"template non-type parameter has a different type %0 in template argument">;
52265233
def note_template_nontype_parm_prev_declaration : Note<
52275234
"previous non-type template parameter with type %0 is here">;
52285235
def err_template_nontype_parm_bad_type : Error<
@@ -5313,15 +5320,10 @@ def err_template_missing_args : Error<
53135320
"%select{class template|function template|variable template|alias template|"
53145321
"template template parameter|concept|template}0 %1 requires template "
53155322
"arguments">;
5316-
def err_template_param_missing_arg : Error<
5317-
"missing template argument for template parameter">;
5318-
def err_template_template_param_missing_param : Error<
5319-
"no template parameter in this template template parameter "
5320-
"corresponds to non-defaulted template parameter of argument template">;
5321-
def err_template_too_many_args : Error<
5322-
"too many template arguments for "
5323+
def err_template_arg_list_different_arity : Error<
5324+
"%select{too few|too many}0 template arguments for "
53235325
"%select{class template|function template|variable template|alias template|"
5324-
"template template parameter|concept|template}0 %1">;
5326+
"template template parameter|concept|template}1 %2">;
53255327
def note_template_decl_here : Note<"template is declared here">;
53265328
def note_template_decl_external : Note<
53275329
"template declaration from hidden source: %0">;
@@ -5359,8 +5361,11 @@ def err_template_arg_not_valid_template : Error<
53595361
"template parameter">;
53605362
def note_template_arg_refers_here_func : Note<
53615363
"template argument refers to function template %0, here">;
5364+
def err_template_arg_template_params_mismatch : Error<
5365+
"template template argument has different template parameters than its "
5366+
"corresponding template template parameter">;
53625367
def note_template_arg_template_params_mismatch : Note<
5363-
"template template argument is incompatible with its "
5368+
"template template argument has different template parameters than its "
53645369
"corresponding template template parameter">;
53655370
def err_non_deduced_mismatch : Error<
53665371
"could not match %diff{$ against $|types}0,1">;
@@ -5922,6 +5927,10 @@ def err_template_parameter_pack_non_pack : Error<
59225927
"%select{template type|non-type template|template template}0 parameter"
59235928
"%select{| pack}1 conflicts with previous %select{template type|"
59245929
"non-type template|template template}0 parameter%select{ pack|}1">;
5930+
def note_template_parameter_pack_non_pack : Note<
5931+
"%select{template type|non-type template|template template}0 parameter"
5932+
"%select{| pack}1 does not match %select{template type|non-type template"
5933+
"|template template}0 parameter%select{ pack|}1 in template argument">;
59255934
def note_template_parameter_pack_here : Note<
59265935
"previous %select{template type|non-type template|template template}0 "
59275936
"parameter%select{| pack}1 declared here">;

clang/include/clang/Sema/Sema.h

+9-28
Original file line numberDiff line numberDiff line change
@@ -11833,7 +11833,7 @@ class Sema final : public SemaBase {
1183311833
bool *ConstraintsNotSatisfied = nullptr);
1183411834

1183511835
bool CheckTemplateTypeArgument(
11836-
TemplateArgumentLoc &Arg,
11836+
TemplateTypeParmDecl *Param, TemplateArgumentLoc &Arg,
1183711837
SmallVectorImpl<TemplateArgument> &SugaredConverted,
1183811838
SmallVectorImpl<TemplateArgument> &CanonicalConverted);
1183911839

@@ -11869,13 +11869,9 @@ class Sema final : public SemaBase {
1186911869
bool PartialOrdering,
1187011870
bool *StrictPackMatch);
1187111871

11872-
/// Print the given named declaration to a string,
11873-
/// using the current PrintingPolicy, except that
11874-
/// TerseOutput will always be set.
11875-
SmallString<128> toTerseString(const NamedDecl &D) const;
11876-
1187711872
void NoteTemplateLocation(const NamedDecl &Decl,
1187811873
std::optional<SourceRange> ParamRange = {});
11874+
void NoteTemplateParameterLocation(const NamedDecl &Decl);
1187911875

1188011876
/// Given a non-type template argument that refers to a
1188111877
/// declaration and the type of its corresponding non-type template
@@ -11990,13 +11986,15 @@ class Sema final : public SemaBase {
1199011986
bool TemplateParameterListsAreEqual(
1199111987
const TemplateCompareNewDeclInfo &NewInstFrom, TemplateParameterList *New,
1199211988
const NamedDecl *OldInstFrom, TemplateParameterList *Old, bool Complain,
11993-
TemplateParameterListEqualKind Kind);
11989+
TemplateParameterListEqualKind Kind,
11990+
SourceLocation TemplateArgLoc = SourceLocation());
1199411991

11995-
bool TemplateParameterListsAreEqual(TemplateParameterList *New,
11996-
TemplateParameterList *Old, bool Complain,
11997-
TemplateParameterListEqualKind Kind) {
11992+
bool TemplateParameterListsAreEqual(
11993+
TemplateParameterList *New, TemplateParameterList *Old, bool Complain,
11994+
TemplateParameterListEqualKind Kind,
11995+
SourceLocation TemplateArgLoc = SourceLocation()) {
1199811996
return TemplateParameterListsAreEqual(nullptr, New, nullptr, Old, Complain,
11999-
Kind);
11997+
Kind, TemplateArgLoc);
1200011998
}
1200111999

1200212000
/// Check whether a template can be declared within this scope.
@@ -12876,11 +12874,6 @@ class Sema final : public SemaBase {
1287612874

1287712875
/// We are performing partial ordering for template template parameters.
1287812876
PartialOrderingTTP,
12879-
12880-
/// We are Checking a Template Parameter, so for any diagnostics which
12881-
/// occur in this scope, we will add a context note which points to this
12882-
/// template parameter.
12883-
CheckTemplateParameter,
1288412877
} Kind;
1288512878

1288612879
/// Was the enclosing context a non-instantiation SFINAE context?
@@ -13108,11 +13101,6 @@ class Sema final : public SemaBase {
1310813101
PartialOrderingTTP, TemplateDecl *PArg,
1310913102
SourceRange InstantiationRange = SourceRange());
1311013103

13111-
struct CheckTemplateParameter {};
13112-
/// \brief Note that we are checking a template parameter.
13113-
InstantiatingTemplate(Sema &SemaRef, CheckTemplateParameter,
13114-
NamedDecl *Param);
13115-
1311613104
/// Note that we have finished instantiating this template.
1311713105
void Clear();
1311813106

@@ -13146,13 +13134,6 @@ class Sema final : public SemaBase {
1314613134
InstantiatingTemplate &operator=(const InstantiatingTemplate &) = delete;
1314713135
};
1314813136

13149-
/// For any diagnostics which occur within its scope, adds a context note
13150-
/// pointing to the declaration of the template parameter.
13151-
struct CheckTemplateParameterRAII : InstantiatingTemplate {
13152-
CheckTemplateParameterRAII(Sema &S, NamedDecl *Param)
13153-
: InstantiatingTemplate(S, CheckTemplateParameter(), Param) {}
13154-
};
13155-
1315613137
bool SubstTemplateArgument(const TemplateArgumentLoc &Input,
1315713138
const MultiLevelTemplateArgumentList &TemplateArgs,
1315813139
TemplateArgumentLoc &Output,

clang/lib/Frontend/FrontendActions.cpp

+8-16
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,7 @@ class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
403403
}
404404

405405
private:
406-
static std::optional<std::string>
407-
toString(CodeSynthesisContext::SynthesisKind Kind) {
406+
static std::string toString(CodeSynthesisContext::SynthesisKind Kind) {
408407
switch (Kind) {
409408
case CodeSynthesisContext::TemplateInstantiation:
410409
return "TemplateInstantiation";
@@ -462,25 +461,21 @@ class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
462461
return "TypeAliasTemplateInstantiation";
463462
case CodeSynthesisContext::PartialOrderingTTP:
464463
return "PartialOrderingTTP";
465-
case CodeSynthesisContext::CheckTemplateParameter:
466-
return std::nullopt;
467464
}
468-
return std::nullopt;
465+
return "";
469466
}
470467

471468
template <bool BeginInstantiation>
472469
static void displayTemplightEntry(llvm::raw_ostream &Out, const Sema &TheSema,
473470
const CodeSynthesisContext &Inst) {
474471
std::string YAML;
475472
{
476-
std::optional<TemplightEntry> Entry =
477-
getTemplightEntry<BeginInstantiation>(TheSema, Inst);
478-
if (!Entry)
479-
return;
480473
llvm::raw_string_ostream OS(YAML);
481474
llvm::yaml::Output YO(OS);
475+
TemplightEntry Entry =
476+
getTemplightEntry<BeginInstantiation>(TheSema, Inst);
482477
llvm::yaml::EmptyContext Context;
483-
llvm::yaml::yamlize(YO, *Entry, true, Context);
478+
llvm::yaml::yamlize(YO, Entry, true, Context);
484479
}
485480
Out << "---" << YAML << "\n";
486481
}
@@ -560,13 +555,10 @@ class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
560555
}
561556

562557
template <bool BeginInstantiation>
563-
static std::optional<TemplightEntry>
564-
getTemplightEntry(const Sema &TheSema, const CodeSynthesisContext &Inst) {
558+
static TemplightEntry getTemplightEntry(const Sema &TheSema,
559+
const CodeSynthesisContext &Inst) {
565560
TemplightEntry Entry;
566-
std::optional<std::string> Kind = toString(Inst.Kind);
567-
if (!Kind)
568-
return std::nullopt;
569-
Entry.Kind = *Kind;
561+
Entry.Kind = toString(Inst.Kind);
570562
Entry.Event = BeginInstantiation ? "Begin" : "End";
571563
llvm::raw_string_ostream OS(Entry.Name);
572564
printEntryName(TheSema, Inst.Entity, OS);

clang/lib/Sema/SemaInit.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -7271,7 +7271,7 @@ static void CheckCXX98CompatAccessibleCopy(Sema &S,
72717271

72727272
void InitializationSequence::PrintInitLocationNote(Sema &S,
72737273
const InitializedEntity &Entity) {
7274-
if (Entity.isParameterKind() && Entity.getDecl()) {
7274+
if (Entity.isParamOrTemplateParamKind() && Entity.getDecl()) {
72757275
if (Entity.getDecl()->getLocation().isInvalid())
72767276
return;
72777277

@@ -7280,8 +7280,9 @@ void InitializationSequence::PrintInitLocationNote(Sema &S,
72807280
<< Entity.getDecl()->getDeclName();
72817281
else
72827282
S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here);
7283-
} else if (Entity.getKind() == InitializedEntity::EK_RelatedResult &&
7284-
Entity.getMethodDecl())
7283+
}
7284+
else if (Entity.getKind() == InitializedEntity::EK_RelatedResult &&
7285+
Entity.getMethodDecl())
72857286
S.Diag(Entity.getMethodDecl()->getLocation(),
72867287
diag::note_method_return_type_change)
72877288
<< Entity.getMethodDecl()->getDeclName();

clang/lib/Sema/SemaLambda.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1506,13 +1506,14 @@ void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro,
15061506
TemplateParameterList *TemplateParams =
15071507
getGenericLambdaTemplateParameterList(LSI, *this);
15081508
if (TemplateParams) {
1509-
for (auto *TP : TemplateParams->asArray()) {
1509+
for (const auto *TP : TemplateParams->asArray()) {
15101510
if (!TP->getIdentifier())
15111511
continue;
1512-
CheckTemplateParameterRAII CTP(*this, TP);
15131512
for (const auto &Capture : Intro.Captures) {
1514-
if (Capture.Id == TP->getIdentifier())
1513+
if (Capture.Id == TP->getIdentifier()) {
15151514
Diag(Capture.Loc, diag::err_template_param_shadow) << Capture.Id;
1515+
NoteTemplateParameterLocation(*TP);
1516+
}
15161517
}
15171518
}
15181519
}

clang/lib/Sema/SemaLookup.cpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -1580,13 +1580,9 @@ llvm::DenseSet<Module*> &Sema::getLookupModules() {
15801580
unsigned N = CodeSynthesisContexts.size();
15811581
for (unsigned I = CodeSynthesisContextLookupModules.size();
15821582
I != N; ++I) {
1583-
auto &Ctx = CodeSynthesisContexts[I];
1584-
// FIXME: Are there any other context kinds that shouldn't be looked at
1585-
// here?
1586-
if (Ctx.Kind == CodeSynthesisContext::PartialOrderingTTP ||
1587-
Ctx.Kind == CodeSynthesisContext::CheckTemplateParameter)
1588-
continue;
1589-
Module *M = Ctx.Entity ? getDefiningModule(*this, Ctx.Entity) : nullptr;
1583+
Module *M = CodeSynthesisContexts[I].Entity ?
1584+
getDefiningModule(*this, CodeSynthesisContexts[I].Entity) :
1585+
nullptr;
15901586
if (M && !LookupModulesCache.insert(M).second)
15911587
M = nullptr;
15921588
CodeSynthesisContextLookupModules.push_back(M);
@@ -3707,8 +3703,7 @@ Sema::LookupLiteralOperator(Scope *S, LookupResult &R,
37073703
TemplateParameterList *Params = FD->getTemplateParameters();
37083704
if (Params->size() == 1) {
37093705
IsTemplate = true;
3710-
NamedDecl *Param = Params->getParam(0);
3711-
if (!Param->isTemplateParameterPack() && !StringLit) {
3706+
if (!Params->getParam(0)->isTemplateParameterPack() && !StringLit) {
37123707
// Implied but not stated: user-defined integer and floating literals
37133708
// only ever use numeric literal operator templates, not templates
37143709
// taking a parameter of class type.
@@ -3721,7 +3716,6 @@ Sema::LookupLiteralOperator(Scope *S, LookupResult &R,
37213716
if (StringLit) {
37223717
SFINAETrap Trap(*this);
37233718
CheckTemplateArgumentInfo CTAI;
3724-
CheckTemplateParameterRAII CTP(*this, Param);
37253719
TemplateArgumentLoc Arg(TemplateArgument(StringLit), StringLit);
37263720
if (CheckTemplateArgument(
37273721
Params->getParam(0), Arg, FD, R.getNameLoc(), R.getNameLoc(),

0 commit comments

Comments
 (0)