• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #38002

06 Feb 2025 06:14AM UTC coverage: 20.322% (-2.4%) from 22.722%
#38002

push

local

web-flow
bpart: Fully switch to partitioned semantics (#57253)

This is the final PR in the binding partitions series (modulo bugs and
tweaks), i.e. it closes #54654 and thus closes #40399, which was the
original design sketch.

This thus activates the full designed semantics for binding partitions,
in particular allowing safe replacement of const bindings. It in
particular allows struct redefinitions. This thus closes
timholy/Revise.jl#18 and also closes #38584.

The biggest semantic change here is probably that this gets rid of the
notion of "resolvedness" of a binding. Previously, a lot of the behavior
of our implementation depended on when bindings were "resolved", which
could happen at basically an arbitrary point (in the compiler, in REPL
completion, in a different thread), making a lot of the semantics around
bindings ill- or at least implementation-defined. There are several
related issues in the bugtracker, so this closes #14055 closes #44604
closes #46354 closes #30277

It is also the last step to close #24569.
It also supports bindings for undef->defined transitions and thus closes
#53958 closes #54733 - however, this is not activated yet for
performance reasons and may need some further optimization.

Since resolvedness no longer exists, we need to replace it with some
hopefully more well-defined semantics. I will describe the semantics
below, but before I do I will make two notes:

1. There are a number of cases where these semantics will behave
slightly differently than the old semantics absent some other task going
around resolving random bindings.
2. The new behavior (except for the replacement stuff) was generally
permissible under the old semantics if the bindings happened to be
resolved at the right time.

With all that said, there are essentially three "strengths" of bindings:

1. Implicit Bindings: Anything implicitly obtained from `using Mod`, "no
binding", plus slightly more exotic corner cases around conflicts

2. Weakly declared bindin... (continued)

11 of 111 new or added lines in 7 files covered. (9.91%)

1273 existing lines in 68 files now uncovered.

9908 of 48755 relevant lines covered (20.32%)

105126.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.17
/base/essentials.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
using Core: CodeInfo, SimpleVector, donotdelete, compilerbarrier, memoryrefnew, memoryrefget, memoryrefset!
4

5
const Callable = Union{Function,Type}
6

7
const Bottom = Union{}
8

9
# Define minimal array interface here to help code used in macros:
10
length(a::Array{T, 0}) where {T} = 1
×
11
length(a::Array{T, 1}) where {T} = getfield(a, :size)[1]
132,102,099✔
12
length(a::Array{T, 2}) where {T} = (sz = getfield(a, :size); sz[1] * sz[2])
×
13
# other sizes are handled by generic prod definition for AbstractArray
14
length(a::GenericMemory) = getfield(a, :length)
2,987,813✔
15
throw_boundserror(A, I) = (@noinline; throw(BoundsError(A, I)))
×
16

17
# multidimensional getindex will be defined later on
18

19
==(a::GlobalRef, b::GlobalRef) = a.mod === b.mod && a.name === b.name
1✔
20

21
"""
22
    AbstractSet{T}
23

24
Supertype for set-like types whose elements are of type `T`.
25
[`Set`](@ref), [`BitSet`](@ref) and other types are subtypes of this.
26
"""
27
abstract type AbstractSet{T} end
28

29
"""
30
    AbstractDict{K, V}
31

32
Supertype for dictionary-like types with keys of type `K` and values of type `V`.
33
[`Dict`](@ref), [`IdDict`](@ref) and other types are subtypes of this.
34
An `AbstractDict{K, V}` should be an iterator of `Pair{K, V}`.
35
"""
36
abstract type AbstractDict{K,V} end
37

38
## optional pretty printer:
39
#const NamedTuplePair{N, V, names, T<:NTuple{N, Any}} = Pairs{Symbol, V, NTuple{N, Symbol}, NamedTuple{names, T}}
40
#export NamedTuplePair
41

42
macro _gc_preserve_begin(arg1)
43
    Expr(:gc_preserve_begin, esc(arg1))
44
end
45

46
macro _gc_preserve_end(token)
47
    Expr(:gc_preserve_end, esc(token))
48
end
49

50
"""
51
    @nospecialize
52

53
Applied to a function argument name, hints to the compiler that the method
54
implementation should not be specialized for different types of that argument,
55
but instead use the declared type for that argument.
56
It can be applied to an argument within a formal argument list,
57
or in the function body.
58
When applied to an argument, the macro must wrap the entire argument expression, e.g.,
59
`@nospecialize(x::Real)` or `@nospecialize(i::Integer...)` rather than wrapping just the argument name.
60
When used in a function body, the macro must occur in statement position and
61
before any code.
62

63
When used without arguments, it applies to all arguments of the parent scope.
64
In local scope, this means all arguments of the containing function.
65
In global (top-level) scope, this means all methods subsequently defined in the current module.
66

67
Specialization can reset back to the default by using [`@specialize`](@ref).
68

69
```julia
70
function example_function(@nospecialize x)
71
    ...
72
end
73

74
function example_function(x, @nospecialize(y = 1))
75
    ...
76
end
77

78
function example_function(x, y, z)
79
    @nospecialize x y
80
    ...
81
end
82

83
@nospecialize
84
f(y) = [x for x in y]
85
@specialize
86
```
87

88
!!! note
89
    `@nospecialize` affects code generation but not inference: it limits the diversity
90
    of the resulting native code, but it does not impose any limitations (beyond the
91
    standard ones) on type-inference. Use [`Base.@nospecializeinfer`](@ref) together with
92
    `@nospecialize` to additionally suppress inference.
93

94
# Examples
95

96
```julia-repl
97
julia> f(A::AbstractArray) = g(A)
98
f (generic function with 1 method)
99

100
julia> @noinline g(@nospecialize(A::AbstractArray)) = A[1]
101
g (generic function with 1 method)
102

103
julia> @code_typed f([1.0])
104
CodeInfo(
105
1 ─ %1 = invoke Main.g(_2::AbstractArray)::Float64
106
└──      return %1
107
) => Float64
108
```
109

110
Here, the `@nospecialize` annotation results in the equivalent of
111

112
```julia
113
f(A::AbstractArray) = invoke(g, Tuple{AbstractArray}, A)
114
```
115

116
ensuring that only one version of native code will be generated for `g`,
117
one that is generic for any `AbstractArray`.
118
However, the specific return type is still inferred for both `g` and `f`,
119
and this is still used in optimizing the callers of `f` and `g`.
120
"""
121
macro nospecialize(vars...)
2✔
122
    if nfields(vars) === 1
2✔
123
        # in argument position, need to fix `@nospecialize x=v` to `@nospecialize (kw x v)`
124
        var = getfield(vars, 1)
2✔
125
        if isa(var, Expr) && var.head === :(=)
2✔
126
            var.head = :kw
×
127
        end
128
    end
129
    return Expr(:meta, :nospecialize, vars...)
2✔
130
end
131

132
"""
133
    @specialize
134

135
Reset the specialization hint for an argument back to the default.
136
For details, see [`@nospecialize`](@ref).
137
"""
138
macro specialize(vars...)
139
    if nfields(vars) === 1
140
        # in argument position, need to fix `@specialize x=v` to `@specialize (kw x v)`
141
        var = getfield(vars, 1)
142
        if isa(var, Expr) && var.head === :(=)
143
            var.head = :kw
144
        end
145
    end
146
    return Expr(:meta, :specialize, vars...)
147
end
148

149
"""
150
    @isdefined s -> Bool
151

152
Tests whether variable `s` is defined in the current scope.
153

154
See also [`isdefined`](@ref) for field properties and [`isassigned`](@ref) for
155
array indexes or [`haskey`](@ref) for other mappings.
156

157
# Examples
158
```jldoctest
159
julia> @isdefined newvar
160
false
161

162
julia> newvar = 1
163
1
164

165
julia> @isdefined newvar
166
true
167

168
julia> function f()
169
           println(@isdefined x)
170
           x = 3
171
           println(@isdefined x)
172
       end
173
f (generic function with 1 method)
174

175
julia> f()
176
false
177
true
178
```
179
"""
180
macro isdefined(s::Symbol)
181
    return Expr(:escape, Expr(:isdefined, s))
182
end
183

184
_nameof(m::Module) = ccall(:jl_module_name, Ref{Symbol}, (Any,), m)
×
185

186
function _is_internal(__module__)
×
187
    return _nameof(__module__) === :Base ||
×
188
      _nameof(ccall(:jl_base_relative_to, Any, (Any,), __module__)::Module) === :Compiler
189
end
190

191
# can be used in place of `@assume_effects :total` (supposed to be used for bootstrapping)
192
macro _total_meta()
193
    return _is_internal(__module__) && Expr(:meta, Expr(:purity,
194
        #=:consistent=#true,
195
        #=:effect_free=#true,
196
        #=:nothrow=#true,
197
        #=:terminates_globally=#true,
198
        #=:terminates_locally=#false,
199
        #=:notaskstate=#true,
200
        #=:inaccessiblememonly=#true,
201
        #=:noub=#true,
202
        #=:noub_if_noinbounds=#false,
203
        #=:consistent_overlay=#false,
204
        #=:nortcall=#true))
205
end
206
# can be used in place of `@assume_effects :foldable` (supposed to be used for bootstrapping)
207
macro _foldable_meta()
208
    return _is_internal(__module__) && Expr(:meta, Expr(:purity,
209
        #=:consistent=#true,
210
        #=:effect_free=#true,
211
        #=:nothrow=#false,
212
        #=:terminates_globally=#true,
213
        #=:terminates_locally=#false,
214
        #=:notaskstate=#true,
215
        #=:inaccessiblememonly=#true,
216
        #=:noub=#true,
217
        #=:noub_if_noinbounds=#false,
218
        #=:consistent_overlay=#false,
219
        #=:nortcall=#true))
220
end
221
# can be used in place of `@assume_effects :terminates_locally` (supposed to be used for bootstrapping)
222
macro _terminates_locally_meta()
223
    return _is_internal(__module__) && Expr(:meta, Expr(:purity,
224
        #=:consistent=#false,
225
        #=:effect_free=#false,
226
        #=:nothrow=#false,
227
        #=:terminates_globally=#false,
228
        #=:terminates_locally=#true,
229
        #=:notaskstate=#false,
230
        #=:inaccessiblememonly=#false,
231
        #=:noub=#false,
232
        #=:noub_if_noinbounds=#false,
233
        #=:consistent_overlay=#false,
234
        #=:nortcall=#false))
235
end
236
# can be used in place of `@assume_effects :terminates_globally` (supposed to be used for bootstrapping)
237
macro _terminates_globally_meta()
238
    return _is_internal(__module__) && Expr(:meta, Expr(:purity,
239
        #=:consistent=#false,
240
        #=:effect_free=#false,
241
        #=:nothrow=#false,
242
        #=:terminates_globally=#true,
243
        #=:terminates_locally=#true,
244
        #=:notaskstate=#false,
245
        #=:inaccessiblememonly=#false,
246
        #=:noub=#false,
247
        #=:noub_if_noinbounds=#false,
248
        #=:consistent_overlay=#false,
249
        #=:nortcall=#false))
250
end
251
# can be used in place of `@assume_effects :terminates_globally :notaskstate` (supposed to be used for bootstrapping)
252
macro _terminates_globally_notaskstate_meta()
253
    return _is_internal(__module__) && Expr(:meta, Expr(:purity,
254
        #=:consistent=#false,
255
        #=:effect_free=#false,
256
        #=:nothrow=#false,
257
        #=:terminates_globally=#true,
258
        #=:terminates_locally=#true,
259
        #=:notaskstate=#true,
260
        #=:inaccessiblememonly=#false,
261
        #=:noub=#false,
262
        #=:noub_if_noinbounds=#false,
263
        #=:consistent_overlay=#false,
264
        #=:nortcall=#false))
265
end
266
# can be used in place of `@assume_effects :terminates_globally :noub` (supposed to be used for bootstrapping)
267
macro _terminates_globally_noub_meta()
268
    return _is_internal(__module__) && Expr(:meta, Expr(:purity,
269
        #=:consistent=#false,
270
        #=:effect_free=#false,
271
        #=:nothrow=#false,
272
        #=:terminates_globally=#true,
273
        #=:terminates_locally=#true,
274
        #=:notaskstate=#false,
275
        #=:inaccessiblememonly=#false,
276
        #=:noub=#true,
277
        #=:noub_if_noinbounds=#false,
278
        #=:consistent_overlay=#false,
279
        #=:nortcall=#false))
280
end
281
# can be used in place of `@assume_effects :effect_free :terminates_locally` (supposed to be used for bootstrapping)
282
macro _effect_free_terminates_locally_meta()
283
    return _is_internal(__module__) && Expr(:meta, Expr(:purity,
284
        #=:consistent=#false,
285
        #=:effect_free=#true,
286
        #=:nothrow=#false,
287
        #=:terminates_globally=#false,
288
        #=:terminates_locally=#true,
289
        #=:notaskstate=#false,
290
        #=:inaccessiblememonly=#false,
291
        #=:noub=#false,
292
        #=:noub_if_noinbounds=#false,
293
        #=:consistent_overlay=#false,
294
        #=:nortcall=#false))
295
end
296
# can be used in place of `@assume_effects :nothrow :noub` (supposed to be used for bootstrapping)
297
macro _nothrow_noub_meta()
298
    return _is_internal(__module__) && Expr(:meta, Expr(:purity,
299
        #=:consistent=#false,
300
        #=:effect_free=#false,
301
        #=:nothrow=#true,
302
        #=:terminates_globally=#false,
303
        #=:terminates_locally=#false,
304
        #=:notaskstate=#false,
305
        #=:inaccessiblememonly=#false,
306
        #=:noub=#true,
307
        #=:noub_if_noinbounds=#false,
308
        #=:consistent_overlay=#false,
309
        #=:nortcall=#false))
310
end
311
# can be used in place of `@assume_effects :nothrow` (supposed to be used for bootstrapping)
312
macro _nothrow_meta()
313
    return _is_internal(__module__) && Expr(:meta, Expr(:purity,
314
        #=:consistent=#false,
315
        #=:effect_free=#false,
316
        #=:nothrow=#true,
317
        #=:terminates_globally=#false,
318
        #=:terminates_locally=#false,
319
        #=:notaskstate=#false,
320
        #=:inaccessiblememonly=#false,
321
        #=:noub=#false,
322
        #=:noub_if_noinbounds=#false,
323
        #=:consistent_overlay=#false,
324
        #=:nortcall=#false))
325
end
326
# can be used in place of `@assume_effects :nothrow` (supposed to be used for bootstrapping)
327
macro _noub_meta()
328
    return _is_internal(__module__) && Expr(:meta, Expr(:purity,
329
        #=:consistent=#false,
330
        #=:effect_free=#false,
331
        #=:nothrow=#false,
332
        #=:terminates_globally=#false,
333
        #=:terminates_locally=#false,
334
        #=:notaskstate=#false,
335
        #=:inaccessiblememonly=#false,
336
        #=:noub=#true,
337
        #=:noub_if_noinbounds=#false,
338
        #=:consistent_overlay=#false,
339
        #=:nortcall=#false))
340
end
341
# can be used in place of `@assume_effects :notaskstate` (supposed to be used for bootstrapping)
342
macro _notaskstate_meta()
343
    return _is_internal(__module__) && Expr(:meta, Expr(:purity,
344
        #=:consistent=#false,
345
        #=:effect_free=#false,
346
        #=:nothrow=#false,
347
        #=:terminates_globally=#false,
348
        #=:terminates_locally=#false,
349
        #=:notaskstate=#true,
350
        #=:inaccessiblememonly=#false,
351
        #=:noub=#false,
352
        #=:noub_if_noinbounds=#false,
353
        #=:consistent_overlay=#false,
354
        #=:nortcall=#false))
355
end
356
# can be used in place of `@assume_effects :noub_if_noinbounds` (supposed to be used for bootstrapping)
357
macro _noub_if_noinbounds_meta()
358
    return _is_internal(__module__) && Expr(:meta, Expr(:purity,
359
        #=:consistent=#false,
360
        #=:effect_free=#false,
361
        #=:nothrow=#false,
362
        #=:terminates_globally=#false,
363
        #=:terminates_locally=#false,
364
        #=:notaskstate=#false,
365
        #=:inaccessiblememonly=#false,
366
        #=:noub=#false,
367
        #=:noub_if_noinbounds=#true,
368
        #=:consistent_overlay=#false,
369
        #=:nortcall=#false))
370
end
371

372
# another version of inlining that propagates an inbounds context
373
macro _propagate_inbounds_meta()
374
    return Expr(:meta, :inline, :propagate_inbounds)
375
end
376
macro _nospecializeinfer_meta()
377
    return Expr(:meta, :nospecializeinfer)
378
end
379

380
default_access_order(a::GenericMemory{:not_atomic}) = :not_atomic
×
381
default_access_order(a::GenericMemory{:atomic}) = :monotonic
×
382
default_access_order(a::GenericMemoryRef{:not_atomic}) = :not_atomic
×
383
default_access_order(a::GenericMemoryRef{:atomic}) = :monotonic
×
384

385
getindex(A::GenericMemory, i::Int) = (@_noub_if_noinbounds_meta;
12✔
386
    memoryrefget(memoryrefnew(memoryrefnew(A), i, @_boundscheck), default_access_order(A), false))
2,990,218✔
387
getindex(A::GenericMemoryRef) = memoryrefget(A, default_access_order(A), @_boundscheck)
×
388

389
"""
390
    nameof(m::Module) -> Symbol
391

392
Get the name of a `Module` as a [`Symbol`](@ref).
393

394
# Examples
395
```jldoctest
396
julia> nameof(Base.Broadcast)
397
:Broadcast
398
```
399
"""
400
nameof(m::Module) = (@_total_meta; ccall(:jl_module_name, Ref{Symbol}, (Any,), m))
35✔
401

402
typeof(function iterate end).name.constprop_heuristic = Core.ITERATE_HEURISTIC
403

404
"""
405
    convert(T, x)
406

407
Convert `x` to a value of type `T`.
408

409
If `T` is an [`Integer`](@ref) type, an [`InexactError`](@ref) will be raised if `x`
410
is not representable by `T`, for example if `x` is not integer-valued, or is outside the
411
range supported by `T`.
412

413
# Examples
414
```jldoctest
415
julia> convert(Int, 3.0)
416
3
417

418
julia> convert(Int, 3.5)
419
ERROR: InexactError: Int64(3.5)
420
Stacktrace:
421
[...]
422
```
423

424
If `T` is a [`AbstractFloat`](@ref) type, then it will return the
425
closest value to `x` representable by `T`. Inf is treated as one
426
ulp greater than `floatmax(T)` for purposes of determining nearest.
427

428
```jldoctest
429
julia> x = 1/3
430
0.3333333333333333
431

432
julia> convert(Float32, x)
433
0.33333334f0
434

435
julia> convert(BigFloat, x)
436
0.333333333333333314829616256247390992939472198486328125
437
```
438

439
If `T` is a collection type and `x` a collection, the result of
440
`convert(T, x)` may alias all or part of `x`.
441
```jldoctest
442
julia> x = Int[1, 2, 3];
443

444
julia> y = convert(Vector{Int}, x);
445

446
julia> y === x
447
true
448
```
449

450
See also: [`round`](@ref), [`trunc`](@ref), [`oftype`](@ref), [`reinterpret`](@ref).
451
"""
452
function convert end
453

454
# ensure this is never ambiguous, and therefore fast for lookup
455
convert(T::Type{Union{}}, x...) = throw(ArgumentError("cannot convert a value to Union{} for assignment"))
×
456

457
convert(::Type{Type}, x::Type) = x # the ssair optimizer is strongly dependent on this method existing to avoid over-specialization
×
458
                                   # in the absence of inlining-enabled
459
                                   # (due to fields typed as `Type`, which is generally a bad idea)
460
# These end up being called during bootstrap and then would be invalidated if not for the following:
461
convert(::Type{String}, x::String) = x
×
462

463
"""
464
    @eval [mod,] ex
465

466
Evaluate an expression with values interpolated into it using `eval`.
467
If two arguments are provided, the first is the module to evaluate in.
468
"""
469
macro eval(ex)
470
    return Expr(:let, Expr(:(=), :eval_local_result,
471
            Expr(:escape, Expr(:call, GlobalRef(Core, :eval), __module__, Expr(:quote, ex)))),
472
        Expr(:block,
473
            Expr(:var"latestworld-if-toplevel"),
474
            :eval_local_result))
475
end
476
macro eval(mod, ex)
477
    return Expr(:let, Expr(:(=), :eval_local_result,
478
            Expr(:escape, Expr(:call, GlobalRef(Core, :eval), mod, Expr(:quote, ex)))),
479
        Expr(:block,
480
            Expr(:var"latestworld-if-toplevel"),
481
            :eval_local_result))
482
end
483

484
# use `@eval` here to directly form `:new` expressions avoid implicit `convert`s
485
# in order to achieve better effects inference
486
@eval struct Pairs{K, V, I, A} <: AbstractDict{K, V}
487
    data::A
488
    itr::I
489
    Pairs{K, V, I, A}(data, itr) where {K, V, I, A} = $(Expr(:new, :(Pairs{K, V, I, A}), :(data isa A ? data : convert(A, data)), :(itr isa I ? itr : convert(I, itr))))
×
490
    Pairs{K, V}(data::A, itr::I) where {K, V, I, A} = $(Expr(:new, :(Pairs{K, V, I, A}), :data, :itr))
×
491
    Pairs{K}(data::A, itr::I) where {K, I, A} = $(Expr(:new, :(Pairs{K, eltype(A), I, A}), :data, :itr))
3,229✔
492
    Pairs(data::A, itr::I) where  {I, A} = $(Expr(:new, :(Pairs{eltype(I), eltype(A), I, A}), :data, :itr))
×
493
end
UNCOV
494
pairs(::Type{NamedTuple}) = Pairs{Symbol, V, NTuple{N, Symbol}, NamedTuple{names, T}} where {V, N, names, T<:NTuple{N, Any}}
×
495

496
"""
497
    Base.Pairs(values, keys) <: AbstractDict{eltype(keys), eltype(values)}
498

499
Transforms an indexable container into a Dictionary-view of the same data.
500
Modifying the key-space of the underlying data may invalidate this object.
501
"""
502
Pairs
503

504
argtail(x, rest...) = rest
501✔
505

506
"""
507
    tail(x::Tuple)::Tuple
508

509
Return a `Tuple` consisting of all but the first component of `x`.
510

511
See also: [`front`](@ref Base.front), [`rest`](@ref Base.rest), [`first`](@ref), [`Iterators.peel`](@ref).
512

513
# Examples
514
```jldoctest
515
julia> Base.tail((1,2,3))
516
(2, 3)
517

518
julia> Base.tail(())
519
ERROR: ArgumentError: Cannot call tail on an empty tuple.
520
```
521
"""
522
tail(x::Tuple) = argtail(x...)
28,885✔
523
tail(::Tuple{}) = throw(ArgumentError("Cannot call tail on an empty tuple."))
×
524

525
function unwrap_unionall(@nospecialize(a))
14,516✔
526
    @_foldable_meta
14,515✔
527
    while isa(a,UnionAll)
78,547✔
528
        a = a.body
876✔
529
    end
876✔
530
    return a
77,661✔
531
end
532

533
function rewrap_unionall(@nospecialize(t), @nospecialize(u))
15✔
534
    @_foldable_meta
×
535
    if !isa(u, UnionAll)
15,568✔
536
        return t
15,553✔
537
    end
538
    return UnionAll(u.var, rewrap_unionall(t, u.body))
25✔
539
end
540

541
function rewrap_unionall(t::Core.TypeofVararg, @nospecialize(u))
×
542
    @_foldable_meta
×
543
    isdefined(t, :T) || return t
×
544
    if !isa(u, UnionAll)
×
545
        return t
×
546
    end
547
    T = rewrap_unionall(t.T, u)
×
548
    if !isdefined(t, :N) || t.N === u.var
×
549
        return Vararg{T}
×
550
    end
551
    return Vararg{T, t.N}
×
552
end
553

554
# replace TypeVars in all enclosing UnionAlls with fresh TypeVars
555
function rename_unionall(@nospecialize(u))
×
556
    if !isa(u, UnionAll)
×
557
        return u
×
558
    end
559
    var = u.var::TypeVar
×
560
    body = UnionAll(var, rename_unionall(u.body))
×
561
    nv = TypeVar(var.name, var.lb, var.ub)
×
562
    return UnionAll(nv, body{nv})
×
563
end
564

565
# remove concrete constraint on diagonal TypeVar if it comes from troot
566
function widen_diagonal(@nospecialize(t), troot::UnionAll)
567
    body = ccall(:jl_widen_diagonal, Any, (Any, Any), t, troot)
5✔
568
end
569

570
function isvarargtype(@nospecialize(t))
571
    return isa(t, Core.TypeofVararg)
262,087✔
572
end
573

574
function isvatuple(@nospecialize(t))
575
    @_foldable_meta
×
576
    t = unwrap_unionall(t)
×
577
    if isa(t, DataType)
×
578
        n = length(t.parameters)
5,583✔
579
        return n > 0 && isvarargtype(t.parameters[n])
5,781✔
580
    end
581
    return false
×
582
end
583

584
function unwrapva(@nospecialize(t))
585
    isa(t, Core.TypeofVararg) || return t
3,170✔
586
    return isdefined(t, :T) ? t.T : Any
9✔
587
end
588

589
function unconstrain_vararg_length(va::Core.TypeofVararg)
590
    # construct a new Vararg type where its length is unconstrained,
591
    # but its element type still captures any dependencies the input
592
    # element type may have had on the input length
593
    return Vararg{unwrapva(va)}
1✔
594
end
595

596
import Core: typename
597

598
_tuple_error(T::Type, x) = (@noinline; throw(MethodError(convert, (T, x))))
×
599

600
convert(::Type{T}, x::T) where {T<:Tuple} = x
×
601
function convert(::Type{T}, x::NTuple{N,Any}) where {N, T<:Tuple}
×
602
    # First see if there could be any conversion of the input type that'd be a subtype of the output.
603
    # If not, we'll throw an explicit MethodError (otherwise, it might throw a typeassert).
604
    if typeintersect(NTuple{N,Any}, T) === Union{}
×
605
        _tuple_error(T, x)
×
606
    end
607
    function cvt1(n)
×
608
        @inline
×
609
        Tn = fieldtype(T, n)
×
610
        xn = getfield(x, n, #=boundscheck=#false)
×
611
        xn isa Tn && return xn
367✔
612
        return convert(Tn, xn)
2,388✔
613
    end
614
    return ntuple(cvt1, Val(N))::NTuple{N,Any}
2,226✔
615
end
616

617
# optimizations?
618
# converting to tuple types of fixed length
619
#convert(::Type{T}, x::T) where {N, T<:NTuple{N,Any}} = x
620
#convert(::Type{T}, x::NTuple{N,Any}) where {N, T<:NTuple{N,Any}} =
621
#    ntuple(n -> convert(fieldtype(T, n), x[n]), Val(N))
622
#convert(::Type{T}, x::Tuple{Vararg{Any}}) where {N, T<:NTuple{N,Any}} =
623
#    throw(MethodError(convert, (T, x)))
624
# converting to tuple types of indefinite length
625
#convert(::Type{Tuple{Vararg{V}}}, x::Tuple{Vararg{V}}) where {V} = x
626
#convert(::Type{NTuple{N, V}}, x::NTuple{N, V}) where {N, V} = x
627
#function convert(T::Type{Tuple{Vararg{V}}}, x::Tuple) where {V}
628
#    @isdefined(V) || (V = fieldtype(T, 1))
629
#    return map(t -> convert(V, t), x)
630
#end
631
#function convert(T::Type{NTuple{N, V}}, x::NTuple{N, Any}) where {N, V}
632
#    @isdefined(V) || (V = fieldtype(T, 1))
633
#    return map(t -> convert(V, t), x)
634
#end
635
# short tuples
636
#convert(::Type{Tuple{}}, ::Tuple{}) = ()
637
#convert(::Type{Tuple{S}}, x::Tuple{S}) where {S} = x
638
#convert(::Type{Tuple{S, T}}, x::Tuple{S, T}) where {S, T} = x
639
#convert(::Type{Tuple{S, T, U}}, x::Tuple{S, T, U}) where {S, T, U} = x
640
#convert(::Type{Tuple{S}}, x::Tuple{Any}) where {S} = (convert(S, x[1]),)
641
#convert(::Type{Tuple{S, T}}, x::Tuple{Any, Any}) where {S, T} = (convert(S, x[1]), convert(T, x[2]),)
642
#convert(::Type{Tuple{S, T, U}}, x::Tuple{Any, Any, Any}) where {S, T, U} = (convert(S, x[1]), convert(T, x[2]), convert(U, x[3]))
643
#convert(::Type{Tuple{}}, x::Tuple) = _tuple_error(Tuple{}, x)
644
#convert(::Type{Tuple{S}}, x::Tuple) = _tuple_error(Tuple{S}, x)
645
#convert(::Type{Tuple{S, T}}, x::Tuple{Any, Any}) where {S, T} =_tuple_error(Tuple{S, T}, x)
646
#convert(::Type{Tuple{S, T, U}}, x::Tuple{Any, Any, Any}) where {S, T, U} = _tuple_error(Tuple{S, T, U}, x)
647

648
"""
649
    oftype(x, y)
650

651
Convert `y` to the type of `x` i.e. `convert(typeof(x), y)`.
652

653
# Examples
654
```jldoctest
655
julia> x = 4;
656

657
julia> y = 3.;
658

659
julia> oftype(x, y)
660
3
661

662
julia> oftype(y, x)
663
4.0
664
```
665
"""
666
oftype(x, y) = y isa typeof(x) ? y : convert(typeof(x), y)::typeof(x)
616✔
667

668
unsigned(x::Int) = reinterpret(UInt, x)
34,464,053✔
669
signed(x::UInt) = reinterpret(Int, x)
19,855✔
670

671
"""
672
    cconvert(T,x)
673

674
Convert `x` to a value to be passed to C code as type `T`, typically by calling `convert(T, x)`.
675

676
In cases where `x` cannot be safely converted to `T`, unlike [`convert`](@ref), `cconvert` may
677
return an object of a type different from `T`, which however is suitable for
678
[`unsafe_convert`](@ref) to handle. The result of this function should be kept valid (for the GC)
679
until the result of [`unsafe_convert`](@ref) is not needed anymore.
680
This can be used to allocate memory that will be accessed by the `ccall`.
681
If multiple objects need to be allocated, a tuple of the objects can be used as return value.
682

683
Neither `convert` nor `cconvert` should take a Julia object and turn it into a `Ptr`.
684
"""
685
function cconvert end
686

687
cconvert(T::Type, x) = x isa T ? x : convert(T, x) # do the conversion eagerly in most cases
622,279✔
688
cconvert(::Type{Union{}}, x...) = convert(Union{}, x...)
×
689
cconvert(::Type{<:Ptr}, x) = x # but defer the conversion to Ptr to unsafe_convert
10✔
690
unsafe_convert(::Type{T}, x::T) where {T} = x # unsafe_convert (like convert) defaults to assuming the convert occurred
41✔
691
unsafe_convert(::Type{T}, x::T) where {T<:Ptr} = x  # to resolve ambiguity with the next method
4✔
692
unsafe_convert(::Type{P}, x::Ptr) where {P<:Ptr} = convert(P, x)
798,894✔
693

694
"""
695
    reinterpret(::Type{Out}, x::In)
696

697
Change the type-interpretation of the binary data in the isbits value `x`
698
to that of the isbits type `Out`.
699
The size (ignoring padding) of `Out` has to be the same as that of the type of `x`.
700
For example, `reinterpret(Float32, UInt32(7))` interprets the 4 bytes corresponding to `UInt32(7)` as a
701
[`Float32`](@ref). Note that `reinterpret(In, reinterpret(Out, x)) === x`
702

703
```jldoctest
704
julia> reinterpret(Float32, UInt32(7))
705
1.0f-44
706

707
julia> reinterpret(NTuple{2, UInt8}, 0x1234)
708
(0x34, 0x12)
709

710
julia> reinterpret(UInt16, (0x34, 0x12))
711
0x1234
712

713
julia> reinterpret(Tuple{UInt16, UInt8}, (0x01, 0x0203))
714
(0x0301, 0x02)
715
```
716

717
!!! note
718

719
    The treatment of padding differs from reinterpret(::DataType, ::AbstractArray).
720

721
!!! warning
722

723
    Use caution if some combinations of bits in `Out` are not considered valid and would
724
    otherwise be prevented by the type's constructors and methods. Unexpected behavior
725
    may result without additional validation.
726

727
"""
728
function reinterpret(::Type{Out}, x) where {Out}
7,260✔
729
    @inline
7,258✔
730
    if isprimitivetype(Out) && isprimitivetype(typeof(x))
7,258✔
731
        return bitcast(Out, x)
57,210,608✔
732
    end
733
    # only available when Base is fully loaded.
734
    return _reinterpret(Out, x)
×
735
end
736

737
"""
738
    sizeof(T::DataType)
739
    sizeof(obj)
740

741
Size, in bytes, of the canonical binary representation of the given `DataType` `T`, if any.
742
Or the size, in bytes, of object `obj` if it is not a `DataType`.
743

744
See also [`Base.summarysize`](@ref).
745

746
# Examples
747
```jldoctest
748
julia> sizeof(Float32)
749
4
750

751
julia> sizeof(ComplexF64)
752
16
753

754
julia> sizeof(1.0)
755
8
756

757
julia> sizeof(collect(1.0:10.0))
758
80
759

760
julia> struct StructWithPadding
761
           x::Int64
762
           flag::Bool
763
       end
764

765
julia> sizeof(StructWithPadding) # not the sum of `sizeof` of fields due to padding
766
16
767

768
julia> sizeof(Int64) + sizeof(Bool) # different from above
769
9
770
```
771

772
If `DataType` `T` does not have a specific size, an error is thrown.
773

774
```jldoctest
775
julia> sizeof(AbstractArray)
776
ERROR: Abstract type AbstractArray does not have a definite size.
777
Stacktrace:
778
[...]
779
```
780
"""
781
sizeof(x) = Core.sizeof(x)
676✔
782

783
"""
784
    ifelse(condition::Bool, x, y)
785

786
Return `x` if `condition` is `true`, otherwise return `y`. This differs from `?` or `if` in
787
that it is an ordinary function, so all the arguments are evaluated first. In some cases,
788
using `ifelse` instead of an `if` statement can eliminate the branch in generated code and
789
provide higher performance in tight loops.
790

791
# Examples
792
```jldoctest
793
julia> ifelse(1 > 2, 1, 2)
794
2
795
```
796
"""
797
ifelse(condition::Bool, x, y) = Core.ifelse(condition, x, y)
14,660,619✔
798

799
"""
800
    esc(e)
801

802
Only valid in the context of an [`Expr`](@ref) returned from a macro. Prevents the macro hygiene
803
pass from turning embedded variables into gensym variables. See the [Macros](@ref man-macros)
804
section of the Metaprogramming chapter of the manual for more details and examples.
805
"""
806
esc(@nospecialize(e)) = Expr(:escape, e)
854✔
807

808
"""
809
    @boundscheck(blk)
810

811
Annotates the expression `blk` as a bounds checking block, allowing it to be elided by [`@inbounds`](@ref).
812

813
!!! note
814
    The function in which `@boundscheck` is written must be inlined into
815
    its caller in order for `@inbounds` to have effect.
816

817
# Examples
818
```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*"
819
julia> @inline function g(A, i)
820
           @boundscheck checkbounds(A, i)
821
           return "accessing (\$A)[\$i]"
822
       end;
823

824
julia> f1() = return g(1:2, -1);
825

826
julia> f2() = @inbounds return g(1:2, -1);
827

828
julia> f1()
829
ERROR: BoundsError: attempt to access 2-element UnitRange{Int64} at index [-1]
830
Stacktrace:
831
 [1] throw_boundserror(::UnitRange{Int64}, ::Tuple{Int64}) at ./abstractarray.jl:455
832
 [2] checkbounds at ./abstractarray.jl:420 [inlined]
833
 [3] g at ./none:2 [inlined]
834
 [4] f1() at ./none:1
835
 [5] top-level scope
836

837
julia> f2()
838
"accessing (1:2)[-1]"
839
```
840

841
!!! warning
842

843
    The `@boundscheck` annotation allows you, as a library writer, to opt-in to
844
    allowing *other code* to remove your bounds checks with [`@inbounds`](@ref).
845
    As noted there, the caller must verify—using information they can access—that
846
    their accesses are valid before using `@inbounds`. For indexing into your
847
    [`AbstractArray`](@ref) subclasses, for example, this involves checking the
848
    indices against its [`axes`](@ref). Therefore, `@boundscheck` annotations
849
    should only be added to a [`getindex`](@ref) or [`setindex!`](@ref)
850
    implementation after you are certain its behavior is correct.
851
"""
852
macro boundscheck(blk)
853
    return Expr(:if, Expr(:boundscheck), esc(blk))
854
end
855

856
"""
857
    @inbounds(blk)
858

859
Eliminates array bounds checking within expressions.
860

861
In the example below the in-range check for referencing
862
element `i` of array `A` is skipped to improve performance.
863

864
```julia
865
function sum(A::AbstractArray)
866
    r = zero(eltype(A))
867
    for i in eachindex(A)
868
        @inbounds r += A[i]
869
    end
870
    return r
871
end
872
```
873

874
!!! warning
875

876
    Using `@inbounds` may return incorrect results/crashes/corruption
877
    for out-of-bounds indices. The user is responsible for checking it manually.
878
    Only use `@inbounds` when you are certain that all accesses are in bounds (as
879
    undefined behavior, e.g. crashes, might occur if this assertion is violated). For
880
    example, using `1:length(A)` instead of `eachindex(A)` in a function like
881
    the one above is _not_ safely inbounds because the first index of `A` may not
882
    be `1` for all user defined types that subtype `AbstractArray`.
883
"""
884
macro inbounds(blk)
885
    return Expr(:block,
886
        Expr(:inbounds, true),
887
        Expr(:local, Expr(:(=), :val, esc(blk))),
888
        Expr(:inbounds, :pop),
889
        :val)
890
end
891

892
"""
893
    @label name
894

895
Labels a statement with the symbolic label `name`. The label marks the end-point
896
of an unconditional jump with [`@goto name`](@ref).
897
"""
898
macro label(name::Symbol)
899
    return esc(Expr(:symboliclabel, name))
900
end
901

902
"""
903
    @goto name
904

905
`@goto name` unconditionally jumps to the statement at the location [`@label name`](@ref).
906

907
`@label` and `@goto` cannot create jumps to different top-level statements. Attempts cause an
908
error. To still use `@goto`, enclose the `@label` and `@goto` in a block.
909
"""
910
macro goto(name::Symbol)
911
    return esc(Expr(:symbolicgoto, name))
912
end
913

914
# linear indexing
915
function getindex(A::Array, i::Int)
42,033✔
916
    @_noub_if_noinbounds_meta
117,572✔
917
    @boundscheck ult_int(bitcast(UInt, sub_int(i, 1)), bitcast(UInt, length(A))) || throw_boundserror(A, (i,))
127,056,609✔
918
    memoryrefget(memoryrefnew(getfield(A, :ref), i, false), :not_atomic, false)
127,001,362✔
919
end
920
# simple Array{Any} operations needed for bootstrap
921
function setindex!(A::Array{Any}, @nospecialize(x), i::Int)
922
    @_noub_if_noinbounds_meta
2✔
923
    @boundscheck ult_int(bitcast(UInt, sub_int(i, 1)), bitcast(UInt, length(A))) || throw_boundserror(A, (i,))
4,527,358✔
924
    memoryrefset!(memoryrefnew(getfield(A, :ref), i, false), x, :not_atomic, false)
4,527,380✔
925
    return A
4,527,382✔
926
end
927
setindex!(A::Memory{Any}, @nospecialize(x), i::Int) = (memoryrefset!(memoryrefnew(memoryrefnew(A), i, @_boundscheck), x, :not_atomic, @_boundscheck); A)
6,489✔
928
setindex!(A::MemoryRef{T}, x) where {T} = (memoryrefset!(A, convert(T, x), :not_atomic, @_boundscheck); A)
×
929
setindex!(A::MemoryRef{Any}, @nospecialize(x)) = (memoryrefset!(A, x, :not_atomic, @_boundscheck); A)
×
930

931
# SimpleVector
932

933
getindex(v::SimpleVector, i::Int) = (@_foldable_meta; Core._svec_ref(v, i))
58,388✔
934
function length(v::SimpleVector)
935
    @_total_meta
×
936
    t = @_gc_preserve_begin v
98,080✔
937
    len = unsafe_load(Ptr{Int}(pointer_from_objref(v)))
98,080✔
938
    @_gc_preserve_end t
98,080✔
939
    return len
×
940
end
941
firstindex(v::SimpleVector) = 1
×
942
lastindex(v::SimpleVector) = length(v)
44✔
943
iterate(v::SimpleVector, i=1) = (length(v) < i ? nothing : (v[i], i + 1))
91,984✔
944
eltype(::Type{SimpleVector}) = Any
×
945
keys(v::SimpleVector) = OneTo(length(v))
×
946
isempty(v::SimpleVector) = (length(v) == 0)
10,940✔
947
axes(v::SimpleVector) = (OneTo(length(v)),)
×
948
axes(v::SimpleVector, d::Integer) = d <= 1 ? axes(v)[d] : OneTo(1)
×
949

950
function ==(v1::SimpleVector, v2::SimpleVector)
673✔
951
    length(v1)==length(v2) || return false
720✔
952
    for i = 1:length(v1)
626✔
953
        v1[i] == v2[i] || return false
2,183✔
954
    end
3,682✔
955
    return true
558✔
956
end
957

958
map(f, v::SimpleVector) = Any[ f(v[i]) for i = 1:length(v) ]
×
959

UNCOV
960
getindex(v::SimpleVector, I::AbstractArray) = Core.svec(Any[ v[i] for i in I ]...)
×
961

962
unsafe_convert(::Type{Ptr{Any}}, sv::SimpleVector) = convert(Ptr{Any},pointer_from_objref(sv)) + sizeof(Ptr)
3,395✔
963

964
"""
965
    isassigned(array, i) -> Bool
966

967
Test whether the given array has a value associated with index `i`. Return `false`
968
if the index is out of bounds, or has an undefined reference.
969

970
# Examples
971
```jldoctest
972
julia> isassigned(rand(3, 3), 5)
973
true
974

975
julia> isassigned(rand(3, 3), 3 * 3 + 1)
976
false
977

978
julia> mutable struct Foo end
979

980
julia> v = similar(rand(3), Foo)
981
3-element Vector{Foo}:
982
 #undef
983
 #undef
984
 #undef
985

986
julia> isassigned(v, 1)
987
false
988
```
989
"""
990
function isassigned end
991

992
function isassigned(v::SimpleVector, i::Int)
×
993
    @boundscheck 1 <= i <= length(v) || return false
×
994
    return true
×
995
end
996

997

998
"""
999
    Colon()
1000

1001
Colons (:) are used to signify indexing entire objects or dimensions at once.
1002

1003
Very few operations are defined on Colons directly; instead they are converted
1004
by [`to_indices`](@ref) to an internal vector type (`Base.Slice`) to represent the
1005
collection of indices they span before being used.
1006

1007
The singleton instance of `Colon` is also a function used to construct ranges;
1008
see [`:`](@ref).
1009
"""
1010
struct Colon <: Function
1011
end
1012
const (:) = Colon()
1013

1014

1015
"""
1016
    Val(c)
1017

1018
Return `Val{c}()`, which contains no run-time data. Types like this can be used to
1019
pass the information between functions through the value `c`, which must be an `isbits`
1020
value or a `Symbol`. The intent of this construct is to be able to dispatch on constants
1021
directly (at compile time) without having to test the value of the constant at run time.
1022

1023
# Examples
1024
```jldoctest
1025
julia> f(::Val{true}) = "Good"
1026
f (generic function with 1 method)
1027

1028
julia> f(::Val{false}) = "Bad"
1029
f (generic function with 2 methods)
1030

1031
julia> f(Val(true))
1032
"Good"
1033
```
1034
"""
1035
struct Val{x}
1036
end
1✔
1037

1038
Val(x) = Val{x}()
1✔
1039

1040
"""
1041
    invokelatest(f, args...; kwargs...)
1042

1043
Calls `f(args...; kwargs...)`, but guarantees that the most recent method of `f`
1044
will be executed.   This is useful in specialized circumstances,
1045
e.g. long-running event loops or callback functions that may
1046
call obsolete versions of a function `f`.
1047
(The drawback is that `invokelatest` is somewhat slower than calling
1048
`f` directly, and the type of the result cannot be inferred by the compiler.)
1049

1050
!!! compat "Julia 1.9"
1051
    Prior to Julia 1.9, this function was not exported, and was called as `Base.invokelatest`.
1052
"""
1053
function invokelatest(@nospecialize(f), @nospecialize args...; kwargs...)
516✔
1054
    @inline
55✔
1055
    kwargs = merge(NamedTuple(), kwargs)
56✔
1056
    if isempty(kwargs)
55✔
1057
        return Core._call_latest(f, args...)
513✔
1058
    end
1059
    return Core._call_latest(Core.kwcall, kwargs, f, args...)
3✔
1060
end
1061

1062
"""
1063
    invoke_in_world(world, f, args...; kwargs...)
1064

1065
Call `f(args...; kwargs...)` in a fixed world age, `world`.
1066

1067
This is useful for infrastructure running in the user's Julia session which is
1068
not part of the user's program. For example, things related to the REPL, editor
1069
support libraries, etc. In these cases it can be useful to prevent unwanted
1070
method invalidation and recompilation latency, and to prevent the user from
1071
breaking supporting infrastructure by mistake.
1072

1073
The current world age can be queried using [`Base.get_world_counter()`](@ref)
1074
and stored for later use within the lifetime of the current Julia session, or
1075
when serializing and reloading the system image.
1076

1077
Technically, `invoke_in_world` will prevent any function called by `f` from
1078
being extended by the user during their Julia session. That is, generic
1079
function method tables seen by `f` (and any functions it calls) will be frozen
1080
as they existed at the given `world` age. In a sense, this is like the opposite
1081
of [`invokelatest`](@ref).
1082

1083
!!! note
1084
    It is not valid to store world ages obtained in precompilation for later use.
1085
    This is because precompilation generates a "parallel universe" where the
1086
    world age refers to system state unrelated to the main Julia session.
1087
"""
1088
function invoke_in_world(world::UInt, @nospecialize(f), @nospecialize args...; kwargs...)
21✔
1089
    @inline
2✔
1090
    kwargs = Base.merge(NamedTuple(), kwargs)
2✔
1091
    if isempty(kwargs)
2✔
1092
        return Core._call_in_world(world, f, args...)
20✔
1093
    end
1094
    return Core._call_in_world(world, Core.kwcall, kwargs, f, args...)
×
1095
end
1096

1097
"""
1098
    inferencebarrier(x)
1099

1100
A shorthand for `compilerbarrier(:type, x)` causes the type of this statement to be inferred as `Any`.
1101
See [`Base.compilerbarrier`](@ref) for more info.
1102
"""
1103
inferencebarrier(@nospecialize(x)) = compilerbarrier(:type, x)
32✔
1104

1105
"""
1106
    isempty(collection) -> Bool
1107

1108
Determine whether a collection is empty (has no elements).
1109

1110
!!! warning
1111

1112
    `isempty(itr)` may consume the next element of a stateful iterator `itr`
1113
    unless an appropriate [`Base.isdone(itr)`](@ref) method is defined.
1114
    Stateful iterators *should* implement `isdone`, but you may want to avoid
1115
    using `isempty` when writing generic code which should support any iterator
1116
    type.
1117

1118
# Examples
1119
```jldoctest
1120
julia> isempty([])
1121
true
1122

1123
julia> isempty([1 2 3])
1124
false
1125
```
1126
"""
1127
function isempty(itr)
×
1128
    d = isdone(itr)
×
1129
    d !== missing && return d
×
1130
    iterate(itr) === nothing
1✔
1131
end
1132

1133
"""
1134
    values(iterator)
1135

1136
For an iterator or collection that has keys and values, return an iterator
1137
over the values.
1138
This function simply returns its argument by default, since the elements
1139
of a general iterator are normally considered its "values".
1140

1141
# Examples
1142
```jldoctest
1143
julia> d = Dict("a"=>1, "b"=>2);
1144

1145
julia> values(d)
1146
ValueIterator for a Dict{String, Int64} with 2 entries. Values:
1147
  2
1148
  1
1149

1150
julia> values([2])
1151
1-element Vector{Int64}:
1152
 2
1153
```
1154
"""
1155
values(itr) = itr
×
1156

1157
"""
1158
    Missing
1159

1160
A type with no fields whose singleton instance [`missing`](@ref) is used
1161
to represent missing values.
1162

1163
See also: [`skipmissing`](@ref), [`nonmissingtype`](@ref), [`Nothing`](@ref).
1164
"""
1165
struct Missing end
1166

1167
"""
1168
    missing
1169

1170
The singleton instance of type [`Missing`](@ref) representing a missing value.
1171

1172
See also: [`NaN`](@ref), [`skipmissing`](@ref), [`nonmissingtype`](@ref).
1173
"""
1174
const missing = Missing()
1175

1176
"""
1177
    ismissing(x)
1178

1179
Indicate whether `x` is [`missing`](@ref).
1180

1181
See also: [`skipmissing`](@ref), [`isnothing`](@ref), [`isnan`](@ref).
1182
"""
1183
ismissing(x) = x === missing
107✔
1184

1185
function popfirst! end
1186

1187
"""
1188
    peek(stream[, T=UInt8])
1189

1190
Read and return a value of type `T` from a stream without advancing the current position
1191
in the stream.   See also [`startswith(stream, char_or_string)`](@ref).
1192

1193
# Examples
1194

1195
```jldoctest
1196
julia> b = IOBuffer("julia");
1197

1198
julia> peek(b)
1199
0x6a
1200

1201
julia> position(b)
1202
0
1203

1204
julia> peek(b, Char)
1205
'j': ASCII/Unicode U+006A (category Ll: Letter, lowercase)
1206
```
1207

1208
!!! compat "Julia 1.5"
1209
    The method which accepts a type requires Julia 1.5 or later.
1210
"""
1211
function peek end
1212

1213
"""
1214
    @__LINE__ -> Int
1215

1216
Expand to the line number of the location of the macrocall.
1217
Return `0` if the line number could not be determined.
1218
"""
1219
macro __LINE__()
1220
    return __source__.line
1221
end
1222

1223
# Iteration
1224
"""
1225
    isdone(itr, [state]) -> Union{Bool, Missing}
1226

1227
This function provides a fast-path hint for iterator completion.
1228
This is useful for stateful iterators that want to avoid having elements
1229
consumed if they are not going to be exposed to the user (e.g. when checking
1230
for done-ness in `isempty` or `zip`).
1231

1232
Stateful iterators that want to opt into this feature should define an `isdone`
1233
method that returns true/false depending on whether the iterator is done or
1234
not. Stateless iterators need not implement this function.
1235

1236
If the result is `missing`, callers may go ahead and compute
1237
`iterate(x, state) === nothing` to compute a definite answer.
1238

1239
See also [`iterate`](@ref), [`isempty`](@ref)
1240
"""
1241
isdone(itr, state...) = missing
32✔
1242

1243
"""
1244
    iterate(iter [, state]) -> Union{Nothing, Tuple{Any, Any}}
1245

1246
Advance the iterator to obtain the next element. If no elements
1247
remain, `nothing` should be returned. Otherwise, a 2-tuple of the
1248
next element and the new iteration state should be returned.
1249
"""
1250
function iterate end
1251

1252
"""
1253
    isiterable(T) -> Bool
1254

1255
Test if type `T` is an iterable collection type or not,
1256
that is whether it has an `iterate` method or not.
1257
"""
1258
function isiterable(T)::Bool
1259
    return hasmethod(iterate, Tuple{T})
3✔
1260
end
1261

1262
"""
1263
    @world(sym, world)
1264

1265
Resolve the binding `sym` in world `world`. See [`invoke_in_world`](@ref) for running
1266
arbitrary code in fixed worlds. `world` may be `UnitRange`, in which case the macro
1267
will error unless the binding is valid and has the same value across the entire world
1268
range.
1269

1270
As a special case, the world `∞` always refers to the latest world, even if that world
1271
is newer than the world currently running.
1272

1273
The `@world` macro is primarily used in the printing of bindings that are no longer
1274
available in the current world.
1275

1276
## Example
1277
```julia-repl
1278
julia> struct Foo; a::Int; end
1279
Foo
1280

1281
julia> fold = Foo(1)
1282

1283
julia> Int(Base.get_world_counter())
1284
26866
1285

1286
julia> struct Foo; a::Int; b::Int end
1287
Foo
1288

1289
julia> fold
1290
@world(Foo, 26866)(1)
1291
```
1292

1293
!!! compat "Julia 1.12"
1294
    This functionality requires at least Julia 1.12.
1295
"""
1296
macro world(sym, world)
1297
    if world == :∞
1298
        world = Expr(:call, get_world_counter)
1299
    end
1300
    if isa(sym, Symbol)
1301
        return :($(_resolve_in_world)($(esc(world)), $(QuoteNode(GlobalRef(__module__, sym)))))
1302
    elseif isa(sym, GlobalRef)
1303
        return :($(_resolve_in_world)($(esc(world)), $(QuoteNode(sym))))
1304
    elseif isa(sym, Expr) && sym.head === :(.) &&
1305
            length(sym.args) == 2 && isa(sym.args[2], QuoteNode) && isa(sym.args[2].value, Symbol)
1306
        return :($(_resolve_in_world)($(esc(world)), $(GlobalRef)($(esc(sym.args[1])), $(sym.args[2]))))
1307
    else
1308
        error("`@world` requires a symbol or GlobalRef")
1309
    end
1310
end
1311

1312
_resolve_in_world(world::Integer, gr::GlobalRef) =
1✔
1313
    invoke_in_world(UInt(world), Core.getglobal, gr.mod, gr.name)
1314

1315
# Special constprop heuristics for various binary opes
1316
typename(typeof(function + end)).constprop_heuristic  = Core.SAMETYPE_HEURISTIC
1317
typename(typeof(function - end)).constprop_heuristic  = Core.SAMETYPE_HEURISTIC
1318
typename(typeof(function * end)).constprop_heuristic  = Core.SAMETYPE_HEURISTIC
1319
typename(typeof(function == end)).constprop_heuristic = Core.SAMETYPE_HEURISTIC
1320
typename(typeof(function != end)).constprop_heuristic = Core.SAMETYPE_HEURISTIC
1321
typename(typeof(function <= end)).constprop_heuristic = Core.SAMETYPE_HEURISTIC
1322
typename(typeof(function >= end)).constprop_heuristic = Core.SAMETYPE_HEURISTIC
1323
typename(typeof(function < end)).constprop_heuristic  = Core.SAMETYPE_HEURISTIC
1324
typename(typeof(function > end)).constprop_heuristic  = Core.SAMETYPE_HEURISTIC
1325
typename(typeof(function << end)).constprop_heuristic = Core.SAMETYPE_HEURISTIC
1326
typename(typeof(function >> end)).constprop_heuristic = Core.SAMETYPE_HEURISTIC
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc