Skip to content

Commit

Permalink
fix pkgeval
Browse files Browse the repository at this point in the history
wip
  • Loading branch information
aviatesk committed Aug 13, 2024
1 parent 5fef1ed commit 5a0b413
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
19 changes: 16 additions & 3 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,13 @@ function abstract_call_builtin(interp::AbstractInterpreter, f::Builtin, (; fargs
else
thentype = form_partially_defined_struct(argtype2, argtypes[3])
if thentype !== nothing
return Conditional(a, thentype, argtype2)
elsetype = argtype2
if rt === Const(false)
thentype = Bottom
elseif rt === Const(true)
elsetype = Bottom
end
return Conditional(a, thentype, elsetype)
end
end
end
Expand All @@ -2045,10 +2051,16 @@ function form_partially_defined_struct(@nospecialize(obj), @nospecialize(name))
name isa Const || return nothing
objt0 = widenconst(obj)
objt = unwrap_unionall(objt0)
objt isa DataType || return nothing
isabstracttype(objt) && return nothing
fldidx = try_compute_fieldidx(objt, name.val)
fldidx === nothing && return nothing
fldidx datatype_min_ninitialized(objt) && return nothing
nminfld = datatype_min_ninitialized(objt)
if ismutabletype(objt)
fldidx == nminfld+1 || return nothing
else
fldidx > nminfld || return nothing
end
return PartialStruct(objt0, Any[fieldtype(objt0, i) for i = 1:fldidx])
end

Expand Down Expand Up @@ -3111,7 +3123,8 @@ end
@nospecializeinfer function widenreturn_partials(𝕃ᵢ::PartialsLattice, @nospecialize(rt), info::BestguessInfo)
if isa(rt, PartialStruct)
fields = copy(rt.fields)
anyrefine = !isvarargtype(rt.fields[end]) && length(rt.fields) > datatype_min_ninitialized(rt.typ)
anyrefine = !isvarargtype(rt.fields[end]) &&
length(rt.fields) > datatype_min_ninitialized(unwrap_unionall(rt.typ))
𝕃 = typeinf_lattice(info.interp)
= strictpartialorder(𝕃)
for i in 1:length(fields)
Expand Down
13 changes: 5 additions & 8 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -419,30 +419,27 @@ end
else
return Bottom
end
if 1 <= idx <= datatype_min_ninitialized(a1)
if 1 idx datatype_min_ninitialized(a1)
return Const(true)
elseif a1.name === _NAMEDTUPLE_NAME
if isconcretetype(a1)
return Const(false)
else
ns = a1.parameters[1]
if isa(ns, Tuple)
return Const(1 <= idx <= length(ns))
return Const(1 idx length(ns))
end
end
elseif idx <= 0 || (!isvatuple(a1) && idx > fieldcount(a1))
elseif idx 0 || (!isvatuple(a1) && idx > fieldcount(a1))
return Const(false)
elseif isa(arg1, Const)
if !ismutabletype(a1) || isconst(a1, idx)
return Const(isdefined(arg1.val, idx))
end
elseif isa(arg1, PartialStruct)
nflds = length(arg1.fields)
if !isvarargtype(arg1.fields[end])
if 1 idx nflds
if 1 idx length(arg1.fields)
return Const(true)
elseif !ismutabletype(a1) || isconst(a1, idx)
return Const(false)
end
end
elseif !isvatuple(a1)
Expand Down Expand Up @@ -1005,7 +1002,7 @@ end
nflds = nfields(sv)
ismod = sv isa Module
elseif isa(s00, PartialStruct)
sty = s00.typ
sty = unwrap_unionall(s00.typ)
nflds = fieldcount_noerror(sty)
ismod = false
else
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/typelattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ with `Int` values.
If `typ` is a struct, `fields` represents the fields of the struct that are guaranteed to be
initialized. For instance, if the length of `fields` of `PartialStruct` representing a
struct with 4 fields is 3, the 4th field may be uninitialized. If the length is four, all
struct with 4 fields is 3, the 4th field may not be initialized. If the length is 4, all
fields are guaranteed to be initialized.
If `typ` is a tuple, the last element of `fields` may be `Vararg`. In this case, it is
Expand Down
21 changes: 16 additions & 5 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,7 @@ let nfields_tfunc(@nospecialize xs...) =
@test sizeof_nothrow(String)
@test !sizeof_nothrow(Type{String})
@test sizeof_tfunc(Type{Union{Int64, Int32}}) == Const(Core.sizeof(Union{Int64, Int32}))
let PT = Core.Compiler.PartialStruct(Tuple{Int64,UInt64}, Any[Const(10), UInt64])
let PT = Core.PartialStruct(Tuple{Int64,UInt64}, Any[Const(10), UInt64])
@test sizeof_tfunc(PT) === Const(16)
@test nfields_tfunc(PT) === Const(2)
@test sizeof_nothrow(PT)
Expand Down Expand Up @@ -4752,18 +4752,18 @@ end

init = Base.ImmutableDict{Number,Number}()
a = Const(init)
b = Core.Compiler.PartialStruct(typeof(init), Any[Const(init), Any, ComplexF64])
b = Core.PartialStruct(typeof(init), Any[Const(init), Any, ComplexF64])
c = Core.Compiler.tmerge(a, b)
@test (a, c) && (b, c)
@test c === typeof(init)

a = Core.Compiler.PartialStruct(typeof(init), Any[Const(init), ComplexF64, ComplexF64])
a = Core.PartialStruct(typeof(init), Any[Const(init), ComplexF64, ComplexF64])
c = Core.Compiler.tmerge(a, b)
@test (a, c) && (b, c)
@test c.fields[2] === Any # or Number
@test c.fields[3] === ComplexF64

b = Core.Compiler.PartialStruct(typeof(init), Any[Const(init), ComplexF32, Union{ComplexF32,ComplexF64}])
b = Core.PartialStruct(typeof(init), Any[Const(init), ComplexF32, Union{ComplexF32,ComplexF64}])
c = Core.Compiler.tmerge(a, b)
@test (a, c)
@test (b, c)
Expand Down Expand Up @@ -5887,7 +5887,11 @@ end
end == Val{true}
@test Base.infer_return_type((Any,Any,)) do a, b
Val(isdefined(PartiallyInitialized1(a, b), :c))
end == Val{false}
end >: Val{false}
@test Base.infer_return_type((PartiallyInitialized1,)) do x
@assert isdefined(x, :a)
return Val(isdefined(x, :c))
end == Val
@test Base.infer_return_type((Any,Any,Any)) do a, b, c
Val(isdefined(PartiallyInitialized1(a, b, c), :c))
end == Val{true}
Expand Down Expand Up @@ -5949,6 +5953,13 @@ end |> Core.Compiler.is_nothrow
return x[]
end
end |> Core.Compiler.is_nothrow
@test Base.infer_effects((Any,Any); optimize=false) do a, c
x = PartiallyInitialized2(a)
x.c = c
if isdefined(x, :c)
return x.b
end
end |> !Core.Compiler.is_nothrow

# End to end test case for the partially initialized struct with `PartialStruct`
@noinline broadcast_noescape1(a) = (broadcast(identity, a); nothing)
Expand Down
6 changes: 3 additions & 3 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ end
@test ntuple(identity, Val(n)) == ntuple(identity, n)
end

@test Core.Compiler.return_type(ntuple, Tuple{typeof(identity), Val}) == Tuple{Vararg{Int}}
@test Base.infer_return_type(ntuple, Tuple{typeof(identity), Val}) == Tuple{Vararg{Int}}
end

struct A_15703{N}
Expand Down Expand Up @@ -835,8 +835,8 @@ end
@test @inferred(Base.circshift(t3, 7)) == ('b', 'c', 'd', 'a')
@test @inferred(Base.circshift(t3, -1)) == ('b', 'c', 'd', 'a')
@test_throws MethodError circshift(t1, 'a')
@test Core.Compiler.return_type(circshift, Tuple{Tuple,Integer}) <: Tuple
@test Core.Compiler.return_type(circshift, Tuple{Tuple{Vararg{Any,10}},Integer}) <: Tuple{Vararg{Any,10}}
@test Base.infer_return_type(circshift, Tuple{Tuple,Integer}) <: Tuple
@test Base.infer_return_type(circshift, Tuple{Tuple{Vararg{Any,10}},Integer}) <: Tuple{Vararg{Any,10}}
for len 0:5
v = 1:len
t = Tuple(v)
Expand Down

0 comments on commit 5a0b413

Please sign in to comment.