Skip to content
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

idea: new syntax #4

Open
sam0x17 opened this issue Jul 31, 2023 · 4 comments
Open

idea: new syntax #4

sam0x17 opened this issue Jul 31, 2023 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@sam0x17
Copy link
Owner

sam0x17 commented Jul 31, 2023

Yandros proposed a new syntax that looks like this:

#[docify::docify]
/// some comments some comments
/// @embed("examples/samples.rs", MyCoolStruct)
/// another example:
/// @embed("src/something.rs", MyThing)
/// some closing comments
pub struct Foo;

Originally mentioned here: paritytech/substrate#14672 (comment)

If we were to go with this syntax, we would finally be able to limit expansion of docify embeds to only be attempted if the caller is #[cfg(doc)] which we currently lack the context information to be able to do, and which is currently impossible in #[doc = ..] position but is possible for an "outer attribute" approach like this.

@yjhmelody
Copy link

Is there any way to bootstrap? Use this crate to improve documentation for itself.

@sam0x17
Copy link
Owner Author

sam0x17 commented Nov 16, 2023

Yeah I'm planning to make use of the new features within docify as they get added. The tricky thing is it's not actually possible to use all of docify fully within docify at least on the macro side of the crate because you end up with a circular dependency. This is why most of the tests are hoisted to the top level of the crate.

@kianenigma
Copy link

similar issue mersinvald/aquamarine#5

@sam0x17
Copy link
Owner Author

sam0x17 commented Mar 29, 2024

btw, yes it seems to be a fundamental issue in rust atm that you can't generate an inner attribute from a macro. If we could write custom inner attributes this would be different, but we cannot.

even if you put a macro like this in another crate:

macro_rules! my_macro {
    () => {
        //! hey 
    }
}

and then in the main crate call foo::my_macro!(); as the first line, it won't work because the macro invocation itself already puts us in item position, and there is no way to do custom inner attributes

update: MIGHT be possible if we remove the semi-colon and try to force the macro to not be item position

error: macro rhs must be delimited
 --> bar/src/lib.rs:3:11
  |
3 |     () => //! hey
  |           ^^^^^^^

error: could not document `bar`

so decl macros can't do it, will try proc

nope, totally impossible:

#[proc_macro]
pub fn my_macro(_: TokenStream) -> TokenStream {
    "//! hey".parse().unwrap()
}
bar::my_macro!()
bar::my_macro! {}
bar::my_macro!();
error[E0753]: expected outer doc comment
 --> src/lib.rs:1:1
  |
1 | bar::my_macro! {}
  | ^^^^^^^^^^^^^^^^^
error: macros that expand to items must be delimited with braces or followed by a semicolon
 --> src/lib.rs:1:15
  |
1 | bar::my_macro!()
  |               ^^
error: expected item after doc comment
 --> src/lib.rs:1:1
  |
1 | bar::my_macro!();
  | ^^^^^^^^^^^^^^^^ this doc comment doesn't document anything
  |
  = note: this error originates in the macro `bar::my_macro` (in Nightly builds, run with -Z macro-backtrace for more info)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants