Skip to content

[Sema] Hide underscored names from typo-correction #20125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Sema/TypeCheckNameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,12 @@ static unsigned getCallEditDistance(DeclName writtenName,
StringRef writtenBase = writtenName.getBaseName().userFacingName();
StringRef correctedBase = correctedName.getBaseName().userFacingName();

// Don't typo-correct to a name with a leading underscore unless the typed
// name also begins with an underscore.
if (correctedBase.startswith("_") && !writtenBase.startswith("_")) {
return UnreasonableCallEditDistance;
}

unsigned distance = writtenBase.edit_distance(correctedBase, maxEditDistance);

// Bound the distance to UnreasonableCallEditDistance.
Expand Down
6 changes: 4 additions & 2 deletions test/Parse/recovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func useContainer() -> () {
func exists() -> Bool { return true }
}

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

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

// rdar://19605567
// expected-error@+1{{use of unresolved identifier 'esp'}}
// expected-error@+1{{use of unresolved identifier 'esp'; did you mean 'test'?}}
switch esp {
case let (jeb):
// expected-error@+5{{operator with postfix spacing cannot start a subexpression}}
Expand Down
16 changes: 15 additions & 1 deletion test/Sema/typo_correction.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift -typo-correction-limit 20
// RUN: %target-typecheck-verify-swift -typo-correction-limit 22
// RUN: not %target-swift-frontend -typecheck -disable-typo-correction %s 2>&1 | %FileCheck %s -check-prefix=DISABLED
// RUN: not %target-swift-frontend -typecheck -typo-correction-limit 0 %s 2>&1 | %FileCheck %s -check-prefix=DISABLED
// RUN: not %target-swift-frontend -typecheck -DIMPORT_FAIL %s 2>&1 | %FileCheck %s -check-prefix=DISABLED
Expand Down Expand Up @@ -177,3 +177,17 @@ func boo() { // expected-note {{did you mean 'boo'?}}
}
}
}

// Don't show underscored names as typo corrections unless the typed name also
// begins with an underscore.
func test_underscored_no_match() {
let _ham = 0
_ = ham
// expected-error@-1 {{use of unresolved identifier 'ham'}}
}

func test_underscored_match() {
let _eggs = 4 // expected-note {{'_eggs' declared here}}
_ = _fggs + 1
// expected-error@-1 {{use of unresolved identifier '_fggs'; did you mean '_eggs'?}}
}