Skip to content

Commit d8b9810

Browse files
authored
set @max_methods 2 for error (#54086)
1 parent c0611e8 commit d8b9810

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

base/error.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ throw
2727

2828
## native julia error handling ##
2929

30+
# This is `Experimental.@max_methods 2 function error end`, which is not available at this point in bootstrap.
31+
# NOTE It is important to always be able to infer the return type of `error` as `Union{}`,
32+
# but there's a hitch when a package globally sets `@max_methods 1` and it causes inference
33+
# for `error(::Any)` to fail (JuliaLang/julia#54029).
34+
# This definition site `@max_methods 2` setting overrides any global `@max_methods 1` settings
35+
# on package side, guaranteeing that return type inference on `error` is successful always.
36+
function error end
37+
typeof(error).name.max_methods = UInt8(2)
38+
3039
"""
3140
error(message::AbstractString)
3241

test/error.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,15 @@ end
127127
visited = test_exceptions(Base)
128128
test_exceptions(Core, visited)
129129
end
130+
131+
# inference quality test for `error`
132+
@test Base.infer_return_type(error, (Any,)) === Union{}
133+
@test Base.infer_return_type(xs->error(xs...), (Vector{Any},)) === Union{}
134+
module Issue54029
135+
export raise54029
136+
Base.Experimental.@max_methods 1
137+
raise54029(x) = error(x)
138+
end
139+
using .Issue54029
140+
@test Base.infer_return_type(raise54029, (Any,)) === Union{}
141+
@test Base.infer_return_type(xs->raise54029(xs...), (Vector{Any},)) === Union{}

0 commit comments

Comments
 (0)