-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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-12421] Incorrect/unhelpful error when incorrectly calling return in failable initializer #54860
Comments
Comment by Hassan ElDesouky (JIRA) pilky (JIRA User), @xedin
|
hassaneldesouky (JIRA User) As far as I could see the incorrect error is already removed on master, and this is compiling correctly, which is not the expected behavior I think ... |
Comment by Hassan ElDesouky (JIRA) Thank you @LucianoPAlmeida I realized after posting the comment that I have to do it before TypeCheckStmt.cpp#L454. |
No problem! But one thing I just noticed is that this code should compile because when a result stmt has no result it means that it is a `return;` so is valid. |
Comment by Hassan ElDesouky (JIRA) PR: #30707 |
pilky (JIRA User) Although this message may seem confusing is it the expected behavior because you can return; early on failable initializers but all members should be initialized in all paths, and that just doesn't happen in the guard early return of your example. So what the error is trying to say is that you should initialize label member before return; on the guard statement. guard let actualLabel = label else {
self.label = ''" // Label should be initialized before return
return // It's allowed, but don't fail the init just return early
}
self.label = actualLabel Maybe it should say something like `self.label should be initialized in all branches` ... but I'm not sure |
Comment by Martin Pilkington (JIRA) @LucianoPAlmeida The case I gave is just the simplest case to reproduce the issue. Often you will have multiple guard statements making it easier to mistype, and make having to initialise all the other properties more complex. There is also the problem that the reason for having a failable initialiser is you don't want the properties to be set and a value to be returned as it would be in an invalid state. While the error is technically correct, ideally all compiler errors should point to and explain the actual problem. Firstly, for any initialiser I would expect the error to be on the line with the return statement of the branch where one or more properties aren't set rather than the method definition. And in the case of a failable initialiser I would expect the primary error/fix-it to be that that the user probably wanted to type |
Environment
Xcode 11.4 (11E146)
Additional Detail from JIRA
md5: 50d6e39b9624d565f57eea856086a4de
Issue Description:
If you create a failable initialiser and accidentally add a simple
return
rather thanreturn nil
when you want to fail, the Compiler gives an error at the start of the init about a property not being initialised.Ideally this should detect that you have done a simple
return
rather than areturn nil
and show an error at that line, ideally with a fixit to add the nil back inThe text was updated successfully, but these errors were encountered: