rust-lang / rust-lang/rust

using `super` in doctests errors

Open
#130,274 6 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-doctests A-resolve D-terse T-rustdoc
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure from the lib.rs snippet by running cargo test. Then trace the doctest compilation path responsible for module resolution and inspect how super is handled. Done means the doctest compiles successfully and cargo test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.