Skip to content

Commit 31bc1b7

Browse files
MasonProtterKristofferC
authored andcommitted
Use type wrapper directly rather than typename in FieldError (#58507)
(cherry picked from commit 347fb7c)
1 parent bc65d98 commit 31bc1b7

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

base/errorshow.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ end
378378

379379
function showerror(io::IO, exc::FieldError)
380380
@nospecialize
381-
print(io, "FieldError: type $(exc.type |> nameof) has no field `$(exc.field)`")
381+
print(io, "FieldError: type $(exc.type.name.wrapper) has no field `$(exc.field)`")
382382
Base.Experimental.show_error_hints(io, exc)
383383
end
384384

@@ -1117,7 +1117,7 @@ Experimental.register_error_hint(fielderror_dict_hint_handler, FieldError)
11171117
function fielderror_listfields_hint_handler(io, exc)
11181118
fields = fieldnames(exc.type)
11191119
if isempty(fields)
1120-
print(io, "; $(nameof(exc.type)) has no fields at all.")
1120+
print(io, "; $(exc.type.name.wrapper) has no fields at all.")
11211121
else
11221122
print(io, ", available fields: $(join(map(k -> "`$k`", fields), ", "))")
11231123
end

test/errorshow.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,8 @@ end
852852

853853
# Check error message first
854854
errorMsg = sprint(Base.showerror, ex)
855-
@test occursin("FieldError: type FieldFoo has no field `c`", errorMsg)
855+
@test occursin("FieldError: type", errorMsg)
856+
@test occursin("FieldFoo has no field `c`", errorMsg)
856857
@test occursin("available fields: `a`, `b`", errorMsg)
857858
@test occursin("Available properties: `x`, `y`", errorMsg)
858859

@@ -877,6 +878,24 @@ end
877878
@test occursin(hintExpected, errorMsg)
878879
end
879880

881+
module FieldErrorTest
882+
struct Point end
883+
p = Point()
884+
end
885+
886+
@testset "FieldError with changing fields" begin
887+
# https://discourse.julialang.org/t/better-error-message-for-modified-structs-in-julia-1-12/129265
888+
err_str1 = @except_str FieldErrorTest.p.x FieldError
889+
@test occursin("FieldErrorTest.Point", err_str1)
890+
@eval FieldErrorTest struct Point{T}
891+
x::T
892+
y::T
893+
end
894+
err_str2 = @except_str FieldErrorTest.p.x FieldError
895+
@test occursin("@world", err_str2)
896+
@test occursin("FieldErrorTest.Point", err_str2)
897+
end
898+
880899
# UndefVar error hints
881900
module A53000
882901
export f

0 commit comments

Comments
 (0)