Skip to content

rustc suggests to use path::_; with as _ trait re-exports #86035

Closed
@jplatte

Description

@jplatte

Given the following code (playground):

//! ```
//! use playground::S;
//! S.method()
//! ```

pub struct S;
pub use m::Tr as _;

mod m {
    pub trait Tr { fn method(&self); }
    impl Tr for crate::S { fn method(&self) {} }
}

The current output for compiling the doctest is:

error[E0599]: no method named `method` found for struct `S` in the current scope
 --> src/lib.rs:3:3
  |
5 | S.method()
  |   ^^^^^^ method not found in `S`
  |
  = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
  |
3 | use crate::playground::_;
  |

I'm not sure what the output should be. Originally I hit this issue with tracing, which re-exports one trait (soon multiple) as _ in its prelude module. They are of course reachable in another path, so that one could be prefered¹, but I realized that this issue could also crop up with a situation such as what I've constructed above, where the trait is actually unnamable. In that case, either the compiler has to suggest as * import, or it has to not emit the help item at all.

¹ probably needs an annotation like #[doc(canonical)] (rust-lang/rfcs#3011)

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-doctestsArea: Documentation tests, run by rustdocA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler 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