Skip to content

Commit 6a74f93

Browse files
committed
replace ANY with @nospecialize annotation. part of #11339
1 parent 84720fc commit 6a74f93

Some content is hidden

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

58 files changed

+523
-397
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`, which needs to be imported from `Base`. ([#22666]).
18+
1619
Breaking changes
1720
----------------
1821

base/array.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ getindex(::Type{T}, x) where {T} = (@_inline_meta; a = Array{T,1}(1); @inbounds
238238
getindex(::Type{T}, x, y) where {T} = (@_inline_meta; a = Array{T,1}(2); @inbounds (a[1] = x; a[2] = y); a)
239239
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)
240240

241-
function getindex(::Type{Any}, vals::ANY...)
241+
function getindex(::Type{Any}, @nospecialize vals...)
242242
a = Array{Any,1}(length(vals))
243243
@inbounds for i = 1:length(vals)
244244
a[i] = vals[i]
@@ -486,9 +486,9 @@ function _collect_indices(indsA, A)
486486
end
487487

488488
if isdefined(Core, :Inference)
489-
_default_eltype(itrt::ANY) = Core.Inference.return_type(first, Tuple{itrt})
489+
_default_eltype(@nospecialize itrt) = Core.Inference.return_type(first, Tuple{itrt})
490490
else
491-
_default_eltype(itr::ANY) = Any
491+
_default_eltype(@nospecialize itr) = Any
492492
end
493493

494494
_array_for(::Type{T}, itr, ::HasLength) where {T} = Array{T,1}(Int(length(itr)::Integer))
@@ -694,7 +694,7 @@ function push!(a::Array{T,1}, item) where T
694694
return a
695695
end
696696

697-
function push!(a::Array{Any,1}, item::ANY)
697+
function push!(a::Array{Any,1}, @nospecialize item)
698698
_growend!(a, 1)
699699
arrayset(a, item, length(a))
700700
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-34
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,23 @@ else
183183
end
184184

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

188197
abstract type Exception end
189198
mutable struct ErrorException <: Exception
190199
msg::AbstractString
191200
ErrorException(msg::AbstractString) = new(msg)
192201
end
193202

194-
Expr(args::ANY...) = _expr(args...)
195-
196203
macro _noinline_meta()
197204
Expr(:meta, :noinline)
198205
end
@@ -201,8 +208,8 @@ struct BoundsError <: Exception
201208
a::Any
202209
i::Any
203210
BoundsError() = new()
204-
BoundsError(a::ANY) = (@_noinline_meta; new(a))
205-
BoundsError(a::ANY, i) = (@_noinline_meta; new(a,i))
211+
BoundsError(@nospecialize(a)) = (@_noinline_meta; new(a))
212+
BoundsError(@nospecialize(a), i) = (@_noinline_meta; new(a,i))
206213
end
207214
struct DivideError <: Exception end
208215
struct OverflowError <: Exception end
@@ -218,8 +225,8 @@ struct InterruptException <: Exception end
218225
struct DomainError <: Exception
219226
val
220227
msg
221-
DomainError(val::ANY) = (@_noinline_meta; new(val))
222-
DomainError(val::ANY, msg::ANY) = (@_noinline_meta; new(val, msg))
228+
DomainError(@nospecialize(val)) = (@_noinline_meta; new(val))
229+
DomainError(@nospecialize(val), @nospecialize(msg)) = (@_noinline_meta; new(val, msg))
223230
end
224231
mutable struct TypeError <: Exception
225232
func::Symbol
@@ -232,7 +239,7 @@ struct InexactError <: Exception
232239
T::Type
233240
val
234241

235-
InexactError(f::Symbol, T::ANY, val::ANY) = (@_noinline_meta; new(f, T, val))
242+
InexactError(f::Symbol, @nospecialize(T), @nospecialize(val)) = (@_noinline_meta; new(f, T, val))
236243
end
237244

238245
abstract type DirectIndexString <: AbstractString end
@@ -244,16 +251,16 @@ getptls() = ccall(:jl_get_ptls_states, Ptr{Void}, ())
244251

245252
include(m::Module, fname::String) = ccall(:jl_load_, Any, (Any, Any), m, fname)
246253

247-
eval(e::ANY) = eval(Main, e)
248-
eval(m::Module, e::ANY) = ccall(:jl_toplevel_eval_in, Any, (Any, Any), m, e)
254+
eval(@nospecialize(e)) = eval(Main, e)
255+
eval(m::Module, @nospecialize(e)) = ccall(:jl_toplevel_eval_in, Any, (Any, Any), m, e)
249256

250-
kwfunc(f::ANY) = ccall(:jl_get_keyword_sorter, Any, (Any,), f)
257+
kwfunc(@nospecialize(f)) = ccall(:jl_get_keyword_sorter, Any, (Any,), f)
251258

252-
kwftype(t::ANY) = typeof(ccall(:jl_get_kwsorter, Any, (Any,), t))
259+
kwftype(@nospecialize(t)) = typeof(ccall(:jl_get_kwsorter, Any, (Any,), t))
253260

254261
mutable struct Box
255262
contents::Any
256-
Box(x::ANY) = new(x)
263+
Box(@nospecialize(x)) = new(x)
257264
Box() = new()
258265
end
259266

@@ -262,18 +269,18 @@ end
262269
mutable struct WeakRef
263270
value
264271
WeakRef() = WeakRef(nothing)
265-
WeakRef(v::ANY) = ccall(:jl_gc_new_weakref_th, Ref{WeakRef},
266-
(Ptr{Void}, Any), getptls(), v)
272+
WeakRef(@nospecialize(v)) = ccall(:jl_gc_new_weakref_th, Ref{WeakRef},
273+
(Ptr{Void}, Any), getptls(), v)
267274
end
268275

269276
TypeVar(n::Symbol) =
270277
ccall(:jl_new_typevar, Ref{TypeVar}, (Any, Any, Any), n, Union{}, Any)
271-
TypeVar(n::Symbol, ub::ANY) =
278+
TypeVar(n::Symbol, @nospecialize(ub)) =
272279
ccall(:jl_new_typevar, Ref{TypeVar}, (Any, Any, Any), n, Union{}, ub)
273-
TypeVar(n::Symbol, lb::ANY, ub::ANY) =
280+
TypeVar(n::Symbol, @nospecialize(lb), @nospecialize(ub)) =
274281
ccall(:jl_new_typevar, Ref{TypeVar}, (Any, Any, Any), n, lb, ub)
275282

276-
UnionAll(v::TypeVar, t::ANY) = ccall(:jl_type_unionall, Any, (Any, Any), v, t)
283+
UnionAll(v::TypeVar, @nospecialize(t)) = ccall(:jl_type_unionall, Any, (Any, Any), v, t)
277284

278285
Void() = nothing
279286

@@ -288,26 +295,26 @@ VecElement(arg::T) where {T} = VecElement{T}(arg)
288295
# used by lowering of splicing unquote
289296
splicedexpr(hd::Symbol, args::Array{Any,1}) = (e=Expr(hd); e.args=args; e)
290297

291-
_new(typ::Symbol, argty::Symbol) = eval(:((::Type{$typ})(n::$argty) = $(Expr(:new, typ, :n))))
298+
_new(typ::Symbol, argty::Symbol) = eval(Core, :((::Type{$typ})(@nospecialize n::$argty) = $(Expr(:new, typ, :n))))
292299
_new(:LabelNode, :Int)
293300
_new(:GotoNode, :Int)
294301
_new(:NewvarNode, :SlotNumber)
295-
_new(:QuoteNode, :ANY)
302+
_new(:QuoteNode, :Any)
296303
_new(:SSAValue, :Int)
297-
eval(:((::Type{LineNumberNode})(l::Int) = $(Expr(:new, :LineNumberNode, :l, nothing))))
298-
eval(:((::Type{LineNumberNode})(l::Int, f::ANY) = $(Expr(:new, :LineNumberNode, :l, :f))))
299-
eval(:((::Type{GlobalRef})(m::Module, s::Symbol) = $(Expr(:new, :GlobalRef, :m, :s))))
300-
eval(:((::Type{SlotNumber})(n::Int) = $(Expr(:new, :SlotNumber, :n))))
301-
eval(:((::Type{TypedSlot})(n::Int, t::ANY) = $(Expr(:new, :TypedSlot, :n, :t))))
304+
eval(Core, :((::Type{LineNumberNode})(l::Int) = $(Expr(:new, :LineNumberNode, :l, nothing))))
305+
eval(Core, :((::Type{LineNumberNode})(l::Int, @nospecialize(f)) = $(Expr(:new, :LineNumberNode, :l, :f))))
306+
eval(Core, :((::Type{GlobalRef})(m::Module, s::Symbol) = $(Expr(:new, :GlobalRef, :m, :s))))
307+
eval(Core, :((::Type{SlotNumber})(n::Int) = $(Expr(:new, :SlotNumber, :n))))
308+
eval(Core, :((::Type{TypedSlot})(n::Int, @nospecialize(t)) = $(Expr(:new, :TypedSlot, :n, :t))))
302309

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

305-
Task(f::ANY) = ccall(:jl_new_task, Ref{Task}, (Any, Int), f, 0)
312+
Task(@nospecialize(f)) = ccall(:jl_new_task, Ref{Task}, (Any, Int), f, 0)
306313

307314
# simple convert for use by constructors of types in Core
308315
# note that there is no actual conversion defined here,
309316
# so the methods and ccall's in Core aren't permitted to use convert
310-
convert(::Type{Any}, x::ANY) = x
317+
convert(::Type{Any}, @nospecialize(x)) = x
311318
convert(::Type{T}, x::T) where {T} = x
312319
cconvert(::Type{T}, x) where {T} = convert(T, x)
313320
unsafe_convert(::Type{T}, x::T) where {T} = x
@@ -382,16 +389,16 @@ function write(io::IO, x::String)
382389
return nb
383390
end
384391

385-
show(io::IO, x::ANY) = ccall(:jl_static_show, Void, (Ptr{Void}, Any), io_pointer(io), x)
392+
show(io::IO, @nospecialize x) = ccall(:jl_static_show, Void, (Ptr{Void}, Any), io_pointer(io), x)
386393
print(io::IO, x::Char) = ccall(:jl_uv_putc, Void, (Ptr{Void}, Char), io_pointer(io), x)
387394
print(io::IO, x::String) = (write(io, x); nothing)
388-
print(io::IO, x::ANY) = show(io, x)
389-
print(io::IO, x::ANY, a::ANY...) = (print(io, x); print(io, a...))
395+
print(io::IO, @nospecialize x) = show(io, x)
396+
print(io::IO, @nospecialize(x), @nospecialize a...) = (print(io, x); print(io, a...))
390397
println(io::IO) = (write(io, 0x0a); nothing) # 0x0a = '\n'
391-
println(io::IO, x::ANY...) = (print(io, x...); println(io))
398+
println(io::IO, @nospecialize x...) = (print(io, x...); println(io))
392399

393-
show(a::ANY) = show(STDOUT, a)
394-
print(a::ANY...) = print(STDOUT, a...)
395-
println(a::ANY...) = println(STDOUT, a...)
400+
show(@nospecialize a) = show(STDOUT, a)
401+
print(@nospecialize a...) = print(STDOUT, a...)
402+
println(@nospecialize a...) = println(STDOUT, a...)
396403

397404
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/Dates.jl

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import ..Base: ==, div, fld, mod, rem, gcd, lcm, +, -, *, /, %
3636
import ..Base.broadcast
3737

3838
using Base.Iterators
39+
using Base.@nospecialize
3940

4041
include("types.jl")
4142
include("periods.jl")

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(x)
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
@@ -1587,6 +1587,9 @@ end
15871587
@deprecate readstring(cmd::AbstractCmd, stdin::Redirectable) readstring(pipeline(stdin, cmd))
15881588
@deprecate eachline(cmd::AbstractCmd, stdin; chomp::Bool=true) eachline(pipeline(stdin, cmd), chomp=chomp)
15891589

1590+
# ::ANY is deprecated in src/method.c
1591+
# also remove all instances of `jl_ANY_flag` in src/
1592+
15901593
# END 0.7 deprecations
15911594

15921595
# BEGIN 1.0 deprecations

base/distributed/Distributed.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Base: getindex, wait, put!, take!, fetch, isready, push!, length,
1010
using Base: Process, Semaphore, JLOptions, AnyDict, buffer_writes, wait_connected,
1111
VERSION_STRING, sync_begin, sync_add, sync_end, async_run_thunk,
1212
binding_module, notify_error, atexit, julia_exename, julia_cmd,
13-
AsyncGenerator, display_error, acquire, release, invokelatest
13+
AsyncGenerator, display_error, acquire, release, invokelatest, @nospecialize
1414

1515
# NOTE: clusterserialize.jl imports additional symbols from Base.Serializer for use
1616

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

0 commit comments

Comments
 (0)