Skip to content

Intradoc Self:: links are "not in scope" when used in impl blocks across modules #93205

Closed
@MarijnS95

Description

@MarijnS95

I tried this code:

/// Autogenerated code goes here
mod generated {
    pub struct MyNewType(pub(crate) u32);
    impl MyNewType {
        pub const FOO: Self = Self(1);
        pub const BAR: Self = Self(2);

        /// Reexport of [`Self::FOO`]
        pub const FOO2: Self = Self::FOO;
        /// Reexport of [`Self::FOO_BAR`]
        pub const FOO_BAR2: Self = Self::FOO_BAR;
    }
}

pub use generated::*;

/// Manual extensions for autogenerated code go here
mod prelude {
    impl super::MyNewType {
        /// Contraction of [`Self::FOO`] | [`Self::BAR`]
        pub const FOO_BAR: Self = Self(Self::FOO.0 | Self::BAR.0);

        /// Contraction of [`Self::FOO`] and [`Self::BAR`], see also [`Self::FOO_BAR`]
        pub fn raw_foo_bar() -> u32 {
            Self::FOO.0 | Self::BAR.0
        }
    }
}

I expected to see this happen: Self:: intradoc links for the impl block residing in mod prelude (where MyNewType is not defined) are valid links.

Instead, this happened: Rustdoc thinks that Self, resolved to MyNewType, is not in scope here:

warning: unresolved link to `MyNewType::FOO`
  --> src/lib.rs:20:30
   |
20 |         /// Contraction of [`Self::FOO`] | [`Self::BAR`]
   |                              ^^^^^^^^^ no item named `MyNewType` in scope
   |
   = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default

warning: unresolved link to `MyNewType::BAR`
  --> src/lib.rs:20:46
   |
20 |         /// Contraction of [`Self::FOO`] | [`Self::BAR`]
   |                                              ^^^^^^^^^ no item named `MyNewType` in scope

warning: unresolved link to `MyNewType::FOO`
  --> src/lib.rs:23:30
   |
23 |         /// Contraction of [`Self::FOO`] and [`Self::BAR`], see also [`Self::FOO_BAR`]
   |                              ^^^^^^^^^ no item named `MyNewType` in scope

warning: unresolved link to `MyNewType::BAR`
  --> src/lib.rs:23:48
   |
23 |         /// Contraction of [`Self::FOO`] and [`Self::BAR`], see also [`Self::FOO_BAR`]
   |                                                ^^^^^^^^^ no item named `MyNewType` in scope

warning: unresolved link to `MyNewType::FOO_BAR`
  --> src/lib.rs:23:72
   |
23 |         /// Contraction of [`Self::FOO`] and [`Self::BAR`], see also [`Self::FOO_BAR`]
   |                                                                        ^^^^^^^^^^^^^ no item named `MyNewType` in scope

warning: `doc_experiments` (lib doc) generated 5 warnings

A workaround is to make sure that MyNewType, without super:: prefix, is in scope:

-    impl super::MyNewType
+    use super::MyNewType;
+    impl MyNewType {

In other words, it seems that Self in intradoc links resolve to the name of the item, not the full path?

Meta

rustc --version --verbose:

rustc 1.60.0-nightly (9ad5d82f8 2022-01-18)
binary: rustc
commit-hash: 9ad5d82f822b3cb67637f11be2e65c5662b66ec0
commit-date: 2022-01-18
host: x86_64-unknown-linux-gnu
release: 1.60.0-nightly
LLVM version: 13.0.0

Metadata

Metadata

Assignees

Labels

A-intra-doc-linksArea: Intra-doc links, the ability to link to items in docs by nameC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.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