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] doesn't document method implemented in const item blocks. #83026

Closed
rustonaut opened this issue Mar 11, 2021 · 3 comments · Fixed by #104889
Closed

[rustdoc] doesn't document method implemented in const item blocks. #83026

rustonaut opened this issue Mar 11, 2021 · 3 comments · Fixed by #104889
Labels
C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@rustonaut
Copy link

Rustdoc doesn't document method implemented in const _:() = {} blocks.

(They don't show up at all.)

Minified example:

pub struct A(u8);

// this brakes rustdoc but is valid and useful in larger impl helper macros to limit imports to the macro body.
const _: () = {
    impl A {
        // YES THIS LINE IS NO doc comment, the method doesn't even show up at all
        pub fn new(inner: u8) -> Self {
            Self(inner)
        }
    }
};

// just to show the method is implemented
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_has_new() {
        let _ = A::new(12);
    }
}

I expected to see this happen: A::new to show up in rustdoc (without any doc comment)

Instead, this happened: A::new is not visible at all in the generated documentation.

Why is it important that rustdoc documents such items.

const _:() = {} blocks are currently the only way to have scopes on top-level items which are necessary
for producing cleaner code by e.g. grouping imports with impl. blocks or easily feature gating multiple
impl's and imports without needing to repeat the #[cfg(...)] on each item (making changes easy to accidentally partial brake code by forgetting a place where it changed).

I personally did run into this when I had a large amount of methods and traits implemented very similar on two types, where for various reasons a shared trait was not possible (and not appropriate either). I used const _:()={} to make sure the imports for the many method and trait impl.s do stay inside of the macro and do not pollute the outer scope. As far as I know this trick is also not uncommon for proc-macros.

Meta

appears on 1.47, stable, beta, nightly:

rustc 1.47.0 (18bf6b4f0 2020-10-07)
binary: rustc
commit-hash: 18bf6b4f01a6feaf7259ba7cdae58031af1b7b39
commit-date: 2020-10-07
host: x86_64-unknown-linux-gnu
release: 1.47.0
LLVM version: 11.0
rustc 1.50.0 (cb75ad5db 2021-02-10)
binary: rustc
commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b
commit-date: 2021-02-10
host: x86_64-unknown-linux-gnu
release: 1.50.0
rustc 1.51.0-beta.4 (4d25f4607 2021-03-05)
binary: rustc
commit-hash: 4d25f4607015a56d18d7c6c649441608a9298845
commit-date: 2021-03-05
host: x86_64-unknown-linux-gnu
release: 1.51.0-beta.4
LLVM version: 11.0.1
rustc 1.52.0-nightly (8f349be27 2021-03-08)
binary: rustc
commit-hash: 8f349be27815d43d462a32faeb270a22a68486b6
commit-date: 2021-03-08
host: x86_64-unknown-linux-gnu
release: 1.52.0-nightly
LLVM version: 12.0.0
@rustonaut rustonaut added the C-bug Category: This is a bug. label Mar 11, 2021
@jonas-schievink jonas-schievink added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Mar 11, 2021
@jyn514
Copy link
Member

jyn514 commented Mar 12, 2021

This was added in #67877. cc @dtolnay

@dtolnay
Copy link
Member

dtolnay commented Mar 12, 2021

That PR is the underscore const itself (see screenshot), not method impls defined inside of the const definition. If it also hides associated methods that come from inside the const definition, that was not intentional.

@dtolnay
Copy link
Member

dtolnay commented Mar 12, 2021

I tested with the following small change and the associated method A::new is still not documented by rustdoc, so it's almost definitely unrelated to that PR.

- const _: () = {
+ pub const K: () = {

@jyn514 jyn514 changed the title [rustdoc] doesn't document method implemented in const _:() = {} blocks. [rustdoc] doesn't document method implemented in const item blocks. Mar 30, 2021
@bors bors closed this as completed in cce9e72 Dec 22, 2022
Aaron1011 pushed a commit to Aaron1011/rust that referenced this issue Jan 6, 2023
…st-expr, r=notriddle

Fix impl block in const expr

Fixes rust-lang#83026.

The problem was that we didn't visit block expressions. Considering how big the [walk_expr](https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_hir/intravisit.rs.html#678) function is, I decided to instead implement the `hir` visitor on the struct. It also answers the question which was in a comment for `RustdocVisitor`: we should have used a visitor instead of our ad-hoc implementation.

Adding this visitor also added some extra checks that weren't present before (check changes in `rustdoc-ui` tests).

r? `@notriddle`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. 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.

5 participants