Skip to content

Commit

Permalink
add some type info to Base to avoid excess recursion in inference
Browse files Browse the repository at this point in the history
Fixes #33336.
This addresses some commonly-occurring cases where having too little
type info makes inference see a lot of recursion in Base that is
not actually possible.
  • Loading branch information
JeffBezanson committed Oct 6, 2019
1 parent 0aa59a0 commit daf87a5
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
10 changes: 5 additions & 5 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1190,13 +1190,13 @@ mightalias(A::AbstractArray, B::AbstractArray) = !isbits(A) && !isbits(B) && !_i
mightalias(x, y) = false

_isdisjoint(as::Tuple{}, bs::Tuple{}) = true
_isdisjoint(as::Tuple{}, bs::Tuple{Any}) = true
_isdisjoint(as::Tuple{}, bs::Tuple{UInt}) = true
_isdisjoint(as::Tuple{}, bs::Tuple) = true
_isdisjoint(as::Tuple{Any}, bs::Tuple{}) = true
_isdisjoint(as::Tuple{Any}, bs::Tuple{Any}) = as[1] != bs[1]
_isdisjoint(as::Tuple{Any}, bs::Tuple) = !(as[1] in bs)
_isdisjoint(as::Tuple{UInt}, bs::Tuple{}) = true
_isdisjoint(as::Tuple{UInt}, bs::Tuple{UInt}) = as[1] != bs[1]
_isdisjoint(as::Tuple{UInt}, bs::Tuple) = !(as[1] in bs)
_isdisjoint(as::Tuple, bs::Tuple{}) = true
_isdisjoint(as::Tuple, bs::Tuple{Any}) = !(bs[1] in as)
_isdisjoint(as::Tuple, bs::Tuple{UInt}) = !(bs[1] in as)
_isdisjoint(as::Tuple, bs::Tuple) = !(as[1] in bs) && _isdisjoint(tail(as), bs)

"""
Expand Down
2 changes: 1 addition & 1 deletion base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ Base.@propagate_inbounds _newindex(ax::Tuple{}, I::Tuple{}) = ()
@inline function _newindexer(indsA::Tuple)
ind1 = indsA[1]
keep, Idefault = _newindexer(tail(indsA))
(Base.length(ind1)!=1, keep...), (first(ind1), Idefault...)
(Base.length(ind1)::Integer != 1, keep...), (first(ind1), Idefault...)
end

@inline function Base.getindex(bc::Broadcasted, I::Union{Integer,CartesianIndex})
Expand Down
4 changes: 2 additions & 2 deletions base/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Core.NamedTuple
if nameof(@__MODULE__) === :Base

@eval function NamedTuple{names,T}(args::Tuple) where {names, T <: Tuple}
if length(args) != length(names)
if length(args) != length(names::Tuple)
throw(ArgumentError("Wrong number of arguments to named tuple constructor."))
end
# Note T(args) might not return something of type T; e.g.
Expand Down Expand Up @@ -251,7 +251,7 @@ julia> merge((a=1, b=2, c=3), [:b=>4, :d=>5])
function merge(a::NamedTuple, itr)
names = Symbol[]
vals = Any[]
inds = IdDict()
inds = IdDict{Any,Int}()
for (k,v) in itr
oldind = get(inds, k, 0)
if oldind > 0
Expand Down
5 changes: 3 additions & 2 deletions base/ntuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ ntuple(f, ::Val{3}) = (@_inline_meta; (f(1), f(2), f(3)))
end
end

@inline function fill_to_length(t::Tuple, val, ::Val{N}) where {N}
@inline function fill_to_length(t::Tuple, val, ::Val{_N}) where {_N}
M = length(t)
N = _N::Int
M > N && throw(ArgumentError("input tuple of length $M, requested $N"))
if @generated
quote
(t..., $(fill(:val, N-length(t.parameters))...))
(t..., $(fill(:val, (_N::Int) - length(t.parameters))...))
end
else
(t..., fill(val, N-M)...)
Expand Down
2 changes: 1 addition & 1 deletion base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function fieldname(t::DataType, i::Integer)
throw(ArgumentError("type does not have definite field names"))
end
names = _fieldnames(t)
n_fields = length(names)
n_fields = length(names)::Int
field_label = n_fields == 1 ? "field" : "fields"
i > n_fields && throw(ArgumentError("Cannot access field $i since type $t only has $n_fields $field_label."))
i < 1 && throw(ArgumentError("Field numbers must be positive integers. $i is invalid."))
Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ end
function show_datatype(io::IO, x::DataType)
istuple = x.name === Tuple.name
if (!isempty(x.parameters) || istuple) && x !== Tuple
n = length(x.parameters)
n = length(x.parameters)::Int

# Print homogeneous tuples with more than 3 elements compactly as NTuple{N, T}
if istuple && n > 3 && all(i -> (x.parameters[1] === i), x.parameters)
Expand Down

0 comments on commit daf87a5

Please sign in to comment.