Skip to content

On nightly rustc, E0599 emits a massive diagnostic to suggest wrapping the receiver in every combination of {Box,Pin,Rc,Arc}::new({,&,&mut} expr) #84769

Closed
@PatchMixolydic

Description

@PatchMixolydic

Given the following code (playground):

trait Bar {}
impl Bar for String {}

fn main() {
  let s = String::from("hey");
  let x: &dyn Bar = &s;
  x.as_ref();
}

The output on 1.53.0-nightly (2021-04-29 478a07df05e3fe8df964) is:

Long diagnostic
error[E0599]: the method `as_ref` exists for reference `&dyn Bar`, but its trait bounds were not satisfied
   --> src/main.rs:7:5
    |
1   | trait Bar {}
    | --------- doesn't satisfy `dyn Bar: AsRef<_>`
...
7   |   x.as_ref();
    |     ^^^^^^ method cannot be called on `&dyn Bar` due to unsatisfied trait bounds
    |
    = note: the following trait bounds were not satisfied:
            `dyn Bar: AsRef<_>`
            which is required by `&dyn Bar: AsRef<_>`
help: consider wrapping the receiver expression with the appropriate type
    |
7   |   Box::new(x).as_ref();
    |   ^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
    |
7   |   Pin::new(x).as_ref();
    |   ^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
    |
7   |   Arc::new(x).as_ref();
    |   ^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
    |
7   |   Rc::new(x).as_ref();
    |   ^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
    |
7   |   Box::new(&mut x).as_ref();
    |   ^^^^^^^^^^^^^  ^
help: consider wrapping the receiver expression with the appropriate type
    |
7   |   Pin::new(&mut x).as_ref();
    |   ^^^^^^^^^^^^^  ^
help: consider wrapping the receiver expression with the appropriate type
    |
7   |   Arc::new(&mut x).as_ref();
    |   ^^^^^^^^^^^^^  ^
help: consider wrapping the receiver expression with the appropriate type
    |
7   |   Rc::new(&mut x).as_ref();
    |   ^^^^^^^^^^^^  ^
help: consider wrapping the receiver expression with the appropriate type
    |
7   |   Box::new(&x).as_ref();
    |   ^^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
    |
7   |   Pin::new(&x).as_ref();
    |   ^^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
    |
7   |   Arc::new(&x).as_ref();
    |   ^^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
    |
7   |   Rc::new(&x).as_ref();
    |   ^^^^^^^^^ ^

Box, Pin, Arc, and Rc are suggested three times each: once for x, once for &x, and once for &mut x. I'm not sure if this is a problem, but this seems incredibly verbose to me.

Note that the output on stable 1.51 and beta 1.52 is different:

error[E0599]: the method `as_ref` exists for reference `&dyn Bar`, but its trait bounds were not satisfied
 --> src/main.rs:7:5
  |
1 | trait Bar {}
  | --------- doesn't satisfy `dyn Bar: AsRef<_>`
...
7 |   x.as_ref();
  |     ^^^^^^ method cannot be called on `&dyn Bar` due to unsatisfied trait bounds
  |
  = note: the following trait bounds were not satisfied:
          `dyn Bar: AsRef<_>`
          which is required by `&dyn Bar: AsRef<_>`
  = help: items from traits can only be used if the trait is implemented and in scope
  = note: the following trait defines an item `as_ref`, perhaps you need to implement it:
          candidate #1: `AsRef`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-verboseDiagnostics: Too much output caused by a single piece of incorrect code.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions