Skip to content

Commit d8cf10b

Browse files
aviateskantoine-levitt
authored andcommitted
improve type stabilities where _methods_by_ftype is used (JuliaLang#39937)
1 parent c76ba94 commit d8cf10b

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

base/compiler/typeinfer.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,8 @@ function _return_type(interp::AbstractInterpreter, @nospecialize(f), @nospeciali
978978
rt = widenconst(rt)
979979
end
980980
else
981-
for match in _methods(f, t, -1, get_world_counter(interp))
981+
for match in _methods(f, t, -1, get_world_counter(interp))::Vector
982+
match = match::Core.MethodMatch
982983
ty = typeinf_type(interp, match.method, match.spec_types, match.sparams)
983984
ty === nothing && return Any
984985
rt = tmerge(rt, ty)

base/reflection.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -937,8 +937,8 @@ function methods(@nospecialize(f), @nospecialize(t),
937937
world = typemax(UInt)
938938
# Lack of specialization => a comprehension triggers too many invalidations via _collect, so collect the methods manually
939939
ms = Method[]
940-
for m in _methods(f, t, -1, world)
941-
m::Core.MethodMatch
940+
for m in _methods(f, t, -1, world)::Vector
941+
m = m::Core.MethodMatch
942942
(mod === nothing || m.method.module mod) && push!(ms, m.method)
943943
end
944944
MethodList(ms, typeof(f).name.mt)
@@ -1186,7 +1186,8 @@ function code_typed_by_type(@nospecialize(tt::Type);
11861186
error("signature does not correspond to a generic function")
11871187
end
11881188
asts = []
1189-
for match in matches
1189+
for match in matches::Vector
1190+
match = match::Core.MethodMatch
11901191
meth = func_for_method_checked(match.method, tt, match.sparams)
11911192
(code, ty) = Core.Compiler.typeinf_code(interp, meth, match.spec_types, match.sparams, optimize)
11921193
code === nothing && error("inference not successful") # inference disabled?
@@ -1218,7 +1219,8 @@ function return_types(@nospecialize(f), @nospecialize(types=Tuple), interp=Core.
12181219
types = to_tuple_type(types)
12191220
rt = []
12201221
world = get_world_counter()
1221-
for match in _methods(f, types, -1, world)
1222+
for match in _methods(f, types, -1, world)::Vector
1223+
match = match::Core.MethodMatch
12221224
meth = func_for_method_checked(match.method, types, match.sparams)
12231225
ty = Core.Compiler.typeinf_type(interp, meth, match.spec_types, match.sparams)
12241226
ty === nothing && error("inference not successful") # inference disabled?
@@ -1250,7 +1252,8 @@ function print_statement_costs(io::IO, @nospecialize(tt::Type);
12501252
end
12511253
params = Core.Compiler.OptimizationParams(interp)
12521254
cst = Int[]
1253-
for match in matches
1255+
for match in matches::Vector
1256+
match = match::Core.MethodMatch
12541257
meth = func_for_method_checked(match.method, tt, match.sparams)
12551258
(code, ty) = Core.Compiler.typeinf_code(interp, meth, match.spec_types, match.sparams, true)
12561259
code === nothing && error("inference not successful") # inference disabled?
@@ -1508,14 +1511,15 @@ function isambiguous(m1::Method, m2::Method; ambiguous_bottom::Bool=false)
15081511
ms = _methods_by_ftype(ti, -1, typemax(UInt), true, min, max, has_ambig)::Vector
15091512
has_ambig[] == 0 && return false
15101513
if !ambiguous_bottom
1511-
filter!(ms) do m
1514+
filter!(ms) do m::Core.MethodMatch
15121515
return !has_bottom_parameter(m.spec_types)
15131516
end
15141517
end
15151518
# if ml-matches reported the existence of an ambiguity over their
15161519
# intersection, see if both m1 and m2 may be involved in it
15171520
have_m1 = have_m2 = false
15181521
for match in ms
1522+
match = match::Core.MethodMatch
15191523
m = match.method
15201524
m === m1 && (have_m1 = true)
15211525
m === m2 && (have_m2 = true)

stdlib/REPL/src/REPLCompletions.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,9 @@ function get_type_call(expr::Expr)
413413
end
414414
# use _methods_by_ftype as the function is supplied as a type
415415
world = Base.get_world_counter()
416-
matches = Base._methods_by_ftype(Tuple{ft, args...}, -1, world)
416+
matches = Base._methods_by_ftype(Tuple{ft, args...}, -1, world)::Vector
417417
length(matches) == 1 || return (Any, false)
418-
match = first(matches)
418+
match = first(matches)::Core.MethodMatch
419419
# Typeinference
420420
interp = Core.Compiler.NativeInterpreter()
421421
return_type = Core.Compiler.typeinf_type(interp, match.method, match.spec_types, match.sparams)

stdlib/Test/src/Test.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,9 @@ function detect_ambiguities(mods::Module...;
14801480
ambig = Int32[0]
14811481
ms = Base._methods_by_ftype(m.sig, -1, typemax(UInt), true, UInt[typemin(UInt)], UInt[typemax(UInt)], ambig)
14821482
ambig[1] == 0 && continue
1483+
isa(ms, Bool) && continue
14831484
for match2 in ms
1485+
match2 = match2::Core.MethodMatch
14841486
m2 = match2.method
14851487
if !(m === m2 || Base.morespecific(m2.sig, m.sig))
14861488
if Base.isambiguous(m, m2; ambiguous_bottom)

0 commit comments

Comments
 (0)