Skip to content

inference: improve isdefined_tfunc accuracy for MustAlias #58743

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

Merged
merged 1 commit into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Compiler/src/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ end
return isdefined_tfunc(𝕃, arg1, sym)
end
@nospecs function isdefined_tfunc(𝕃::AbstractLattice, arg1, sym)
if arg1 isa MustAlias
arg1 = widenmustalias(arg1)
end
arg1t = arg1 isa Const ? typeof(arg1.val) : isconstType(arg1) ? typeof(arg1.parameters[1]) : widenconst(arg1)
a1 = unwrap_unionall(arg1t)
if isa(a1, DataType) && !isabstracttype(a1)
Expand Down
19 changes: 13 additions & 6 deletions Compiler/test/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2313,12 +2313,6 @@ let 𝕃ᵢ = InferenceLattice(MustAliasesLattice(BaseInferenceLattice.instance)
@test ifelse_tfunc(MustAlias(2, AliasableField{Any}, 1, Int), Int, Int) === Union{}
end

@testset "issue #56913: `BoundsError` in type inference" begin
R = UnitRange{Int}
@test Type{AbstractVector} == Base.infer_return_type(Base.promote_typeof, Tuple{R, R, Vector{Any}, Vararg{R}})
@test Type{AbstractVector} == Base.infer_return_type(Base.promote_typeof, Tuple{R, R, Vector{Any}, R, Vararg{R}})
end

maybeget_mustalias_tmerge(x::AliasableField) = x.f
maybeget_mustalias_tmerge(x) = x
@test Base.return_types((Union{Nothing,AliasableField{Any}},); interp=MustAliasInterpreter()) do x
Expand Down Expand Up @@ -2593,6 +2587,19 @@ end |> only === Compiler.InterMustAlias
return 0
end == Integer

# `isdefined` accuracy for `MustAlias`
@test Base.infer_return_type((Any,); interp=MustAliasInterpreter()) do x
xx = Ref{Any}(x)
xxx = Some{Any}(xx)
Val(isdefined(xxx.value, :x))
end == Val{true}

@testset "issue #56913: `BoundsError` in type inference" begin
R = UnitRange{Int}
@test Type{AbstractVector} == Base.infer_return_type(Base.promote_typeof, Tuple{R, R, Vector{Any}, Vararg{R}})
@test Type{AbstractVector} == Base.infer_return_type(Base.promote_typeof, Tuple{R, R, Vector{Any}, R, Vararg{R}})
end

function f25579(g)
h = g[]
t = (h === nothing)
Expand Down