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

rustdoc: show macro interface but not implementation #17616

Closed
kmcallister opened this issue Sep 28, 2014 · 11 comments
Closed

rustdoc: show macro interface but not implementation #17616

kmcallister opened this issue Sep 28, 2014 · 11 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@kmcallister
Copy link
Contributor

e.g. http://doc.rust-lang.org/std/macro.bitflags!.html has 120 lines of macro code before anything useful.

@kmcallister kmcallister added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Sep 28, 2014
@gamazeps
Copy link
Contributor

Hi, i'm a bit new to rustdoc (but willing to learn ;) )
How would you change the order of the doc and the code, as the doc is already written before the code in http://doc.rust-lang.org/src/std/home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/libstd/bitflags.rs.html ?

@jdm
Copy link
Contributor

jdm commented Sep 29, 2014

You would probably want to reverse the order of calls in item_macro in html/render.rs (http://mxr.mozilla.org/rust/source/src/librustdoc/html/render.rs#2183).

(For the record, I determined this by grepping the librustdoc for the word 'macro')

@gamazeps
Copy link
Contributor

Thx :)
I checked the doc of another file (c_str.rs) and the only visible difference betwen the two of them was the fact that the doc was written with instead /*! ... */ of ///.

But looking at other macros in std, it seems that the code beeing present before the doc is a recurring issue...

Anyways, thaks a lot for the answer (you kinda fixed the issue yourself ^^)

@gamazeps
Copy link
Contributor

Switching the two function calls somehow works, yet the traits, methods and example stay after the implementation. Is that good eough @kmcallister ?
I'll look into it a little more tonight

gamazeps added a commit to gamazeps/rust that referenced this issue Oct 1, 2014
May be better to also put the traits and methods before ...
Closes rust-lang#17616
@gamazeps
Copy link
Contributor

gamazeps commented Oct 1, 2014

@kmcallister look at @alexcrichton comment on the PR, this issue can be closed

@kmcallister
Copy link
Contributor Author

cc @alexcrichton

If the rule is that definition always comes before docs, then it's a problem that we document macros like structs or enums rather than like functions. We certainly don't want to put the full source of every function before its description.

And I think there will be plenty of macros as large as bitflags!. A sophisticated macro abstracts in the same way a function does; you don't need to understand the full implementation to use it. If we put the full implementation in rustdoc, that suggests that macros are merely for convenience in abbreviating code, which limits the usefulness of the feature.

Maybe the simple fix is a #[doc(summarize)] attribute for macro_rules! that just leaves off the RHS (which is still visible through the "src" link.)

@alexcrichton
Copy link
Member

While we don't put the full source of functions/items/etc, we do put their interface. The interface of a macro is how you invoke it, which is the bare minimum of what must be displayed, and currently it does this by displaying the whole macro. A better solution to that problem in my opinion would be to only show the arms of the macro that provide information about how to invoke it, not what it generates.

@kmcallister
Copy link
Contributor Author

A better solution to that problem in my opinion would be to only show the arms of the macro that provide information about how to invoke it, not what it generates.

Agreed. If we move some code around, we can parse into LHSes and RHSes on macro definition, and then have rustdoc display only the former. I think I can get this in place for 1.0-stable.

@kmcallister kmcallister self-assigned this Jan 11, 2015
@kmcallister kmcallister changed the title macro docs should put the implementation at the bottom rustdoc: show macro interface but not implementation Jan 11, 2015
@kmcallister kmcallister added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Jan 12, 2015
@kmcallister
Copy link
Contributor Author

Not going to have time to work on this for 1.0-final, sorry.

@nixpulvis
Copy link

FWIW I also had the exact same thought when looking at the documentation for bitflag! as well. The proposed change to only show the left arm of the macro would look like this.

macro_rules! bitflags {
    ($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
        $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+
    }) => { ... },
    ($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
        $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+,
    }) => { ... },
}

A welcome change.

@m4rw3r
Copy link
Contributor

m4rw3r commented Nov 17, 2015

I have a similar issue, though it cannot easily be solved by just showing the left arm of the macro since there are multiple internal rules and they refer to each other: https://github.com/m4rw3r/chomp/blob/60cb0fb111e079c472c02821cee5e82b0eef2b82/src/macros.rs#L71

If we just go with the left arm of the macro it will result in a lot of nonsensical entrypoints:

macro_rules! parse {
    ( @RET($i:expr); @ $t_ty:ty , $e_ty:ty : $e:expr ) => { ... };
    ( @RET($i:expr); $e:expr ) => { ... };
    ( @ERR($i:expr); @ $t_ty:ty , $e_ty:ty : $e:expr ) => { ... };
    // ...
    ( $i:expr ; err $($tail:tt)+ ) => { ... };
    // ...
    ( $i:expr ) => { $i };
}

And skipping the internal rules will make the macro even more incomprehensible since the entrypoints themselves just parse a bit of the tokens for each invocation.

For my case I would think that just hiding the whole macro definition would solve the problem (maybe with some button to expand the definition), since I have to explain how the macro is used anyway since it by itself says nothing unless people are very experienced with rust macros.

As a workaround I have defined a hidden macro which contains the actual definition: https://github.com/m4rw3r/chomp/blob/5016d0b9e636156927e514f430e33510d9fa94f7/src/macros.rs#L76

@Manishearth Manishearth added the E-help-wanted Call for participation: Help is requested to fix this issue. label Nov 24, 2015
bors added a commit that referenced this issue Nov 28, 2015
Fixes #17616

New docs for `panic!`:
```rust
macro_rules! panic {
    () => { ... };
    ($msg:expr) => { ... };
    ($fmt:expr, $($arg:tt)+) => { ... };
}
```

New docs for `assert!`:
```rust
macro_rules! assert {
    ( $ cond : expr ) => { ... };
    (
$ cond : expr , $ ( $ arg : tt ) + ) => { ... };
}
```
<sup>not pretty, but at least it's not worse 😂
lnicola pushed a commit to lnicola/rust that referenced this issue Jul 28, 2024
Fix incorrect generic parameter hint defaults

Missed this in the review but we should show const param hints, not lifetime param hints by default
RalfJung pushed a commit to RalfJung/rust that referenced this issue Aug 1, 2024
Fix incorrect generic parameter hint defaults

Missed this in the review but we should show const param hints, not lifetime param hints by default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants