From 60fd75e28d6fb69328c0de63dbfbdcc1d636f9cb Mon Sep 17 00:00:00 2001 From: KristofferC Date: Wed, 3 Jan 2024 16:03:54 +0100 Subject: [PATCH 1/3] add an error hint for min/max on an array --- base/errorshow.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/base/errorshow.jl b/base/errorshow.jl index 39cdf14d34de8..74a26378c00f7 100644 --- a/base/errorshow.jl +++ b/base/errorshow.jl @@ -1014,6 +1014,17 @@ end Experimental.register_error_hint(string_concatenation_hint_handler, MethodError) + +# Display a hint in case the user tries to use the min or max function on an array +function min_max_on_array(io, ex, arg_types, kwargs) + @nospecialize + if (ex.f === max || ex.f === min) && length(arg_types) == 1 && only(arg_types) <: AbstractArray + print(io, "\nFinding the minimum or maximum element of an array is performed with `minimum` and `maximum` respectively.") + end +end + +Experimental.register_error_hint(min_max_on_array, MethodError) + # ExceptionStack implementation size(s::ExceptionStack) = size(s.stack) getindex(s::ExceptionStack, i::Int) = s.stack[i] From dcceabe5e0bd2f765ef713d3a3a6b1273bc7dd62 Mon Sep 17 00:00:00 2001 From: KristofferC Date: Thu, 4 Jan 2024 11:38:37 +0100 Subject: [PATCH 2/3] tweak wording, add tests --- base/errorshow.jl | 11 ++++++----- test/errorshow.jl | 9 +++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/base/errorshow.jl b/base/errorshow.jl index 74a26378c00f7..796272f2b89c5 100644 --- a/base/errorshow.jl +++ b/base/errorshow.jl @@ -1015,15 +1015,16 @@ end Experimental.register_error_hint(string_concatenation_hint_handler, MethodError) -# Display a hint in case the user tries to use the min or max function on an array -function min_max_on_array(io, ex, arg_types, kwargs) +# Display a hint in case the user tries to use the min or max function on an iterable +function min_max_on_iterable(io, ex, arg_types, kwargs) @nospecialize - if (ex.f === max || ex.f === min) && length(arg_types) == 1 && only(arg_types) <: AbstractArray - print(io, "\nFinding the minimum or maximum element of an array is performed with `minimum` and `maximum` respectively.") + if (ex.f === max || ex.f === min) && length(arg_types) == 1 && Base.isiterable(only(arg_types)) + f_correct = ex.f === max ? "maximum" : "minimum" + print(io, "\nFinding the $f_correct of an iterable is performed with `$f_correct`.") end end -Experimental.register_error_hint(min_max_on_array, MethodError) +Experimental.register_error_hint(min_max_on_iterable, MethodError) # ExceptionStack implementation size(s::ExceptionStack) = size(s.stack) diff --git a/test/errorshow.jl b/test/errorshow.jl index 1210fd7abcbd3..e0767c8090470 100644 --- a/test/errorshow.jl +++ b/test/errorshow.jl @@ -986,6 +986,15 @@ let err_str @test occursin("String concatenation is performed with *", err_str) end +let err_str + err_str = @except_str min([1,2,3]) MethodError + @test occursin("Finding the minimum of an iterable is performed with `minimum`.", err_str) + err_str = @except_str min((i for i in 1:3)) MethodError + @test occursin("Finding the minimum of an iterable is performed with `minimum`.", err_str) + err_str = @except_str max([1,2,3]) MethodError + @test occursin("Finding the maximum of an iterable is performed with `maximum`.", err_str) +end + @testset "unused argument names" begin g(::Int) = backtrace() bt = g(1) From 864f887888437cdcb6091b825b386a291ce2a3fe Mon Sep 17 00:00:00 2001 From: KristofferC Date: Mon, 8 Jan 2024 14:46:07 +0100 Subject: [PATCH 3/3] register error hint in tests --- test/errorshow.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/errorshow.jl b/test/errorshow.jl index 109ce271bfcf9..1baedaa6f137b 100644 --- a/test/errorshow.jl +++ b/test/errorshow.jl @@ -8,6 +8,7 @@ include("testenv.jl") # re-register only the error hints that are being tested here ( Base.Experimental.register_error_hint(Base.noncallable_number_hint_handler, MethodError) Base.Experimental.register_error_hint(Base.string_concatenation_hint_handler, MethodError) +Base.Experimental.register_error_hint(Base.min_max_on_iterable, MethodError) @testset "SystemError" begin err = try; systemerror("reason", Cint(0)); false; catch ex; ex; end::SystemError