Skip to content

Commit 53aa65a

Browse files
authored
inference: improve robustness of getfield_tfunc (#42378)
I found `getfield_tfunc` can easily throw on this code path. In the added test case, I intentionally used `PartialStruct` as an example target that can cause an error, but the same thing can happen for other arbitrary `CompilerTypes` objects.
1 parent 53da724 commit 53aa65a

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

base/compiler/tfuncs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ function getfield_tfunc(@nospecialize(s00), @nospecialize(name))
883883
return Bottom # can't index fields with Bool
884884
end
885885
if !isa(name, Const)
886+
name = widenconst(name)
886887
if !(Int <: name || Symbol <: name)
887888
return Bottom
888889
end

test/compiler/inference.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ barTuple2() = fooTuple{tuple(:y)}()
241241
Dict{Int64,Tuple{UnitRange{Int64},UnitRange{Int64}}},
242242
Core.Compiler.Const(:vals)) == Array{Tuple{UnitRange{Int64},UnitRange{Int64}},1}
243243

244+
# assert robustness of `getfield_tfunc`
245+
struct GetfieldRobustness
246+
field::String
247+
end
248+
@test Base.return_types((GetfieldRobustness,String,)) do obj, s
249+
t = (10, s) # to form `PartialStruct`
250+
getfield(obj, t)
251+
end |> only === Union{}
252+
244253
# issue #12476
245254
function f12476(a)
246255
(k, v) = a

0 commit comments

Comments
 (0)