Skip to content

Commit 9d23ba2

Browse files
committed
[Sema] Hide underscored names from typo-correction
Identifiers with a leading underscore are now hidden from typo-correction unless the typed name also begins with an underscore.
1 parent 5c03a0a commit 9d23ba2

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
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/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)