Skip to content

Commit fe67534

Browse files
committed
[NFC] Adopt new diagnostic features across Sema
1 parent 9c6b3bb commit fe67534

24 files changed

+306
-380
lines changed

include/swift/AST/DiagnosticEngine.h

+2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ namespace swift {
4242
class Decl;
4343
class DeclAttribute;
4444
class DiagnosticEngine;
45+
class FuncDecl;
4546
class GeneratedSourceInfo;
4647
class SourceManager;
48+
class TypeAliasDecl;
4749
class ValueDecl;
4850
class SourceFile;
4951

include/swift/AST/DiagnosticsSema.def

+151-161
Large diffs are not rendered by default.

lib/Sema/CSApply.cpp

+18-25
Original file line numberDiff line numberDiff line change
@@ -3283,11 +3283,11 @@ namespace {
32833283
void
32843284
diagnoseDeprecatedConditionalConformanceOuterAccess(UnresolvedDotExpr *UDE,
32853285
ValueDecl *choice) {
3286-
auto getBaseName = [](DeclContext *context) -> DeclName {
3286+
auto getValueDecl = [](DeclContext *context) -> ValueDecl * {
32873287
if (auto generic = context->getSelfNominalTypeDecl()) {
3288-
return generic->getName();
3288+
return generic;
32893289
} else if (context->isModuleScopeContext())
3290-
return context->getParentModule()->getName();
3290+
return context->getParentModule();
32913291
else
32923292
llvm_unreachable("Unsupported base");
32933293
};
@@ -3299,31 +3299,25 @@ namespace {
32993299
auto exampleInner = result.front();
33003300
auto innerChoice = exampleInner.getValueDecl();
33013301
auto innerDC = exampleInner.getDeclContext()->getInnermostTypeContext();
3302-
auto innerParentDecl = innerDC->getSelfNominalTypeDecl();
3303-
auto innerBaseName = getBaseName(innerDC);
3302+
auto innerParentDecl = getValueDecl(innerDC);
33043303

3305-
auto choiceKind = choice->getDescriptiveKind();
33063304
auto choiceDC = choice->getDeclContext();
3307-
auto choiceBaseName = getBaseName(choiceDC);
3308-
auto choiceParentDecl = choiceDC->getAsDecl();
3309-
auto choiceParentKind = choiceParentDecl
3310-
? choiceParentDecl->getDescriptiveKind()
3311-
: DescriptiveDeclKind::Module;
3305+
auto choiceParentDecl = getValueDecl(choiceDC);
33123306

33133307
auto &DE = cs.getASTContext().Diags;
33143308
DE.diagnose(UDE->getLoc(),
33153309
diag::warn_deprecated_conditional_conformance_outer_access,
3316-
UDE->getName(), choiceKind, choiceParentKind, choiceBaseName,
3317-
innerChoice->getDescriptiveKind(),
3318-
innerParentDecl->getDescriptiveKind(), innerBaseName);
3310+
UDE->getName(),
3311+
choice->getDescriptiveKind(), choiceParentDecl,
3312+
innerChoice->getDescriptiveKind(), innerParentDecl);
33193313

3320-
auto name = choiceBaseName.getBaseIdentifier();
3314+
auto name = choiceParentDecl->getName().getBaseIdentifier();
33213315
SmallString<32> namePlusDot = name.str();
33223316
namePlusDot.push_back('.');
33233317

33243318
DE.diagnose(UDE->getLoc(),
33253319
diag::fix_deprecated_conditional_conformance_outer_access,
3326-
namePlusDot, choiceKind, name)
3320+
namePlusDot, choice->getDescriptiveKind())
33273321
.fixItInsert(UDE->getStartLoc(), namePlusDot);
33283322
}
33293323

@@ -3986,7 +3980,7 @@ namespace {
39863980
// initializer failable.
39873981
de.diagnose(otherCtorRef->getLoc(),
39883982
diag::delegate_chain_nonoptional_to_optional,
3989-
isChaining, otherCtor->getName());
3983+
isChaining, otherCtor);
39903984
de.diagnose(otherCtorRef->getLoc(), diag::init_force_unwrap)
39913985
.fixItInsertAfter(expr->getEndLoc(), "!");
39923986
de.diagnose(inCtor->getLoc(), diag::init_propagate_failure)
@@ -4826,7 +4820,7 @@ namespace {
48264820
if (!func->getDeclContext()->isTypeContext()) {
48274821
de.diagnose(E->getLoc(), diag::expr_selector_not_method,
48284822
func->getDeclContext()->isModuleScopeContext(),
4829-
func->getName())
4823+
func)
48304824
.highlight(subExpr->getSourceRange());
48314825
de.diagnose(func, diag::decl_declared_here, func);
48324826
return E;
@@ -4861,7 +4855,7 @@ namespace {
48614855
// If this isn't a property on a type, complain.
48624856
if (!var->getDeclContext()->isTypeContext()) {
48634857
de.diagnose(E->getLoc(), diag::expr_selector_not_property,
4864-
isa<ParamDecl>(var), var->getName())
4858+
isa<ParamDecl>(var), var)
48654859
.highlight(subExpr->getSourceRange());
48664860
de.diagnose(var, diag::decl_declared_here, var);
48674861
return E;
@@ -4874,7 +4868,7 @@ namespace {
48744868
var->isSetterAccessibleFrom(dc);
48754869
auto primaryDiag =
48764870
de.diagnose(E->getLoc(), diag::expr_selector_expected_method,
4877-
isSettable, var->getName());
4871+
isSettable, var);
48784872
primaryDiag.highlight(subExpr->getSourceRange());
48794873

48804874
// The point at which we will insert the modifier.
@@ -4888,10 +4882,10 @@ namespace {
48884882

48894883
// Add notes for the getter and setter, respectively.
48904884
de.diagnose(modifierLoc, diag::expr_selector_add_modifier, false,
4891-
var->getName())
4885+
var)
48924886
.fixItInsert(modifierLoc, "getter: ");
48934887
de.diagnose(modifierLoc, diag::expr_selector_add_modifier, true,
4894-
var->getName())
4888+
var)
48954889
.fixItInsert(modifierLoc, "setter: ");
48964890

48974891
// Bail out now. We don't know what the user wanted, so
@@ -4948,7 +4942,7 @@ namespace {
49484942
// problems.
49494943
if (auto protocolDecl = dyn_cast<ProtocolDecl>(foundDecl->getDeclContext())) {
49504944
de.diagnose(E->getLoc(), diag::expr_selector_cannot_be_used,
4951-
foundDecl->getBaseName(), protocolDecl->getName());
4945+
foundDecl, protocolDecl);
49524946
return E;
49534947
}
49544948

@@ -4966,8 +4960,7 @@ namespace {
49664960
Swift3ObjCInferenceWarnings::Minimal) {
49674961
de.diagnose(E->getLoc(), diag::expr_selector_swift3_objc_inference,
49684962
foundDecl, foundDecl->getDeclContext()
4969-
->getSelfNominalTypeDecl()
4970-
->getName())
4963+
->getSelfNominalTypeDecl())
49714964
.highlight(subExpr->getSourceRange());
49724965
de.diagnose(foundDecl, diag::make_decl_objc,
49734966
foundDecl->getDescriptiveKind())

lib/Sema/CSDiagnostics.cpp

+20-21
Original file line numberDiff line numberDiff line change
@@ -2017,7 +2017,7 @@ bool TrailingClosureAmbiguityFailure::diagnoseAsNote() {
20172017
for (const auto &choicePair : choicesByLabel) {
20182018
auto diag = emitDiagnosticAt(
20192019
expr->getLoc(), diag::ambiguous_because_of_trailing_closure,
2020-
choicePair.first.empty(), choicePair.second->getName());
2020+
choicePair.first.empty(), choicePair.second);
20212021
swift::fixItEncloseTrailingClosure(getASTContext(), diag, callExpr,
20222022
choicePair.first);
20232023
}
@@ -2644,7 +2644,7 @@ bool ContextualFailure::diagnoseAsError() {
26442644
auto fnType = fromType->getAs<FunctionType>();
26452645
if (!fnType) {
26462646
emitDiagnostic(diag::expected_result_in_contextual_member,
2647-
choice->getName(), fromType, toType);
2647+
choice, fromType, toType);
26482648
return true;
26492649
}
26502650

@@ -2680,16 +2680,16 @@ bool ContextualFailure::diagnoseAsError() {
26802680
if (fnType->getResult()->isEqual(toType)) {
26812681
auto diag = emitDiagnostic(
26822682
diag::expected_parens_in_contextual_member_type,
2683-
choice->getName(), fnType->getResult());
2683+
choice, fnType->getResult());
26842684
applyFixIt(diag);
26852685
} else {
26862686
auto diag = emitDiagnostic(diag::expected_parens_in_contextual_member,
2687-
choice->getName());
2687+
choice);
26882688
applyFixIt(diag);
26892689
}
26902690
} else {
26912691
emitDiagnostic(diag::expected_argument_in_contextual_member,
2692-
choice->getName(), params.front().getPlainType());
2692+
choice, params.front().getPlainType());
26932693
}
26942694

