Skip to content

Commit f908e82

Browse files
authored
fix mismatch in max_methods between return_type and its t-func (#30743)
1 parent ee6d662 commit f908e82

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const _REF_NAME = Ref.body.name
1616
call_result_unused(frame::InferenceState, pc::LineNum=frame.currpc) =
1717
isexpr(frame.src.code[frame.currpc], :call) && isempty(frame.ssavalue_uses[pc])
1818

19-
function abstract_call_gf_by_type(@nospecialize(f), argtypes::Vector{Any}, @nospecialize(atype), sv::InferenceState)
19+
function abstract_call_gf_by_type(@nospecialize(f), argtypes::Vector{Any}, @nospecialize(atype), sv::InferenceState,
20+
max_methods = sv.params.MAX_METHODS)
2021
atype_params = unwrap_unionall(atype).parameters
2122
ft = unwrap_unionall(atype_params[1]) # TODO: ccall jl_first_argument_datatype here
2223
isa(ft, DataType) || return Any # the function being called is unknown. can't properly handle this backedge right now
@@ -41,12 +42,12 @@ function abstract_call_gf_by_type(@nospecialize(f), argtypes::Vector{Any}, @nosp
4142
splitsigs = switchtupleunion(atype)
4243
applicable = Any[]
4344
for sig_n in splitsigs
44-
xapplicable = _methods_by_ftype(sig_n, sv.params.MAX_METHODS, sv.params.world, min_valid, max_valid)
45+
xapplicable = _methods_by_ftype(sig_n, max_methods, sv.params.world, min_valid, max_valid)
4546
xapplicable === false && return Any
4647
append!(applicable, xapplicable)
4748
end
4849
else
49-
applicable = _methods_by_ftype(atype, sv.params.MAX_METHODS, sv.params.world, min_valid, max_valid)
50+
applicable = _methods_by_ftype(atype, max_methods, sv.params.world, min_valid, max_valid)
5051
if applicable === false
5152
# this means too many methods matched
5253
# (assume this will always be true, so we don't compute / update valid age in this case)
@@ -489,7 +490,8 @@ function abstract_iteration(@nospecialize(itertype), vtypes::VarTable, sv::Infer
489490
end
490491

491492
# do apply(af, fargs...), where af is a function value
492-
function abstract_apply(@nospecialize(aft), aargtypes::Vector{Any}, vtypes::VarTable, sv::InferenceState)
493+
function abstract_apply(@nospecialize(aft), aargtypes::Vector{Any}, vtypes::VarTable, sv::InferenceState,
494+
max_methods = sv.params.MAX_METHODS)
493495
if !isa(aft, Const) && (!isType(aft) || has_free_typevars(aft))
494496
if !isconcretetype(aft) || (aft <: Builtin)
495497
# non-constant function of unknown type: bail now,
@@ -522,12 +524,12 @@ function abstract_apply(@nospecialize(aft), aargtypes::Vector{Any}, vtypes::VarT
522524
end
523525
for ct in ctypes
524526
if isa(aft, Const)
525-
rt = abstract_call(aft.val, (), ct, vtypes, sv)
527+
rt = abstract_call(aft.val, (), ct, vtypes, sv, max_methods)
526528
elseif isconstType(aft)
527-
rt = abstract_call(aft.parameters[1], (), ct, vtypes, sv)
529+
rt = abstract_call(aft.parameters[1], (), ct, vtypes, sv, max_methods)
528530
else
529531
astype = argtypes_to_type(ct)
530-
rt = abstract_call_gf_by_type(nothing, ct, astype, sv)
532+
rt = abstract_call_gf_by_type(nothing, ct, astype, sv, max_methods)
531533
end
532534
res = tmerge(res, rt)
533535
if res === Any
@@ -568,9 +570,9 @@ function pure_eval_call(@nospecialize(f), argtypes::Vector{Any}, @nospecialize(a
568570
end
569571
end
570572

571-
function abstract_call(@nospecialize(f), fargs::Union{Tuple{},Vector{Any}}, argtypes::Vector{Any}, vtypes::VarTable, sv::InferenceState)
573+
function abstract_call(@nospecialize(f), fargs::Union{Tuple{},Vector{Any}}, argtypes::Vector{Any}, vtypes::VarTable, sv::InferenceState, max_methods = sv.params.MAX_METHODS)
572574
if f === _apply
573-
return abstract_apply(argtypes[2], argtypes[3:end], vtypes, sv)
575+
return abstract_apply(argtypes[2], argtypes[3:end], vtypes, sv, max_methods)
574576
end
575577

576578
la = length(argtypes)
@@ -779,7 +781,7 @@ function abstract_call(@nospecialize(f), fargs::Union{Tuple{},Vector{Any}}, argt
779781
return Type # don't try to infer these function edges directly -- it won't actually come up with anything useful
780782
end
781783

782-
return abstract_call_gf_by_type(f, argtypes, atype, sv)
784+
return abstract_call_gf_by_type(f, argtypes, atype, sv, max_methods)
783785
end
784786

785787
# wrapper around `abstract_call` for first computing if `f` is available

base/compiler/tfuncs.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,11 +1335,11 @@ function return_type_tfunc(argtypes::Vector{Any}, vtypes::VarTable, sv::Inferenc
13351335
end
13361336
astype = argtypes_to_type(argtypes_vec)
13371337
if isa(aft, Const)
1338-
rt = abstract_call(aft.val, (), argtypes_vec, vtypes, sv)
1338+
rt = abstract_call(aft.val, (), argtypes_vec, vtypes, sv, -1)
13391339
elseif isconstType(aft)
1340-
rt = abstract_call(aft.parameters[1], (), argtypes_vec, vtypes, sv)
1340+
rt = abstract_call(aft.parameters[1], (), argtypes_vec, vtypes, sv, -1)
13411341
else
1342-
rt = abstract_call_gf_by_type(nothing, argtypes_vec, astype, sv)
1342+
rt = abstract_call_gf_by_type(nothing, argtypes_vec, astype, sv, -1)
13431343
end
13441344
if isa(rt, Const)
13451345
# output was computed to be constant

test/compiler/inference.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,3 +2196,13 @@ j30385(T, y) = k30385(f30385(T, y))
21962196

21972197
@test Base.return_types(Tuple, (NamedTuple{<:Any,Tuple{Any,Int}},)) == Any[Tuple{Any,Int}]
21982198
@test Base.return_types(Base.splat(tuple), (typeof((a=1,)),)) == Any[Tuple{Int}]
2199+
2200+
# test that return_type_tfunc isn't affected by max_methods differently than return_type
2201+
_rttf_test(::Int8) = 0
2202+
_rttf_test(::Int16) = 0
2203+
_rttf_test(::Int32) = 0
2204+
_rttf_test(::Int64) = 0
2205+
_rttf_test(::Int128) = 0
2206+
_call_rttf_test() = Core.Compiler.return_type(_rttf_test, Tuple{Any})
2207+
@test Core.Compiler.return_type(_rttf_test, Tuple{Any}) === Int
2208+
@test _call_rttf_test() === Int

0 commit comments

Comments
 (0)