DocStringExtensions.jl icon indicating copy to clipboard operation
DocStringExtensions.jl copied to clipboard

TYPEDSIGNATURES error with @generated functions

Open tomerarnon opened this issue 2 years ago • 3 comments

This happens whether the function is @generated, or defined with if @generated ... method.

julia> using DocStringExtensions

julia> begin
       """
           $TYPEDSIGNATURES
       example
       """
       @generated function example(x)
           :(return x)
       end
       end
example

help?> example
search: example

ERROR: BoundsError: attempt to access Core.SimpleVector at index [2]
Stacktrace:
  [1] getindex
    @ ./essentials.jl:697 [inlined]
  [2] may_invoke_generator(method::Method, atype::Any, sparams::Core.SimpleVector)
    @ Base ./reflection.jl:1166
  [3] func_for_method_checked
    @ ./reflection.jl:1188 [inlined]
  [4] return_types(f::Any, types::Any; world::UInt64, interp::Core.Compiler.NativeInterpreter)
    @ Base ./reflection.jl:1415
  [5] return_types
    @ ./reflection.jl:1398 [inlined]
  [6] printmethod(buffer::IOBuffer, binding::Base.Docs.Binding, func::Function, method::Method, typesig::Type)
    @ DocStringExtensions ~/.julia/packages/DocStringExtensions/JVu77/src/utilities.jl:363
  [7] format(#unused#::DocStringExtensions.TypedMethodSignatures, buf::IOBuffer, doc::Base.Docs.DocStr)
    @ DocStringExtensions ~/.julia/packages/DocStringExtensions/JVu77/src/abbreviations.jl:398
  [8] formatdoc(buf::IOBuffer, doc::Base.Docs.DocStr, part::DocStringExtensions.TypedMethodSignatures)
    @ DocStringExtensions ~/.julia/packages/DocStringExtensions/JVu77/src/abbreviations.jl:25
  [9] formatdoc(d::Base.Docs.DocStr)
    @ REPL /Applications/Julia-1.9.2.app/Contents/Resources/julia/share/julia/stdlib/v1.9/REPL/src/docview.jl:88
 [10] parsedoc(d::Base.Docs.DocStr)
    @ REPL /Applications/Julia-1.9.2.app/Contents/Resources/julia/share/julia/stdlib/v1.9/REPL/src/docview.jl:96
 [11] map!(f::typeof(Base.Docs.parsedoc), dest::Vector{Any}, A::Vector{Base.Docs.DocStr})
    @ Base ./abstractarray.jl:3254
 [12] mapany(f::Function, A::Vector{Base.Docs.DocStr})
    @ Base ./abstractarray.jl:3263
 [13] doc(binding::Base.Docs.Binding, sig::Type)
    @ REPL /Applications/Julia-1.9.2.app/Contents/Resources/julia/share/julia/stdlib/v1.9/REPL/src/docview.jl:193
 [14] doc(binding::Base.Docs.Binding)
    @ REPL /Applications/Julia-1.9.2.app/Contents/Resources/julia/share/julia/stdlib/v1.9/REPL/src/docview.jl:160
 [15] top-level scope
    @ /Applications/Julia-1.9.2.app/Contents/Resources/julia/share/julia/stdlib/v1.9/REPL/src/docview.jl:481

SIGNATURES is fine:


julia> begin
       """    
           $SIGNATURES
       example
       """
       @generated function example(x)
           :(return x)
       end
       end
example

help?> example
search: example

  example(x)
  

  example

In my real example, this is happening because I have a template which splices $TYPEDSIGNATURES for all methods. I would indeed like to show the type annotation of the generated function if possible.

tomerarnon avatar Oct 05 '23 01:10 tomerarnon

Looks like this is failing in Base.return_types. What happens if you instead of documenting the generated function directly wrap it in an "outer" function like so:

@generated function _example(x)
    :(return x)
end

"""
    $TYPEDSIGNATURES

example
"""
example(x) = _example(x)

Slightly more indirection, I know, but might be a solution until the bug can be fixed.

MichaelHatherly avatar Oct 05 '23 07:10 MichaelHatherly

That indirection does circumvent the error, thanks!

tomerarnon avatar Oct 05 '23 18:10 tomerarnon

Great, we'll leave this open as a hint to future users, and a reminder to work out some kind of fix if possible.

MichaelHatherly avatar Oct 05 '23 19:10 MichaelHatherly