Skip to content

Commit

Permalink
inference: guard error of fieldtype in :new expression handling
Browse files Browse the repository at this point in the history
Separated from #46111.
This really doesn't matter usually though as the frontend doesn't form
`:new` expression anyway.
  • Loading branch information
aviatesk committed Jul 20, 2022
1 parent 028e9ff commit c185f44
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
28 changes: 12 additions & 16 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1967,27 +1967,22 @@ function abstract_eval_statement(interp::AbstractInterpreter, @nospecialize(e),
if isconcretedispatch(t)
fcount = fieldcount(t)
nargs = length(e.args) - 1
is_nothrow && (is_nothrow = fcount nargs)
fcount < nargs && @goto always_throw # guard error from `fieldtype`
ats = Vector{Any}(undef, nargs)
local anyrefine = false
local allconst = true
for i = 2:length(e.args)
at = widenconditional(abstract_eval_value(interp, e.args[i], vtypes, sv))
ft = fieldtype(t, i-1)
for i = 1:nargs
at = widenconditional(abstract_eval_value(interp, e.args[i+1], vtypes, sv))
ft = fieldtype(t, i)
is_nothrow && (is_nothrow = at ft)
at = tmeet(at, ft)
if at === Bottom
t = Bottom
tristate_merge!(sv, EFFECTS_THROWS)
@goto t_computed
elseif !isa(at, Const)
allconst = false
end
at === Bottom && @goto always_throw
allconst &= isa(at, Const)
if !anyrefine
anyrefine = has_nontrivial_const_info(at) || # constant information
at ft # just a type-level information, but more precise than the declared type
end
ats[i-1] = at
ats[i] = at
end
# For now, don't allow:
# - Const/PartialStruct of mutables
Expand Down Expand Up @@ -2055,9 +2050,7 @@ function abstract_eval_statement(interp::AbstractInterpreter, @nospecialize(e),
t = sp_type_rewrap(e.args[2], sv.linfo, true)
for i = 3:length(e.args)
if abstract_eval_value(interp, e.args[i], vtypes, sv) === Bottom
t = Bottom
tristate_merge!(sv, EFFECTS_THROWS)
@goto t_computed
@goto always_throw
end
end
effects = EFFECTS_UNKNOWN
Expand Down Expand Up @@ -2117,10 +2110,13 @@ function abstract_eval_statement(interp::AbstractInterpreter, @nospecialize(e),
end
end
end
elseif false
@label always_throw
t = Bottom
tristate_merge!(sv, EFFECTS_THROWS)
else
t = abstract_eval_value_expr(interp, e, vtypes, sv)
end
@label t_computed
@assert !isa(t, TypeVar) "unhandled TypeVar"
if isa(t, DataType) && isdefined(t, :instance)
# replace singleton types with their equivalent Const object
Expand Down
4 changes: 4 additions & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4102,3 +4102,7 @@ struct Issue45780
end
f45780() = Val{Issue45780(@Base.Experimental.opaque ()->1).oc()}()
@test (@inferred f45780()) == Val{1}()

@test @eval Base.return_types() do
$(Expr(:new, Base.RefValue{Any}, nothing, nothing))
end |> only === Core.Compiler.Bottom

0 comments on commit c185f44

Please sign in to comment.