Skip to content

Commit 55907fb

Browse files
Adding no corrections diagnostic for dynamic member
1 parent c62f833 commit 55907fb

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ ERROR(could_not_find_value_member_corrected,none,
8484
ERROR(could_not_find_value_dynamic_member_corrected,none,
8585
"value of type %0 has no dynamic member %2 using key path from root type %1; did you mean %3?",
8686
(Type, Type, DeclName, DeclName))
87+
ERROR(could_not_find_value_dynamic_member,none,
88+
"value of type %0 has no dynamic member %2 using key path from root type %1",
89+
(Type, Type, DeclName))
8790

8891
ERROR(could_not_find_type_member,none,
8992
"type %0 has no member %1", (Type, DeclName))

lib/Sema/CSDiagnostics.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,7 +2208,6 @@ bool MissingMemberFailure::diagnoseAsError() {
22082208
}
22092209

22102210
auto baseType = resolveType(getBaseType())->getWithoutSpecifierType();
2211-
auto baseExprType = getType(baseExpr)->getWithoutSpecifierType();
22122211

22132212
DeclNameLoc nameLoc(anchor->getStartLoc());
22142213
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(anchor)) {
@@ -2234,16 +2233,9 @@ bool MissingMemberFailure::diagnoseAsError() {
22342233
};
22352234

22362235
TypoCorrectionResults corrections(TC, getName(), nameLoc);
2237-
auto tryTypoCorrection = [&] {
2238-
TC.performTypoCorrection(getDC(), DeclRefKind::Ordinary, baseType,
2236+
auto tryTypoCorrection = [&] (Type type) {
2237+
TC.performTypoCorrection(getDC(), DeclRefKind::Ordinary, type,
22392238
defaultMemberLookupOptions, corrections);
2240-
// If locator points to the member found via key path dynamic member lookup,
2241-
// emit typo corrections for the wrapper type too.
2242-
if (getLocator()->isForKeyPathDynamicMemberLookup()) {
2243-
TC.performTypoCorrection(getDC(), DeclRefKind::Ordinary,
2244-
baseExprType, defaultMemberLookupOptions,
2245-
corrections);
2246-
}
22472239
};
22482240

22492241
if (getName().getBaseName().getKind() == DeclBaseName::Kind::Subscript) {
@@ -2262,7 +2254,7 @@ bool MissingMemberFailure::diagnoseAsError() {
22622254
.highlight(baseExpr->getSourceRange());
22632255
} else if (auto metatypeTy = baseType->getAs<MetatypeType>()) {
22642256
auto instanceTy = metatypeTy->getInstanceType();
2265-
tryTypoCorrection();
2257+
tryTypoCorrection(baseType);
22662258

22672259
if (DeclName rightName =
22682260
findCorrectEnumCaseName(instanceTy, corrections, getName())) {
@@ -2306,11 +2298,17 @@ bool MissingMemberFailure::diagnoseAsError() {
23062298
.fixItInsertAfter(baseExpr->getEndLoc(), " as AnyObject)");
23072299
return true;
23082300
}
2309-
2310-
tryTypoCorrection();
2311-
2312-
if (auto correction = corrections.claimUniqueCorrection()) {
2313-
if (getLocator()->isForKeyPathDynamicMemberLookup()) {
2301+
2302+
tryTypoCorrection(baseType);
2303+
2304+
// If locator points to the member found via key path dynamic member lookup,
2305+
// we provide a custom diagnostic and emit typo corrections for the wrapper type too.
2306+
if (getLocator()->isForKeyPathDynamicMemberLookup()) {
2307+
auto baseExprType = getType(baseExpr)->getWithoutSpecifierType();
2308+
2309+
tryTypoCorrection(baseExprType);
2310+
2311+
if (auto correction = corrections.claimUniqueCorrection()) {
23142312
auto diagnostic = emitDiagnostic(
23152313
anchor->getLoc(),
23162314
diag::could_not_find_value_dynamic_member_corrected,
@@ -2320,6 +2318,15 @@ bool MissingMemberFailure::diagnoseAsError() {
23202318
.highlight(nameLoc.getSourceRange());
23212319
correction->addFixits(diagnostic);
23222320
} else {
2321+
auto diagnostic = emitDiagnostic(
2322+
anchor->getLoc(),
2323+
diag::could_not_find_value_dynamic_member,
2324+
baseExprType, baseType, getName());
2325+
diagnostic.highlight(baseExpr->getSourceRange())
2326+
.highlight(nameLoc.getSourceRange());
2327+
}
2328+
} else {
2329+
if (auto correction = corrections.claimUniqueCorrection()) {
23232330
auto diagnostic = emitDiagnostic(
23242331
anchor->getLoc(),
23252332
diag::could_not_find_value_member_corrected,
@@ -2328,9 +2335,9 @@ bool MissingMemberFailure::diagnoseAsError() {
23282335
diagnostic.highlight(baseExpr->getSourceRange())
23292336
.highlight(nameLoc.getSourceRange());
23302337
correction->addFixits(diagnostic);
2338+
} else {
2339+
emitBasicError(baseType);
23312340
}
2332-
} else {
2333-
emitBasicError(baseType);
23342341
}
23352342
}
23362343

test/attr/attr_dynamic_member_lookup.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ struct SR10597_W<T> {
705705
}
706706

707707
_ = SR10597_W<SR10597>(SR10597()).wooooo // expected-error {{value of type 'SR10597_W<SR10597>' has no dynamic member 'wooooo' using key path from root type 'SR10597'; did you mean 'wooo'?}}
708+
_ = SR10597_W<SR10597>(SR10597()).bla // expected-error {{value of type 'SR10597_W<SR10597>' has no dynamic member 'bla' using key path from root type 'SR10597'}}
708709

709710
final class SR10597_1 {
710711
var woo: Int? // expected-note 2 {{'woo' declared here}}
@@ -719,4 +720,5 @@ struct SR10597_1_W<T> {
719720
}
720721
}
721722

722-
_ = SR10597_1_W<SR10597_1>(SR10597_1()).wooo // expected-error {{value of type 'SR10597_1_W<SR10597_1>' has no dynamic member 'wooo' using key path from root type 'SR10597_1'; did you mean 'woo'?}}
723+
_ = SR10597_1_W<SR10597_1>(SR10597_1()).wooo // expected-error {{value of type 'SR10597_1_W<SR10597_1>' has no dynamic member 'wooo' using key path from root type 'SR10597_1'; did you mean 'woo'?}}
724+
_ = SR10597_1_W<SR10597_1>(SR10597_1()).bla // expected-error {{value of type 'SR10597_1_W<SR10597_1>' has no dynamic member 'bla' using key path from root type 'SR10597_1'}}

0 commit comments

Comments
 (0)