Skip to content

Commit 43643d2

Browse files
committed
replace ANY with @nospecialize annotation. part of #11339
1 parent 6541e45 commit 43643d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+499
-385
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Language changes
1313
* The syntax `1.+2` is deprecated, since it is ambiguous: it could mean either
1414
`1 .+ 2` (the current meaning) or `1. + 2` ([#19089]).
1515

16+
* Declaring arguments as `x::ANY` to avoid specialization has been replaced
17+
by `@nospecialize x` ([#22666]).
18+
1619
Breaking changes
1720
----------------
1821

base/array.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ getindex(::Type{T}, x) where {T} = (@_inline_meta; a = Array{T,1}(1); @inbounds
247247
getindex(::Type{T}, x, y) where {T} = (@_inline_meta; a = Array{T,1}(2); @inbounds (a[1] = x; a[2] = y); a)
248248
getindex(::Type{T}, x, y, z) where {T} = (@_inline_meta; a = Array{T,1}(3); @inbounds (a[1] = x; a[2] = y; a[3] = z); a)
249249

250-
function getindex(::Type{Any}, vals::ANY...)
250+
function getindex(::Type{Any}, @nospecialize vals...)
251251
a = Array{Any,1}(length(vals))
252252
@inbounds for i = 1:length(vals)
253253
a[i] = vals[i]
@@ -495,9 +495,9 @@ function _collect_indices(indsA, A)
495495
end
496496

497497
if isdefined(Core, :Inference)
498-
_default_eltype(itrt::ANY) = Core.Inference.return_type(first, Tuple{itrt})
498+
_default_eltype(@nospecialize itrt) = Core.Inference.return_type(first, Tuple{itrt})
499499
else
500-
_default_eltype(itr::ANY) = Any
500+
_default_eltype(@nospecialize itr) = Any
501501
end
502502

503503
_array_for(::Type{T}, itr, ::HasLength) where {T} = Array{T,1}(Int(length(itr)::Integer))
@@ -703,7 +703,7 @@ function push!(a::Array{T,1}, item) where T
703703
return a
704704
end
705705

706-
function push!(a::Array{Any,1}, item::ANY)
706+
function push!(a::Array{Any,1}, @nospecialize item)
707707
_growend!(a, 1)
708708
arrayset(a, item, length(a))
709709
return a

base/associative.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ function sizehint!(t::ObjectIdDict, newsz)
461461
rehash!(t, newsz)
462462
end
463463

464-
function setindex!(t::ObjectIdDict, v::ANY, k::ANY)
464+
function setindex!(t::ObjectIdDict, @nospecialize(v), @nospecialize(k))
465465
if t.ndel >= ((3*length(t.ht))>>2)
466466
rehash!(t, max(length(t.ht)>>1, 32))
467467
t.ndel = 0
@@ -470,22 +470,22 @@ function setindex!(t::ObjectIdDict, v::ANY, k::ANY)
470470
return t
471471
end
472472

473-
get(t::ObjectIdDict, key::ANY, default::ANY) =
473+
get(t::ObjectIdDict, @nospecialize(key), @nospecialize(default)) =
474474
ccall(:jl_eqtable_get, Any, (Any, Any, Any), t.ht, key, default)
475475

476-
function pop!(t::ObjectIdDict, key::ANY, default::ANY)
476+
function pop!(t::ObjectIdDict, @nospecialize(key), @nospecialize(default))
477477
val = ccall(:jl_eqtable_pop, Any, (Any, Any, Any), t.ht, key, default)
478478
# TODO: this can underestimate `ndel`
479479
val === default || (t.ndel += 1)
480480
return val
481481
end
482482

483-
function pop!(t::ObjectIdDict, key::ANY)
483+
function pop!(t::ObjectIdDict, @nospecialize(key))
484484
val = pop!(t, key, secret_table_token)
485485
val !== secret_table_token ? val : throw(KeyError(key))
486486
end
487487

488-
function delete!(t::ObjectIdDict, key::ANY)
488+
function delete!(t::ObjectIdDict, @nospecialize(key))
489489
pop!(t, key, secret_table_token)
490490
t
491491
end

base/base.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ mutable struct MethodError <: Exception
5454
f
5555
args
5656
world::UInt
57-
MethodError(f::ANY, args::ANY, world::UInt) = new(f, args, world)
57+
MethodError(@nospecialize(f), @nospecialize(args), world::UInt) = new(f, args, world)
5858
end
59-
MethodError(f::ANY, args::ANY) = MethodError(f, args, typemax(UInt))
59+
MethodError(@nospecialize(f), @nospecialize(args)) = MethodError(f, args, typemax(UInt))
6060

6161
"""
6262
EOFError()
@@ -122,7 +122,7 @@ ccall(:jl_get_system_hooks, Void, ())
122122
==(w::WeakRef, v) = isequal(w.value, v)
123123
==(w, v::WeakRef) = isequal(w, v.value)
124124

125-
function finalizer(o::ANY, f::ANY)
125+
function finalizer(@nospecialize(o), @nospecialize(f))
126126
if isimmutable(o)
127127
error("objects of type ", typeof(o), " cannot be finalized")
128128
end
@@ -138,8 +138,8 @@ function finalizer(o::T, f::Ptr{Void}) where T
138138
Core.getptls(), o, f)
139139
end
140140

141-
finalize(o::ANY) = ccall(:jl_finalize_th, Void, (Ptr{Void}, Any,),
142-
Core.getptls(), o)
141+
finalize(@nospecialize(o)) = ccall(:jl_finalize_th, Void, (Ptr{Void}, Any,),
142+
Core.getptls(), o)
143143

144144
gc(full::Bool=true) = ccall(:jl_gc_collect, Void, (Int32,), full)
145145
gc_enable(on::Bool) = ccall(:jl_gc_enable, Int32, (Int32,), on) != 0

base/boot.jl

+41-31
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,36 @@ else
182182
end
183183

184184
function Typeof end
185-
(f::typeof(Typeof))(x::ANY) = isa(x,Type) ? Type{x} : typeof(x)
185+
ccall(:jl_toplevel_eval_in, Any, (Any, Any),
186+
Core, quote
187+
(f::typeof(Typeof))(x) = ($(_expr(:meta,:nospecialize,:x)); isa(x,Type) ? Type{x} : typeof(x))
188+
end)
189+
190+
ccall(:jl_toplevel_eval_in, Any, (Any, Any),
191+
Core, quote
192+
Expr(args...) = ($(_expr(:meta,:nospecialize,:args)); _expr(args...))
193+
end)
186194

187195
abstract type Exception end
188196
mutable struct ErrorException <: Exception
189197
msg::AbstractString
190198
ErrorException(msg::AbstractString) = new(msg)
191199
end
192200

193-
Expr(args::ANY...) = _expr(args...)
194-
195201
macro _noinline_meta()
196202
Expr(:meta, :noinline)
197203
end
198204

205+
macro nospecialize(x)
206+
Expr(:meta, :nospecialize, x)
207+
end
208+
199209
struct BoundsError <: Exception
200210
a::Any
201211
i::Any
202212
BoundsError() = new()
203-
BoundsError(a::ANY) = (@_noinline_meta; new(a))
204-
BoundsError(a::ANY, i) = (@_noinline_meta; new(a,i))
213+
BoundsError(@nospecialize(a)) = (@_noinline_meta; new(a))
214+
BoundsError(@nospecialize(a), i) = (@_noinline_meta; new(a,i))
205215
end
206216
struct DivideError <: Exception end
207217
struct DomainError <: Exception end
@@ -232,16 +242,16 @@ getptls() = ccall(:jl_get_ptls_states, Ptr{Void}, ())
232242

233243
include(m::Module, fname::String) = ccall(:jl_load_, Any, (Any, Any), m, fname)
234244

235-
eval(e::ANY) = eval(Main, e)
236-
eval(m::Module, e::ANY) = ccall(:jl_toplevel_eval_in, Any, (Any, Any), m, e)
245+
eval(@nospecialize(e)) = eval(Main, e)
246+
eval(m::Module, @nospecialize(e)) = ccall(:jl_toplevel_eval_in, Any, (Any, Any), m, e)
237247

238-
kwfunc(f::ANY) = ccall(:jl_get_keyword_sorter, Any, (Any,), f)
248+
kwfunc(@nospecialize(f)) = ccall(:jl_get_keyword_sorter, Any, (Any,), f)
239249

240-
kwftype(t::ANY) = typeof(ccall(:jl_get_kwsorter, Any, (Any,), t))
250+
kwftype(@nospecialize(t)) = typeof(ccall(:jl_get_kwsorter, Any, (Any,), t))
241251

242252
mutable struct Box
243253
contents::Any
244-
Box(x::ANY) = new(x)
254+
Box(@nospecialize(x)) = new(x)
245255
Box() = new()
246256
end
247257

@@ -250,18 +260,18 @@ end
250260
mutable struct WeakRef
251261
value
252262
WeakRef() = WeakRef(nothing)
253-
WeakRef(v::ANY) = ccall(:jl_gc_new_weakref_th, Ref{WeakRef},
254-
(Ptr{Void}, Any), getptls(), v)
263+
WeakRef(@nospecialize(v)) = ccall(:jl_gc_new_weakref_th, Ref{WeakRef},
264+
(Ptr{Void}, Any), getptls(), v)
255265
end
256266

257267
TypeVar(n::Symbol) =
258268
ccall(:jl_new_typevar, Ref{TypeVar}, (Any, Any, Any), n, Union{}, Any)
259-
TypeVar(n::Symbol, ub::ANY) =
269+
TypeVar(n::Symbol, @nospecialize(ub)) =
260270
ccall(:jl_new_typevar, Ref{TypeVar}, (Any, Any, Any), n, Union{}, ub)
261-
TypeVar(n::Symbol, lb::ANY, ub::ANY) =
271+
TypeVar(n::Symbol, @nospecialize(lb), @nospecialize(ub)) =
262272
ccall(:jl_new_typevar, Ref{TypeVar}, (Any, Any, Any), n, lb, ub)
263273

264-
UnionAll(v::TypeVar, t::ANY) = ccall(:jl_type_unionall, Any, (Any, Any), v, t)
274+
UnionAll(v::TypeVar, @nospecialize(t)) = ccall(:jl_type_unionall, Any, (Any, Any), v, t)
265275

266276
Void() = nothing
267277

@@ -276,26 +286,26 @@ VecElement(arg::T) where {T} = VecElement{T}(arg)
276286
# used by lowering of splicing unquote
277287
splicedexpr(hd::Symbol, args::Array{Any,1}) = (e=Expr(hd); e.args=args; e)
278288

279-
_new(typ::Symbol, argty::Symbol) = eval(:((::Type{$typ})(n::$argty) = $(Expr(:new, typ, :n))))
289+
_new(typ::Symbol, argty::Symbol) = eval(Core, :((::Type{$typ})(@nospecialize n::$argty) = $(Expr(:new, typ, :n))))
280290
_new(:LabelNode, :Int)
281291
_new(:GotoNode, :Int)
282292
_new(:NewvarNode, :SlotNumber)
283-
_new(:QuoteNode, :ANY)
293+
_new(:QuoteNode, :Any)
284294
_new(:SSAValue, :Int)
285-
eval(:((::Type{LineNumberNode})(l::Int) = $(Expr(:new, :LineNumberNode, :l, nothing))))
286-
eval(:((::Type{LineNumberNode})(l::Int, f::ANY) = $(Expr(:new, :LineNumberNode, :l, :f))))
287-
eval(:((::Type{GlobalRef})(m::Module, s::Symbol) = $(Expr(:new, :GlobalRef, :m, :s))))
288-
eval(:((::Type{SlotNumber})(n::Int) = $(Expr(:new, :SlotNumber, :n))))
289-
eval(:((::Type{TypedSlot})(n::Int, t::ANY) = $(Expr(:new, :TypedSlot, :n, :t))))
295+
eval(Core, :((::Type{LineNumberNode})(l::Int) = $(Expr(:new, :LineNumberNode, :l, nothing))))
296+
eval(Core, :((::Type{LineNumberNode})(l::Int, @nospecialize(f)) = $(Expr(:new, :LineNumberNode, :l, :f))))
297+
eval(Core, :((::Type{GlobalRef})(m::Module, s::Symbol) = $(Expr(:new, :GlobalRef, :m, :s))))
298+
eval(Core, :((::Type{SlotNumber})(n::Int) = $(Expr(:new, :SlotNumber, :n))))
299+
eval(Core, :((::Type{TypedSlot})(n::Int, @nospecialize(t)) = $(Expr(:new, :TypedSlot, :n, :t))))
290300

291301
Module(name::Symbol=:anonymous, std_imports::Bool=true) = ccall(:jl_f_new_module, Ref{Module}, (Any, Bool), name, std_imports)
292302

293-
Task(f::ANY) = ccall(:jl_new_task, Ref{Task}, (Any, Int), f, 0)
303+
Task(@nospecialize(f)) = ccall(:jl_new_task, Ref{Task}, (Any, Int), f, 0)
294304

295305
# simple convert for use by constructors of types in Core
296306
# note that there is no actual conversion defined here,
297307
# so the methods and ccall's in Core aren't permitted to use convert
298-
convert(::Type{Any}, x::ANY) = x
308+
convert(::Type{Any}, @nospecialize(x)) = x
299309
convert(::Type{T}, x::T) where {T} = x
300310
cconvert(::Type{T}, x) where {T} = convert(T, x)
301311
unsafe_convert(::Type{T}, x::T) where {T} = x
@@ -370,16 +380,16 @@ function write(io::IO, x::String)
370380
return nb
371381
end
372382

373-
show(io::IO, x::ANY) = ccall(:jl_static_show, Void, (Ptr{Void}, Any), io_pointer(io), x)
383+
show(io::IO, @nospecialize x) = ccall(:jl_static_show, Void, (Ptr{Void}, Any), io_pointer(io), x)
374384
print(io::IO, x::Char) = ccall(:jl_uv_putc, Void, (Ptr{Void}, Char), io_pointer(io), x)
375385
print(io::IO, x::String) = (write(io, x); nothing)
376-
print(io::IO, x::ANY) = show(io, x)
377-
print(io::IO, x::ANY, a::ANY...) = (print(io, x); print(io, a...))
386+
print(io::IO, @nospecialize x) = show(io, x)
387+
print(io::IO, @nospecialize(x), @nospecialize a...) = (print(io, x); print(io, a...))
378388
println(io::IO) = (write(io, 0x0a); nothing) # 0x0a = '\n'
379-
println(io::IO, x::ANY...) = (print(io, x...); println(io))
389+
println(io::IO, @nospecialize x...) = (print(io, x...); println(io))
380390

381-
show(a::ANY) = show(STDOUT, a)
382-
print(a::ANY...) = print(STDOUT, a...)
383-
println(a::ANY...) = println(STDOUT, a...)
391+
show(@nospecialize a) = show(STDOUT, a)
392+
print(@nospecialize a...) = print(STDOUT, a...)
393+
println(@nospecialize a...) = println(STDOUT, a...)
384394

385395
ccall(:jl_set_istopmod, Void, (Any, Bool), Core, true)

base/client.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ end
142142
display_error(er, bt) = display_error(STDERR, er, bt)
143143
display_error(er) = display_error(er, [])
144144

145-
function eval_user_input(ast::ANY, show_value)
145+
function eval_user_input(@nospecialize(ast), show_value)
146146
errcount, lasterr, bt = 0, (), nothing
147147
while true
148148
try

base/dates/adjusters.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ lastdayofquarter(dt::DateTime) = DateTime(lastdayofquarter(Date(dt)))
186186
struct DateFunction
187187
f::Function
188188
# validate boolean, single-arg inner constructor
189-
function DateFunction(f::ANY, dt::TimeType)
189+
function DateFunction(@nospecialize(f), dt::TimeType)
190190
isa(f(dt), Bool) || throw(ArgumentError("Provided function must take a single TimeType argument and return true or false"))
191191
return new(f)
192192
end

base/deepcopy.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function deepcopy_internal(x::String, stackdict::ObjectIdDict)
5151
return y
5252
end
5353

54-
function deepcopy_internal(x::ANY, stackdict::ObjectIdDict)
54+
function deepcopy_internal(@nospecialize(x), stackdict::ObjectIdDict)
5555
T = typeof(x)::DataType
5656
nf = nfields(T)
5757
(isbits(T) || nf == 0) && return x
@@ -78,7 +78,7 @@ function deepcopy_internal(x::Array, stackdict::ObjectIdDict)
7878
_deepcopy_array_t(x, eltype(x), stackdict)
7979
end
8080

81-
function _deepcopy_array_t(x::ANY, T, stackdict::ObjectIdDict)
81+
function _deepcopy_array_t(@nospecialize(x), T, stackdict::ObjectIdDict)
8282
if isbits(T)
8383
return (stackdict[x]=copy(x))
8484
end

base/deprecated.jl

+6-3
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ end
11141114
@deprecate(SharedArray(filename::AbstractString, ::Type{T}, dims::NTuple, offset; kwargs...) where {T},
11151115
SharedArray{T}(filename, dims, offset; kwargs...))
11161116

1117-
@noinline function is_intrinsic_expr(x::ANY)
1117+
@noinline function is_intrinsic_expr(@nospecialize(x))
11181118
Base.depwarn("is_intrinsic_expr is deprecated. There are no intrinsic functions anymore.", :is_intrinsic_expr)
11191119
return false
11201120
end
@@ -1372,11 +1372,11 @@ _current_module() = ccall(:jl_get_current_module, Ref{Module}, ())
13721372
depwarn("binding_module(symbol) is deprecated, use `binding_module(module, symbol)` instead.", :binding_module)
13731373
return binding_module(_current_module(), s)
13741374
end
1375-
@noinline function expand(x::ANY)
1375+
@noinline function expand(@nospecialize(x))
13761376
depwarn("expand(x) is deprecated, use `expand(module, x)` instead.", :expand)
13771377
return expand(_current_module(), x)
13781378
end
1379-
@noinline function macroexpand(x::ANY)
1379+
@noinline function macroexpand(@nospecialize(x))
13801380
depwarn("macroexpand(x) is deprecated, use `macroexpand(module, x)` instead.", :macroexpand)
13811381
return macroexpand(_current_module(), x)
13821382
end
@@ -1543,6 +1543,9 @@ end
15431543
@deprecate cat_t{N,T}(::Type{Val{N}}, ::Type{T}, A, B) cat_t(Val(N), T, A, B) false
15441544
@deprecate reshape{N}(A::AbstractArray, ::Type{Val{N}}) reshape(A, Val(N))
15451545

1546+
# ::ANY is deprecated in src/method.c
1547+
# also remove all instances of `jl_ANY_flag` in src/
1548+
15461549
# END 0.7 deprecations
15471550

15481551
# BEGIN 1.0 deprecations

base/distributed/clusterserialize.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function lookup_object_number(s::ClusterSerializer, n::UInt64)
3232
return get(known_object_data, n, nothing)
3333
end
3434

35-
function remember_object(s::ClusterSerializer, o::ANY, n::UInt64)
35+
function remember_object(s::ClusterSerializer, @nospecialize(o), n::UInt64)
3636
known_object_data[n] = o
3737
if isa(o, TypeName) && !haskey(object_numbers, o)
3838
# set up reverse mapping for serialize

base/distributed/remotecall.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ Waits and fetches a value from `x` depending on the type of `x`:
484484
485485
Does not remove the item fetched.
486486
"""
487-
fetch(x::ANY) = x
487+
fetch(@nospecialize x) = x
488488

489489
isready(rv::RemoteValue, args...) = isready(rv.c, args...)
490490

base/docs/Docs.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ mutable struct DocStr
146146
data :: Dict{Symbol, Any}
147147
end
148148

149-
function docstr(binding::Binding, typesig::ANY = Union{})
149+
function docstr(binding::Binding, @nospecialize typesig = Union{})
150150
for m in modules
151151
dict = meta(m)
152152
if haskey(dict, binding)
@@ -229,7 +229,7 @@ end
229229
230230
Adds a new docstring `str` to the docsystem of `__module__` for `binding` and signature `sig`.
231231
"""
232-
function doc!(__module__::Module, b::Binding, str::DocStr, sig::ANY = Union{})
232+
function doc!(__module__::Module, b::Binding, str::DocStr, @nospecialize sig = Union{})
233233
initmeta(__module__)
234234
m = get!(meta(__module__), b, MultiDoc())
235235
if haskey(m.docs, sig)

base/docs/basedocs.jl

-8
Original file line numberDiff line numberDiff line change
@@ -689,14 +689,6 @@ The singleton instance of type `Void`, used by convention when there is no value
689689
"""
690690
nothing
691691

692-
"""
693-
ANY
694-
695-
Equivalent to `Any` for dispatch purposes, but signals the compiler to skip code
696-
generation specialization for that field.
697-
"""
698-
ANY
699-
700692
"""
701693
Core.TypeofBottom
702694

base/docs/utils.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ Strip all Markdown markup from x, leaving the result in plain text. Used
416416
internally by apropos to make docstrings containing more than one markdown
417417
element searchable.
418418
"""
419-
stripmd(x::ANY) = string(x) # for random objects interpolated into the docstring
419+
stripmd(@nospecialize x) = string(x) # for random objects interpolated into the docstring
420420
stripmd(x::AbstractString) = x # base case
421421
stripmd(x::Void) = " "
422422
stripmd(x::Vector) = string(map(stripmd, x)...)

0 commit comments

Comments
 (0)