-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SILGen] Insert an unreachable if a function declaration's parameter is uninhabited #20528
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
It can be useful to declare a function that takes an uninhabited type. (To fulfill a protocol requirement, e.g.) I envisioned a warning if you supplied a body for such a function—the compiler should let you omit a body altogether (https://bugs.swift.org/browse/SR-8434). i.e. I think this should be valid code: // Even though this returns an `Int`, it doesn't have a `return`. It doesn't need one: it's unreachable.
func f(_: Never) -> Int {} But I think should get a warning if you write any code in the function body: func f(_: Never) -> Int {
return 4 // warning: `f` takes an uninhabited parameter. It's code will never be executed.
} |
Interesting, although I am not too sure about it. It seems a bit strange for a function with a return type to not have a return statement 🤔 |
It's also strange to write a body for a function that can never be executed. 🙂 But I definitely don't think it should be a warning to declare a function that takes an uninhabited parameter: that can be useful. |
As Matt points out, there are still times where you have to write such a function today, even if it doesn't do anything. protocol ContrivedExample {
associatedtype Assoc
func doSomething(to value: Assoc)
func doSomethingElse()
}
struct Impl: ContrivedExample {
typealias Assoc = Never
func doSomething(to value: Never) { /*required by protocol*/ }
func doSomethingElse() { print("ok") }
} So I don't think we can take this as is. |
I see. So the warning should only be emitted if the body has a return statement? |
I kind of regret adding the StarterBug tag now. The point of this task is really to make it so that
compiles, by generating an |
@jrose-apple Ah! I was wondering if we would need to tweak the type checker as well because otherwise it would complain about a missing return statement. |
I think that error comes from SIL-level diagnostics, but I could be wrong. |
Hmm, you're right, there's a It seems maybe we can tweak |
You don’t want |
Hmm, I gave it a shot but it doesn't seem to emit an |
Nevermind, I was running the |
At the moment, I am getting a segfault when invoking the frontend with
|
I'm not a SIL expert, but assuming you're building with assertions but didn't get one, that sounds like the SILPrinter is trying to print a null value for something. |
@jrose-apple I think so, seems like the basic block is null when When I try emitting the IR then I don't get this crash. |
…eType instead of getType
@jrose-apple I have fixed the issue and now it works fine! 🎉 |
Build failed |
Build failed |
Build failed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know Jordan doesn't particularly like the word uninhabited
in diagnostics, but this case seems uncommon so I'm okay with this.
Heh, it slipped by me in looking at the code. That's true; it would probably be nicer to say "is an enum with no cases" or "contains an enum with no cases" as appropriate. |
Hmm, I have noticed both "uninhabited" being used in diagnostics (ex: |
Unrelated Foundation failure, I think… @swift-ci Please test Linux |
Yeah, all good now! |
I don't expect anything to come up here, but @swift-ci Please test source compatibility |
That took a long time haha, but all good here as well 👌 |
Is this good to merge now? @jrose-apple @jckarter |
Yeah, looks good. Thanks @theblixguy! |
Awesome! 🎉 |
PR swiftlang#20528 was merged yesterday, which inserts an `unreachable` in functions with uninhabited parameters. Unfortunately this means any statement in the function body, even ones that themselves are never-returning or don't have any executable code, cause the warning. Silence the warnings by deleting the bodies of these functions.
This PR adds a change to
SILGenProlog
(andSILGenStmt
as well) to create anunreachable
at the start of a function if any of the function's parameters are uninhabited.Resolves SR-8434 and SR-8435