Skip to content

Commit d5d5718

Browse files
authored
add some type info to Base to avoid excess recursion in inference (#33476)
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.
1 parent 0aa59a0 commit d5d5718

File tree

6 files changed

+14
-13
lines changed

6 files changed

+14
-13
lines changed

base/abstractarray.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -1190,13 +1190,13 @@ mightalias(A::AbstractArray, B::AbstractArray) = !isbits(A) && !isbits(B) && !_i
11901190
mightalias(x, y) = false
11911191

11921192
_isdisjoint(as::Tuple{}, bs::Tuple{}) = true
1193-
_isdisjoint(as::Tuple{}, bs::Tuple{Any}) = true
1193+
_isdisjoint(as::Tuple{}, bs::Tuple{UInt}) = true
11941194
_isdisjoint(as::Tuple{}, bs::Tuple) = true
1195-
_isdisjoint(as::Tuple{Any}, bs::Tuple{}) = true
1196-
_isdisjoint(as::Tuple{Any}, bs::Tuple{Any}) = as[1] != bs[1]
1197-
_isdisjoint(as::Tuple{Any}, bs::Tuple) = !(as[1] in bs)
1195+
_isdisjoint(as::Tuple{UInt}, bs::Tuple{}) = true
1196+
_isdisjoint(as::Tuple{UInt}, bs::Tuple{UInt}) = as[1] != bs[1]
1197+
_isdisjoint(as::Tuple{UInt}, bs::Tuple) = !(as[1] in bs)
11981198
_isdisjoint(as::Tuple, bs::Tuple{}) = true
1199-
_isdisjoint(as::Tuple, bs::Tuple{Any}) = !(bs[1] in as)
1199+
_isdisjoint(as::Tuple, bs::Tuple{UInt}) = !(bs[1] in as)
12001200
_isdisjoint(as::Tuple, bs::Tuple) = !(as[1] in bs) && _isdisjoint(tail(as), bs)
12011201

12021202
"""

base/broadcast.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ Base.@propagate_inbounds _newindex(ax::Tuple{}, I::Tuple{}) = ()
555555
@inline function _newindexer(indsA::Tuple)
556556
ind1 = indsA[1]
557557
keep, Idefault = _newindexer(tail(indsA))
558-
(Base.length(ind1)!=1, keep...), (first(ind1), Idefault...)
558+
(Base.length(ind1)::Integer != 1, keep...), (first(ind1), Idefault...)
559559
end
560560

561561
@inline function Base.getindex(bc::Broadcasted, I::Union{Integer,CartesianIndex})

base/namedtuple.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Core.NamedTuple
6464
if nameof(@__MODULE__) === :Base
6565

6666
@eval function NamedTuple{names,T}(args::Tuple) where {names, T <: Tuple}
67-
if length(args) != length(names)
67+
if length(args) != length(names::Tuple)
6868
throw(ArgumentError("Wrong number of arguments to named tuple constructor."))
6969
end
7070
# Note T(args) might not return something of type T; e.g.
@@ -251,8 +251,8 @@ julia> merge((a=1, b=2, c=3), [:b=>4, :d=>5])
251251
function merge(a::NamedTuple, itr)
252252
names = Symbol[]
253253
vals = Any[]
254-
inds = IdDict()
255-
for (k,v) in itr
254+
inds = IdDict{Symbol,Int}()
255+
for (k::Symbol, v) in itr
256256
oldind = get(inds, k, 0)
257257
if oldind > 0
258258
vals[oldind] = v

base/ntuple.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ ntuple(f, ::Val{3}) = (@_inline_meta; (f(1), f(2), f(3)))
5555
end
5656
end
5757

58-
@inline function fill_to_length(t::Tuple, val, ::Val{N}) where {N}
58+
@inline function fill_to_length(t::Tuple, val, ::Val{_N}) where {_N}
5959
M = length(t)
60+
N = _N::Int
6061
M > N && throw(ArgumentError("input tuple of length $M, requested $N"))
6162
if @generated
6263
quote
63-
(t..., $(fill(:val, N-length(t.parameters))...))
64+
(t..., $(fill(:val, (_N::Int) - length(t.parameters))...))
6465
end
6566
else
6667
(t..., fill(val, N-M)...)

base/reflection.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function fieldname(t::DataType, i::Integer)
147147
throw(ArgumentError("type does not have definite field names"))
148148
end
149149
names = _fieldnames(t)
150-
n_fields = length(names)
150+
n_fields = length(names)::Int
151151
field_label = n_fields == 1 ? "field" : "fields"
152152
i > n_fields && throw(ArgumentError("Cannot access field $i since type $t only has $n_fields $field_label."))
153153
i < 1 && throw(ArgumentError("Field numbers must be positive integers. $i is invalid."))

base/show.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ end
523523
function show_datatype(io::IO, x::DataType)
524524
istuple = x.name === Tuple.name
525525
if (!isempty(x.parameters) || istuple) && x !== Tuple
526-
n = length(x.parameters)
526+
n = length(x.parameters)::Int
527527

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

0 commit comments

Comments
 (0)