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

Assert that exit never returns #51857

Merged
merged 4 commits into from
Oct 25, 2023
Merged

Conversation

jakobnissen
Copy link
Contributor

@jakobnissen jakobnissen commented Oct 25, 2023

Typeassert exit()::Union{}, such that the compiler knows that exit never returns, and thus can refine types based on this information.

Edit: This has been changed to manually calling error() instead of typeasserting.

Closes #51856

Note to reviewers: I don't think the solution exit()::Union{} is particularly nice. For example, it shows up in JET as an error because exit() is still inferred to Nothing before the typeassert. But I don't know how to directly tell the compiler that this function doesn't return.

Typeassert `exit()::Union{}`, such that the compiler knows that `exit` never
returns, and thus can refine types based on this information.
@giordano
Copy link
Contributor

For the case reported in #51856 I get

julia> using JET

julia> function parse_plus_one(s::String)
           n = tryparse(Int, s)
           if isnothing(n)
               println(stderr, "Could not parse as `Int`: \"$(s)\"")
               exit()
           end
           n
       end
parse_plus_one (generic function with 1 method)

julia> Base.exit(n) = ccall(:jl_exit, Cvoid, (Int32,), n)::Union{}

julia> @report_call parse_plus_one("15")
═════ 1 possible error found ═════
┌ parse_plus_one(s::String) @ Main ./REPL[7]:5
│┌ exit() @ Base ./initdefs.jl:29
││┌ exit(n::Int64) @ Main ./REPL[8]:1
│││ invalid builtin function call: typeassert($(Expr(:foreigncall, :(:jl_exit), Nothing, svec(Int32), 0, :(:ccall), :(%2), :(%1))), Union{})
││└────────────────────

which doesn't look much better?

@jakobnissen
Copy link
Contributor Author

No, it would be better if the compiler actually knew that exit returned Union{} instead of asserting it (which essentially is just like adding error - because the compiler "knows" that nothing::Union{} will error). So, the JET error is not improved.
But actual inference of the compiler IS improved, i.e. this change will allow the compiler to refine the return type of exit()

@jakobnissen jakobnissen added the status:awaiting review PR is complete and seems ready to merge. Has tests and news/compat if needed. CI failures unrelated. label Oct 25, 2023
@KristofferC
Copy link
Sponsor Member

Why not just add the error()?

@jakobnissen
Copy link
Contributor Author

jakobnissen commented Oct 25, 2023

I thought that the typeassert was more explicit and would achieve the same thing.

@KristofferC
Copy link
Sponsor Member

Except spurious static analysis warnings?

@jakobnissen
Copy link
Contributor Author

Good point. I'll change it, but maybe @aviatesk should take a quick look at this, if he has time.

Copy link
Sponsor Member

@aviatesk aviatesk left a comment

Choose a reason for hiding this comment

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

LGTM.

With this change, JET will emit another warning in a case when exit is called deterministically, but JET should probably have a special case to suppress it:

julia> myexit(n) = (@ccall jl_exit(n::Cint)::Cvoid; error())
myexit (generic function with 1 method)

julia> report_call((Int,)) do n
           myexit(n)
       end
[ Info: Loading JET...
═════ 1 possible error found ═════
┌ (::var"#10#11")(n::Int64) @ Main ./REPL[2]:2
│┌ myexit(n::Int64) @ Main ./REPL[1]:1
││┌ error() @ Base ./error.jl:42
│││ may throw: Base.throw(Base.ErrorException((Base).string::typeof(string)(s::Tuple{}...)::String)::ErrorException)
││└────────────────────

@aviatesk aviatesk added status:merge me PR is reviewed. Merge when all tests are passing and removed status:awaiting review PR is complete and seems ready to merge. Has tests and news/compat if needed. CI failures unrelated. labels Oct 25, 2023
@Keno
Copy link
Member

Keno commented Oct 25, 2023

Why not do ccall(:jl_exit, Union{}, (Int32,), 1)?

@jakobnissen
Copy link
Contributor Author

Of course! That's better. Done, and thanks!

@vtjnash vtjnash merged commit 946ab71 into JuliaLang:master Oct 25, 2023
2 of 5 checks passed
@jakobnissen jakobnissen deleted the error_bottom branch October 26, 2023 05:48
@giordano giordano removed the status:merge me PR is reviewed. Merge when all tests are passing label Oct 30, 2023
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.

exit() inferred to return nothing, not Union{}
6 participants