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-14836] Improve diagnostic on missing 'do' before brace #57183

Closed
typesanitizer opened this issue Jun 27, 2021 · 5 comments
Closed

[SR-14836] Improve diagnostic on missing 'do' before brace #57183

typesanitizer opened this issue Jun 27, 2021 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers parser Area → compiler: The legacy C++ parser

Comments

@typesanitizer
Copy link

Previous ID SR-14836
Radar rdar://problem/79875348
Original Reporter @typesanitizer
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, Parser, StarterBug
Assignee None
Priority Medium

md5: 9992a54fd0fa4a7f3c669b06dd3c6b39

Issue Description:

Coming from C-based languages, one can potentially forget that do is explicitly needed in Swift.

let x = 10
{
    print("Hello")
} 

This gives a bunch of not-so-useful errors on main

tmp.swift:1:5: error: computed property must have an explicit type
let x = 10
    ^
     : <# Type #>
tmp.swift:2:1: error: 'let' declarations cannot be computed properties
{
^
tmp.swift:1:9: error: variable with getter/setter cannot have an initial value
let x = 10
        ^~
tmp.swift:3:5: error: cannot convert return expression of type '()' to return type 'Int'
    print("Hello")
    ^~~~~~~~~~~~~~ 

Perhaps we should have some heuristics (maybe check the column numbers of the let/var and compare it with the column number for the opening brace) to detect this situation and pinpoint the problem more clearly, suggesting that you add a do with a fix-it.

@typesanitizer
Copy link
Author

@swift-ci create

@rintaro
Copy link
Mannequin

rintaro mannequin commented Jun 30, 2021

If 'PatternBindingDecl' have the initializer and the following '{' at new line does not start with 'didSet' or 'willSet', we could avoid parsing '{' as an accessor block and let the existing diagnostics work:

test.swift:3:3: error: closure expression is unused
  {
  ^
test.swift:3:3: note: did you mean to use a 'do' statement?
  {
  ^
  do 

@swift-ci
Copy link
Collaborator

swift-ci commented Jul 1, 2021

Comment by jiaren wang (JIRA)

@rintaro I'm interested in this. Could you give me more infomation about how 'PatternBindingDecl' works.

@rintaro
Copy link
Mannequin

rintaro mannequin commented Jul 1, 2021

Hi jiaren wang (JIRA User), thank you for being interested in this!

Parsing PartternBindingDecl happens in parseDeclVar in 'ParseDecl.cpp'. This function is rather long because the grammar of var/let is quite complex. But anyway, the initializer is parsed here, right after that, there is a check of '{'. You need to check if this '{' is at new line (i.e. Tok.isAtStartOfLine()), and the next token (i.e. peekToken()) is NOT willSet, didSet etc., skip this block in case the initializer (i.e. PatternInit value in this func) is not null.

@typesanitizer
Copy link
Author

Fixed by jiaren wang (JIRA User) in #38260

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers parser Area → compiler: The legacy C++ parser
Projects
None yet
Development

No branches or pull requests

2 participants