-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Writing testable documentation examples for proc macros is not possible #58700
Comments
Is #58696 also related? If rustdoc would properly inline the docs I'd expect doctests to also "just work". I've also hit this issue with a custom derive I'm currently working on, but since those aren't really named items putting docs and examples on the derivable trait instead seems like the best solution for those. |
@jonas-schievink Yes, good find, it's indeed very related. Although I'm not sure if a working |
Split off from rust-lang#62855 Currently, rustdoc ignores any doc comments found on 'pub use' statements. As described in issue rust-lang#58700, this makes it impossible to properly document procedural macros. Any doc comments must be written on the procedural macro definition, which must occur in a dedicated proc-macro crate. This means that any doc comments or doc tests cannot reference items defined in re-exporting crate, despite the fact that such items may be required to use the procedural macro. To solve this issue, this commit allows doc comments to be written on 'pub use' statements. For consistency, this applies to *all* 'pub use' statements, not just those importing procedural macros. When inlining documentation, documentation on 'pub use' statements will be prepended to the documentation of the inlined item. For example, the following items: ```rust mod other_mod { /// Doc comment from definition pub struct MyStruct; } /// Doc comment from 'pub use' /// pub use other_mod::MyStruct; ``` will caues the documentation for the re-export of 'MyStruct' to be rendered as: ``` Doc comment from 'pub use' Doc comment from definition ``` Note the empty line in the 'pub use' doc comments - because doc comments are concatenated as-is, this ensure that the doc comments on the definition start on a new line.
…laumeGomez Use doc comments from 'pub use' statements Split off from #62855 Currently, rustdoc ignores any doc comments found on 'pub use' statements. As described in issue #58700, this makes it impossible to properly document procedural macros. Any doc comments must be written on the procedural macro definition, which must occur in a dedicated proc-macro crate. This means that any doc comments or doc tests cannot reference items defined in re-exporting crate, despite the fact that such items may be required to use the procedural macro. To solve this issue, this commit allows doc comments to be written on 'pub use' statements. For consistency, this applies to *all* 'pub use' statements, not just those importing procedural macros. When inlining documentation, documentation on 'pub use' statements will be prepended to the documentation of the inlined item. For example, the following items: ```rust mod other_mod { /// Doc comment from definition pub struct MyStruct; } /// Doc comment from 'pub use' /// pub use other_mod::MyStruct; ``` will caues the documentation for the re-export of 'MyStruct' to be rendered as: ``` Doc comment from 'pub use' Doc comment from definition ``` Note the empty line in the 'pub use' doc comments - because doc comments are concatenated as-is, this ensure that the doc comments on the definition start on a new line.
…final, r=petrochenkov Improve Rustdoc's handling of procedural macros Fixes rust-lang#58700 Fixes rust-lang#58696 Fixes rust-lang#49553 Fixes rust-lang#52210 This commit removes the special rustdoc handling for proc macros, as we can now retrieve their span and attributes just like any other item. A new command-line option is added to rustdoc: `--crate-type`. This takes the same options as rustc's `--crate-type` option. However, all values other than `proc-macro` are treated the same. This allows Rustdoc to enable 'proc macro mode' when handling a proc macro crate. In compiletest, a new 'rustdoc-flags' option is added. This allows us to pass in the '--proc-macro-crate' flag in the absence of Cargo. I've opened [an additional PR to Cargo](rust-lang/cargo#7159) to support passing in this flag. These two PRS can be merged in any order - the Cargo changes will not take effect until the 'cargo' submodule is updated in this repository.
…final, r=petrochenkov Improve Rustdoc's handling of procedural macros Fixes rust-lang#58700 Fixes rust-lang#58696 Fixes rust-lang#49553 Fixes rust-lang#52210 This commit removes the special rustdoc handling for proc macros, as we can now retrieve their span and attributes just like any other item. A new command-line option is added to rustdoc: `--crate-type`. This takes the same options as rustc's `--crate-type` option. However, all values other than `proc-macro` are treated the same. This allows Rustdoc to enable 'proc macro mode' when handling a proc macro crate. In compiletest, a new 'rustdoc-flags' option is added. This allows us to pass in the '--proc-macro-crate' flag in the absence of Cargo. I've opened [an additional PR to Cargo](rust-lang/cargo#7159) to support passing in this flag. These two PRS can be merged in any order - the Cargo changes will not take effect until the 'cargo' submodule is updated in this repository.
…trochenkov Improve Rustdoc's handling of procedural macros Fixes #58700 Fixes #58696 Fixes #49553 Fixes #52210 This commit removes the special rustdoc handling for proc macros, as we can now retrieve their span and attributes just like any other item. A new command-line option is added to rustdoc: `--crate-type`. This takes the same options as rustc's `--crate-type` option. However, all values other than `proc-macro` are treated the same. This allows Rustdoc to enable 'proc macro mode' when handling a proc macro crate. In compiletest, a new 'rustdoc-flags' option is added. This allows us to pass in the '--proc-macro-crate' flag in the absence of Cargo. I've opened [an additional PR to Cargo](rust-lang/cargo#7159) to support passing in this flag. These two PRS can be merged in any order - the Cargo changes will not take effect until the 'cargo' submodule is updated in this repository.
This moves documentation of derive macros from the trait to the reexport. And it removes the dirty hack for `mesh!`. This was possible because this issue was closed: rust-lang/rust#58700
Suppose our main crate
foo
defines a traitFoo
. Now we want to write aderive(Foo)
proc macro (or any other kind of proc macro which emits code usingFoo
). We define that proc macro in thefoo-macro
crate. Our main cratefoo
wants to rexport the proc macro so that users only have to depend onfoo
.The code for this example:
Cargo.toml
src/lib.rs
foo-macro/Cargo.toml
foo-macro/src/lib.rs
The question is now: how to document the custom derive in a way such that
cargo test
can test included example code?. I don't think it's currently possible.If we document the function
derive_foo
in the proc macro crate, we have to make all examplesignore
because they cannot be compiled, because compiling them would require using thefoo
crate. But this leads to a cyclic dependency (not evendev-dependencies
works).foo-macros
cannot depend onfoo
.But we also can't document the reexport (that documentation is ignored/not shown). So it's not possible to write the documentation in the
foo
crate either. As a consequence, we don't have checked documentation tests -- which probably leads to stale and incorrect example code.Now I can think of three workarounds:
#[cfg(not(rustdoc))]
to the reexport, and add some other item with#[cfg(rustdoc)]
and the actual documentation to yourfoo
crate. That way, the reexport doesn't happen when rustdoc generates the documentation and instead renders the other dummy item with the correct documentation. For function like proc macros, it might be fine to have amacro_rules
macro as dummy item, but for other kinds of proc macros, this... absolutely not nice.foo
crate (likeno_macro_reexport
). When that feature is activated, your main crate does not reexport macros and does not depend on thefoo-macro
crate. Now thefoo-macro
crate can depend onfoo
viadev-dependencies
and activate thatno_macro_reexport
feature. That prevents the cyclic dependency. But now your main crate has a feature that should be implementation detail and you often have to build your main crate twice.serde
does it). I don't think that's a good solution at all: rustdoc should be able to also properly document proc macros.So neither of these workarounds is particularly nice. I guess it's clear that there should be some kind of solution to this.
Potentially related:
$crate
) #54363The text was updated successfully, but these errors were encountered: