Skip to content

Commit

Permalink
document module docstring scope (JuliaLang#54429)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpapp authored and lazarusA committed Jul 12, 2024
1 parent 24d824c commit e4fe543
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 14 additions & 4 deletions doc/src/manual/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,20 @@ end
Adds docstring `"..."` to the `Module` `M`. Adding the docstring above the `Module` is the preferred
syntax, however both are equivalent.

The module docstring is evaluated *inside* the scope of the module, allowing
access to all the symbols defined in and imported into the module:

```julia
"The magic number is $(MAGIC)."
module DocStringEval
const MAGIC = 42
end
```

Documenting a `baremodule` by placing a docstring above the expression automatically imports
`@doc` into the module. These imports must be done manually when the module expression is not
documented:

```julia
"..."
baremodule M
Expand All @@ -527,10 +541,6 @@ f(x) = x
end
```

Documenting a `baremodule` by placing a docstring above the expression automatically imports
`@doc` into the module. These imports must be done manually when the module expression is not
documented.

### Global Variables

```julia
Expand Down
4 changes: 2 additions & 2 deletions test/docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ module NoDocStrings end
# General tests for docstrings.

const LINE_NUMBER = @__LINE__() + 1
"DocsTest"
"DocsTest, evaluating $(K)" # test that module docstring is evaluated within module
module DocsTest

using Markdown
Expand Down Expand Up @@ -265,7 +265,7 @@ fnospecialize(@nospecialize(x::AbstractArray)) = 2
end

let md = meta(DocsTest)[@var(DocsTest)]
@test docstrings_equal(md.docs[Union{}], doc"DocsTest")
@test docstrings_equal(md.docs[Union{}], doc"DocsTest, evaluating K")
# Check that plain docstrings store a module reference.
# https://github.com/JuliaLang/julia/pull/13017#issuecomment-138618663
@test md.docs[Union{}].data[:module] == DocsTest
Expand Down

0 comments on commit e4fe543

Please sign in to comment.