Skip to content

Commit b3bc06e

Browse files
authored
Merge pull request #16766 from AnthonyLatsis/generalize_declared_here
[Sema] Generalize the 'declared here' diagnostic
2 parents 249fa47 + 492dca8 commit b3bc06e

File tree

11 files changed

+63
-59
lines changed

11 files changed

+63
-59
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: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@
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,
4445
"%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,
4651
"%1 '%0' is implicitly declared", (StringRef, StringRef))
4752
NOTE(extended_type_declared_here,none,
@@ -766,8 +771,6 @@ ERROR(precedence_group_lower_within_module,none,
766771
"precedence group cannot be given lower precedence than group in same"
767772
" module; make the other precedence group higher than this one instead",
768773
())
769-
NOTE(precedence_group_declared_here,none,"precedence group declared here",
770-
())
771774
ERROR(precedence_group_redeclared,none,
772775
"precedence group redeclared", ())
773776
NOTE(previous_precedence_group_decl,none,
@@ -1689,8 +1692,6 @@ NOTE(protocol_witness_nonconform_type,none,
16891692
"possibly intended match %0 does not "
16901693
"%select{inherit from|conform to}2 %1", (Type, Type, bool))
16911694

1692-
NOTE(protocol_requirement_here,none,
1693-
"requirement %0 declared here", (DeclName))
16941695
NOTE(protocol_conformance_here,none,
16951696
"%select{|class }0%1 declares conformance to protocol %2 here",
16961697
(bool, DeclName, DeclName))
@@ -1753,8 +1754,6 @@ ERROR(typealias_outside_of_protocol,none,
17531754

17541755
ERROR(circular_protocol_def,none,
17551756
"circular protocol inheritance %0", (StringRef))
1756-
NOTE(protocol_here,none,
1757-
"protocol %0 declared here", (Identifier))
17581757
ERROR(objc_protocol_inherits_non_objc_protocol,none,
17591758
"@objc protocol %0 cannot refine non-@objc protocol %1", (Type, Type))
17601759
WARNING(protocol_composition_with_postfix,none,
@@ -2072,8 +2071,6 @@ ERROR(superclass_of_open_not_open,none,
20722071
"superclass %0 of open class must be open", (Type))
20732072
ERROR(circular_class_inheritance,none,
20742073
"circular class inheritance %0", (StringRef))
2075-
NOTE(class_here,none,
2076-
"class %0 declared here", (Identifier))
20772074
ERROR(inheritance_from_final_class,none,
20782075
"inheritance from a final class %0", (Identifier))
20792076
ERROR(inheritance_from_unspecialized_objc_generic_class,none,
@@ -2141,8 +2138,6 @@ WARNING(enum_raw_type_access_warn,none,
21412138
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
21422139
(bool, AccessLevel, AccessLevel, bool))
21432140

2144-
NOTE(enum_here,none,
2145-
"enum %0 declared here", (Identifier))
21462141
ERROR(empty_enum_raw_type,none,
21472142
"an enum with no cases cannot declare a raw type", ())
21482143
ERROR(enum_raw_value_without_raw_type,none,
@@ -2247,8 +2242,6 @@ ERROR(property_behavior_not_protocol,none,
22472242
"property behavior name must refer to a protocol", ())
22482243
ERROR(property_behavior_protocol_reqt_ambiguous,none,
22492244
"property behavior protocol has ambiguous %0 member", (Identifier))
2250-
NOTE(property_behavior_protocol_reqt_here,none,
2251-
"%0 declared here", (Identifier))
22522245
ERROR(property_behavior_protocol_no_value,none,
22532246
"property behavior protocol does not have a 'value' property", ())
22542247
ERROR(property_behavior_protocol_no_initStorage,none,
@@ -2685,8 +2678,6 @@ ERROR(type_parameter_count_mismatch,none,
26852678
(Identifier, unsigned, unsigned, bool))
26862679
ERROR(generic_type_requires_arguments,none,
26872680
"reference to generic type %0 requires arguments in <...>", (Type))
2688-
NOTE(generic_type_declared_here,none,
2689-
"generic type %0 declared here", (Identifier))
26902681
NOTE(descriptive_generic_type_declared_here,none,
26912682
"%0 declared here", (StringRef))
26922683

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
@@ -1592,8 +1592,8 @@ namespace {
15921592
false)
15931593
.highlight(SourceRange(expr->getLAngleLoc(),
15941594
expr->getRAngleLoc()));
1595-
tc.diagnose(bgt->getDecl(), diag::generic_type_declared_here,
1596-
bgt->getDecl()->getName());
1595+
tc.diagnose(bgt->getDecl(), diag::kind_identifier_declared_here,
1596+
DescriptiveDeclKind::GenericType, bgt->getDecl()->getName());
15971597
return Type();
15981598
}
15991599

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

@@ -3428,11 +3429,9 @@ static void checkVarBehavior(VarDecl *decl, TypeChecker &TC) {
34283429
diag::property_behavior_protocol_reqt_ambiguous,
34293430
TC.Context.Id_initStorage);
34303431
TC.diagnose(defaultInitStorageDecl->getLoc(),
3431-
diag::property_behavior_protocol_reqt_here,
3432-
TC.Context.Id_initStorage);
3432+
diag::identifier_declared_here, TC.Context.Id_initStorage);
34333433
TC.diagnose(parameterizedInitStorageDecl->getLoc(),
3434-
diag::property_behavior_protocol_reqt_here,
3435-
TC.Context.Id_initStorage);
3434+
diag::identifier_declared_here, TC.Context.Id_initStorage);
34363435
conformance->setInvalid();
34373436
continue;
34383437
}
@@ -3501,8 +3500,7 @@ static void checkVarBehavior(VarDecl *decl, TypeChecker &TC) {
35013500
TC.diagnose(behavior->getLoc(),
35023501
diag::property_behavior_invalid_parameter_reqt,
35033502
behaviorProto->getName());
3504-
TC.diagnose(varReqt->getLoc(),
3505-
diag::property_behavior_protocol_reqt_here,
3503+
TC.diagnose(varReqt->getLoc(), diag::identifier_declared_here,
35063504
TC.Context.Id_parameter);
35073505
conformance->setInvalid();
35083506
continue;
@@ -3772,7 +3770,8 @@ void TypeChecker::validateDecl(PrecedenceGroupDecl *PGD) {
37723770
== dc->getParentModule()) {
37733771
if (!PGD->isInvalid()) {
37743772
diagnose(rel.NameLoc, diag::precedence_group_lower_within_module);
3775-
diagnose(group->getNameLoc(), diag::precedence_group_declared_here);
3773+
diagnose(group->getNameLoc(), diag::kind_declared_here,
3774+
DescriptiveDeclKind::PrecedenceGroup);
37763775
isInvalid = true;
37773776
}
37783777
} else {
@@ -4476,7 +4475,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
44764475
SmallVector<EnumDecl *, 8> path;
44774476
path.push_back(ED);
44784477
checkCircularity(TC, ED, diag::circular_enum_inheritance,
4479-
diag::enum_here, path);
4478+
DescriptiveDeclKind::Enum, path);
44804479
}
44814480

44824481
for (Decl *member : ED->getMembers())
@@ -4636,7 +4635,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
46364635
SmallVector<ClassDecl *, 8> path;
46374636
path.push_back(CD);
46384637
checkCircularity(TC, CD, diag::circular_class_inheritance,
4639-
diag::class_here, path);
4638+
DescriptiveDeclKind::Class, path);
46404639
}
46414640

46424641
for (Decl *Member : CD->getMembers())
@@ -4760,7 +4759,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
47604759
SmallVector<ProtocolDecl *, 8> path;
47614760
path.push_back(PD);
47624761
checkCircularity(TC, PD, diag::circular_protocol_def,
4763-
diag::protocol_here, path);
4762+
DescriptiveDeclKind::Protocol, path);
47644763

47654764
// Make sure the parent protocols have been fully validated.
47664765
for (auto inherited : PD->getLocalProtocols()) {
@@ -6877,7 +6876,7 @@ void TypeChecker::validateDecl(ValueDecl *D) {
68776876
if (mentionsItself) {
68786877
diagnose(defaultDefinition.getLoc(), diag::recursive_type_reference,
68796878
assocType->getDescriptiveKind(), assocType->getName());
6880-
diagnose(assocType, diag::type_declared_here);
6879+
diagnose(assocType, diag::kind_declared_here, DescriptiveDeclKind::Type);
68816880
}
68826881
}
68836882
}
@@ -7007,8 +7006,8 @@ void TypeChecker::validateDecl(ValueDecl *D) {
70077006
diagnose(proto->getLoc(),
70087007
diag::objc_protocol_inherits_non_objc_protocol,
70097008
proto->getDeclaredType(), inherited->getDeclaredType());
7010-
diagnose(inherited->getLoc(), diag::protocol_here,
7011-
inherited->getName());
7009+
diagnose(inherited->getLoc(), diag::kind_identifier_declared_here,
7010+
DescriptiveDeclKind::Protocol, inherited->getName());
70127011
isObjC = None;
70137012
}
70147013
}

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)