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

using super in doctests errors #130274

Open
y86-dev opened this issue Sep 12, 2024 · 5 comments
Open

using super in doctests errors #130274

y86-dev opened this issue Sep 12, 2024 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-doctests Area: Documentation tests, run by rustdoc A-resolve Area: Name resolution D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@y86-dev
Copy link
Contributor

y86-dev commented Sep 12, 2024

I tried this code:

/// ```
/// type Y = ();
/// mod m {
///     use super::Y;
///     fn f() -> Y {}
/// }
/// ```
pub struct X;

in lib.rs.

I expected to see this happen: successful cargo test

Instead, this happened:

cargo test
   Compiling rust v0.0.0 (/tmp/tmp)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.13s
     Running unittests src/lib.rs (target/debug/deps/rust-48a0867617020c3e)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests rust

running 1 test
test src/lib.rs - X (line 1) ... FAILED

failures:

---- src/lib.rs - X (line 1) stdout ----
error[E0432]: unresolved import `super::Y`
 --> src/lib.rs:4:9
  |
5 |     use super::Y;
  |         ^^^^^^^^ no `Y` in the root

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0432`.
Couldn't compile the test.

failures:
    src/lib.rs - X (line 1)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.02s

error: doctest failed, to rerun pass `--doc`

The same error happens when using super::Y directly in the return type.

Meta

rustc --version --verbose:

rustc 1.83.0-nightly (8d6b88b16 2024-09-11)
binary: rustc
commit-hash: 8d6b88b168e45ee1624699c19443c49665322a91
commit-date: 2024-09-11
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
@y86-dev y86-dev added the C-bug Category: This is a bug. label Sep 12, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 12, 2024
@Urgau
Copy link
Member

Urgau commented Sep 12, 2024

Each doctest is put in it's own function, and since super always refer to a module, it's expected that it won't find what's inside the doctest function, whenever that behaviour is something we want or not and can be fix is another thing.

fn doctest() {
    type Y = ();
    mod m {
        use super::Y;  // unresolved import `super::Y`
        fn f() -> Y {}
    }
}

@fmease
Copy link
Member

fmease commented Sep 12, 2024

cc #79260 the underlying rustc issue

@Darksonn
Copy link
Contributor

Even if this is intended behavior, is there any chance we could include a note in the error explaining this?

@GuillaumeGomez GuillaumeGomez added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-error-handling Area: Error handling and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 12, 2024
@fmease fmease added A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name resolution A-doctests Area: Documentation tests, run by rustdoc D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. and removed C-bug Category: This is a bug. A-error-handling Area: Error handling labels Sep 12, 2024
@ehuss
Copy link
Contributor

ehuss commented Sep 12, 2024

BTW, you can add fn main() {} to your doctest to make it work:

/// ```
/// type Y = ();
/// mod m {
///     use super::Y;
///     fn f() -> Y {}
/// }
/// # fn main() {}
/// ```
pub struct X;

@Darksonn
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-doctests Area: Documentation tests, run by rustdoc A-resolve Area: Name resolution D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
Status: No status
Development

No branches or pull requests

7 participants