Skip to content

Commit 347fb7c

Browse files
authored
Use type wrapper directly rather than typename in FieldError (#58507)
1 parent 2a9f33c commit 347fb7c

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
@@ -382,7 +382,7 @@ end
382382

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

@@ -1127,7 +1127,7 @@ Experimental.register_error_hint(fielderror_dict_hint_handler, FieldError)
11271127
function fielderror_listfields_hint_handler(io, exc)
11281128
fields = fieldnames(exc.type)
11291129
if isempty(fields)
1130-
print(io, "; $(nameof(exc.type)) has no fields at all.")
1130+
print(io, "; $(exc.type.name.wrapper) has no fields at all.")
11311131
else
11321132
print(io, ", available fields: $(join(map(k -> "`$k`", fields), ", "))")
11331133
end

test/errorshow.jl

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

858858
# Check error message first
859859
errorMsg = sprint(Base.showerror, ex)
860-
@test occursin("FieldError: type FieldFoo has no field `c`", errorMsg)
860+
@test occursin("FieldError: type", errorMsg)
861+
@test occursin("FieldFoo has no field `c`", errorMsg)
861862
@test occursin("available fields: `a`, `b`", errorMsg)
862863
@test occursin("Available properties: `x`, `y`", errorMsg)
863864

@@ -882,6 +883,24 @@ end
882883
@test occursin(hintExpected, errorMsg)
883884
end
884885

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

0 commit comments

Comments
 (0)