Skip to content

Commit 7313a90

Browse files
committed
[Sema] Generalize the 'declared here' diagnostic
1 parent 3d1bfcb commit 7313a90

File tree

11 files changed

+66
-62
lines changed

11 files changed

+66
-62
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,16 @@ enum class DescriptiveDeclKind : uint8_t {
122122
PrecedenceGroup,
123123
TypeAlias,
124124
GenericTypeParam,
125-
AssociatedType,
125+
AssociatedType,
126+
Type,
126127
Enum,
127128
Struct,
128129
Class,
129130
Protocol,
130131
GenericEnum,
131132
GenericStruct,
132133
GenericClass,
134+
GenericType,
133135
Subscript,
134136
Constructor,
135137
Destructor,
@@ -149,6 +151,7 @@ enum class DescriptiveDeclKind : uint8_t {
149151
EnumElement,
150152
Module,
151153
MissingMember,
154+
Requirement,
152155
};
153156

154157
/// Keeps track of stage of circularity checking for the given protocol.

include/swift/AST/DiagnosticsSema.def

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,20 @@
3737
DIAG(NOTE,ID,Options,Text,Signature)
3838
#endif
3939

40-
41-
NOTE(type_declared_here,none,
42-
"type declared here", ())
40+
NOTE(kind_declname_declared_here,none,
41+
"%0 %1 declared here", (DescriptiveDeclKind, DeclName))
42+
NOTE(kind_identifier_declared_here,none,
43+
"%0 %1 declared here", (DescriptiveDeclKind, Identifier))
4344
NOTE(decl_declared_here,none,
44-
"%0 declared here", (DeclName))
45+
"%0 declared here", (DeclName))
46+
NOTE(identifier_declared_here,none,
47+
"%0 declared here", (Identifier))
48+
NOTE(kind_declared_here,none,
49+
"%0 declared here", (DescriptiveDeclKind))
4550
NOTE(implicit_member_declared_here,none,
46-
"%1 '%0' is implicitly declared", (StringRef, StringRef))
51+
"%1 '%0' is implicitly declared", (StringRef, StringRef))
4752
NOTE(extended_type_declared_here,none,
48-
"extended type declared here", ())
53+
"extended type declared here", ())
4954

5055
//------------------------------------------------------------------------------
5156
// Constraint solver diagnostics
@@ -759,8 +764,6 @@ ERROR(precedence_group_lower_within_module,none,
759764
"precedence group cannot be given lower precedence than group in same"
760765
" module; make the other precedence group higher than this one instead",
761766
())
762-
NOTE(precedence_group_declared_here,none,"precedence group declared here",
763-
())
764767
ERROR(precedence_group_redeclared,none,
765768
"precedence group redeclared", ())
766769
NOTE(previous_precedence_group_decl,none,
@@ -1682,8 +1685,6 @@ NOTE(protocol_witness_nonconform_type,none,
16821685
"possibly intended match %0 does not "
16831686
"%select{inherit from|conform to}2 %1", (Type, Type, bool))
16841687

1685-
NOTE(protocol_requirement_here,none,
1686-
"requirement %0 declared here", (DeclName))
16871688
NOTE(protocol_conformance_here,none,
16881689
"%select{|class }0%1 declares conformance to protocol %2 here",
16891690
(bool, DeclName, DeclName))
@@ -1746,8 +1747,6 @@ ERROR(typealias_outside_of_protocol,none,
17461747

17471748
ERROR(circular_protocol_def,none,
17481749
"circular protocol inheritance %0", (StringRef))
1749-
NOTE(protocol_here,none,
1750-
"protocol %0 declared here", (Identifier))
17511750
ERROR(objc_protocol_inherits_non_objc_protocol,none,
17521751
"@objc protocol %0 cannot refine non-@objc protocol %1", (Type, Type))
17531752
WARNING(protocol_composition_with_postfix,none,
@@ -2065,8 +2064,6 @@ ERROR(superclass_of_open_not_open,none,
20652064
"superclass %0 of open class must be open", (Type))
20662065
ERROR(circular_class_inheritance,none,
20672066
"circular class inheritance %0", (StringRef))
2068-
NOTE(class_here,none,
2069-
"class %0 declared here", (Identifier))
20702067
ERROR(inheritance_from_final_class,none,
20712068
"inheritance from a final class %0", (Identifier))
20722069
ERROR(inheritance_from_unspecialized_objc_generic_class,none,
@@ -2134,8 +2131,6 @@ WARNING(enum_raw_type_access_warn,none,
21342131
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
21352132
(bool, AccessLevel, AccessLevel, bool))
21362133

2137-
NOTE(enum_here,none,
2138-
"enum %0 declared here", (Identifier))
21392134
ERROR(empty_enum_raw_type,none,
21402135
"an enum with no cases cannot declare a raw type", ())
21412136
ERROR(enum_raw_value_without_raw_type,none,
@@ -2240,8 +2235,6 @@ ERROR(property_behavior_not_protocol,none,
22402235
"property behavior name must refer to a protocol", ())
22412236
ERROR(property_behavior_protocol_reqt_ambiguous,none,
22422237
"property behavior protocol has ambiguous %0 member", (Identifier))
2243-
NOTE(property_behavior_protocol_reqt_here,none,
2244-
"%0 declared here", (Identifier))
22452238
ERROR(property_behavior_protocol_no_value,none,
22462239
"property behavior protocol does not have a 'value' property", ())
22472240
ERROR(property_behavior_protocol_no_initStorage,none,
@@ -2675,8 +2668,6 @@ ERROR(type_parameter_count_mismatch,none,
26752668
(Identifier, unsigned, unsigned, bool))
26762669
ERROR(generic_type_requires_arguments,none,
26772670
"reference to generic type %0 requires arguments in <...>", (Type))
2678-
NOTE(generic_type_declared_here,none,
2679-
"generic type %0 declared here", (Identifier))
26802671
NOTE(descriptive_generic_type_declared_here,none,
26812672
"%0 declared here", (StringRef))
26822673

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,8 +2837,8 @@ bool ASTContext::diagnoseObjCUnsatisfiedOptReqConflicts(SourceFile &sf) {
28372837
true,
28382838
classDecl->getFullName(),
28392839
protocolName);
2840-
Diags.diagnose(req, diag::protocol_requirement_here,
2841-
reqDiagInfo.second);
2840+
Diags.diagnose(req, diag::kind_declname_declared_here,
2841+
DescriptiveDeclKind::Requirement, reqDiagInfo.second);
28422842

28432843
anyDiagnosed = true;
28442844
}

lib/AST/Decl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,15 @@ StringRef Decl::getDescriptiveKindName(DescriptiveDeclKind K) {
252252
ENTRY(TypeAlias, "type alias");
253253
ENTRY(GenericTypeParam, "generic parameter");
254254
ENTRY(AssociatedType, "associated type");
255+
ENTRY(Type, "type");
255256
ENTRY(Enum, "enum");
256257
ENTRY(Struct, "struct");
257258
ENTRY(Class, "class");
258259
ENTRY(Protocol, "protocol");
259260
ENTRY(GenericEnum, "generic enum");
260261
ENTRY(GenericStruct, "generic struct");
261262
ENTRY(GenericClass, "generic class");
263+
ENTRY(GenericType, "generic type");
262264
ENTRY(Subscript, "subscript");
263265
ENTRY(Constructor, "initializer");
264266
ENTRY(Destructor, "deinitializer");
@@ -278,6 +280,7 @@ StringRef Decl::getDescriptiveKindName(DescriptiveDeclKind K) {
278280
ENTRY(EnumElement, "enum element");
279281
ENTRY(Module, "module");
280282
ENTRY(MissingMember, "missing member placeholder");
283+
ENTRY(Requirement, "requirement");
281284
}
282285
#undef ENTRY
283286
llvm_unreachable("bad DescriptiveDeclKind");

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,8 +1555,8 @@ namespace {
15551555
false)
15561556
.highlight(SourceRange(expr->getLAngleLoc(),
15571557
expr->getRAngleLoc()));
1558-
tc.diagnose(bgt->getDecl(), diag::generic_type_declared_here,
1559-
bgt->getDecl()->getName());
1558+
tc.diagnose(bgt->getDecl(), diag::kind_identifier_declared_here,
1559+
DescriptiveDeclKind::GenericType, bgt->getDecl()->getName());
15601560
return Type();
15611561
}
15621562

lib/Sema/CodeSynthesis.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,11 +1738,9 @@ void swift::maybeAddAccessorsToVariable(VarDecl *var, TypeChecker &TC) {
17381738
TC.diagnose(behavior->getLoc(),
17391739
diag::property_behavior_protocol_reqt_ambiguous,
17401740
TC.Context.Id_value);
1741-
TC.diagnose(valueProp->getLoc(),
1742-
diag::property_behavior_protocol_reqt_here,
1741+
TC.diagnose(valueProp->getLoc(), diag::identifier_declared_here,
17431742
TC.Context.Id_value);
1744-
TC.diagnose(foundVar->getLoc(),
1745-
diag::property_behavior_protocol_reqt_here,
1743+
TC.diagnose(foundVar->getLoc(), diag::identifier_declared_here,
17461744
TC.Context.Id_value);
17471745
break;
17481746
}

lib/Sema/DerivedConformances.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ bool DerivedConformance::checkAndDiagnoseDisallowedContext(
402402
TC.diagnose(ConformanceDecl->getLoc(),
403403
diag::cannot_synthesize_in_crossfile_extension,
404404
getProtocolType());
405-
TC.diagnose(Nominal->getLoc(), diag::type_declared_here);
405+
TC.diagnose(Nominal->getLoc(), diag::kind_declared_here,
406+
DescriptiveDeclKind::Type);
406407
return true;
407408
}
408409

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ class FindCapturedVars : public ASTWalker {
271271
NTD->getDescriptiveKind(),
272272
D->getBaseName().getIdentifier());
273273

274-
TC.diagnose(NTD->getLoc(), diag::type_declared_here);
274+
TC.diagnose(NTD->getLoc(), diag::kind_declared_here,
275+
DescriptiveDeclKind::Type);
275276

276277
TC.diagnose(D, diag::decl_declared_here, D->getFullName());
277278
return { false, DRE };

lib/Sema/TypeCheckDecl.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ static void breakInheritanceCycle(EnumDecl *enumDecl) {
707707
template<typename T>
708708
static void checkCircularity(TypeChecker &tc, T *decl,
709709
Diag<StringRef> circularDiag,
710-
Diag<Identifier> declHereDiag,
710+
DescriptiveDeclKind declKind,
711711
SmallVectorImpl<T *> &path) {
712712
switch (decl->getCircularityCheck()) {
713713
case CircularityCheck::Checked:
@@ -750,7 +750,8 @@ static void checkCircularity(TypeChecker &tc, T *decl,
750750
// Diagnose the cycle.
751751
tc.diagnose(decl->getLoc(), circularDiag, pathStr);
752752
for (auto i = cycleStart + 1, iEnd = path.end(); i != iEnd; ++i) {
753-
tc.diagnose(*i, declHereDiag, (*i)->getName());
753+
tc.diagnose(*i, diag::kind_identifier_declared_here,
754+
declKind, (*i)->getName());
754755
}
755756

756757
// Set this declaration as invalid, then break the cycle somehow.
@@ -766,7 +767,7 @@ static void checkCircularity(TypeChecker &tc, T *decl,
766767
decl->setCircularityCheck(CircularityCheck::Checking);
767768
T *scratch = nullptr;
768769
for (auto inherited : getInheritedForCycleCheck(tc, decl, &scratch)) {
769-
checkCircularity(tc, inherited, circularDiag, declHereDiag, path);
770+
checkCircularity(tc, inherited, circularDiag, declKind, path);
770771
}
771772
decl->setCircularityCheck(CircularityCheck::Checked);
772773
path.pop_back();
@@ -1835,7 +1836,7 @@ static void highlightOffendingType(TypeChecker &TC, InFlightDiagnostic &diag,
18351836

18361837
if (auto CITR = dyn_cast<ComponentIdentTypeRepr>(complainRepr)) {
18371838
const ValueDecl *VD = CITR->getBoundDecl();
1838-
TC.diagnose(VD, diag::type_declared_here);
1839+
TC.diagnose(VD, diag::kind_declared_here, DescriptiveDeclKind::Type);
18391840
}
18401841
}
18411842

@@ -3424,11 +3425,9 @@ static void checkVarBehavior(VarDecl *decl, TypeChecker &TC) {
34243425
diag::property_behavior_protocol_reqt_ambiguous,
34253426
TC.Context.Id_initStorage);
34263427
TC.diagnose(defaultInitStorageDecl->getLoc(),
3427-
diag::property_behavior_protocol_reqt_here,
3428-
TC.Context.Id_initStorage);
3428+
diag::identifier_declared_here, TC.Context.Id_initStorage);
34293429
TC.diagnose(parameterizedInitStorageDecl->getLoc(),
3430-
diag::property_behavior_protocol_reqt_here,
3431-
TC.Context.Id_initStorage);
3430+
diag::identifier_declared_here, TC.Context.Id_initStorage);
34323431
conformance->setInvalid();
34333432
continue;
34343433
}
@@ -3497,8 +3496,7 @@ static void checkVarBehavior(VarDecl *decl, TypeChecker &TC) {
34973496
TC.diagnose(behavior->getLoc(),
34983497
diag::property_behavior_invalid_parameter_reqt,
34993498
behaviorProto->getName());
3500-
TC.diagnose(varReqt->getLoc(),
3501-
diag::property_behavior_protocol_reqt_here,
3499+
TC.diagnose(varReqt->getLoc(), diag::identifier_declared_here,
35023500
TC.Context.Id_parameter);
35033501
conformance->setInvalid();
35043502
continue;
@@ -3768,7 +3766,8 @@ void TypeChecker::validateDecl(PrecedenceGroupDecl *PGD) {
37683766
== dc->getParentModule()) {
37693767
if (!PGD->isInvalid()) {
37703768
diagnose(rel.NameLoc, diag::precedence_group_lower_within_module);
3771-
diagnose(group->getNameLoc(), diag::precedence_group_declared_here);
3769+
diagnose(group->getNameLoc(), diag::kind_declared_here,
3770+
DescriptiveDeclKind::PrecedenceGroup);
37723771
isInvalid = true;
37733772
}
37743773
} else {
@@ -4472,7 +4471,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
44724471
SmallVector<EnumDecl *, 8> path;
44734472
path.push_back(ED);
44744473
checkCircularity(TC, ED, diag::circular_enum_inheritance,
4475-
diag::enum_here, path);
4474+
DescriptiveDeclKind::Enum, path);
44764475
}
44774476

44784477
for (Decl *member : ED->getMembers())
@@ -4632,7 +4631,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
46324631
SmallVector<ClassDecl *, 8> path;
46334632
path.push_back(CD);
46344633
checkCircularity(TC, CD, diag::circular_class_inheritance,
4635-
diag::class_here, path);
4634+
DescriptiveDeclKind::Class, path);
46364635
}
46374636

46384637
for (Decl *Member : CD->getMembers())
@@ -4756,7 +4755,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
47564755
SmallVector<ProtocolDecl *, 8> path;
47574756
path.push_back(PD);
47584757
checkCircularity(TC, PD, diag::circular_protocol_def,
4759-
diag::protocol_here, path);
4758+
DescriptiveDeclKind::Protocol, path);
47604759

47614760
// Make sure the parent protocols have been fully validated.
47624761
for (auto inherited : PD->getLocalProtocols()) {
@@ -6873,7 +6872,7 @@ void TypeChecker::validateDecl(ValueDecl *D) {
68736872
if (mentionsItself) {
68746873
diagnose(defaultDefinition.getLoc(), diag::recursive_type_reference,
68756874
assocType->getDescriptiveKind(), assocType->getName());
6876-
diagnose(assocType, diag::type_declared_here);
6875+
diagnose(assocType, diag::kind_declared_here, DescriptiveDeclKind::Type);
68776876
}
68786877
}
68796878
}
@@ -7003,8 +7002,8 @@ void TypeChecker::validateDecl(ValueDecl *D) {
70037002
diagnose(proto->getLoc(),
70047003
diag::objc_protocol_inherits_non_objc_protocol,
70057004
proto->getDeclaredType(), inherited->getDeclaredType());
7006-
diagnose(inherited->getLoc(), diag::protocol_here,
7007-
inherited->getName());
7005+
diagnose(inherited->getLoc(), diag::kind_identifier_declared_here,
7006+
DescriptiveDeclKind::Protocol, inherited->getName());
70087007
isObjC = None;
70097008
}
70107009
}

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2732,9 +2732,9 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
27322732
}
27332733
}
27342734

2735-
diags.diagnose(requirement, diag::protocol_requirement_here,
2735+
diags.diagnose(requirement, diag::kind_declname_declared_here,
2736+
DescriptiveDeclKind::Requirement,
27362737
requirement->getFullName());
2737-
27382738
});
27392739
}
27402740

@@ -2835,7 +2835,8 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
28352835
}
28362836
}
28372837

2838-
diags.diagnose(requirement, diag::protocol_requirement_here,
2838+
diags.diagnose(requirement, diag::kind_declname_declared_here,
2839+
DescriptiveDeclKind::Requirement,
28392840
requirement->getFullName());
28402841
});
28412842
break;
@@ -2873,7 +2874,8 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
28732874
witness->getFullName(),
28742875
conformance->getProtocol()->getFullName());
28752876
emitDeclaredHereIfNeeded(diags, diagLoc, witness);
2876-
diags.diagnose(requirement, diag::protocol_requirement_here,
2877+
diags.diagnose(requirement, diag::kind_declname_declared_here,
2878+
DescriptiveDeclKind::Requirement,
28772879
requirement->getFullName());
28782880
});
28792881
break;
@@ -3516,7 +3518,8 @@ void ConformanceChecker::checkConformance(MissingWitnessDiagnosisKind Kind) {
35163518
"@nonobjc ");
35173519
}
35183520

3519-
TC.diagnose(requirement, diag::protocol_requirement_here,
3521+
TC.diagnose(requirement, diag::kind_declname_declared_here,
3522+
DescriptiveDeclKind::Requirement,
35203523
requirement->getFullName());
35213524

35223525
Conformance->setInvalid();
@@ -4484,7 +4487,8 @@ static void diagnosePotentialWitness(TypeChecker &tc,
44844487
.fixItInsert(witness->getAttributeInsertionLoc(false), "@nonobjc ");
44854488
}
44864489

4487-
tc.diagnose(req, diag::protocol_requirement_here, req->getFullName());
4490+
tc.diagnose(req, diag::kind_declname_declared_here,
4491+
DescriptiveDeclKind::Requirement, req->getFullName());
44884492
}
44894493

44904494
/// Whether the given protocol is "NSCoding".

0 commit comments

Comments
 (0)