Skip to content

Commit

Permalink
Improve printing for length and size method error (#53146)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash authored Feb 4, 2024
1 parent 6be4235 commit 3d8b107
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
23 changes: 19 additions & 4 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1025,15 +1025,30 @@ 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 iterable
function min_max_on_iterable(io, ex, arg_types, kwargs)
# or tries to use something like `collect` on an iterator without defining either IteratorSize or length
function methods_on_iterable(io, ex, arg_types, kwargs)
@nospecialize
if (ex.f === max || ex.f === min) && length(arg_types) == 1 && Base.isiterable(only(arg_types))
f_correct = ex.f === max ? "maximum" : "minimum"
f = ex.f
if (f === max || f === min) && length(arg_types) == 1 && Base.isiterable(only(arg_types))
f_correct = f === max ? "maximum" : "minimum"
print(io, "\nFinding the $f_correct of an iterable is performed with `$f_correct`.")
end
if (f === Base.length || f === Base.size) && length(arg_types) >= 1
arg_type_tuple = Tuple{arg_types...}
if hasmethod(iterate, arg_type_tuple)
iterkind = IteratorSize(arg_types[1])
if iterkind isa HasLength
print(io, "\nYou may need to implement the `length` method or define `IteratorSize` for this type to be `SizeUnknown`.")
elseif iterkind isa HasShape
print(io, "\nYou may need to implement the `length` and `size` methods for `IteratorSize` `HasShape`.")
end
end
end
nothing
end

Experimental.register_error_hint(min_max_on_iterable, MethodError)
Experimental.register_error_hint(methods_on_iterable, MethodError)


# ExceptionStack implementation
size(s::ExceptionStack) = size(s.stack)
Expand Down
27 changes: 23 additions & 4 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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)
Base.Experimental.register_error_hint(Base.methods_on_iterable, MethodError)

@testset "SystemError" begin
err = try; systemerror("reason", Cint(0)); false; catch ex; ex; end::SystemError
Expand Down Expand Up @@ -1000,13 +1000,32 @@ let err_str
@test occursin("String concatenation is performed with *", err_str)
end

struct MissingLength; end
struct MissingSize; end
Base.IteratorSize(::Type{MissingSize}) = Base.HasShape{2}()
Base.iterate(::MissingLength) = nothing
Base.iterate(::MissingSize) = nothing

let err_str
expected = "Finding the minimum of an iterable is performed with `minimum`."
err_str = @except_str min([1,2,3]) MethodError
@test occursin("Finding the minimum of an iterable is performed with `minimum`.", err_str)
@test occursin(expected, 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)
@test occursin(expected, err_str)
expected = "Finding the maximum of an iterable is performed with `maximum`."
err_str = @except_str max([1,2,3]) MethodError
@test occursin("Finding the maximum of an iterable is performed with `maximum`.", err_str)
@test occursin(expected, err_str)

expected = "You may need to implement the `length` method or define `IteratorSize` for this type to be `SizeUnknown`."
err_str = @except_str length(MissingLength()) MethodError
@test occursin(expected, err_str)
err_str = @except_str collect(MissingLength()) MethodError
@test occursin(expected, err_str)
expected = "You may need to implement the `length` and `size` methods for `IteratorSize` `HasShape`."
err_str = @except_str size(MissingSize()) MethodError
@test occursin(expected, err_str)
err_str = @except_str collect(MissingSize()) MethodError
@test occursin(expected, err_str)
end

@testset "unused argument names" begin
Expand Down

2 comments on commit 3d8b107

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The package evaluation job you requested has completed - possible new issues were detected.
The full report is available.

Please sign in to comment.