Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LazyString in interpolated error messages involving types #54737

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ end
# give Enum types scalar behavior in broadcasting
Base.broadcastable(x::Enum) = Ref(x)

@noinline enum_argument_error(typename, x) = throw(ArgumentError(string("invalid value for Enum $(typename): $x")))
@noinline enum_argument_error(typename, x) = throw(ArgumentError(LazyString("invalid value for Enum ", typename, ": ", x)))

"""
@enum EnumName[::BaseType] value1[=x] value2[=y]
Expand Down Expand Up @@ -143,18 +143,19 @@ julia> Symbol(apple)
"""
macro enum(T::Union{Symbol,Expr}, syms...)
if isempty(syms)
throw(ArgumentError("no arguments given for Enum $T"))
throw(ArgumentError(LazyString("no arguments given for Enum ", T)))
end
basetype = Int32
typename = T
if isa(T, Expr) && T.head === :(::) && length(T.args) == 2 && isa(T.args[1], Symbol)
typename = T.args[1]
basetype = Core.eval(__module__, T.args[2])
if !isa(basetype, DataType) || !(basetype <: Integer) || !isbitstype(basetype)
throw(ArgumentError("invalid base type for Enum $typename, $T=::$basetype; base type must be an integer primitive type"))
throw(ArgumentError(
LazyString("invalid base type for Enum ", typename, ", ", T, "=::", basetype, "; base type must be an integer primitive type")))
end
elseif !isa(T, Symbol)
throw(ArgumentError("invalid type expression for enum $T"))
throw(ArgumentError(LazyString("invalid type expression for enum ", T)))
end
values = Vector{basetype}()
seen = Set{Symbol}()
Expand All @@ -169,32 +170,32 @@ macro enum(T::Union{Symbol,Expr}, syms...)
s isa LineNumberNode && continue
if isa(s, Symbol)
if i == typemin(basetype) && !isempty(values)
throw(ArgumentError("overflow in value \"$s\" of Enum $typename"))
throw(ArgumentError(LazyString("overflow in value \"", s, "\" of Enum ", typename)))
end
elseif isa(s, Expr) &&
(s.head === :(=) || s.head === :kw) &&
length(s.args) == 2 && isa(s.args[1], Symbol)
i = Core.eval(__module__, s.args[2]) # allow exprs, e.g. uint128"1"
if !isa(i, Integer)
throw(ArgumentError("invalid value for Enum $typename, $s; values must be integers"))
throw(ArgumentError(LazyString("invalid value for Enum ", typename, ", ", s, "; values must be integers")))
end
i = convert(basetype, i)
s = s.args[1]
hasexpr = true
else
throw(ArgumentError(string("invalid argument for Enum ", typename, ": ", s)))
throw(ArgumentError(LazyString("invalid argument for Enum ", typename, ": ", s)))
end
s = s::Symbol
if !Base.isidentifier(s)
throw(ArgumentError("invalid name for Enum $typename; \"$s\" is not a valid identifier"))
throw(ArgumentError(LazyString("invalid name for Enum ", typename, "; \"", s, "\" is not a valid identifier")))
end
if hasexpr && haskey(namemap, i)
throw(ArgumentError("both $s and $(namemap[i]) have value $i in Enum $typename; values must be unique"))
throw(ArgumentError(LazyString("both ", s, " and ", namemap[i], " have value ", i, " in Enum ", typename, "; values must be unique")))
end
namemap[i] = s
push!(values, i)
if s in seen
throw(ArgumentError("name \"$s\" in Enum $typename is not unique"))
throw(ArgumentError(LazyString("name \"", s, "\" in Enum ", typename, " is not unique")))
end
push!(seen, s)
if length(values) == 1
Expand Down
16 changes: 9 additions & 7 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ julia> checkindex(Bool, 1:20, 21)
false
```
"""
checkindex(::Type{Bool}, inds, i) = throw(ArgumentError("unable to check bounds for indices of type $(typeof(i))"))
checkindex(::Type{Bool}, inds, i) = throw(ArgumentError(LazyString("unable to check bounds for indices of type ", typeof(i))))
checkindex(::Type{Bool}, inds::AbstractUnitRange, i::Real) = (first(inds) <= i) & (i <= last(inds))
checkindex(::Type{Bool}, inds::IdentityUnitRange, i::Real) = checkindex(Bool, inds.indices, i)
checkindex(::Type{Bool}, inds::OneTo{T}, i::T) where {T<:BitInteger} = unsigned(i - one(i)) < unsigned(last(inds))
Expand Down Expand Up @@ -1541,12 +1541,14 @@ much more common case where aliasing does not occur. By default,
unaliascopy(A::Array) = copy(A)
unaliascopy(A::AbstractArray)::typeof(A) = (@noinline; _unaliascopy(A, copy(A)))
_unaliascopy(A::T, C::T) where {T} = C
_unaliascopy(A, C) = throw(ArgumentError("""
an array of type `$(typename(typeof(A)).wrapper)` shares memory with another argument
and must make a preventative copy of itself in order to maintain consistent semantics,
but `copy(::$(typeof(A)))` returns a new array of type `$(typeof(C))`.
To fix, implement:
`Base.unaliascopy(A::$(typename(typeof(A)).wrapper))::typeof(A)`"""))
function _unaliascopy(A, C)
Aw = typename(typeof(A)).wrapper
throw(ArgumentError(LazyString("an array of type `", Aw, "` shares memory with another argument ",
"and must make a preventative copy of itself in order to maintain consistent semantics, ",
"but `copy(::", typeof(A), ")` returns a new array of type `", typeof(C), "`.\n",
"""To fix, implement:
`Base.unaliascopy(A::""", Aw, ")::typeof(A)`")))
end
unaliascopy(A) = A

"""
Expand Down
2 changes: 1 addition & 1 deletion base/c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ function ccall_macro_lower(convention, func, rettype, types, args, nreq)
check = quote
if !isa(func, Ptr{Cvoid})
name = $name
throw(ArgumentError("interpolated function `$name` was not a Ptr{Cvoid}, but $(typeof(func))"))
throw(ArgumentError(LazyString("interpolated function `", name, "` was not a Ptr{Cvoid}, but ", typeof(func))))
end
end
push!(statements, check)
Expand Down
4 changes: 2 additions & 2 deletions base/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ to_index(I::AbstractArray{Bool}) = LogicalIndex(I)
to_index(I::AbstractArray) = I
to_index(I::AbstractArray{Union{}}) = I
to_index(I::AbstractArray{<:Union{AbstractArray, Colon}}) =
throw(ArgumentError("invalid index: $(limitrepr(I)) of type $(typeof(I))"))
throw(ArgumentError(LazyString("invalid index: ", limitrepr(I), " of type ", typeof(I))))
to_index(::Colon) = throw(ArgumentError("colons must be converted by to_indices(...)"))
to_index(i) = throw(ArgumentError("invalid index: $(limitrepr(i)) of type $(typeof(i))"))
to_index(i) = throw(ArgumentError(LazyString("invalid index: ", limitrepr(i), " of type ", typeof(i))))

# The general to_indices is mostly defined in multidimensional.jl, but this
# definition is required for bootstrap:
Expand Down
4 changes: 2 additions & 2 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ julia> bitstring(2.2)
```
"""
function bitstring(x::T) where {T}
isprimitivetype(T) || throw(ArgumentError("$T not a primitive type"))
isprimitivetype(T) || throw(ArgumentError(LazyString(T, " not a primitive type")))
sz = sizeof(T) * 8
str = StringMemory(sz)
i = sz
Expand Down Expand Up @@ -1056,7 +1056,7 @@ julia> digits!([2, 2, 2, 2, 2, 2], 10, base = 2)
function digits!(a::AbstractVector{T}, n::Integer; base::Integer = 10) where T<:Integer
2 <= abs(base) || throw(DomainError(base, "base must be ≥ 2 or ≤ -2"))
hastypemax(T) && abs(base) - 1 > typemax(T) &&
throw(ArgumentError("type $T too small for base $base"))
throw(ArgumentError(LazyString("type ", T, " too small for base ", base)))
isempty(a) && return a

if base > 0
Expand Down
2 changes: 1 addition & 1 deletion base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ previously marked position. Throw an error if the stream is not marked.
See also [`mark`](@ref), [`unmark`](@ref), [`ismarked`](@ref).
"""
function reset(io::T) where T<:IO
ismarked(io) || throw(ArgumentError("$T not marked"))
ismarked(io) || throw(ArgumentError(LazyString(T, " not marked")))
m = io.mark
seek(io, m)
io.mark = -1 # must be after seek, or seek may fail
Expand Down
6 changes: 3 additions & 3 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using .Base:
AbstractRange, AbstractUnitRange, UnitRange, LinearIndices, TupleOrBottom,
(:), |, +, -, *, !==, !, ==, !=, <=, <, >, >=, =>, missing,
any, _counttuple, eachindex, ntuple, zero, prod, reduce, in, firstindex, lastindex,
tail, fieldtypes, min, max, minimum, zero, oneunit, promote, promote_shape
tail, fieldtypes, min, max, minimum, zero, oneunit, promote, promote_shape, LazyString
using Core: @doc

if Base !== Core.Compiler
Expand Down Expand Up @@ -1097,15 +1097,15 @@ _prod_size(t::Tuple) = (_prod_size1(t[1], IteratorSize(t[1]))..., _prod_size(tai
_prod_size1(a, ::HasShape) = size(a)
_prod_size1(a, ::HasLength) = (length(a),)
_prod_size1(a, A) =
throw(ArgumentError("Cannot compute size for object of type $(typeof(a))"))
throw(ArgumentError(LazyString("Cannot compute size for object of type ", typeof(a))))

axes(P::ProductIterator) = _prod_indices(P.iterators)
_prod_indices(::Tuple{}) = ()
_prod_indices(t::Tuple) = (_prod_axes1(t[1], IteratorSize(t[1]))..., _prod_indices(tail(t))...)
_prod_axes1(a, ::HasShape) = axes(a)
_prod_axes1(a, ::HasLength) = (OneTo(length(a)),)
_prod_axes1(a, A) =
throw(ArgumentError("Cannot compute indices for object of type $(typeof(a))"))
throw(ArgumentError(LazyString("Cannot compute indices for object of type ", typeof(a))))

ndims(p::ProductIterator) = length(axes(p))
length(P::ProductIterator) = reduce(checked_mul, size(P); init=1)
Expand Down
2 changes: 1 addition & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ module IteratorsMD
else
# Given the fact that StepRange 1:2:4 === 1:2:3, we lost the original size information
# and thus cannot calculate the correct linear indices when the steps are not 1.
throw(ArgumentError("LinearIndices for $(typeof(inds)) with non-1 step size is not yet supported."))
throw(ArgumentError(LazyString("LinearIndices for ", typeof(inds), " with non-1 step size is not yet supported.")))
end
end

Expand Down
2 changes: 1 addition & 1 deletion base/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ function tryparse_internal(::Type{T}, s::AbstractString, raise::Bool; kwargs...)
return result
end
@noinline _parse_failure(T, s::AbstractString, startpos = firstindex(s), endpos = lastindex(s)) =
throw(ArgumentError("cannot parse $(repr(s[startpos:endpos])) as $T"))
throw(ArgumentError(LazyString("cannot parse ", repr(s[startpos:endpos]), " as ", T)))

tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos::Int, raise::Bool) where T<:Integer =
tryparse_internal(T, s, startpos, endpos, 10, raise)
Expand Down
2 changes: 1 addition & 1 deletion base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ end
checked_den(num::T, den::T) where T<:Integer = checked_den(T, num, den)
checked_den(num::Integer, den::Integer) = checked_den(promote(num, den)...)

@noinline __throw_rational_argerror_zero(T) = throw(ArgumentError("invalid rational: zero($T)//zero($T)"))
@noinline __throw_rational_argerror_zero(T) = throw(ArgumentError(LazyString("invalid rational: zero(", T, ")//zero(", T, ")")))
function Rational{T}(num::Integer, den::Integer) where T<:Integer
iszero(den) && iszero(num) && __throw_rational_argerror_zero(T)
if T <: Union{Unsigned, Bool}
Expand Down
2 changes: 1 addition & 1 deletion base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2478,7 +2478,7 @@ function _sort!(v::AbstractVector, a::Algorithm, o::Ordering, kw)
@getkw lo hi scratch legacy_dispatch_entry
if legacy_dispatch_entry === a
# This error prevents infinite recursion for unknown algorithms
throw(ArgumentError("Base.Sort._sort!(::$(typeof(v)), ::$(typeof(a)), ::$(typeof(o)), ::Any) is not defined"))
throw(ArgumentError(LazyString("Base.Sort._sort!(::", typeof(v), ", ::", typeof(a), ", ::", typeof(o), ", ::Any) is not defined")))
else
sort!(v, lo, hi, a, o)
scratch
Expand Down
5 changes: 3 additions & 2 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ substrides(strds::Tuple{}, ::Tuple{}) = ()
substrides(strds::NTuple{N,Int}, I::Tuple{ScalarIndex, Vararg{Any}}) where N = (substrides(tail(strds), tail(I))...,)
substrides(strds::NTuple{N,Int}, I::Tuple{Slice, Vararg{Any}}) where N = (first(strds), substrides(tail(strds), tail(I))...)
substrides(strds::NTuple{N,Int}, I::Tuple{AbstractRange, Vararg{Any}}) where N = (first(strds)*step(I[1]), substrides(tail(strds), tail(I))...)
substrides(strds, I::Tuple{Any, Vararg{Any}}) = throw(ArgumentError("strides is invalid for SubArrays with indices of type $(typeof(I[1]))"))
substrides(strds, I::Tuple{Any, Vararg{Any}}) = throw(ArgumentError(
LazyString("strides is invalid for SubArrays with indices of type ", typeof(I[1]))))

stride(V::SubArray, d::Integer) = d <= ndims(V) ? strides(V)[d] : strides(V)[end] * size(V)[end]

Expand All @@ -449,7 +450,7 @@ compute_stride1(s, inds, I::Tuple{ScalarIndex, Vararg{Any}}) =
(@inline; compute_stride1(s*length(inds[1]), tail(inds), tail(I)))
compute_stride1(s, inds, I::Tuple{AbstractRange, Vararg{Any}}) = s*step(I[1])
compute_stride1(s, inds, I::Tuple{Slice, Vararg{Any}}) = s
compute_stride1(s, inds, I::Tuple{Any, Vararg{Any}}) = throw(ArgumentError("invalid strided index type $(typeof(I[1]))"))
compute_stride1(s, inds, I::Tuple{Any, Vararg{Any}}) = throw(ArgumentError(LazyString("invalid strided index type ", typeof(I[1]))))

elsize(::Type{<:SubArray{<:Any,<:Any,P}}) where {P} = elsize(P)

Expand Down
2 changes: 1 addition & 1 deletion base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ _totuple(::Type{Tuple{}}, itr, s...) = ()

function _totuple_err(@nospecialize T)
@noinline
throw(ArgumentError("too few elements for tuple type $T"))
throw(ArgumentError(LazyString("too few elements for tuple type ", T)))
end

function _totuple(::Type{T}, itr, s::Vararg{Any,N}) where {T,N}
Expand Down