Skip to content

Overly verbose diagnostic when calling .as_ref() on type not implementing AsRef #89806

Closed
@8051Enthusiast

Description

@8051Enthusiast

Following code seems to lead to overly long diagnostics: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%200u8.as_ref()%0A%7D

fn main() {
    0u8.as_ref()
}

The current output is:

error[E0599]: no method named `as_ref` found for type `u8` in the current scope
   --> b.rs:2:9
    |
2   |     0u8.as_ref()
    |         ^^^^^^ method not found in `u8`
    |
   ::: ~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/convert/mod.rs:159:8
    |
159 |     fn as_ref(&self) -> &T;
    |        ------
    |        |
    |        the method is available for `Box<u8>` here
    |        the method is available for `Arc<u8>` here
    |        the method is available for `Rc<u8>` here
    |        the method is available for `Box<&mut u8>` here
    |        the method is available for `Arc<&mut u8>` here
    |        the method is available for `Rc<&mut u8>` here
    |        the method is available for `Box<&u8>` here
    |        the method is available for `Arc<&u8>` here
    |        the method is available for `Rc<&u8>` here
    |
   ::: ~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/pin.rs:584:12
    |
584 |     pub fn as_ref(&self) -> Pin<&P::Target> {
    |            ------
    |            |
    |            the method is available for `Pin<&mut u8>` here
    |            the method is available for `Pin<&u8>` here
    |
help: consider wrapping the receiver expression with the appropriate type
    |
2   |     Box::new(0u8).as_ref()
    |     ^^^^^^^^^   ^
help: consider wrapping the receiver expression with the appropriate type
    |
2   |     Arc::new(0u8).as_ref()
    |     ^^^^^^^^^   ^
help: consider wrapping the receiver expression with the appropriate type
    |
2   |     Rc::new(0u8).as_ref()
    |     ^^^^^^^^   ^
help: consider wrapping the receiver expression with the appropriate type
    |
2   |     Box::new(&mut 0u8).as_ref()
    |     ^^^^^^^^^^^^^    ^
help: consider wrapping the receiver expression with the appropriate type
    |
2   |     Pin::new(&mut 0u8).as_ref()
    |     ^^^^^^^^^^^^^    ^
help: consider wrapping the receiver expression with the appropriate type
    |
2   |     Arc::new(&mut 0u8).as_ref()
    |     ^^^^^^^^^^^^^    ^
help: consider wrapping the receiver expression with the appropriate type
    |
2   |     Rc::new(&mut 0u8).as_ref()
    |     ^^^^^^^^^^^^    ^
help: consider wrapping the receiver expression with the appropriate type
    |
2   |     Box::new(&0u8).as_ref()
    |     ^^^^^^^^^^   ^
help: consider wrapping the receiver expression with the appropriate type
    |
2   |     Pin::new(&0u8).as_ref()
    |     ^^^^^^^^^^   ^
help: consider wrapping the receiver expression with the appropriate type
    |
2   |     Arc::new(&0u8).as_ref()
    |     ^^^^^^^^^^   ^
help: consider wrapping the receiver expression with the appropriate type
    |
2   |     Rc::new(&0u8).as_ref()
    |     ^^^^^^^^^   ^

Note that this happens not just with 0u8 but with most types not implementing AsRef as far as I can see.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.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