Skip to content

Rustdoc doesn't see trait implementations inside of functions #41480

Closed

Description

@RReverser

It's possible to define trait implementations inside of functions in Rust. This is useful not too often, but particularly nice if you have a macro that both defines trait implementation and calls some initialization for a given type.

Anyway, the following code is valid in Rust itself:

pub struct S;

pub fn a() {
    impl From<u8> for S {
        fn from(_: u8) -> S {
            S
        }
    }
}

But Rustdoc doesn't generate documentation for a given trait, despite it being visible to Rust itself, and being immediately available and unique across entire crate.

I can move impl out of the function and then it gets seen by Rustdoc, but this is rather inconvenient when impl is generated as part of a bigger macro.

Activity

  1. GuillaumeGomez commented on Apr 25, 2017

    @GuillaumeGomez
    Member

    The scope of the implementation is only inside the function and so cannot be used outside of it and so shouldn't be documented. Also, macro != functions. If you make an implementation inside a macro, it's documented.

    Closing it then, don't hesitate to reopen it if I missed something.

  2. RReverser commented on Apr 25, 2017

    @RReverser
    ContributorAuthor

    The scope of the implementation is only inside the function and so cannot be used outside of it and so shouldn't be documented.

    No, this is exactly the problem I was referring to - impl is not scoped. In Rust itself it's visible across entire codebase despite being declared inside of a function.

    Also, macro != functions. If you make an implementation inside a macro, it's documented.

    I referred to macro only because I'm using them inside of such function to generate both impl and a call to some other function.

  3. RReverser commented on Apr 25, 2017

    @RReverser
    ContributorAuthor

    To enhance the example above:

    pub struct S;
    
    pub fn a() {
        impl From<u8> for S {
            fn from(_: u8) -> S {
                S
            }
        }
    }
    
    fn main() {
        let s = S::from(10u8);
    }

    ^^ From works here as expected, as well as other crates that depend on this one, can use it, which is why it's important to have it documented.

    @GuillaumeGomez

  4. petrochenkov commented on Apr 25, 2017

    @petrochenkov
    Contributor

    don't hesitate to reopen it if I missed something

    (GitHub doesn't allow non-members to reopen issues closed by others.)

  5. RReverser commented on Apr 25, 2017

    @RReverser
    ContributorAuthor

    doesn't allow non-members to reopen issues closed by others

    Indeed, thanks for reopening this!

  6. GuillaumeGomez commented on Apr 25, 2017

    @GuillaumeGomez
    Member

    Thanks for reopening it @petrochenkov.

    @RReverser: That's completely crazy. Another example just for the fun:

    pub struct S;
    
    trait Foo {
        fn foo(&self) {}
    }
    
    pub fn a() {
        impl Foo for S {
        }
    }
    
    fn main() {
        S.foo();
    }

    So rustdoc should document these implementations as well. Thanks for opening the issue!

  7. added
    T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    on Apr 25, 2017
  8. added
    T-dev-toolsRelevant to the dev-tools subteam, which will review and decide on the PR/issue.
    and removed
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    on May 18, 2017
  9. Marwes commented on Nov 29, 2017

    @Marwes
    Contributor

    Got bit by this. rustdoc were simply unable to find a trait implementation defined in a function which cargo build picked up fine https://docs.rs/crate/serde_state/0.4.2/builds/77728 . Thought I was going crazy for second there.

  10. QuietMisdreavus commented on Aug 7, 2018

    @QuietMisdreavus
    Contributor

    I've opened #53162 to fix this.

  11. added a commit that references this issue on Sep 7, 2018
  12. added 2 commits that reference this issue on Sep 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-dev-toolsRelevant to the dev-tools subteam, which will review and decide on the PR/issue.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions