From 83872837a1bf9c87559358d9ab75b621b01679db Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Sun, 2 Mar 2025 12:27:32 +0100 Subject: [PATCH 1/4] `@nospecialize` for `string_index_err` and `StringIndexError` The fields of `StringIndexError` are abstractly typed, so there's no reason to specialize, I think. The change seems like it could prevent some invalidation on loading user code. --- base/strings/string.jl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/base/strings/string.jl b/base/strings/string.jl index 79ec12d11cb94..2b638e346504c 100644 --- a/base/strings/string.jl +++ b/base/strings/string.jl @@ -8,8 +8,16 @@ An error occurred when trying to access `str` at index `i` that is not valid. struct StringIndexError <: Exception string::AbstractString index::Integer + function StringIndexError((@nospecialize s::AbstractString), (@nospecialize i::Integer)) + new(s, i) + end +end +function StringIndexError(str, ind) + s = convert(AbstractString, str) + i = convert(Integer, ind) + StringIndexError(s, i) end -@noinline string_index_err(s::AbstractString, i::Integer) = +@noinline string_index_err((@nospecialize s::AbstractString), (@nospecialize i::Integer)) = throw(StringIndexError(s, Int(i))) function Base.showerror(io::IO, exc::StringIndexError) s = exc.string From 793ea227e5c07543c9d0800ee0f5cc276aca1e2e Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Sun, 2 Mar 2025 13:07:38 +0100 Subject: [PATCH 2/4] don't call `Int(i)` --- base/strings/string.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/strings/string.jl b/base/strings/string.jl index 2b638e346504c..e383c620e9e70 100644 --- a/base/strings/string.jl +++ b/base/strings/string.jl @@ -18,7 +18,7 @@ function StringIndexError(str, ind) StringIndexError(s, i) end @noinline string_index_err((@nospecialize s::AbstractString), (@nospecialize i::Integer)) = - throw(StringIndexError(s, Int(i))) + throw(StringIndexError(s, i)) function Base.showerror(io::IO, exc::StringIndexError) s = exc.string print(io, "StringIndexError: ", "invalid index [$(exc.index)]") From 7f373e3c39e8129a4194c18db3ea548aefc51794 Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Mon, 3 Mar 2025 13:16:02 +0100 Subject: [PATCH 3/4] reduce PR scope --- base/strings/string.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/strings/string.jl b/base/strings/string.jl index e383c620e9e70..42d3c346f1f16 100644 --- a/base/strings/string.jl +++ b/base/strings/string.jl @@ -17,8 +17,8 @@ function StringIndexError(str, ind) i = convert(Integer, ind) StringIndexError(s, i) end -@noinline string_index_err((@nospecialize s::AbstractString), (@nospecialize i::Integer)) = - throw(StringIndexError(s, i)) +@noinline string_index_err((@nospecialize s::AbstractString), i::Integer) = + throw(StringIndexError(s, Int(i))) function Base.showerror(io::IO, exc::StringIndexError) s = exc.string print(io, "StringIndexError: ", "invalid index [$(exc.index)]") From 5b535c6d0f3b3032a9c624fa9881382ea703cd27 Mon Sep 17 00:00:00 2001 From: Neven Sajko <4944410+nsajko@users.noreply.github.com> Date: Sat, 8 Mar 2025 20:55:22 +0100 Subject: [PATCH 4/4] don't add redundant methods --- base/strings/string.jl | 8 -------- 1 file changed, 8 deletions(-) diff --git a/base/strings/string.jl b/base/strings/string.jl index 42d3c346f1f16..3bf9df8462c14 100644 --- a/base/strings/string.jl +++ b/base/strings/string.jl @@ -8,14 +8,6 @@ An error occurred when trying to access `str` at index `i` that is not valid. struct StringIndexError <: Exception string::AbstractString index::Integer - function StringIndexError((@nospecialize s::AbstractString), (@nospecialize i::Integer)) - new(s, i) - end -end -function StringIndexError(str, ind) - s = convert(AbstractString, str) - i = convert(Integer, ind) - StringIndexError(s, i) end @noinline string_index_err((@nospecialize s::AbstractString), i::Integer) = throw(StringIndexError(s, Int(i)))