From df72eeaa7b7937499709759dd2ec6eac53191273 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Sat, 5 Oct 2019 14:46:06 -0400 Subject: [PATCH] add some type info to Base to avoid excess recursion in inference 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. --- base/abstractarray.jl | 10 +++++----- base/broadcast.jl | 2 +- base/namedtuple.jl | 4 ++-- base/ntuple.jl | 3 ++- base/reflection.jl | 2 +- base/show.jl | 2 +- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 62547c8d7e01b..9f0ae669caaca 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -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) """ diff --git a/base/broadcast.jl b/base/broadcast.jl index defd53aff6adf..913111e6481c2 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -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}) diff --git a/base/namedtuple.jl b/base/namedtuple.jl index 790a080bd4129..d72e72d618845 100644 --- a/base/namedtuple.jl +++ b/base/namedtuple.jl @@ -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. @@ -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 diff --git a/base/ntuple.jl b/base/ntuple.jl index 0967066f3447e..c3b2f1c5e12ab 100644 --- a/base/ntuple.jl +++ b/base/ntuple.jl @@ -55,8 +55,9 @@ 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 diff --git a/base/reflection.jl b/base/reflection.jl index c2a8f4d0bc95f..813ab0f4bc1ee 100644 --- a/base/reflection.jl +++ b/base/reflection.jl @@ -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.")) diff --git a/base/show.jl b/base/show.jl index e97a1eda9756c..22ff592ace180 100644 --- a/base/show.jl +++ b/base/show.jl @@ -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)