Skip to content

Commit 0c1d8a1

Browse files
authored
Merge pull request #13163 from slavapestov/fixit-source-range-issue
Sema: Use the correct source range when emitting fixits for unresolved member expressions
2 parents 1e2fad5 + e360da5 commit 0c1d8a1

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7362,14 +7362,16 @@ bool FailureDiagnosis::diagnoseMemberFailures(
73627362
// and the source range for y.
73637363
SourceRange memberRange;
73647364
SourceLoc BaseLoc;
7365+
DeclNameLoc NameLoc;
73657366

73667367
Type baseTy, baseObjTy;
73677368
// UnresolvedMemberExpr doesn't have "base" expression,
73687369
// it's represented as ".foo", which means that we need
73697370
// to get base from the context.
7370-
if (isa<UnresolvedMemberExpr>(E)) {
7371+
if (auto *UME = dyn_cast<UnresolvedMemberExpr>(E)) {
73717372
memberRange = E->getSourceRange();
73727373
BaseLoc = E->getLoc();
7374+
NameLoc = UME->getNameLoc();
73737375
baseTy = CS.getContextualType();
73747376
if (!baseTy)
73757377
return false;
@@ -7389,6 +7391,8 @@ bool FailureDiagnosis::diagnoseMemberFailures(
73897391
locator = simplifyLocator(CS, locator, memberRange);
73907392

73917393
BaseLoc = baseExpr->getLoc();
7394+
NameLoc = DeclNameLoc(memberRange.Start);
7395+
73927396
// Retypecheck the anchor type, which is the base of the member expression.
73937397
baseExpr =
73947398
typeCheckArbitrarySubExprIndependently(baseExpr, TCC_AllowLValue);
@@ -7574,7 +7578,7 @@ bool FailureDiagnosis::diagnoseMemberFailures(
75747578

75757579
// FIXME: Dig out the property DeclNameLoc.
75767580
diagnoseUnviableLookupResults(result, baseObjTy, baseExpr, memberName,
7577-
DeclNameLoc(memberRange.Start), BaseLoc);
7581+
NameLoc, BaseLoc);
75787582
return true;
75797583
}
75807584

test/Sema/typo_correction.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,18 @@ func takesAnyObjectArchetype<T : AnyObject>(_ t: T) {
120120
_ = t.rawPointer
121121
// expected-error@-1 {{value of type 'T' has no member 'rawPointer'}}
122122
}
123+
124+
// Typo correction with an UnresolvedDotExpr.
125+
enum Foo {
126+
// note: the fixit is actually for the line with the error below, but
127+
// -verify mode is not smart enough for that yet.
128+
129+
case flashing // expected-note {{did you mean 'flashing'?}}{{8-15=flashing}}
130+
}
131+
132+
func foo(_ a: Foo) {
133+
}
134+
135+
func bar() {
136+
foo(.flashin) // expected-error {{type 'Foo' has no member 'flashin'}}
137+
}

0 commit comments

Comments
 (0)