Skip to content

Use type wrapper directly rather than typename in FieldError #58507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ end

function showerror(io::IO, exc::FieldError)
@nospecialize
print(io, "FieldError: type $(exc.type |> nameof) has no field `$(exc.field)`")
print(io, "FieldError: type $(exc.type.name.wrapper) has no field `$(exc.field)`")
Base.Experimental.show_error_hints(io, exc)
end

Expand Down Expand Up @@ -1127,7 +1127,7 @@ Experimental.register_error_hint(fielderror_dict_hint_handler, FieldError)
function fielderror_listfields_hint_handler(io, exc)
fields = fieldnames(exc.type)
if isempty(fields)
print(io, "; $(nameof(exc.type)) has no fields at all.")
print(io, "; $(exc.type.name.wrapper) has no fields at all.")
else
print(io, ", available fields: $(join(map(k -> "`$k`", fields), ", "))")
end
Expand Down
18 changes: 18 additions & 0 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,24 @@ end
@test occursin(hintExpected, errorMsg)
end

module FieldErrorTest
struct Point end
p = Point()
end

@testset "FieldError with changing fields" begin
# https://discourse.julialang.org/t/better-error-message-for-modified-structs-in-julia-1-12/129265
err_str1 = @except_str FieldErrorTest.p.x FieldError
@test occursin("FieldErrorTest.Point", err_str1)
@eval FieldErrorTest struct Point{T}
x::T
y::T
end
err_str2 = @except_str FieldErrorTest.p.x FieldError
@test occursin("@world", err_str2)
@test occursin("FieldErrorTest.Point", err_str2)
end

# UndefVar error hints
module A53000
export f
Expand Down