Skip to content

Commit 77f4bc6

Browse files
jishnubKristofferC
authored and
KristofferC
committed
LazyString in reinterpretarray error messages (#54704)
This should remove the dynamic dispatch flagged by JET, e.g. in ```julia julia> using JET julia> @report_opt reinterpret(Float64, [1.0im;;]) [ Info: tracking Base ┌ Warning: skipping (::Base.var"#thrownonint#375")(S::Type, T::Type, dim) @ Base reinterpretarray.jl:68 to avoid parsing too much code └ @ Revise ~/.julia/packages/Revise/bAgL0/src/packagedef.jl:1092 ┌ Warning: skipping (::Base.var"#show_bound#661")(io::IO, b) @ Base show.jl:2777 to avoid parsing too much code └ @ Revise ~/.julia/packages/Revise/bAgL0/src/packagedef.jl:1092 ┌ Warning: skipping (::Base.var"#show_bound#661")(io::IO, b) @ Base show.jl:2777 to avoid parsing too much code └ @ Revise ~/.julia/packages/Revise/bAgL0/src/packagedef.jl:1092 ═════ 32 possible errors found ═════ ┌ reinterpret(::Type{Float64}, a::Matrix{ComplexF64}) @ Base ./reinterpretarray.jl:88 │┌ (::Base.var"#thrownonint#375")(S::Type{ComplexF64}, T::Type{Float64}, dim::Int64) @ Base ./reinterpretarray.jl:70 ││┌ string(::String, ::Type{ComplexF64}, ::String, ::Type{Float64}, ::String, ::Int64, ::String) @ Base ./strings/io.jl:189 │││┌ print_to_string(::String, ::Type{ComplexF64}, ::String, ::Type{Float64}, ::String, ::Int64, ::String) @ Base ./strings/io.jl:148 ││││┌ print(io::IOBuffer, x::DataType) @ Base ./strings/io.jl:35 │││││┌ show(io::IOBuffer, x::DataType) @ Base ./show.jl:970 ││││││┌ _show_type(io::IOBuffer, x::Type) @ Base ./show.jl:975 │││││││┌ show_typealias(io::IOBuffer, x::Type) @ Base ./show.jl:810 ││││││││┌ make_typealias(x::Type) @ Base ./show.jl:620 │││││││││┌ modulesof!(s::Set{Module}, x::Type) @ Base ./show.jl:595 ││││││││││ runtime dispatch detected: Base.modulesof!(s::Set{Module}, %20::Any)::Any │││││││││└──────────────────── │││││││││┌ modulesof!(s::Set{Module}, x::Type) @ Base ./show.jl:596 ││││││││││ runtime dispatch detected: Base.modulesof!(s::Set{Module}, %34::Any)::Any │││││││││└──────────────────── │││││││││┌ modulesof!(s::Set{Module}, x::TypeVar) @ Base ./show.jl:589 ││││││││││ runtime dispatch detected: Base.modulesof!(s::Set{Module}, %1::Any)::Set{Module} │││││││││└──────────────────── │││││││┌ show_typealias(io::IOBuffer, x::Type) @ Base ./show.jl:813 ││││││││┌ show_typealias(io::IOBuffer, name::GlobalRef, x::Type, env::Core.SimpleVector, wheres::Vector{TypeVar}) @ Base ./show.jl:760 │││││││││┌ show_typeparams(io::IOContext{IOBuffer}, env::Core.SimpleVector, orig::Core.SimpleVector, wheres::Vector{TypeVar}) @ Base ./show.jl:724 ││││││││││┌ show(io::IOContext{IOBuffer}, tv::TypeVar) @ Base ./show.jl:2788 │││││││││││┌ (::Base.var"#show_bound#661")(io::IOContext{IOBuffer}, b::Any) @ Base ./show.jl:2780 ││││││││││││ runtime dispatch detected: show(io::IOContext{IOBuffer}, b::Any)::Any │││││││││││└──────────────────── │││││││││┌ show_typeparams(io::IOContext{IOBuffer}, env::Core.SimpleVector, orig::Core.SimpleVector, wheres::Vector{TypeVar}) @ Base ./show.jl:719 ││││││││││ runtime dispatch detected: show(io::IOContext{IOBuffer}, %252::Any)::Any │││││││││└──────────────────── │││││││││┌ show_typeparams(io::IOContext{IOBuffer}, env::Core.SimpleVector, orig::Core.SimpleVector, wheres::Vector{TypeVar}) @ Base ./show.jl:722 ││││││││││ runtime dispatch detected: show(io::IOContext{IOBuffer}, %313::Any)::Any │││││││││└──────────────────── │││││││││┌ show_typeparams(io::IOContext{IOBuffer}, env::Core.SimpleVector, orig::Core.SimpleVector, wheres::Vector{TypeVar}) @ Base ./show.jl:727 ││││││││││ runtime dispatch detected: show(io::IOContext{IOBuffer}, %191::Any)::Any │││││││││└──────────────────── ││││││┌ _show_type(io::IOBuffer, x::Type) @ Base ./show.jl:978 │││││││┌ show_datatype(io::IOBuffer, x::DataType) @ Base ./show.jl:1094 ││││││││┌ show_datatype(io::IOBuffer, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1097 │││││││││┌ maybe_kws_nt(x::DataType) @ Base ./show.jl:1085 ││││││││││ runtime dispatch detected: eltype(%76::DataType)::Any │││││││││└──────────────────── ││││││││┌ show_datatype(io::IOBuffer, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1186 │││││││││┌ show_typeparams(io::IOBuffer, env::Core.SimpleVector, orig::Core.SimpleVector, wheres::Vector{TypeVar}) @ Base ./show.jl:724 ││││││││││┌ show(io::IOBuffer, tv::TypeVar) @ Base ./show.jl:2788 │││││││││││┌ (::Base.var"#show_bound#661")(io::IOBuffer, b::Any) @ Base ./show.jl:2780 ││││││││││││ runtime dispatch detected: show(io::IOBuffer, b::Any)::Any │││││││││││└──────────────────── │││││││││┌ show_typeparams(io::IOBuffer, env::Core.SimpleVector, orig::Core.SimpleVector, wheres::Vector{TypeVar}) @ Base ./show.jl:719 ││││││││││ runtime dispatch detected: show(io::IOBuffer, %250::Any)::Any │││││││││└──────────────────── │││││││││┌ show_typeparams(io::IOBuffer, env::Core.SimpleVector, orig::Core.SimpleVector, wheres::Vector{TypeVar}) @ Base ./show.jl:722 ││││││││││ runtime dispatch detected: show(io::IOBuffer, %310::Any)::Any │││││││││└──────────────────── │││││││││┌ show_typeparams(io::IOBuffer, env::Core.SimpleVector, orig::Core.SimpleVector, wheres::Vector{TypeVar}) @ Base ./show.jl:727 ││││││││││ runtime dispatch detected: show(io::IOBuffer, %190::Any)::Any │││││││││└──────────────────── ││││││││┌ show_datatype(io::IOBuffer, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1157 │││││││││ runtime dispatch detected: show(io::IOBuffer, %224::Any)::Any ││││││││└──────────────────── ││││││││┌ show_datatype(io::IOBuffer, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1162 │││││││││ runtime dispatch detected: show(io::IOBuffer, %54::Any)::Any ││││││││└──────────────────── ││││││││┌ show_datatype(io::IOBuffer, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1148 │││││││││ runtime dispatch detected: show(io::IOBuffer, %57::Any)::Any ││││││││└──────────────────── ││││││││┌ show_datatype(io::IOBuffer, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1150 │││││││││ runtime dispatch detected: show(io::IOBuffer, %54::Any)::Any ││││││││└──────────────────── ││││││││┌ show_datatype(io::IOBuffer, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1172 │││││││││ runtime dispatch detected: Base.show_at_namedtuple(io::IOBuffer, %329::Tuple, %328::DataType)::Any ││││││││└──────────────────── ││││││┌ _show_type(io::IOBuffer, x::Type) @ Base ./show.jl:981 │││││││┌ show_unionaliases(io::IOBuffer, x::Union) @ Base ./show.jl:901 ││││││││┌ make_typealiases(x::Union) @ Base ./show.jl:822 │││││││││┌ modulesof!(s::Set{Module}, x::Union) @ Base ./show.jl:595 ││││││││││ runtime dispatch detected: Base.modulesof!(s::Set{Module}, %3::Any)::Any │││││││││└──────────────────── │││││││││┌ modulesof!(s::Set{Module}, x::Union) @ Base ./show.jl:596 ││││││││││ runtime dispatch detected: Base.modulesof!(s::Set{Module}, %17::Any)::Any │││││││││└──────────────────── │││││││┌ show_unionaliases(io::IOBuffer, x::Union) @ Base ./show.jl:914 ││││││││ runtime dispatch detected: show(io::IOBuffer, %89::Any)::Any │││││││└──────────────────── │││││││┌ show_unionaliases(io::IOBuffer, x::Union) @ Base ./show.jl:920 ││││││││ runtime dispatch detected: Base.show_typealias(io::IOBuffer, %206::Any, x::Union, %204::Core.SimpleVector, %205::Vector{TypeVar})::Any │││││││└──────────────────── │││││││┌ show_unionaliases(io::IOBuffer, x::Union) @ Base ./show.jl:928 ││││││││ runtime dispatch detected: Base.show_typealias(io::IOBuffer, %269::Any, x::Union, %267::Core.SimpleVector, %268::Vector{TypeVar})::Any │││││││└──────────────────── ││││││┌ _show_type(io::IOBuffer, x::Type) @ Base ./show.jl:985 │││││││┌ show_delim_array(io::IOBuffer, itr::Vector{Any}, op::Char, delim::Char, cl::Char, delim_one::Bool) @ Base ./show.jl:1392 ││││││││┌ show_delim_array(io::IOBuffer, itr::Vector{Any}, op::Char, delim::Char, cl::Char, delim_one::Bool, i1::Int64, l::Int64) @ Base ./show.jl:1403 │││││││││ runtime dispatch detected: show(%3::IOContext{IOBuffer}, %52::Any)::Any ││││││││└──────────────────── ││││││┌ _show_type(io::IOBuffer, x::Type) @ Base ./show.jl:1012 │││││││┌ show_datatype(io::IOContext{IOBuffer}, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1185 ││││││││┌ show_type_name(io::IOContext{IOBuffer}, tn::Core.TypeName) @ Base ./show.jl:1059 │││││││││ runtime dispatch detected: Base.isvisible(%29::Symbol, %86::Module, %80::Any)::Bool ││││││││└──────────────────── │││││││┌ show_datatype(io::IOContext{IOBuffer}, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1157 ││││││││ runtime dispatch detected: show(io::IOContext{IOBuffer}, %227::Any)::Any │││││││└──────────────────── │││││││┌ show_datatype(io::IOContext{IOBuffer}, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1162 ││││││││ runtime dispatch detected: show(io::IOContext{IOBuffer}, %55::Any)::Any │││││││└──────────────────── │││││││┌ show_datatype(io::IOContext{IOBuffer}, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1148 ││││││││ runtime dispatch detected: show(io::IOContext{IOBuffer}, %58::Any)::Any │││││││└──────────────────── │││││││┌ show_datatype(io::IOContext{IOBuffer}, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1150 ││││││││ runtime dispatch detected: show(io::IOContext{IOBuffer}, %55::Any)::Any │││││││└──────────────────── │││││││┌ show_datatype(io::IOContext{IOBuffer}, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1172 ││││││││ runtime dispatch detected: Base.show_at_namedtuple(io::IOContext{IOBuffer}, %338::Tuple, %337::DataType)::Any │││││││└──────────────────── │││││││┌ show_datatype(io::IOContext{IOBuffer}, x::DataType, wheres::Vector{TypeVar}) @ Base ./show.jl:1180 ││││││││ runtime dispatch detected: Base.show_at_namedtuple(io::IOContext{IOBuffer}, %387::Tuple, %391::DataType)::Any │││││││└──────────────────── ││││││┌ _show_type(io::IOBuffer, x::Type) @ Base ./show.jl:1014 │││││││ runtime dispatch detected: show(%98::IOContext{IOBuffer}, %99::Any)::Any ││││││└──────────────────── │││││┌ show(io::IOBuffer, x::DataType) @ Base ./show.jl:970 ││││││ runtime dispatch detected: Base._show_type(io::IOBuffer, %1::Any)::Nothing │││││└──────────────────── ``` (cherry picked from commit b54c688)
1 parent 60cc3ab commit 77f4bc6

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

base/reinterpretarray.jl

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ struct ReinterpretArray{T,N,S,A<:AbstractArray{S},IsReshaped} <: AbstractArray{T
1313

1414
function throwbits(S::Type, T::Type, U::Type)
1515
@noinline
16-
throw(ArgumentError("cannot reinterpret `$(S)` as `$(T)`, type `$(U)` is not a bits type"))
16+
throw(ArgumentError(LazyString("cannot reinterpret `", S, "` as `", T, "`, type `", U, "` is not a bits type")))
1717
end
1818
function throwsize0(S::Type, T::Type, msg)
1919
@noinline
20-
throw(ArgumentError("cannot reinterpret a zero-dimensional `$(S)` array to `$(T)` which is of a $msg size"))
20+
throw(ArgumentError(LazyString("cannot reinterpret a zero-dimensional `", S, "` array to `", T,
21+
"` which is of a ", msg, " size")))
2122
end
2223
function throwsingleton(S::Type, T::Type)
2324
@noinline
24-
throw(ArgumentError("cannot reinterpret a `$(S)` array to `$(T)` which is a singleton type"))
25+
throw(ArgumentError(LazyString("cannot reinterpret a `", S, "` array to `", T, "` which is a singleton type")))
2526
end
2627

2728
global reinterpret
@@ -67,14 +68,14 @@ struct ReinterpretArray{T,N,S,A<:AbstractArray{S},IsReshaped} <: AbstractArray{T
6768
function reinterpret(::Type{T}, a::A) where {T,N,S,A<:AbstractArray{S, N}}
6869
function thrownonint(S::Type, T::Type, dim)
6970
@noinline
70-
throw(ArgumentError("""
71-
cannot reinterpret an `$(S)` array to `$(T)` whose first dimension has size `$(dim)`.
72-
The resulting array would have non-integral first dimension.
73-
"""))
71+
throw(ArgumentError(LazyString(
72+
"cannot reinterpret an `", S, "` array to `", T, "` whose first dimension has size `", dim,
73+
"`. The resulting array would have a non-integral first dimension.")))
7474
end
7575
function throwaxes1(S::Type, T::Type, ax1)
7676
@noinline
77-
throw(ArgumentError("cannot reinterpret a `$(S)` array to `$(T)` when the first axis is $ax1. Try reshaping first."))
77+
throw(ArgumentError(LazyString("cannot reinterpret a `", S, "` array to `", T,
78+
"` when the first axis is ", ax1, ". Try reshaping first.")))
7879
end
7980
isbitstype(T) || throwbits(S, T, T)
8081
isbitstype(S) || throwbits(S, T, S)
@@ -99,15 +100,19 @@ struct ReinterpretArray{T,N,S,A<:AbstractArray{S},IsReshaped} <: AbstractArray{T
99100
function reinterpret(::typeof(reshape), ::Type{T}, a::A) where {T,S,A<:AbstractArray{S}}
100101
function throwintmult(S::Type, T::Type)
101102
@noinline
102-
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"))
103+
throw(ArgumentError(LazyString("`reinterpret(reshape, T, a)` requires that one of `sizeof(T)` (got ",
104+
sizeof(T), ") and `sizeof(eltype(a))` (got ", sizeof(S), ") be an integer multiple of the other")))
103105
end
104106
function throwsize1(a::AbstractArray, T::Type)
105107
@noinline
106-
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)"))
108+
throw(ArgumentError(LazyString("`reinterpret(reshape, ", T, ", a)` where `eltype(a)` is ", eltype(a),
109+
" requires that `axes(a, 1)` (got ", axes(a, 1), ") be equal to 1:",
110+
sizeof(T) ÷ sizeof(eltype(a)), " (from the ratio of element sizes)")))
107111
end
108112
function throwfromsingleton(S, T)
109113
@noinline
110-
throw(ArgumentError("`reinterpret(reshape, $T, a)` where `eltype(a)` is $S requires that $T be a singleton type, since $S is one"))
114+
throw(ArgumentError(LazyString("`reinterpret(reshape, ", T, ", a)` where `eltype(a)` is ", S,
115+
" requires that ", T, " be a singleton type, since ", S, " is one")))
111116
end
112117
isbitstype(T) || throwbits(S, T, T)
113118
isbitstype(S) || throwbits(S, T, S)
@@ -851,8 +856,8 @@ end
851856
inpackedsize = packedsize(In)
852857
outpackedsize = packedsize(Out)
853858
inpackedsize == outpackedsize ||
854-
throw(ArgumentError("Packed sizes of types $Out and $In do not match; got $outpackedsize \
855-
and $inpackedsize, respectively."))
859+
throw(ArgumentError(LazyString("Packed sizes of types ", Out, " and ", In,
860+
" do not match; got ", outpackedsize, " and ", inpackedsize, ", respectively.")))
856861
in = Ref{In}(x)
857862
out = Ref{Out}()
858863
if struct_subpadding(Out, In)

test/reinterpretarray.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ end
4040
@test_throws ArgumentError("cannot reinterpret `Vector{Int32}` as `Int32`, type `Vector{Int32}` is not a bits type") reinterpret(Int32, Av)
4141
@test_throws ArgumentError("cannot reinterpret a zero-dimensional `Int64` array to `Int32` which is of a different size") reinterpret(Int32, reshape([Int64(0)]))
4242
@test_throws ArgumentError("cannot reinterpret a zero-dimensional `Int32` array to `Int64` which is of a different size") reinterpret(Int64, reshape([Int32(0)]))
43-
@test_throws ArgumentError("""cannot reinterpret an `$Int` array to `Tuple{$Int, $Int}` whose first dimension has size `5`.
44-
The resulting array would have non-integral first dimension.
45-
""") reinterpret(Tuple{Int,Int}, [1,2,3,4,5])
43+
@test_throws ArgumentError("cannot reinterpret an `$Int` array to `Tuple{$Int, $Int}` whose first dimension has size `5`."*
44+
" The resulting array would have a non-integral first dimension.") reinterpret(Tuple{Int,Int}, [1,2,3,4,5])
4645

4746
@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)
4847
@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)

0 commit comments

Comments
 (0)