Skip to content
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

[SR-8510] 'guard' is easy to misspell as 'gaurd', results in large number of follow-on errors, and could have a fixit. #2198

Open
huonw mannequin opened this issue Aug 10, 2018 · 9 comments
Labels
SwiftParser Bugs in the (new) Parser written in Swift

Comments

@huonw
Copy link
Mannequin

huonw mannequin commented Aug 10, 2018

Previous ID SR-8510
Radar rdar://problem/32419789
Original Reporter @huonw
Type Bug
Status In Progress
Resolution
Environment

Master at 2018-08-09

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, DiagnosticsQoI, Parser, StarterBug
Assignee mkita (JIRA)
Priority Medium

md5: 83854736e7d37441acce9e16b297a24a

Issue Description:

In the following code, the guard statement is misspelled, and it results in 6 errors, plus notes and warnings! It'd be neat if the compiler detected when the "consecutive statements" error starts with the identifier gaurd, and assumed that guard was what was meant (along with a fixit so the user swaps it). I think that only doing this when that error occurs is important, because someone could legitimately have var gaurd = 1; gaurd = 2; print(gaurd), and none of those should change to guard.

func foo() -> Int? { return 0 }
func bar() {
    gaurd let x = foo() else { return }
    print(x)
}
guard.swift:3:10: error: consecutive statements on a line must be separated by ';'
    gaurd let x = foo() else { return }
         ^
         ;
guard.swift:3:24: error: consecutive statements on a line must be separated by ';'
    gaurd let x = foo() else { return }
                       ^
                       ;
guard.swift:3:25: error: expected expression
    gaurd let x = foo() else { return }
                        ^
guard.swift:3:5: error: use of unresolved identifier 'gaurd'
    gaurd let x = foo() else { return }
    ^~~~~
guard.swift:3:30: error: closure expression is unused
    gaurd let x = foo() else { return }
                             ^
guard.swift:3:30: note: did you mean to use a 'do' statement?
    gaurd let x = foo() else { return }
                             ^
                             do 
guard.swift:4:11: warning: expression implicitly coerced from 'Int?' to 'Any'
    print(x)
          ^
guard.swift:4:11: note: provide a default value to avoid this warning
    print(x)
          ^
            ?? <#default value#>
guard.swift:4:11: note: force-unwrap the value to avoid this warning
    print(x)
          ^
           !
guard.swift:4:11: note: explicitly cast to 'Any' with 'as Any' to silence this warning
    print(x)
          ^
            as Any

Possible new version:

guard.swift:3:10: error: consecutive statements on a line must be separated by ';'; did you misspell 'guard'?
    gaurd let x = foo() else { return }
    ^~~~~
    guard
@huonw
Copy link
Mannequin Author

huonw mannequin commented Aug 10, 2018

This could also apply arbitrary misspellings of guard, and also to while, if, for, etc. Something like: if the consecutive statements error occurs, see if happens after a single identifier at the start of the line, and then try to typo correct that identifier to one of the statement keywords.

@belkadan
Copy link

@rintaro, think this is possible for a StarterBug? Or would it be too complicated?

@rintaro
Copy link
Mannequin

rintaro mannequin commented Oct 26, 2018

Yeah this is good StarterBug.
I will write up the implementation strategy in my mind.

@swift-ci
Copy link
Contributor

swift-ci commented Dec 3, 2018

Comment by Bruno Rocha (JIRA)

@huonw Are you working on this? I would like to give it a shot if nobody assigned it 🙂

@belkadan
Copy link

belkadan commented Dec 3, 2018

Go for it. (Huon's not working on Swift at all these days, but he's also just the filer, not necessarily the implementer.)

@belkadan
Copy link

Resetting assignees for all Starter Bugs that haven't been touched since 2018.

@swift-ci
Copy link
Contributor

Comment by Noah Wilder (JIRA)

To determine whether or not to offer a spelling suggestion, we can probably use the Levenshtein distance between the typo and the correct spelling, and, if it is within a certain threshold, offer a fix-it to correct it to possible keywords and/or identifiers.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@Rish-it
Copy link

Rish-it commented Jan 27, 2023

@swift-ci @belkadan are you is it still in the works??

@AnthonyLatsis
Copy link
Contributor

@Rish-it You are welcome to work on this! Let me know if you would like to be assigned.

@AnthonyLatsis AnthonyLatsis removed the bug Something isn't working label Sep 9, 2023
@ahoppen ahoppen removed the good first issue Good for newcomers label Sep 14, 2023
@ahoppen ahoppen transferred this issue from apple/swift Sep 14, 2023
@ahoppen ahoppen added the SwiftParser Bugs in the (new) Parser written in Swift label Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
SwiftParser Bugs in the (new) Parser written in Swift
Projects
None yet
Development

No branches or pull requests

5 participants