Skip to content

Commit 2b958e4

Browse files
committed
remove fallback eltype that returns Any for all types
1 parent 5ecfdbe commit 2b958e4

File tree

8 files changed

+18
-10
lines changed

8 files changed

+18
-10
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,9 @@ Deprecated or removed
11471147

11481148
* `signif` has been deprecated in favor of the `sigdigits` keyword argument to `round`.
11491149

1150+
* The fallback definition of `eltype` that returned `Any` for any type is deprecated.
1151+
Code should be restructured to avoid calling `eltype` on types that might not support it ([#26852]).
1152+
11501153
Command-line option changes
11511154
---------------------------
11521155

base/array.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ julia> eltype(fill(0x1, (2,2)))
8484
UInt8
8585
```
8686
"""
87-
eltype(::Type) = Any
87+
function eltype(t::Type)
88+
# TODO: change post-0.7
89+
#throw(MethodError(eltype, (t,)))
90+
depwarn("The fallback definition of `eltype` is deprecated; either define it for your type or avoid calling it on types that might not support it.", :eltype)
91+
return Any
92+
end
8893
eltype(::Type{Bottom}) = throw(ArgumentError("Union{} does not have elements"))
8994
eltype(x) = eltype(typeof(x))
9095

base/compiler/abstractinterpretation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ function precise_container_type(@nospecialize(arg), @nospecialize(typ), vtypes::
350350
else
351351
return Any[ p for p in tti0.parameters ]
352352
end
353-
elseif tti0 <: Array
353+
elseif tti0 isa Type{<:Array{T}} where T
354354
return Any[Vararg{eltype(tti0)}]
355355
else
356356
return Any[abstract_iteration(typ, vtypes, sv)]

base/dict.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Dict(ps::Pair...) = Dict(ps)
134134

135135
function Dict(kv)
136136
try
137-
dict_with_eltype((K, V) -> Dict{K, V}, kv, eltype(kv))
137+
dict_with_eltype((K, V) -> Dict{K, V}, kv, IteratorEltype(typeof(kv)) isa HasEltype ? eltype(kv) : Any)
138138
catch e
139139
if !applicable(start, kv) || !all(x->isa(x,Union{Tuple,Pair}),kv)
140140
throw(ArgumentError("Dict(kv): kv needs to be an iterator of tuples or pairs"))

base/weakkeydict.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ WeakKeyDict(ps::Pair...) = WeakKeyDict{Any,Any}(ps)
5858

5959
function WeakKeyDict(kv)
6060
try
61-
Base.dict_with_eltype((K, V) -> WeakKeyDict{K, V}, kv, eltype(kv))
61+
Base.dict_with_eltype((K, V) -> WeakKeyDict{K, V}, kv, IteratorEltype(typeof(kv)) isa HasEltype ? eltype(kv) : Any)
6262
catch e
6363
if !applicable(start, kv) || !all(x->isa(x,Union{Tuple,Pair}),kv)
6464
throw(ArgumentError("WeakKeyDict(kv): kv needs to be an iterator of tuples or pairs"))

stdlib/Distributed/src/cluster.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ mutable struct WorkerConfig
4242

4343
function WorkerConfig()
4444
wc = new()
45-
for n in 1:length(WorkerConfig.types)
46-
T = eltype(fieldtype(WorkerConfig, n))
45+
for n in 1:fieldcount(WorkerConfig)
4746
setfield!(wc, n, nothing)
4847
end
4948
wc

stdlib/Random/src/Random.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ struct SamplerTrivial{T,E} <: Sampler{E}
132132
self::T
133133
end
134134

135-
SamplerTrivial(x::T) where {T} = SamplerTrivial{T,eltype(T)}(x)
135+
SamplerTrivial(x::T) where {T} = SamplerTrivial{T,Any}(x)
136136

137137
Sampler(::AbstractRNG, x, ::Repetition) = SamplerTrivial(x)
138138

@@ -144,14 +144,14 @@ struct SamplerSimple{T,S,E} <: Sampler{E}
144144
data::S
145145
end
146146

147-
SamplerSimple(x::T, data::S) where {T,S} = SamplerSimple{T,S,eltype(T)}(x, data)
147+
SamplerSimple(x::T, data::S) where {T,S} = SamplerSimple{T,S,Any}(x, data)
148148

149149
Base.getindex(sp::SamplerSimple) = sp.self
150150

151151
# simple sampler carrying a (type) tag T and data
152152
struct SamplerTag{T,S,E} <: Sampler{E}
153153
data::S
154-
SamplerTag{T}(s::S) where {T,S} = new{T,S,eltype(T)}(s)
154+
SamplerTag{T}(s::S) where {T,S} = new{T,S,Any}(s)
155155
end
156156

157157

test/missing.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ end
358358
@test collect(x) isa Vector{Int}
359359

360360
x = skipmissing(v for v in [missing, 1, missing, 2, 4])
361-
@test eltype(x) === Any
361+
# TODO: enable in v0.7
362+
#@test_throws MethodError eltype(x)
362363
@test collect(x) == [1, 2, 4]
363364
@test collect(x) isa Vector{Int}
364365
end

0 commit comments

Comments
 (0)