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

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

Closed
jplatte opened this issue Jun 5, 2021 · 2 comments · Fixed by #91412
Closed

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

jplatte opened this issue Jun 5, 2021 · 2 comments · Fixed by #91412
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-doctests Area: Documentation tests, run by rustdoc A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jplatte
Copy link
Contributor

jplatte commented Jun 5, 2021

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)

@jplatte jplatte added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 5, 2021
@JohnTitor JohnTitor added A-doctests Area: Documentation tests, run by rustdoc A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Jul 1, 2021
@edward-shen
Copy link
Contributor

edward-shen commented Nov 30, 2021

I can verify that this problem still exists on 1.56.1 and 1.59.0, where I also ran into this via the tracing ecosystem, this time with this one-liner and tracing-subscriber:

use tracing_subscriber; // 0.3.3
tracing_subscriber::registry().init();

This results in the following rustc error:

error[E0599]: no method named `init` found for struct `Registry` in the current scope
  --> src/main.rs:2:36
   |
2  |     tracing_subscriber::registry().init();
   |                                    ^^^^ method not found in `Registry`
   |
  ::: /Users/edward-shen/.cargo/registry/src/6github.com-1ecc6299db9ec823/tracing-subscriber-0.3.3/src/util.rs:89:8
   |
89 |     fn init(self) {
   |        ---- the method is available for `Registry` here
   |
   = 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:
   |
1  | use tracing_subscriber::prelude::_;
   |

For more information about this error, try `rustc --explain E0599`.

Not sure how common it is (considering more haven't complained about this), but the real-world implementation that leads to this bug seems like an easy path to hit.

@compiler-errors
Copy link
Member

I've written up fix of this in several parts. Let me put up a PR.

@rustbot claim

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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants