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 reinterpretarray error messages #54704

Merged
merged 1 commit into from
Jun 7, 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
31 changes: 18 additions & 13 deletions base/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ struct ReinterpretArray{T,N,S,A<:AbstractArray{S},IsReshaped} <: AbstractArray{T

function throwbits(S::Type, T::Type, U::Type)
@noinline
throw(ArgumentError("cannot reinterpret `$(S)` as `$(T)`, type `$(U)` is not a bits type"))
throw(ArgumentError(LazyString("cannot reinterpret `", S, "` as `", T, "`, type `", U, "` is not a bits type")))
end
function throwsize0(S::Type, T::Type, msg)
@noinline
throw(ArgumentError("cannot reinterpret a zero-dimensional `$(S)` array to `$(T)` which is of a $msg size"))
throw(ArgumentError(LazyString("cannot reinterpret a zero-dimensional `", S, "` array to `", T,
"` which is of a ", msg, " size")))
end
function throwsingleton(S::Type, T::Type)
@noinline
throw(ArgumentError("cannot reinterpret a `$(S)` array to `$(T)` which is a singleton type"))
throw(ArgumentError(LazyString("cannot reinterpret a `", S, "` array to `", T, "` which is a singleton type")))
end

global reinterpret
Expand Down Expand Up @@ -67,14 +68,14 @@ struct ReinterpretArray{T,N,S,A<:AbstractArray{S},IsReshaped} <: AbstractArray{T
function reinterpret(::Type{T}, a::A) where {T,N,S,A<:AbstractArray{S, N}}
function thrownonint(S::Type, T::Type, dim)
@noinline
throw(ArgumentError("""
cannot reinterpret an `$(S)` array to `$(T)` whose first dimension has size `$(dim)`.
The resulting array would have non-integral first dimension.
"""))
throw(ArgumentError(LazyString(
"cannot reinterpret an `", S, "` array to `", T, "` whose first dimension has size `", dim,
"`. The resulting array would have a non-integral first dimension.")))
end
function throwaxes1(S::Type, T::Type, ax1)
@noinline
throw(ArgumentError("cannot reinterpret a `$(S)` array to `$(T)` when the first axis is $ax1. Try reshaping first."))
throw(ArgumentError(LazyString("cannot reinterpret a `", S, "` array to `", T,
"` when the first axis is ", ax1, ". Try reshaping first.")))
end
isbitstype(T) || throwbits(S, T, T)
isbitstype(S) || throwbits(S, T, S)
Expand All @@ -99,15 +100,19 @@ struct ReinterpretArray{T,N,S,A<:AbstractArray{S},IsReshaped} <: AbstractArray{T
function reinterpret(::typeof(reshape), ::Type{T}, a::A) where {T,S,A<:AbstractArray{S}}
function throwintmult(S::Type, T::Type)
@noinline
throw(ArgumentError("`reinterpret(reshape, T, a)` requires that one of `sizeof(T)` (got $(sizeof(T))) and `sizeof(eltype(a))` (got $(sizeof(S))) be an integer multiple of the other"))
throw(ArgumentError(LazyString("`reinterpret(reshape, T, a)` requires that one of `sizeof(T)` (got ",
sizeof(T), ") and `sizeof(eltype(a))` (got ", sizeof(S), ") be an integer multiple of the other")))
end
function throwsize1(a::AbstractArray, T::Type)
@noinline
throw(ArgumentError("`reinterpret(reshape, $T, a)` where `eltype(a)` is $(eltype(a)) requires that `axes(a, 1)` (got $(axes(a, 1))) be equal to 1:$(sizeof(T) ÷ sizeof(eltype(a))) (from the ratio of element sizes)"))
throw(ArgumentError(LazyString("`reinterpret(reshape, ", T, ", a)` where `eltype(a)` is ", eltype(a),
" requires that `axes(a, 1)` (got ", axes(a, 1), ") be equal to 1:",
sizeof(T) ÷ sizeof(eltype(a)), " (from the ratio of element sizes)")))
end
function throwfromsingleton(S, T)
@noinline
throw(ArgumentError("`reinterpret(reshape, $T, a)` where `eltype(a)` is $S requires that $T be a singleton type, since $S is one"))
throw(ArgumentError(LazyString("`reinterpret(reshape, ", T, ", a)` where `eltype(a)` is ", S,
" requires that ", T, " be a singleton type, since ", S, " is one")))
end
isbitstype(T) || throwbits(S, T, T)
isbitstype(S) || throwbits(S, T, S)
Expand Down Expand Up @@ -851,8 +856,8 @@ end
inpackedsize = packedsize(In)
outpackedsize = packedsize(Out)
inpackedsize == outpackedsize ||
throw(ArgumentError("Packed sizes of types $Out and $In do not match; got $outpackedsize \
and $inpackedsize, respectively."))
throw(ArgumentError(LazyString("Packed sizes of types ", Out, " and ", In,
" do not match; got ", outpackedsize, " and ", inpackedsize, ", respectively.")))
in = Ref{In}(x)
out = Ref{Out}()
if struct_subpadding(Out, In)
Expand Down
5 changes: 2 additions & 3 deletions test/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ end
@test_throws ArgumentError("cannot reinterpret `Vector{Int32}` as `Int32`, type `Vector{Int32}` is not a bits type") reinterpret(Int32, Av)
@test_throws ArgumentError("cannot reinterpret a zero-dimensional `Int64` array to `Int32` which is of a different size") reinterpret(Int32, reshape([Int64(0)]))
@test_throws ArgumentError("cannot reinterpret a zero-dimensional `Int32` array to `Int64` which is of a different size") reinterpret(Int64, reshape([Int32(0)]))
@test_throws ArgumentError("""cannot reinterpret an `$Int` array to `Tuple{$Int, $Int}` whose first dimension has size `5`.
The resulting array would have non-integral first dimension.
""") reinterpret(Tuple{Int,Int}, [1,2,3,4,5])
@test_throws ArgumentError("cannot reinterpret an `$Int` array to `Tuple{$Int, $Int}` whose first dimension has size `5`."*
" The resulting array would have a non-integral first dimension.") reinterpret(Tuple{Int,Int}, [1,2,3,4,5])

@test_throws ArgumentError("`reinterpret(reshape, Complex{Int64}, a)` where `eltype(a)` is Int64 requires that `axes(a, 1)` (got Base.OneTo(4)) be equal to 1:2 (from the ratio of element sizes)") reinterpret(reshape, Complex{Int64}, A)
@test_throws ArgumentError("`reinterpret(reshape, T, a)` requires that one of `sizeof(T)` (got 24) and `sizeof(eltype(a))` (got 16) be an integer multiple of the other") reinterpret(reshape, NTuple{3, Int64}, B)
Expand Down