Skip to content

Allow calls to init without leading self. or super. #962

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 1 commit into from
Oct 21, 2022

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Oct 17, 2022

ASTGen will need to diagnose the missing self. or super.

@ahoppen ahoppen requested a review from bnbarham October 17, 2022 15:52
@ahoppen
Copy link
Member Author

ahoppen commented Oct 17, 2022

@swift-ci Please test

Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

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

I'm surprised this is diagnosed in the syntax tree at all. Why doesn't this belong in semantic analysis?

@ahoppen
Copy link
Member Author

ahoppen commented Oct 18, 2022

IIUC the root cause is that we always consider init as the start of a declaration. If you want to call init on self, you need to disambiguate by prefixing it with self.. At least that’s how the old parser operated and I wanted to maintain this behavior.

Given that init is only valid as a type member and not in code blocks, we could change the rules a little bit and only consider init as the start of a declaration if we are parsing a MemberDeclList. This would make the following a valid parse (it currently isn’t), which will need to be diagnosed in ASTGen. WDYT?

class Foo {
  init() {}
  init(param: Int) {
    init()
  }
}

CC @CodaFi

@DougGregor
Copy link
Member

Given that init is only valid as a type member and not in code blocks, we could change the rules a little bit and only consider init as the start of a declaration if we are parsing a MemberDeclList. This would make the following a valid parse (it currently isn’t), which will need to be diagnosed in ASTGen.

I'd be okay with this. It's not context-sensitive in any real sense, and it would be better for recovery to have init() be an expression wherever we allow them.

ASTGen will need to diagnose the missing `self.` or `super.`
@ahoppen ahoppen force-pushed the ahoppen/self-missing-from-init-call branch from 9ae5fd2 to 1a9bfe7 Compare October 20, 2022 08:04
@ahoppen ahoppen changed the title Add diagnostic if self. is missing in front of init call Allow calls to init without leading self. or super. Oct 20, 2022
@ahoppen
Copy link
Member Author

ahoppen commented Oct 20, 2022

Updated the PR to allow calls to init without self. or super.

@ahoppen
Copy link
Member Author

ahoppen commented Oct 20, 2022

@swift-ci Please test

@ahoppen ahoppen merged commit 3d2402c into swiftlang:main Oct 21, 2022
@ahoppen ahoppen deleted the ahoppen/self-missing-from-init-call branch October 21, 2022 05:31
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.

2 participants