JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.72k stars 5.48k forks source link

Help mode's field access hints are strange #51180

Open LilithHafner opened 1 year ago

LilithHafner commented 1 year ago

I don't really see a pattern of when the REPL provides useful hints on this sort of field access and when it throws what appears to be an internal error:

julia> module M
           struct T 
               "a field with a docstring"
               my_field
           end
           const C = T
           f() = T
       end
WARNING: replacing module M.
Main.M

help?> M.T.field
  Main.M.T has fields my_field.

help?> M.C.field
  Main.M.T has fields my_field.

help?> M.f().field
ERROR: MethodError: no method matching Base.Docs.Binding(::Type{Main.M.T}, ::Symbol)

Closest candidates are:
  Base.Docs.Binding(::Module, ::Symbol)
   @ Base docs/bindings.jl:9

Stacktrace:
 [1] top-level scope
   @ none:1

help?> M.T(1).field
ERROR: MethodError: no method matching Base.Docs.Binding(::Main.M.T, ::Symbol)

Closest candidates are:
  Base.Docs.Binding(::Module, ::Symbol)
   @ Base docs/bindings.jl:9

Stacktrace:
 [1] top-level scope
   @ none:1

help?> M.T(1).my_field
ERROR: MethodError: no method matching Base.Docs.Binding(::Main.M.T, ::Symbol)

Closest candidates are:
  Base.Docs.Binding(::Module, ::Symbol)
   @ Base docs/bindings.jl:9

Stacktrace:
 [1] top-level scope
   @ none:1

julia> x = M.T(1)
Main.M.T(1)

help?> x.field
ERROR: MethodError: no method matching Base.Docs.Binding(::Main.M.T, ::Symbol)

Closest candidates are:
  Base.Docs.Binding(::Module, ::Symbol)
   @ Base docs/bindings.jl:9

Stacktrace:
 [1] top-level scope
   @ ~/.julia/juliaup/julia-1.10.0-beta2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/docview.jl:558
JeffBezanson commented 1 year ago

The method errors should be fixed. Other than that it looks like it basically requires everything in the chain to be constant?

baggepinnen commented 1 year ago

It looks like the docstring for a field is only accessible through the REPL help mode if there's also a docstring for the type.

Without docstring for type:

/home/fredrikb> julia +beta --startup-file=no 
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _' |  |
  | | |_| | | | (_| |  |  Version 1.10.0-beta3 (2023-10-03)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> struct T
           "x"
           x
           "y"
           y
       end

help?> T.x
  T has fields x, and y.

With docstring for type:

julia> "..."
       struct T
           "x"
           x
           "y"
           y
       end
T

help?> T.x
  x

With typed field

If I further add a type parameter for the field x, I instead get the error from the OP

julia> struct T{X}
           "x"
           x::X
           "y"
           y
       end

help?> T.x
ERROR: MethodError: no method matching Base.Docs.Binding(::Type{T}, ::Symbol)

Closest candidates are:
  Base.Docs.Binding(::Module, ::Symbol)
   @ Base docs/bindings.jl:9

Stacktrace:
 [1] top-level scope
   @ ~/.julia/juliaup/julia-1.10.0-beta3+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/docview.jl:558
LilithHafner commented 1 year ago

Thanks for looking into this! Those results seem a little buggy.