@@ -446,6 +446,52 @@ more than one expression is marked then the same docstring is applied to each ex
446
446
end
447
447
448
448
`@__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
+ ```
449
495
"""
450
496
:(Core. @__doc__ )
451
497
0 commit comments