26952695
return true;
@@ -4901,7 +4901,7 @@ bool InvalidDynamicInitOnMetatypeFailure::diagnoseAsError() {
49014901
BaseType->getMetatypeInstanceType())
49024902
.highlight(BaseRange);
49034903
emitDiagnosticAt(Init, diag::note_nonrequired_initializer, Init->isImplicit(),
4904-
Init->getName());
4904+
Init);
49054905
return true;
49064906
}
49074907

@@ -5362,10 +5362,9 @@ bool MissingArgumentsFailure::diagnoseInvalidTupleDestructuring() const {
53625362
if (!funcType)
53635363
return false;
53645364

5365-
auto name = decl->getBaseName();
53665365
auto diagnostic =
53675366
emitDiagnostic(diag::cannot_convert_single_tuple_into_multiple_arguments,
5368-
decl->getDescriptiveKind(), name, name.isSpecial(),
5367+
decl, decl->getBaseName().isSpecial(),
53695368
funcType->getNumParams(), isa<TupleExpr>(argExpr));
53705369

53715370
// If argument is a literal tuple, let's suggest removal of parentheses.
@@ -5990,7 +5989,7 @@ bool InaccessibleMemberFailure::diagnoseAsError() {
59905989
CD->getResultInterfaceType(), accessLevel)
59915990
.highlight(nameLoc.getSourceRange());
59925991
} else {
5993-
emitDiagnosticAt(loc, diag::candidate_inaccessible, Member->getBaseName(),
5992+
emitDiagnosticAt(loc, diag::candidate_inaccessible, Member,
59945993
accessLevel)
59955994
.highlight(nameLoc.getSourceRange());
59965995
}
@@ -6060,19 +6059,19 @@ SourceLoc InvalidMemberRefInKeyPath::getLoc() const {
60606059
}
60616060

60626061
bool InvalidStaticMemberRefInKeyPath::diagnoseAsError() {
6063-
emitDiagnostic(diag::expr_keypath_static_member, getName(),
6062+
emitDiagnostic(diag::expr_keypath_static_member, getMember(),
60646063
isForKeyPathDynamicMemberLookup());
60656064
return true;
60666065
}
60676066

60686067
bool InvalidEnumCaseRefInKeyPath::diagnoseAsError() {
6069-
emitDiagnostic(diag::expr_keypath_enum_case, getName(),
6068+
emitDiagnostic(diag::expr_keypath_enum_case, getMember(),
60706069
isForKeyPathDynamicMemberLookup());
60716070
return true;
60726071
}
60736072

60746073
bool InvalidMemberWithMutatingGetterInKeyPath::diagnoseAsError() {
6075-
emitDiagnostic(diag::expr_keypath_mutating_getter, getName(),
6074+
emitDiagnostic(diag::expr_keypath_mutating_getter, getMember(),
60766075
isForKeyPathDynamicMemberLookup());
60776076
return true;
60786077
}
@@ -6435,7 +6434,7 @@ bool MissingGenericArgumentsFailure::diagnoseForAnchor(
64356434
return true;
64366435

64376436
if (auto *SD = dyn_cast<SubscriptDecl>(DC)) {
6438-
emitDiagnosticAt(SD, diag::note_call_to_subscript, SD->getName());
6437+
emitDiagnosticAt(SD, diag::note_call_to_subscript, SD);
64396438
return true;
64406439
}
64416440

@@ -6446,7 +6445,7 @@ bool MissingGenericArgumentsFailure::diagnoseForAnchor(
64466445
emitDiagnosticAt(AFD,
64476446
AFD->isOperator() ? diag::note_call_to_operator
64486447
: diag::note_call_to_func,
6449-
AFD->getName());
6448+
AFD);
64506449
}
64516450
return true;
64526451
}
@@ -7547,7 +7546,7 @@ bool ExtraneousCallFailure::diagnoseAsError() {
75477546
if (auto *enumCase = dyn_cast<EnumElementDecl>(decl)) {
75487547
auto diagnostic =
75497548
emitDiagnostic(diag::unexpected_arguments_in_enum_case,
7550-
enumCase->getBaseIdentifier());
7549+
enumCase);
75517550
removeParensFixIt(diagnostic);
75527551
return true;
75537552
}
@@ -7601,7 +7600,7 @@ void NonEphemeralConversionFailure::emitSuggestionNotes() const {
76017600
auto *argExpr = getArgExpr();
76027601
emitDiagnosticAt(
76037602
argExpr->getLoc(), diag::ephemeral_pointer_argument_conversion_note,
7604-
getArgType(), getParamType(), getCallee(), getCalleeFullName())
7603+
getArgType(), getParamType(), getCallee())
76057604
.highlight(argExpr->getSourceRange());
76067605

76077606
// Then try to find a suitable alternative.
@@ -7740,11 +7739,11 @@ bool NonEphemeralConversionFailure::diagnoseAsError() {
77407739
auto *argExpr = getArgExpr();
77417740
if (isa<InOutExpr>(argExpr)) {
77427741
emitDiagnosticAt(argExpr->getLoc(), diag::cannot_use_inout_non_ephemeral,
7743-
argDesc, getCallee(), getCalleeFullName())
7742+
argDesc, getCallee())
77447743
.highlight(argExpr->getSourceRange());
77457744
} else {
77467745
emitDiagnosticAt(argExpr->getLoc(), diag::cannot_pass_type_to_non_ephemeral,
7747-
getArgType(), argDesc, getCallee(), getCalleeFullName())
7746+
getArgType(), argDesc, getCallee())
77487747
.highlight(argExpr->getSourceRange());
77497748
}
77507749
emitSuggestionNotes();
@@ -7807,7 +7806,7 @@ bool AssignmentTypeMismatchFailure::diagnoseAsNote() {
78077806
if (auto *decl = overload->choice.getDeclOrNull()) {
78087807
emitDiagnosticAt(decl,
78097808
diag::cannot_convert_candidate_result_to_contextual_type,
7810-
decl->getName(), getFromType(), getToType());
7809+
decl, getFromType(), getToType());
78117810
return true;
78127811
}
78137812
}
@@ -8132,7 +8131,7 @@ bool AbstractRawRepresentableFailure::diagnoseAsNote() {
81328131
if (auto *decl = overload->choice.getDeclOrNull()) {
81338132
diagnostic.emplace(emitDiagnosticAt(
81348133
decl, diag::cannot_convert_candidate_result_to_contextual_type,
8135-
decl->getName(), ExpectedType, RawReprType));
8134+
decl, ExpectedType, RawReprType));
81368135
}
81378136
} else if (auto argConv =
81388137
locator->getLastElementAs<LocatorPathElt::ApplyArgToParam>()) {
@@ -8471,7 +8470,7 @@ bool ReferenceToInvalidDeclaration::diagnoseAsError() {
84718470
// If no errors have been emitted yet, let's emit one
84728471
// about reference to an invalid declaration.
84738472

8474-
emitDiagnostic(diag::reference_to_invalid_decl, decl->getName());
8473+
emitDiagnostic(diag::reference_to_invalid_decl, decl);
84758474
emitDiagnosticAt(decl, diag::decl_declared_here, decl);
84768475
return true;
84778476
}

lib/Sema/ConstraintSystem.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -4739,7 +4739,7 @@ static bool diagnoseAmbiguityWithContextualType(
47394739
contextualTy->is<ProtocolType>()
47404740
? diag::overload_result_type_does_not_conform
47414741
: diag::cannot_convert_candidate_result_to_contextual_type,
4742-
decl->getName(), fnType->getResult(), contextualTy);
4742+
decl, fnType->getResult(), contextualTy);
47434743
} else {
47444744
DE.diagnose(decl, diag::found_candidate_type, type);
47454745
}
@@ -4796,12 +4796,11 @@ static bool diagnoseAmbiguityWithGenericRequirements(
47964796
// Produce "no exact matches" diagnostic.
47974797
auto &ctx = cs.getASTContext();
47984798
auto *choice = *overloadChoices.begin();
4799-
auto name = choice->getName();
48004799

48014800
ctx.Diags.diagnose(getLoc(primaryFix.second->getLocator()->getAnchor()),
48024801
diag::no_overloads_match_exactly_in_call,
4803-
/*isApplication=*/false, choice->getDescriptiveKind(),
4804-
name.isSpecial(), name.getBaseName());
4802+
/*isApplication=*/false, choice,
4803+
choice->getName().isSpecial());
48054804

