Skip to content

Commit 1f80f17

Browse files
committed
Add more docs and tests
1 parent 62ffcfb commit 1f80f17

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

NEWS.md

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ Language changes
1717
may pave the way for inference to be able to intelligently re-use the old
1818
results, once the new method is deleted. ([#53415])
1919

20+
- Macro expansion will no longer eargerly recurse into into `Expr(:toplevel)`
21+
expressions returned from macros. Instead, macro expansion of `:toplevel`
22+
expressions will be delayed until evaluation time. This allows a later
23+
expression within a given `:toplevel` expression to make use of macros
24+
defined earlier in the same `:toplevel` expression. ([#53515])
25+
2026
Compiler/Runtime improvements
2127
-----------------------------
2228

base/docs/Docs.jl

+46
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,52 @@ more than one expression is marked then the same docstring is applied to each ex
446446
end
447447
448448
`@__doc__` has no effect when a macro that uses it is not documented.
449+
450+
!!! compat "Julia 1.12"
451+
452+
This section documents a very subtle corner case that is only relevant to
453+
macros which themselves both define other macros and then attempt to use them
454+
within the same expansion. Such macros were impossible to write prior to
455+
Julia 1.12 and are still quite rare. If you are not writing such a macro,
456+
you may ignore this note.
457+
458+
In versions prior to Julia 1.12, macroexpansion would recursively expand through
459+
`Expr(:toplevel)` blocks. This behavior was changed in Julia 1.12 to allow
460+
macros to recursively define other macros and use them in the same returned
461+
expression. However, to preserve backwards compatibility with existing uses of
462+
`@__doc__`, the doc system will still expand through `Expr(:toplevel)` blocks
463+
when looking for `@__doc__` markers. As a result, macro-defining-macros will
464+
have an observable behavior difference when annotated with a docstring:
465+
466+
```julia
467+
julia> macro macroception()
468+
Expr(:toplevel, :(macro foo() 1 end), :(@foo))
469+
end
470+
471+
julia> @macroception
472+
1
473+
474+
julia> "Docstring" @macroception
475+
ERROR: LoadError: UndefVarError: `@foo` not defined in `Main`
476+
```
477+
478+
The supported workaround is to manually expand the `@__doc__` macro in the
479+
defining macro, which the docsystem will recognize and suppress the recursive
480+
expansion:
481+
482+
```julia
483+
julia> macro macroception()
484+
Expr(:toplevel,
485+
macroexpand(__module__, :(@__doc__ macro foo() 1 end); recursive=false),
486+
:(@foo))
487+
end
488+
489+
julia> @macroception
490+
1
491+
492+
julia> "Docstring" @macroception
493+
1
494+
```
449495
"""
450496
:(Core.@__doc__)
451497

test/docs.jl

+12
Original file line numberDiff line numberDiff line change
@@ -1562,3 +1562,15 @@ Base.@ccallable c51586_long()::Int = 3
15621562
@test_broken isempty(undoc)
15631563
@test undoc == [Symbol("@var")]
15641564
end
1565+
1566+
# Docing the macroception macro
1567+
macro docmacroception()
1568+
Expr(:toplevel, macroexpand(__module__, :(@Base.__doc__ macro docmacrofoo() 1 end); recursive=false), :(@docmacrofoo))
1569+
end
1570+
1571+
"""
1572+
This docmacroception has a docstring
1573+
"""
1574+
@docmacroception()
1575+
1576+
@test Docs.hasdoc(@__MODULE__, :var"@docmacrofoo")

0 commit comments

Comments
 (0)