Skip to content

Commit

Permalink
Fix arrayref nothrow predicate for undef-able arrays
Browse files Browse the repository at this point in the history
Previously the nothrow predicate said we were allowed to remove an unused
call if it was marked as inbounds. However, this is only true if none of
the entries of the array can be `#undef` (i.e. if the element type is a
bitstype of bitsunion). Correct the predicate and add a test case.

Fixes #28326
  • Loading branch information
Keno authored and JeffBezanson committed Jul 31, 2018
1 parent 1f52ab6 commit 81ee3ae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
20 changes: 18 additions & 2 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -995,13 +995,29 @@ function tuple_tfunc(@nospecialize(argtype))
return argtype
end

function array_type_undefable(@nospecialize(a))
if isa(a, Union)
return array_type_undefable(a.a) || array_type_undefable(a.b)
elseif isa(a, UnionAll)
return true
else
etype = (a::DataType).parameters[1]
return !(isbitstype(etype) || isbitsunion(etype))
end
end

function array_builtin_common_nothrow(argtypes::Array{Any,1}, first_idx_idx::Int)
length(argtypes) >= 4 || return false
(argtypes[1] Bool && argtypes[2] Array) || return false
atype = argtypes[2]
(argtypes[1] Bool && atype Array) || return false
for i = first_idx_idx:length(argtypes)
argtypes[i] Int || return false
end
# If we have @inbounds (first argument is false), we're allowed to assume we don't throw
# If we could potentially throw undef ref errors, bail out now.
atype = widenconst(atype)
array_type_undefable(atype) && return false
# If we have @inbounds (first argument is false), we're allowed to assume
# we don't throw bounds errors.
(isa(argtypes[1], Const) && !argtypes[1].val) && return true
# Else we can't really say anything here
# TODO: In the future we may be able to track the shapes of arrays though
Expand Down
11 changes: 11 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6658,3 +6658,14 @@ end
@test isa(foo28208(false, true), Tuple)
@test foo28208(true, false) === missing
@test foo28208(true, true) === nothing

# Issue #28326
function foo28326(a)
try
@inbounds a[1]
return false
catch
return true
end
end
@test foo28326(Vector(undef, 1))

0 comments on commit 81ee3ae

Please sign in to comment.