Skip to content

[DiagnosticsQoI] SR-3600: Better recovery for trying to name an initializer, deinitializer, or subscript #6863

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

Conversation

matthewcarroll
Copy link
Contributor

@matthewcarroll matthewcarroll commented Jan 17, 2017

Add a diagnostic to remove the name of an initializer, deinitializer, or subscript. The identifier token is consumed and skipped to prevent the parser from emitting additional error messages.

Add tests to verify that the name is removed from initializers, deinitializers, and subscripts declared with a name.

https://bugs.swift.org/browse/SR-3600

class A { 
    init x() {} // error: initializers cannot have a name
    deinit y() {} // error deinitializers cannot have a name
    subscript z() -> Int { return 0 } // error subscripts cannot have a name
}

…alizer, deinitializer, or subscript

Add a diagnostic to remove the name of an initializer, deinitializer, or subscript. The identifier token is consumed and skipped to prevent the parser from emitting additional error messages.

Add tests to verify that the name is removed from initializers, deinitializers, and subscripts declared with a name.
@matthewcarroll
Copy link
Contributor Author

@CodaFi,

Would you mind reviewing this when you get a chance?

Thanks!

struct A0 {
subscript // expected-error{{expected '(' for subscript parameters}}
subscript // expected-error {{subscripts cannot have a name}} {{5-7=}}
i : Int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting; because of the following colon, we ought to realize that it's parens that are missing, not a spurious name being added. I'm not sure this is a net improvement in this case.

Mind checking for a colon too? That would help initializers as well, though less so since init foo bar : Int could be a mistake for init foo(bar: Int) or init(foo bar: Int).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it’s preferable to treat init foo bar : Int as a mistake for int(foo bar : Int), since adding the parens would make the syntax valid. If so, I can restrict this diagnostic to require a following paren, and let the existing one handle that case (and do the same for subscripts). What do you think? (No problem if not. I can check for a colon following the identifier.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your way seems reasonable. Thanks, Matthew!

if (Tok.is(tok::identifier)) {
diagnose(Tok, diag::destructor_has_name).fixItRemove(Tok.getLoc());
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: we put else on the same line as } and generally avoid single-statement blocks

@slavapestov
Copy link
Contributor

@swift-ci Please smoke test

…alizer, deinitializer, or subscript

- Restrict this diagnostic to identifiers that are followed by a left paren.
@matthewcarroll
Copy link
Contributor Author

@jrose-apple, @slavapestov,

I added a check for identifiers that are followed by a left paren, added some tests, and fixed the code style issues.

Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks, Matthew!

@jrose-apple
Copy link
Contributor

@swift-ci Please smoke test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants