@@ -396,8 +396,22 @@ function objectdoc(__source__, __module__, str, def, expr, sig = :(Union{}))
396396 @nospecialize str def expr sig
397397 binding = esc (bindingexpr (namify (expr)))
398398 docstr = esc (docexpr (__source__, __module__, lazy_iterpolate (str), metadata (__source__, __module__, expr, false )))
399- # Note: we want to avoid introducing line number nodes here (issue #24468)
400- return Expr (:block , esc (def), :($ (doc!)($ __module__, $ binding, $ docstr, $ (esc (sig)))))
399+ # Store the result of the definition and return it after documenting
400+ docex = :($ (doc!)($ __module__, $ binding, $ docstr, $ (esc (sig))))
401+ if def === nothing
402+ return Expr (:block , docex)
403+ else
404+ exdef = esc (def)
405+ if isexpr (def, :global , 1 ) && def. args[1 ] isa Union{Symbol,GlobalRef}
406+ # Special case: `global x` should return nothing to avoid syntax errors with assigning to a value
407+ val = nothing
408+ else
409+ val = :val
410+ exdef = Expr (:(= ), val, exdef)
411+ end
412+ # Note: we want to avoid introducing line number nodes here (issue #24468) for def
413+ return Expr (:block , exdef, docex, val)
414+ end
401415end
402416
403417function calldoc (__source__, __module__, str, def:: Expr )
@@ -431,7 +445,9 @@ function moduledoc(__source__, __module__, meta, def, def′::Expr)
431445 end
432446end
433447
434- # Shares a single doc, `meta`, between several expressions from the tuple expression `ex`.
448+ # Shares a single doc, `meta`, between several expressions from the tuple expression `ex`
449+ # (but don't actually create the tuple for the result and just return the final one,
450+ # as if this was a C++ comma operator or a block separated by `;` instead of `,`).
435451function multidoc (__source__, __module__, meta, ex:: Expr , define:: Bool )
436452 @nospecialize meta
437453 out = Expr (:block )
@@ -657,7 +673,7 @@ docm(source::LineNumberNode, mod::Module, _, _, x...) = docm(source, mod, x...)
657673# also part of a :where expression, so it unwraps the :where layers until it reaches the
658674# "actual" expression
659675iscallexpr (ex:: Expr ) = isexpr (ex, :where ) ? iscallexpr (ex. args[1 ]) : isexpr (ex, :call )
660- iscallexpr (ex) = false
676+ iscallexpr (@nospecialize ex) = false
661677
662678function docm (source:: LineNumberNode , mod:: Module , meta, ex, define:: Bool = true )
663679 @nospecialize meta ex
@@ -722,7 +738,7 @@ function _docm(source::LineNumberNode, mod::Module, meta, x, define::Bool = true
722738 # f(::T, ::U) where T where U
723739 #
724740 isexpr (x, FUNC_HEADS) && is_signature ((x:: Expr ). args[1 ]) ? objectdoc (source, mod, meta, def, x:: Expr , signature (x:: Expr )) :
725- isexpr (x, [ :function , :macro ]) && ! isexpr ((x:: Expr ). args[1 ], :call ) ? objectdoc (source, mod, meta, def, x:: Expr ) :
741+ ( isexpr (x, :function ) || isexpr (x , :macro )) && ! isexpr ((x:: Expr ). args[1 ], :call ) ? objectdoc (source, mod, meta, def, x:: Expr ) :
726742 iscallexpr (x) ? calldoc (source, mod, meta, x:: Expr ) :
727743
728744 # Type definitions.
@@ -742,7 +758,7 @@ function _docm(source::LineNumberNode, mod::Module, meta, x, define::Bool = true
742758 isexpr (x, BINDING_HEADS) && ! isexpr ((x:: Expr ). args[1 ], :call ) ? objectdoc (source, mod, meta, def, x:: Expr ) :
743759
744760 # Quoted macrocall syntax. `:@time` / `:(Base.@time)`.
745- isquotedmacrocall (x) ? objectdoc (source, mod, meta, def , x) :
761+ isquotedmacrocall (x) ? objectdoc (source, mod, meta, nothing , x) :
746762 # Modules and baremodules.
747763 isexpr (x, :module ) ? moduledoc (source, mod, meta, def, x:: Expr ) :
748764 # Document several expressions with the same docstring. `a, b, c`.
0 commit comments