diff --git a/base/array.jl b/base/array.jl index 300512537ad8d..7d3f231f691b8 100644 --- a/base/array.jl +++ b/base/array.jl @@ -2,6 +2,17 @@ ## array.jl: Dense arrays +""" + DimensionMismatch([msg]) + +The objects called do not have matching dimensionality. Optional argument `msg` is a +descriptive error string. +""" +mutable struct DimensionMismatch <: Exception + msg::AbstractString +end +DimensionMismatch() = DimensionMismatch("") + ## Type aliases for convenience ## """ AbstractVector{T} diff --git a/base/associative.jl b/base/associative.jl index 1d4f48bda6aa5..ba9c7e3ebc10b 100644 --- a/base/associative.jl +++ b/base/associative.jl @@ -2,6 +2,16 @@ # generic operations on associative collections +""" + KeyError(key) + +An indexing operation into an `Associative` (`Dict`) or `Set` like object tried to access or +delete a non-existent element. +""" +mutable struct KeyError <: Exception + key +end + const secret_table_token = :__c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__ haskey(d::Associative, k) = in(k,keys(d)) diff --git a/base/base.jl b/base/base.jl deleted file mode 100644 index 1130730737476..0000000000000 --- a/base/base.jl +++ /dev/null @@ -1,153 +0,0 @@ -# This file is a part of Julia. License is MIT: https://julialang.org/license - -""" - SystemError(prefix::AbstractString, [errno::Int32]) - -A system call failed with an error code (in the `errno` global variable). -""" -mutable struct SystemError <: Exception - prefix::AbstractString - errnum::Int32 - extrainfo - SystemError(p::AbstractString, e::Integer, extrainfo) = new(p, e, extrainfo) - SystemError(p::AbstractString, e::Integer) = new(p, e, nothing) - SystemError(p::AbstractString) = new(p, Libc.errno()) -end - -""" - ParseError(msg) - -The expression passed to the `parse` function could not be interpreted as a valid Julia -expression. -""" -mutable struct ParseError <: Exception - msg::AbstractString -end - -""" - ArgumentError(msg) - -The parameters to a function call do not match a valid signature. Argument `msg` is a -descriptive error string. -""" -mutable struct ArgumentError <: Exception - msg::AbstractString -end - -""" - KeyError(key) - -An indexing operation into an `Associative` (`Dict`) or `Set` like object tried to access or -delete a non-existent element. -""" -mutable struct KeyError <: Exception - key -end - -""" - MethodError(f, args) - -A method with the required type signature does not exist in the given generic function. -Alternatively, there is no unique most-specific method. -""" -mutable struct MethodError <: Exception - f - args - world::UInt - MethodError(@nospecialize(f), @nospecialize(args), world::UInt) = new(f, args, world) -end -MethodError(@nospecialize(f), @nospecialize(args)) = MethodError(f, args, typemax(UInt)) - -""" - EOFError() - -No more data was available to read from a file or stream. -""" -mutable struct EOFError <: Exception end - -""" - DimensionMismatch([msg]) - -The objects called do not have matching dimensionality. Optional argument `msg` is a -descriptive error string. -""" -mutable struct DimensionMismatch <: Exception - msg::AbstractString -end -DimensionMismatch() = DimensionMismatch("") - -""" - AssertionError([msg]) - -The asserted condition did not evaluate to `true`. -Optional argument `msg` is a descriptive error string. -""" -mutable struct AssertionError <: Exception - msg::AbstractString - AssertionError() = new("") - AssertionError(msg) = new(msg) -end - -#Generic wrapping of arbitrary exceptions -#Subtypes should put the exception in an 'error' field -abstract type WrappedException <: Exception end - -""" - LoadError(file::AbstractString, line::Int, error) - -An error occurred while `include`ing, `require`ing, or `using` a file. The error specifics -should be available in the `.error` field. -""" -mutable struct LoadError <: WrappedException - file::AbstractString - line::Int - error -end - -""" - InitError(mod::Symbol, error) - -An error occurred when running a module's `__init__` function. The actual error thrown is -available in the `.error` field. -""" -mutable struct InitError <: WrappedException - mod::Symbol - error -end - -ccall(:jl_get_system_hooks, Void, ()) - - -==(w::WeakRef, v::WeakRef) = isequal(w.value, v.value) -==(w::WeakRef, v) = isequal(w.value, v) -==(w, v::WeakRef) = isequal(w, v.value) - -function finalizer(@nospecialize(o), @nospecialize(f)) - if isimmutable(o) - error("objects of type ", typeof(o), " cannot be finalized") - end - ccall(:jl_gc_add_finalizer_th, Void, (Ptr{Void}, Any, Any), - Core.getptls(), o, f) -end -function finalizer(o::T, f::Ptr{Void}) where T - @_inline_meta - if isimmutable(T) - error("objects of type ", T, " cannot be finalized") - end - ccall(:jl_gc_add_ptr_finalizer, Void, (Ptr{Void}, Any, Ptr{Void}), - Core.getptls(), o, f) -end - -finalize(@nospecialize(o)) = ccall(:jl_finalize_th, Void, (Ptr{Void}, Any,), - Core.getptls(), o) - -gc(full::Bool=true) = ccall(:jl_gc_collect, Void, (Int32,), full) -gc_enable(on::Bool) = ccall(:jl_gc_enable, Int32, (Int32,), on) != 0 - -struct Nullable{T} - hasvalue::Bool - value::T - - Nullable{T}() where {T} = new(false) - Nullable{T}(value::T, hasvalue::Bool=true) where {T} = new(hasvalue, value) -end diff --git a/base/boot.jl b/base/boot.jl index db7cdcbd1159e..be8f10aadd8dd 100644 --- a/base/boot.jl +++ b/base/boot.jl @@ -135,7 +135,7 @@ export ErrorException, BoundsError, DivideError, DomainError, Exception, InterruptException, InexactError, OutOfMemoryError, ReadOnlyMemoryError, OverflowError, StackOverflowError, SegmentationFault, UndefRefError, UndefVarError, - TypeError, + TypeError, ArgumentError, MethodError, AssertionError, LoadError, InitError, # AST representation Expr, GotoNode, LabelNode, LineNumberNode, QuoteNode, GlobalRef, NewvarNode, SSAValue, Slot, SlotNumber, TypedSlot, @@ -244,6 +244,40 @@ struct OverflowError <: Exception msg end +mutable struct ArgumentError <: Exception + msg::AbstractString +end + +mutable struct MethodError <: Exception + f + args + world::UInt + MethodError(@nospecialize(f), @nospecialize(args), world::UInt) = new(f, args, world) +end +const typemax_UInt = ccall(:jl_typemax_uint, Any, (Any,), UInt) +MethodError(@nospecialize(f), @nospecialize(args)) = MethodError(f, args, typemax_UInt) + +mutable struct AssertionError <: Exception + msg::AbstractString + AssertionError() = new("") + AssertionError(msg) = new(msg) +end + +#Generic wrapping of arbitrary exceptions +#Subtypes should put the exception in an 'error' field +abstract type WrappedException <: Exception end + +mutable struct LoadError <: WrappedException + file::AbstractString + line::Int + error +end + +mutable struct InitError <: WrappedException + mod::Symbol + error +end + abstract type DirectIndexString <: AbstractString end String(s::String) = s # no constructor yet diff --git a/base/docs/basedocs.jl b/base/docs/basedocs.jl index 40680e42c4972..1a2691f324782 100644 --- a/base/docs/basedocs.jl +++ b/base/docs/basedocs.jl @@ -682,6 +682,20 @@ A variable referring to the last computed value, automatically set at the intera """ kw"ans" +""" + DevNull + +Used in a stream redirect to discard all data written to it. Essentially equivalent to +/dev/null on Unix or NUL on Windows. Usage: + +```julia +run(pipeline(`cat test.txt`, DevNull)) +``` +""" +DevNull + +# doc strings for code in boot.jl and built-ins + """ nothing @@ -697,18 +711,6 @@ The singleton type containing only the value `Union{}`. """ Core.TypeofBottom -""" - DevNull - -Used in a stream redirect to discard all data written to it. Essentially equivalent to -/dev/null on Unix or NUL on Windows. Usage: - -```julia -run(pipeline(`cat test.txt`, DevNull)) -``` -""" -DevNull - """ Function @@ -727,4 +729,361 @@ true """ Function +""" + ReadOnlyMemoryError() + +An operation tried to write to memory that is read-only. +""" +ReadOnlyMemoryError + +""" + ErrorException(msg) + +Generic error type. The error message, in the `.msg` field, may provide more specific details. +""" +ErrorException + +""" + UndefRefError() + +The item or field is not defined for the given object. +""" +UndefRefError + +""" + Float64(x [, mode::RoundingMode]) + +Create a Float64 from `x`. If `x` is not exactly representable then `mode` determines how +`x` is rounded. + +# Examples +```jldoctest +julia> Float64(pi, RoundDown) +3.141592653589793 + +julia> Float64(pi, RoundUp) +3.1415926535897936 +``` + +See [`RoundingMode`](@ref) for available rounding modes. +""" +Float64(x) + +""" + OutOfMemoryError() + +An operation allocated too much memory for either the system or the garbage collector to +handle properly. +""" +OutOfMemoryError + +""" + BoundsError([a],[i]) + +An indexing operation into an array, `a`, tried to access an out-of-bounds element at index `i`. + +# Examples +```jldoctest +julia> A = ones(7); + +julia> A[8] +ERROR: BoundsError: attempt to access 7-element Array{Float64,1} at index [8] +Stacktrace: + [1] getindex(::Array{Float64,1}, ::Int64) at ./array.jl:586 + +julia> B = ones(2, 3); + +julia> B[2, 4] +ERROR: BoundsError: attempt to access 2×3 Array{Float64,2} at index [2, 4] +Stacktrace: + [1] getindex(::Array{Float64,2}, ::Int64, ::Int64) at ./array.jl:587 + +julia> B[9] +ERROR: BoundsError: attempt to access 2×3 Array{Float64,2} at index [9] +Stacktrace: + [1] getindex(::Array{Float64,2}, ::Int64) at ./array.jl:586 +``` +""" +BoundsError + +""" + InexactError(name::Symbol, T, val) + +Cannot exactly convert `val` to type `T` in a method of function `name`. + +# Examples +```jldoctest +julia> convert(Float64, 1+2im) +ERROR: InexactError: convert(Float64, 1 + 2im) +Stacktrace: + [1] convert(::Type{Float64}, ::Complex{Int64}) at ./complex.jl:37 +``` +""" +InexactError + +""" + DomainError(val) + DomainError(val, msg) + +The argument `val` to a function or constructor is outside the valid domain. + +# Examples +```jldoctest +julia> sqrt(-1) +ERROR: DomainError with -1.0: +sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)). +Stacktrace: + [1] throw_complex_domainerror(::Symbol, ::Float64) at ./math.jl:31 + [2] sqrt at ./math.jl:462 [inlined] + [3] sqrt(::Int64) at ./math.jl:472 +``` +""" +DomainError + +""" + Task(func) + +Create a `Task` (i.e. coroutine) to execute the given function (which must be +callable with no arguments). The task exits when this function returns. + +# Examples +```jldoctest +julia> a() = det(rand(1000, 1000)); + +julia> b = Task(a); +``` + +In this example, `b` is a runnable `Task` that hasn't started yet. +""" +Task + +""" + StackOverflowError() + +The function call grew beyond the size of the call stack. This usually happens when a call +recurses infinitely. +""" +StackOverflowError + +""" + nfields(x) -> Int + +Get the number of fields in the given object. +""" +nfields + +""" + UndefVarError(var::Symbol) + +A symbol in the current scope is not defined. +""" +UndefVarError + +""" + OverflowError(msg) + +The result of an expression is too large for the specified type and will cause a wraparound. +""" +OverflowError + +""" + TypeError(func::Symbol, context::AbstractString, expected::Type, got) + +A type assertion failure, or calling an intrinsic function with an incorrect argument type. +""" +TypeError + +""" + InterruptException() + +The process was stopped by a terminal interrupt (CTRL+C). +""" +InterruptException + +""" + applicable(f, args...) -> Bool + +Determine whether the given generic function has a method applicable to the given arguments. + +# Examples +```jldoctest +julia> function f(x, y) + x + y + end; + +julia> applicable(f, 1) +false + +julia> applicable(f, 1, 2) +true +``` +""" +applicable + +""" + invoke(f, argtypes::Type, args...; kwargs...) + +Invoke a method for the given generic function `f` matching the specified types `argtypes` on the +specified arguments `args` and passing the keyword arguments `kwargs`. The arguments `args` must +conform with the specified types in `argtypes`, i.e. conversion is not automatically performed. +This method allows invoking a method other than the most specific matching method, which is useful +when the behavior of a more general definition is explicitly needed (often as part of the +implementation of a more specific method of the same function). + +# Examples +```jldoctest +julia> f(x::Real) = x^2; + +julia> f(x::Integer) = 1 + invoke(f, Tuple{Real}, x); + +julia> f(2) +5 +``` +""" +invoke + +""" + isa(x, type) -> Bool + +Determine whether `x` is of the given `type`. Can also be used as an infix operator, e.g. +`x isa type`. +""" +isa + +""" + DivideError() + +Integer division was attempted with a denominator value of 0. + +# Examples +```jldoctest +julia> 2/0 +Inf + +julia> div(2, 0) +ERROR: DivideError: integer division error +Stacktrace: + [1] div(::Int64, ::Int64) at ./int.jl:183 +``` +""" +DivideError + +""" + Number + +Abstract supertype for all number types. +""" +Number + +""" + Real <: Number + +Abstract supertype for all real numbers. +""" +Real + +""" + AbstractFloat <: Real + +Abstract supertype for all floating point numbers. +""" +AbstractFloat + +""" + Integer <: Real + +Abstract supertype for all integers. +""" +Integer + +""" + Signed <: Integer + +Abstract supertype for all signed integers. +""" +Signed + +""" + Unsigned <: Integer + +Abstract supertype for all unsigned integers. +""" +Unsigned + +""" + Bool <: Integer + +Boolean type. +""" +Bool + +for bit in (16, 32, 64) + @eval begin + """ + Float$($bit) <: AbstractFloat + + $($bit)-bit floating point number type. + """ + $(Symbol("Float", bit)) + end +end + +for bit in (8, 16, 32, 64, 128) + @eval begin + """ + Int$($bit) <: Signed + + $($bit)-bit signed integer type. + """ + $(Symbol("Int", bit)) + + """ + UInt$($bit) <: Unsigned + + $($bit)-bit unsigned integer type. + """ + $(Symbol("UInt", bit)) + end +end + +""" + ArgumentError(msg) + +The parameters to a function call do not match a valid signature. Argument `msg` is a +descriptive error string. +""" +ArgumentError + +""" + MethodError(f, args) + +A method with the required type signature does not exist in the given generic function. +Alternatively, there is no unique most-specific method. +""" +MethodError + +""" + AssertionError([msg]) + +The asserted condition did not evaluate to `true`. +Optional argument `msg` is a descriptive error string. +""" +AssertionError + +""" + LoadError(file::AbstractString, line::Int, error) + +An error occurred while `include`ing, `require`ing, or `using` a file. The error specifics +should be available in the `.error` field. +""" +LoadError + +""" + InitError(mod::Symbol, error) + +An error occurred when running a module's `__init__` function. The actual error thrown is +available in the `.error` field. +""" +InitError + end diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 616c7ca0dd268..bd96ddbae5b05 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -269,13 +269,6 @@ Stacktrace: """ sizeof(::Type) -""" - ReadOnlyMemoryError() - -An operation tried to write to memory that is read-only. -""" -ReadOnlyMemoryError - """ ceil([T,] x, [digits, [base]]) @@ -490,13 +483,6 @@ not representable. """ floor -""" - ErrorException(msg) - -Generic error type. The error message, in the `.msg` field, may provide more specific details. -""" -ErrorException - """ reverse(v [, start=1 [, stop=length(v) ]] ) @@ -546,13 +532,6 @@ In-place version of [`reverse`](@ref). """ reverse! -""" - UndefRefError() - -The item or field is not defined for the given object. -""" -UndefRefError - """ append!(collection, collection2) -> collection. @@ -675,25 +654,6 @@ julia> a """ select! -""" - Float64(x [, mode::RoundingMode]) - -Create a Float64 from `x`. If `x` is not exactly representable then `mode` determines how -`x` is rounded. - -# Examples -```jldoctest -julia> Float64(pi, RoundDown) -3.141592653589793 - -julia> Float64(pi, RoundUp) -3.1415926535897936 -``` - -See [`RoundingMode`](@ref) for available rounding modes. -""" -Float64(x) - """ union(s1,s2...) ∪(s1,s2...) @@ -865,14 +825,6 @@ Suggest that collection `s` reserve capacity for at least `n` elements. This can """ sizehint! -""" - OutOfMemoryError() - -An operation allocated too much memory for either the system or the garbage collector to -handle properly. -""" -OutOfMemoryError - """ finalize(x) @@ -880,57 +832,6 @@ Immediately run finalizers registered for object `x`. """ finalize -""" - BoundsError([a],[i]) - -An indexing operation into an array, `a`, tried to access an out-of-bounds element at index `i`. - -# Examples -```jldoctest -julia> A = ones(7); - -julia> A[8] -ERROR: BoundsError: attempt to access 7-element Array{Float64,1} at index [8] -Stacktrace: - [1] getindex(::Array{Float64,1}, ::Int64) at ./array.jl:586 - -julia> B = ones(2, 3); - -julia> B[2, 4] -ERROR: BoundsError: attempt to access 2×3 Array{Float64,2} at index [2, 4] -Stacktrace: - [1] getindex(::Array{Float64,2}, ::Int64, ::Int64) at ./array.jl:587 - -julia> B[9] -ERROR: BoundsError: attempt to access 2×3 Array{Float64,2} at index [9] -Stacktrace: - [1] getindex(::Array{Float64,2}, ::Int64) at ./array.jl:586 -``` -""" -BoundsError - -""" - invoke(f, argtypes::Type, args...; kwargs...) - -Invoke a method for the given generic function `f` matching the specified types `argtypes` on the -specified arguments `args` and passing the keyword arguments `kwargs`. The arguments `args` must -conform with the specified types in `argtypes`, i.e. conversion is not automatically performed. -This method allows invoking a method other than the most specific matching method, which is useful -when the behavior of a more general definition is explicitly needed (often as part of the -implementation of a more specific method of the same function). - -# Examples -```jldoctest -julia> f(x::Real) = x^2; - -julia> f(x::Integer) = 1 + invoke(f, Tuple{Real}, x); - -julia> f(2) -5 -``` -""" -invoke - """ parse(str, start; greedy=true, raise=true) @@ -1288,21 +1189,6 @@ false """ isempty -""" - InexactError(name::Symbol, T, val) - -Cannot exactly convert `val` to type `T` in a method of function `name`. - -# Examples -```jldoctest -julia> convert(Float64, 1+2im) -ERROR: InexactError: convert(Float64, 1 + 2im) -Stacktrace: - [1] convert(::Type{Float64}, ::Complex{Int64}) at ./complex.jl:37 -``` -""" -InexactError - """ typemax(T) @@ -1310,50 +1196,6 @@ The highest value representable by the given (real) numeric `DataType`. """ typemax -""" - DomainError(val) - DomainError(val, msg) - -The argument `val` to a function or constructor is outside the valid domain. - -# Examples -```jldoctest -julia> sqrt(-1) -ERROR: DomainError with -1.0: -sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)). -Stacktrace: - [1] throw_complex_domainerror(::Symbol, ::Float64) at ./math.jl:31 - [2] sqrt at ./math.jl:462 [inlined] - [3] sqrt(::Int64) at ./math.jl:472 -``` -""" -DomainError - -""" - Task(func) - -Create a `Task` (i.e. coroutine) to execute the given function (which must be -callable with no arguments). The task exits when this function returns. - -# Examples -```jldoctest -julia> a() = det(rand(1000, 1000)); - -julia> b = Task(a); -``` - -In this example, `b` is a runnable `Task` that hasn't started yet. -""" -Task - -""" - StackOverflowError() - -The function call grew beyond the size of the call stack. This usually happens when a call -recurses infinitely. -""" -StackOverflowError - """ ==(x, y) @@ -1378,13 +1220,6 @@ Seek a stream to its beginning. """ seekstart -""" - nfields(x) -> Int - -Get the number of fields in the given object. -""" -nfields - """ show(stream, mime, x) @@ -1441,13 +1276,6 @@ Equivalent to [`readdlm`](@ref) with `delim` set to comma, and type optionally d """ readcsv -""" - UndefVarError(var::Symbol) - -A symbol in the current scope is not defined. -""" -UndefVarError - """ gc() @@ -1499,13 +1327,6 @@ used only with extreme caution, as it can cause memory use to grow without bound """ gc_enable -""" - OverflowError(msg) - -The result of an expression is too large for the specified type and will cause a wraparound. -""" -OverflowError - """ object_id(x) @@ -1548,13 +1369,6 @@ unpredictable. """ finalizer -""" - TypeError(func::Symbol, context::AbstractString, expected::Type, got) - -A type assertion failure, or calling an intrinsic function with an incorrect argument type. -""" -TypeError - """ setfield!(value, name::Symbol, x) @@ -1668,13 +1482,6 @@ julia> length([1 2; 3 4]) """ length(collection) -""" - InterruptException() - -The process was stopped by a terminal interrupt (CTRL+C). -""" -InterruptException - """ issubnormal(f) -> Bool @@ -1760,14 +1567,6 @@ julia> start([4;2;3]) """ start -""" - isa(x, type) -> Bool - -Determine whether `x` is of the given `type`. Can also be used as an infix operator, e.g. -`x isa type`. -""" -isa - """ done(iter, state) -> Bool @@ -1858,26 +1657,6 @@ true """ convert -""" - applicable(f, args...) -> Bool - -Determine whether the given generic function has a method applicable to the given arguments. - -# Examples -```jldoctest -julia> function f(x, y) - x + y - end; - -julia> applicable(f, 1) -false - -julia> applicable(f, 1, 2) -true -``` -""" -applicable - """ fma(x, y, z) @@ -2045,102 +1824,6 @@ Seek a stream to its end. """ seekend -""" - DivideError() - -Integer division was attempted with a denominator value of 0. - -# Examples -```jldoctest -julia> 2/0 -Inf - -julia> div(2, 0) -ERROR: DivideError: integer division error -Stacktrace: - [1] div(::Int64, ::Int64) at ./int.jl:183 -``` -""" -DivideError - -""" - Number - -Abstract supertype for all number types. -""" -Number - -""" - Real <: Number - -Abstract supertype for all real numbers. -""" -Real - -""" - AbstractFloat <: Real - -Abstract supertype for all floating point numbers. -""" -AbstractFloat - -""" - Integer <: Real - -Abstract supertype for all integers. -""" -Integer - -""" - Signed <: Integer - -Abstract supertype for all signed integers. -""" -Signed - -""" - Unsigned <: Integer - -Abstract supertype for all unsigned integers. -""" -Unsigned - -""" - Bool <: Integer - -Boolean type. -""" -Bool - -for bit in (16, 32, 64) - @eval begin - """ - Float$($bit) <: AbstractFloat - - $($bit)-bit floating point number type. - """ - $(Symbol("Float", bit)) - end -end - -for bit in (8, 16, 32, 64, 128) - @eval begin - """ - Int$($bit) <: Signed - - $($bit)-bit signed integer type. - """ - $(Symbol("Int", bit)) - - """ - UInt$($bit) <: Unsigned - - $($bit)-bit unsigned integer type. - """ - $(Symbol("UInt", bit)) - end -end - """ Vector{T}(n) diff --git a/base/error.jl b/base/error.jl index 60f37eb6c1467..06508810e00b0 100644 --- a/base/error.jl +++ b/base/error.jl @@ -85,7 +85,7 @@ systemerror(p, b::Bool; extrainfo=nothing) = b ? throw(Main.Base.SystemError(str Throw an [`AssertionError`](@ref) if `cond` is `false`. Also available as the macro [`@assert`](@ref). """ -assert(x) = x ? nothing : throw(Main.Base.AssertionError()) +assert(x) = x ? nothing : throw(AssertionError()) """ @assert cond [text] @@ -114,7 +114,7 @@ macro assert(ex, msgs...) # string() might not be defined during bootstrap msg = :(Main.Base.string($(Expr(:quote,msg)))) end - return :($(esc(ex)) ? $(nothing) : throw(Main.Base.AssertionError($msg))) + return :($(esc(ex)) ? $(nothing) : throw(AssertionError($msg))) end struct ExponentialBackOff diff --git a/base/exports.jl b/base/exports.jl index eecd75f36d46e..8a086f914ca3a 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -151,22 +151,15 @@ export Cwstring, # Exceptions - ArgumentError, DimensionMismatch, CapturedException, CompositeException, EOFError, - ErrorException, InvalidStateException, KeyError, - LoadError, - InitError, - MethodError, NullException, ParseError, SystemError, - TypeError, - AssertionError, UnicodeError, # Global constants and variables diff --git a/base/gcutils.jl b/base/gcutils.jl new file mode 100644 index 0000000000000..3d8f04362ba57 --- /dev/null +++ b/base/gcutils.jl @@ -0,0 +1,27 @@ +# This file is a part of Julia. License is MIT: https://julialang.org/license + +==(w::WeakRef, v::WeakRef) = isequal(w.value, v.value) +==(w::WeakRef, v) = isequal(w.value, v) +==(w, v::WeakRef) = isequal(w, v.value) + +function finalizer(@nospecialize(o), @nospecialize(f)) + if isimmutable(o) + error("objects of type ", typeof(o), " cannot be finalized") + end + ccall(:jl_gc_add_finalizer_th, Void, (Ptr{Void}, Any, Any), + Core.getptls(), o, f) +end +function finalizer(o::T, f::Ptr{Void}) where T + @_inline_meta + if isimmutable(T) + error("objects of type ", T, " cannot be finalized") + end + ccall(:jl_gc_add_ptr_finalizer, Void, (Ptr{Void}, Any, Ptr{Void}), + Core.getptls(), o, f) +end + +finalize(@nospecialize(o)) = ccall(:jl_finalize_th, Void, (Ptr{Void}, Any,), + Core.getptls(), o) + +gc(full::Bool=true) = ccall(:jl_gc_collect, Void, (Int32,), full) +gc_enable(on::Bool) = ccall(:jl_gc_enable, Int32, (Int32,), on) != 0 diff --git a/base/io.jl b/base/io.jl index 5959e94434b64..b08f62e5df4c1 100644 --- a/base/io.jl +++ b/base/io.jl @@ -2,6 +2,27 @@ # Generic IO stubs -- all subtypes should implement these (if meaningful) +""" + EOFError() + +No more data was available to read from a file or stream. +""" +mutable struct EOFError <: Exception end + +""" + SystemError(prefix::AbstractString, [errno::Int32]) + +A system call failed with an error code (in the `errno` global variable). +""" +mutable struct SystemError <: Exception + prefix::AbstractString + errnum::Int32 + extrainfo + SystemError(p::AbstractString, e::Integer, extrainfo) = new(p, e, extrainfo) + SystemError(p::AbstractString, e::Integer) = new(p, e, nothing) + SystemError(p::AbstractString) = new(p, Libc.errno()) +end + lock(::IO) = nothing unlock(::IO) = nothing reseteof(x::IO) = nothing diff --git a/base/nullabletype.jl b/base/nullabletype.jl new file mode 100644 index 0000000000000..8c1c9c3d1d91c --- /dev/null +++ b/base/nullabletype.jl @@ -0,0 +1,9 @@ +# This file is a part of Julia. License is MIT: https://julialang.org/license + +struct Nullable{T} + hasvalue::Bool + value::T + + Nullable{T}() where {T} = new(false) + Nullable{T}(value::T, hasvalue::Bool=true) where {T} = new(hasvalue, value) +end diff --git a/base/parse.jl b/base/parse.jl index 87b7c6aed6c92..1ef3f91278cb6 100644 --- a/base/parse.jl +++ b/base/parse.jl @@ -217,6 +217,16 @@ float(a::AbstractArray{<:AbstractString}) = map!(float, similar(a,typeof(float(0 ## interface to parser ## +""" + ParseError(msg) + +The expression passed to the `parse` function could not be interpreted as a valid Julia +expression. +""" +mutable struct ParseError <: Exception + msg::AbstractString +end + function parse(str::AbstractString, pos::Int; greedy::Bool=true, raise::Bool=true) # pos is one based byte offset. # returns (expr, end_pos). expr is () in case of parse error. diff --git a/base/sysimg.jl b/base/sysimg.jl index a30ec87bf7f5e..dec8dc77b5586 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -51,7 +51,7 @@ convert(::Type{T}, arg) where {T<:VecElement} = T(arg) convert(::Type{T}, arg::T) where {T<:VecElement} = arg # init core docsystem -import Core: @doc, @__doc__, @doc_str +import Core: @doc, @__doc__, @doc_str, WrappedException if isdefined(Core, :Inference) import Core.Inference.CoreDocs Core.atdoc!(CoreDocs.docm) @@ -72,7 +72,8 @@ end ## Load essential files and libraries include("essentials.jl") include("ctypes.jl") -include("base.jl") +include("gcutils.jl") +include("nullabletype.jl") include("generator.jl") include("reflection.jl") include("options.jl") diff --git a/src/abi_x86.cpp b/src/abi_x86.cpp index 12870c00d816d..7a65de028e083 100644 --- a/src/abi_x86.cpp +++ b/src/abi_x86.cpp @@ -39,18 +39,22 @@ struct ABI_x86Layout : AbiLayout { +STATIC_INLINE bool is_complex_type(jl_datatype_t *dt) +{ + static jl_sym_t *Complex_sym = NULL; + if (Complex_sym == NULL) + Complex_sym = jl_symbol("Complex"); + return jl_is_datatype(dt) && dt->name->name == Complex_sym && dt->name->module == jl_base_module; +} + inline bool is_complex64(jl_datatype_t *dt) const { - return jl_complex_type != NULL && jl_is_datatype(dt) && - ((jl_datatype_t*)dt)->name == ((jl_datatype_t*)jl_unwrap_unionall((jl_value_t*)jl_complex_type))->name && - jl_tparam0(dt) == (jl_value_t*)jl_float32_type; + return is_complex_type(dt) && jl_tparam0(dt) == (jl_value_t*)jl_float32_type; } inline bool is_complex128(jl_datatype_t *dt) const { - return jl_complex_type != NULL && jl_is_datatype(dt) && - ((jl_datatype_t*)dt)->name == ((jl_datatype_t*)jl_unwrap_unionall((jl_value_t*)jl_complex_type))->name && - jl_tparam0(dt) == (jl_value_t*)jl_float64_type; + return is_complex_type(dt) && jl_tparam0(dt) == (jl_value_t*)jl_float64_type; } bool use_sret(jl_datatype_t *dt) override diff --git a/src/datatype.c b/src/datatype.c index 72f21e2beaf2c..c6bc2e61557ad 100644 --- a/src/datatype.c +++ b/src/datatype.c @@ -484,6 +484,15 @@ JL_DLLEXPORT jl_value_t *jl_new_bits(jl_value_t *bt, void *data) return jl_new_bits_internal(bt, data, &len); } +// used by boot.jl +JL_DLLEXPORT jl_value_t *jl_typemax_uint(jl_value_t *bt) +{ + uint64_t data = 0xffffffffffffffffULL; + jl_value_t *v = jl_gc_alloc(jl_get_ptls_states(), sizeof(size_t), bt); + memcpy(jl_data_ptr(v), &data, sizeof(size_t)); + return v; +} + void jl_assign_bits(void *dest, jl_value_t *bits) { size_t nb = jl_datatype_size(jl_typeof(bits)); diff --git a/src/init.c b/src/init.c index 9d9ef5273e1bc..d708b89e158bb 100644 --- a/src/init.c +++ b/src/init.c @@ -744,11 +744,6 @@ static jl_value_t *core(const char *name) return jl_get_global(jl_core_module, jl_symbol(name)); } -static jl_value_t *basemod(const char *name) -{ - return jl_get_global(jl_base_module, jl_symbol(name)); -} - // fetch references to things defined in boot.jl void jl_get_builtin_hooks(void) { @@ -799,17 +794,11 @@ void jl_get_builtin_hooks(void) jl_weakref_type = (jl_datatype_t*)core("WeakRef"); jl_vecelement_typename = ((jl_datatype_t*)jl_unwrap_unionall(core("VecElement")))->name; -} - -JL_DLLEXPORT void jl_get_system_hooks(void) -{ - if (jl_argumenterror_type) return; // only do this once - jl_argumenterror_type = (jl_datatype_t*)basemod("ArgumentError"); - jl_methoderror_type = (jl_datatype_t*)basemod("MethodError"); - jl_loaderror_type = (jl_datatype_t*)basemod("LoadError"); - jl_initerror_type = (jl_datatype_t*)basemod("InitError"); - jl_complex_type = (jl_unionall_t*)basemod("Complex"); + jl_argumenterror_type = (jl_datatype_t*)core("ArgumentError"); + jl_methoderror_type = (jl_datatype_t*)core("MethodError"); + jl_loaderror_type = (jl_datatype_t*)core("LoadError"); + jl_initerror_type = (jl_datatype_t*)core("InitError"); } void jl_get_builtins(void) diff --git a/src/jltypes.c b/src/jltypes.c index d50ad990ae9d8..78c294740e3e4 100644 --- a/src/jltypes.c +++ b/src/jltypes.c @@ -65,7 +65,6 @@ jl_datatype_t *jl_float32_type; jl_datatype_t *jl_float64_type; jl_datatype_t *jl_floatingpoint_type; jl_datatype_t *jl_number_type; -jl_unionall_t *jl_complex_type; jl_datatype_t *jl_signed_type; JL_DLLEXPORT jl_value_t *jl_emptytuple=NULL; @@ -1832,14 +1831,10 @@ void jl_init_types(void) jl_emptytuple_type->instance = jl_emptytuple; // non-primitive definitions follow - jl_int32_type = NULL; jl_int32_type = jl_new_primitivetype((jl_value_t*)jl_symbol("Int32"), core, jl_any_type, jl_emptysvec, 32); - jl_int64_type = NULL; jl_int64_type = jl_new_primitivetype((jl_value_t*)jl_symbol("Int64"), core, jl_any_type, jl_emptysvec, 64); - - jl_uint8_type = NULL; jl_uint8_type = jl_new_primitivetype((jl_value_t*)jl_symbol("UInt8"), core, jl_any_type, jl_emptysvec, 8); diff --git a/src/julia.h b/src/julia.h index 6a0768575e597..c35e0b2273719 100644 --- a/src/julia.h +++ b/src/julia.h @@ -550,7 +550,6 @@ extern JL_DLLEXPORT jl_datatype_t *jl_float64_type; extern JL_DLLEXPORT jl_datatype_t *jl_floatingpoint_type; extern JL_DLLEXPORT jl_datatype_t *jl_number_type; extern JL_DLLEXPORT jl_datatype_t *jl_void_type; -extern JL_DLLEXPORT jl_unionall_t *jl_complex_type; extern JL_DLLEXPORT jl_datatype_t *jl_signed_type; extern JL_DLLEXPORT jl_datatype_t *jl_voidpointer_type; extern JL_DLLEXPORT jl_unionall_t *jl_pointer_type; diff --git a/src/staticdata.c b/src/staticdata.c index 2a2b00c94cd73..499378aa62404 100644 --- a/src/staticdata.c +++ b/src/staticdata.c @@ -1340,7 +1340,6 @@ JL_DLLEXPORT void jl_save_system_image(const char *fname) extern int jl_boot_file_loaded; extern void jl_get_builtins(void); extern void jl_get_builtin_hooks(void); -extern void jl_get_system_hooks(void); extern void jl_gc_set_permalloc_region(void *start, void *end); // Takes in a path of the form "usr/lib/julia/sys.so" (jl_restore_system_image should be passed the same string) @@ -1496,9 +1495,6 @@ static void jl_restore_system_image_from_stream(ios_t *f) jl_get_builtins(); jl_get_builtin_hooks(); - if (jl_base_module) { - jl_get_system_hooks(); - } jl_boot_file_loaded = 1; jl_init_box_caches(); diff --git a/src/toplevel.c b/src/toplevel.c index d113857aff4b0..c53b07f402fcf 100644 --- a/src/toplevel.c +++ b/src/toplevel.c @@ -120,7 +120,6 @@ static void jl_module_load_time_initialize(jl_module_t *m) } } -extern void jl_get_system_hooks(void); jl_value_t *jl_eval_module_expr(jl_module_t *parent_module, jl_expr_t *ex) { jl_ptls_t ptls = jl_get_ptls_states();