Skip to content

Commit

Permalink
fix nonsetable_type_hint_handler (#55962)
Browse files Browse the repository at this point in the history
The current implementation is wrong, causing it to display inappropriate
hints like the following:
```julia
julia> s = Some("foo");

julia> s[] = "bar"
ERROR: MethodError: no method matching setindex!(::Some{String}, ::String)
The function `setindex!` exists, but no method is defined for this combination of argument types.
You attempted to index the type String, rather than an instance of the type. Make sure you create the type using its constructor: d = String([...]) rather than d = String
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1
```
  • Loading branch information
aviatesk authored Oct 2, 2024
1 parent dd31084 commit a45d701
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ function nonsetable_type_hint_handler(io, ex, arg_types, kwargs)
print(io, "\nAre you trying to index into an array? For multi-dimensional arrays, separate the indices with commas: ")
printstyled(io, "a[1, 2]", color=:cyan)
print(io, " rather than a[1][2]")
else isType(T)
elseif isType(T)
Tx = T.parameters[1]
print(io, "\nYou attempted to index the type $Tx, rather than an instance of the type. Make sure you create the type using its constructor: ")
printstyled(io, "d = $Tx([...])", color=:cyan)
Expand Down
14 changes: 8 additions & 6 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -739,31 +739,33 @@ end
pop!(Base.Experimental._hint_handlers[DomainError]) # order is undefined, don't copy this

struct ANumber <: Number end
let err_str
err_str = @except_str ANumber()(3 + 4) MethodError
let err_str = @except_str ANumber()(3 + 4) MethodError
@test occursin("objects of type $(curmod_prefix)ANumber are not callable", err_str)
@test count(==("Maybe you forgot to use an operator such as *, ^, %, / etc. ?"), split(err_str, '\n')) == 1
# issue 40478
err_str = @except_str ANumber()(3 + 4) MethodError
@test count(==("Maybe you forgot to use an operator such as *, ^, %, / etc. ?"), split(err_str, '\n')) == 1
end

let err_str
a = [1 2; 3 4];
let a = [1 2; 3 4];
err_str = @except_str (a[1][2] = 5) MethodError
@test occursin("\nAre you trying to index into an array? For multi-dimensional arrays, separate the indices with commas: ", err_str)
@test occursin("a[1, 2]", err_str)
@test occursin("rather than a[1][2]", err_str)
end

let err_str
d = Dict
let d = Dict
err_str = @except_str (d[1] = 5) MethodError
@test occursin("\nYou attempted to index the type Dict, rather than an instance of the type. Make sure you create the type using its constructor: ", err_str)
@test occursin("d = Dict([...])", err_str)
@test occursin(" rather than d = Dict", err_str)
end

let s = Some("foo")
err_str = @except_str (s[] = "bar") MethodError
@test !occursin("You attempted to index the type String", err_str)
end

# Execute backtrace once before checking formatting, see #38858
backtrace()

Expand Down

0 comments on commit a45d701

Please sign in to comment.