48064805
for (const auto &entry : aggregate) {
48074806
entry.second->diagnose(*entry.first, /*asNote=*/true);
@@ -4894,8 +4893,7 @@ static bool diagnoseAmbiguity(
48944893
}
48954894

48964895
DE.diagnose(anchor->getLoc(), diag::no_overloads_match_exactly_in_call,
4897-
/*isApplication=*/false, decl->getDescriptiveKind(),
4898-
name.isSpecial(), name.getBaseName());
4896+
/*isApplication=*/false, decl, name.isSpecial());
48994897
} else {
49004898
bool isApplication = llvm::any_of(solutions, [&](const auto &S) {
49014899
return llvm::any_of(S.argumentLists, [&](const auto &pair) {
@@ -4905,8 +4903,7 @@ static bool diagnoseAmbiguity(
49054903

49064904
DE.diagnose(getLoc(commonAnchor),
49074905
diag::no_overloads_match_exactly_in_call, isApplication,
4908-
decl->getDescriptiveKind(), name.isSpecial(),
4909-
name.getBaseName());
4906+
decl, name.isSpecial());
49104907
}
49114908

49124909
// Produce candidate notes

lib/Sema/DerivedConformanceCodable.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -2088,8 +2088,7 @@ ValueDecl *DerivedConformance::deriveEncodable(ValueDecl *requirement) {
20882088
ConformanceDecl->diagnose(diag::type_does_not_conform,
20892089
Nominal->getDeclaredType(), getProtocolType());
20902090
requirement->diagnose(diag::no_witnesses, diag::RequirementKind::Func,
2091-
requirement->getName(), getProtocolType(),
2092-
/*AddFixIt=*/false);
2091+
requirement, getProtocolType(), /*AddFixIt=*/false);
20932092

20942093
return nullptr;
20952094
}
@@ -2120,8 +2119,7 @@ ValueDecl *DerivedConformance::deriveDecodable(ValueDecl *requirement) {
21202119
ConformanceDecl->diagnose(diag::type_does_not_conform,
21212120
Nominal->getDeclaredType(), getProtocolType());
21222121
requirement->diagnose(diag::no_witnesses, diag::RequirementKind::Constructor,
2123-
requirement->getName(), getProtocolType(),
2124-
/*AddFixIt=*/false);
2122+
requirement, getProtocolType(), /*AddFixIt=*/false);
21252123

21262124
return nullptr;
21272125
}

lib/Sema/DerivedConformanceDifferentiable.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,7 @@ ValueDecl *DerivedConformance::deriveDifferentiable(ValueDecl *requirement) {
641641
Nominal->getDeclaredType(), getProtocolType());
642642
requirement->diagnose(diag::no_witnesses,
643643
getProtocolRequirementKind(requirement),
644-
requirement->getName(), getProtocolType(),
645-
/*AddFixIt=*/false);
644+
requirement, getProtocolType(), /*AddFixIt=*/false);
646645

647646
// If derivation is possible, cancel the diagnostic and perform derivation.
648647
if (canDeriveDifferentiable(Nominal, getConformanceContext(), requirement)) {
@@ -669,7 +668,7 @@ DerivedConformance::deriveDifferentiable(AssociatedTypeDecl *requirement) {
669668
DiagnosticTransaction diagnosticTransaction(Context.Diags);
670669
ConformanceDecl->diagnose(diag::type_does_not_conform,
671670
Nominal->getDeclaredType(), getProtocolType());
672-
requirement->diagnose(diag::no_witnesses_type, requirement->getName());
671+
requirement->diagnose(diag::no_witnesses_type, requirement);
673672

674673
// If derivation is possible, cancel the diagnostic and perform derivation.
675674
if (canDeriveDifferentiable(Nominal, getConformanceContext(), requirement)) {

lib/Sema/MiscDiagnostics.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -5295,7 +5295,7 @@ static void diagnoseDeprecatedWritableKeyPath(const Expr *E,
52955295
!storage->isSetterAccessibleFrom(DC)) {
52965296
Ctx.Diags.diagnose(keyPathExpr->getLoc(),
52975297
swift::diag::expr_deprecated_writable_keypath,
5298-
storage->getName());
5298+
storage);
52995299
}
53005300
}
53015301
}

0 commit comments

Comments
 (0)