Skip to content

Commit fcff56d

Browse files
authored
Merge pull request #20125 from adamshin/typo-correction-underscore
[Sema] Hide underscored names from typo-correction
2 parents 0829b8a + a93ec9d commit fcff56d

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,12 @@ static unsigned getCallEditDistance(DeclName writtenName,
602602
StringRef writtenBase = writtenName.getBaseName().userFacingName();
603603
StringRef correctedBase = correctedName.getBaseName().userFacingName();
604604

605+
// Don't typo-correct to a name with a leading underscore unless the typed
606+
// name also begins with an underscore.
607+
if (correctedBase.startswith("_") && !writtenBase.startswith("_")) {
608+
return UnreasonableCallEditDistance;
609+
}
610+
605611
unsigned distance = writtenBase.edit_distance(correctedBase, maxEditDistance);
606612

607613
// Bound the distance to UnreasonableCallEditDistance.

test/Parse/recovery.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ func useContainer() -> () {
3333
func exists() -> Bool { return true }
3434
}
3535

36-
func test(a: BadAttributes) -> () { // expected-note * {{did you mean 'test'?}}
36+
// expected-note @+2 {{did you mean 'test'?}}
37+
// expected-note @+1 {{'test' declared here}}
38+
func test(a: BadAttributes) -> () {
3739
_ = a.exists() // no-warning
3840
}
3941

@@ -617,7 +619,7 @@ func foo1(bar!=baz) {} // expected-note {{did you mean 'foo1'?}}
617619
func foo2(bar! = baz) {}// expected-note {{did you mean 'foo2'?}}
618620

619621
// rdar://19605567
620-
// expected-error@+1{{use of unresolved identifier 'esp'}}
622+
// expected-error@+1{{use of unresolved identifier 'esp'; did you mean 'test'?}}
621623
switch esp {
622624
case let (jeb):
623625
// expected-error@+5{{operator with postfix spacing cannot start a subexpression}}

test/Sema/typo_correction.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -typo-correction-limit 20
1+
// RUN: %target-typecheck-verify-swift -typo-correction-limit 22
22
// RUN: not %target-swift-frontend -typecheck -disable-typo-correction %s 2>&1 | %FileCheck %s -check-prefix=DISABLED
33
// RUN: not %target-swift-frontend -typecheck -typo-correction-limit 0 %s 2>&1 | %FileCheck %s -check-prefix=DISABLED
44
// RUN: not %target-swift-frontend -typecheck -DIMPORT_FAIL %s 2>&1 | %FileCheck %s -check-prefix=DISABLED
@@ -177,3 +177,17 @@ func boo() { // expected-note {{did you mean 'boo'?}}
177177
}
178178
}
179179
}
180+
181+
// Don't show underscored names as typo corrections unless the typed name also
182+
// begins with an underscore.
183+
func test_underscored_no_match() {
184+
let _ham = 0
185+
_ = ham
186+
// expected-error@-1 {{use of unresolved identifier 'ham'}}
187+
}
188+
189+
func test_underscored_match() {
190+
let _eggs = 4 // expected-note {{'_eggs' declared here}}
191+
_ = _fggs + 1
192+
// expected-error@-1 {{use of unresolved identifier '_fggs'; did you mean '_eggs'?}}
193+
}

0 commit comments

Comments
 (0)