Missing a trait import in doctests suggests importing from `crate::<cratename>`
Open
@yuk1ty is already working on this.
Since May 9, 2025.
A-diagnostics
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
pub trait Runnable {
fn run();
}
/// ```
/// use tmp::NoopTask;
///
/// NoopTask::run();
/// ```
pub struct NoopTask;
impl Runnable for NoopTask {
fn run() {}
}
Current output
---- src/lib.rs - NoopTask (line 5) stdout ----
error[E0599]: no function or associated item named `run` found for struct `NoopTask` in the current scope
--> src/lib.rs:9:11
|
7 | NoopTask::run();
| ^^^ function or associated item not found in `NoopTask`
|
= help: items from traits can only be used if the trait is in scope
help: trait `Runnable` which provides `run` is implemented but not in scope; perhaps you want to import it
|
2 + use crate::tmp::Runnable;
|
Desired output
---- src/lib.rs - NoopTask (line 5) stdout ----
error[E0599]: no function or associated item named `run` found for struct `NoopTask` in the current scope
--> src/lib.rs:9:11
|
7 | NoopTask::run();
| ^^^ function or associated item not found in `NoopTask`
|
= help: items from traits can only be used if the trait is in scope
help: trait `Runnable` which provides `run` is implemented but not in scope; perhaps you want to import it
|
2 + use tmp::Runnable;
|
Rationale and extra context
This happens when running a doctest without all traits imported.
I'm not quite sure whether this is a diagnostics issue, as the way to import that trait does surprisingly work. However, it's makes the docs quite confusing.
On top of that a unimported struct does suggest importing tmp::NoopRunnable instead of crate::tmp::NoopRunnable .
Other cases
Rust Version
rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: x86_64-unknown-linux-gnu
release: 1.85.1
LLVM version: 19.1.7
Anything else?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.