rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.05k stars 12.69k forks source link

Diagnostic message for std trait implementation includes unstable std type #108994

Open ChrisDenton opened 1 year ago

ChrisDenton commented 1 year ago

Background

Within std I'm attempting to add an unstable type to a trait (see here for the impl). However, after compiling the new std, the unstable type is shown in error messages even without the feature enabled.

Code

struct Biscuits;
fn main() {
    std::fs::metadata(&Biscuits).unwrap();
}

Current Output

error[E0277]: the trait bound `Biscuits: AsRef<Path>` is not satisfied
    --> temp.rs:3:21
     |
3    |     std::fs::metadata(&Biscuits).unwrap();
     |     -----------------  ^^^^^^^^ the trait `AsRef<Path>` is not implemented for `Biscuits`
     |     |
     |     required by a bound introduced by this call
     |
     = help: the trait `AsPath` is implemented for `&NativePath`
     = note: required for `&Biscuits` to implement `AsRef<Path>`
     = note: required for `&Biscuits` to implement `AsPath`
note: required by a bound in `std::fs::metadata`
    --> R:\rust\library\std\src\fs.rs:1848:20
     |
1848 | pub fn metadata<P: AsPath>(path: P) -> io::Result<Metadata> {
     |                    ^^^^^^ required by this bound in `metadata`

Desired Output

Remove the first help: message.

error[E0277]: the trait bound `Biscuits: AsRef<Path>` is not satisfied
    --> temp.rs:3:21
     |
3    |     std::fs::metadata(&Biscuits).unwrap();
     |     -----------------  ^^^^^^^^ the trait `AsRef<Path>` is not implemented for `Biscuits`
     |     |
     |     required by a bound introduced by this call
     |
     = note: required for `&Biscuits` to implement `AsRef<Path>`
     = note: required for `&Biscuits` to implement `AsPath`
note: required by a bound in `std::fs::metadata`
    --> R:\rust\library\std\src\fs.rs:1848:20
     |
1848 | pub fn metadata<P: AsPath>(path: P) -> io::Result<Metadata> {
     |                    ^^^^^^ required by this bound in `metadata`
jyn514 commented 1 year ago

this reminds me of https://github.com/rust-lang/rust/pull/44026

ChrisDenton commented 1 year ago

I had a very quick look at this. A simple fix would be something like:

diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
index 704b0d0bd1c..541cd4981e8 100644
--- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
+++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
@@ -2101,6 +2101,10 @@ fn report_similar_impl_candidates(
                     }
                     // Avoid mentioning types that are private to another crate
                     else if let ty::Adt(def, _) = self_ty.peel_refs().kind() {
+                        // Avoid mentioning unstable types on stable
+                        if !self.tcx.sess.opts.unstable_features.is_nightly_build() && self.tcx.has_attr(def.did(), sym::unstable) {
+                            return false;
+                        }
                         // FIXME(compiler-errors): This could be generalized, both to
                         // be more granular, and probably look past other `#[fundamental]`
                         // types, too.

But I don't know if this is the best fix or if it would have any side effects. It would also need a test.