Skip to content

Commit

Permalink
fix help?> StructType.field command (#57107)
Browse files Browse the repository at this point in the history
`help?> StructType.field` currently errors when `StructType` doesn't
have any field documentation at all. This commit adds a proper guard
against such cases.
  • Loading branch information
aviatesk authored Jan 21, 2025
1 parent 2e6ffbc commit 90d346f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
18 changes: 11 additions & 7 deletions stdlib/REPL/src/docview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -662,13 +662,17 @@ function fielddoc(binding::Binding, field::Symbol)
for mod in modules
dict = meta(mod; autoinit=false)
isnothing(dict) && continue
if haskey(dict, binding)
multidoc = dict[binding]
if haskey(multidoc.docs, Union{})
fields = multidoc.docs[Union{}].data[:fields]
if haskey(fields, field)
doc = fields[field]
return isa(doc, Markdown.MD) ? doc : Markdown.parse(doc)
multidoc = get(dict, binding, nothing)
if multidoc !== nothing
structdoc = get(multidoc.docs, Union{}, nothing)
if structdoc !== nothing
fieldsdoc = get(structdoc.data, :fields, nothing)
if fieldsdoc !== nothing
fielddoc = get(fieldsdoc, field, nothing)
if fielddoc !== nothing
return isa(fielddoc, Markdown.MD) ?
fielddoc : Markdown.parse(fielddoc)
end
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions stdlib/REPL/test/docview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ end
@test endswith(get_help_standard("StructWithOneField.not_a_field"), "StructWithOneField` has field `field1`.\n")
@test endswith(get_help_standard("StructWithTwoFields.not_a_field"), "StructWithTwoFields` has fields `field1`, and `field2`.\n")
@test endswith(get_help_standard("StructWithThreeFields.not_a_field"), "StructWithThreeFields` has fields `field1`, `field2`, and `field3`.\n")

# Shouldn't error if the struct doesn't have any field documentations at all.
@test endswith(get_help_standard("Int.not_a_field"), "`$Int` has no fields.\n")
end

module InternalWarningsTests
Expand Down

0 comments on commit 90d346f

Please sign in to comment.