Skip to content

Macros in conditional blocks lose their docstrings #33900

@timholy

Description

@timholy

On Julia master (so the conditional below evaluates as true).

This works:

julia> module MyMod

       """
       A docstring
       """
       macro mymac(ex)
           return esc(ex)
       end

       end
Main.MyMod

help?> MyMod.@mymac
  A docstring

But it doesn't if we put it inside a conditional expression (e.g., dependent on the Julia version):

julia> module MyMod

       if VERSION >= v"1.2.0"
       """
       A docstring
       """
       macro mymac(ex)
           return esc(ex)
       end
       end

       end
WARNING: replacing module MyMod.
Main.MyMod

help?> MyMod.@mymac
  No documentation found.

  Main.MyMod.@mymac is a macro.

  # 1 method for macro "@mymac":
  [1] @mymac(__source__::LineNumberNode, __module__::Module, ex) in Main.MyMod at REPL[3]:8

However we can fix it with the post-hoc @doc mechanism:

julia> module MyMod

       if VERSION >= v"1.2.0"
       macro mymac(ex)
           return esc(ex)
       end
       @doc "A docstring" :(MyMod.@mymac)
       end

       end
WARNING: replacing module MyMod.
Main.MyMod

help?> MyMod.@mymac
  A docstring

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsystemThe documentation building system

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions