Skip to content

Misleading Error with &dyn #95598

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

Closed
C0D3-M4513R opened this issue Apr 2, 2022 · 3 comments
Closed

Misleading Error with &dyn #95598

C0D3-M4513R opened this issue Apr 2, 2022 · 3 comments
Labels
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.

Comments

@C0D3-M4513R
Copy link

Given the following code

use std::path::Path;
// use std::ptr::{null, null_mut};

///Returns a function, which compares a &str against a name
fn cmp(name:&dyn ToString)
{}
fn cmp1(name:&dyn AsRef<Path>)
{}
fn main(){
    cmp("test");
    cmp1("test");
}

The current output is:

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> src/main.rs:10:9
   |
10 |     cmp("test");
   |     --- ^^^^^^ doesn't have a size known at compile-time
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `Sized` is not implemented for `str`
   = note: required for the cast to the object type `dyn ToString`

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> src/main.rs:11:10
   |
11 |     cmp1("test");
   |     ---- ^^^^^^ doesn't have a size known at compile-time
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `Sized` is not implemented for `str`
   = note: required for the cast to the object type `dyn AsRef<Path>`

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

Stating, that &str does not have a known size at compile time is just wrong.
I think that rust sees, that the function parameter has a Borrowed type and that the function call also provides a borrowed type. So rust probably procedes to check the actual types 'behind' the borrow.

It is just, that here I would expect a message asking me if I would have liked to borrow here, since that would solve this error.

@C0D3-M4513R C0D3-M4513R 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 Apr 2, 2022
@C0D3-M4513R
Copy link
Author

C0D3-M4513R commented Apr 2, 2022

My use case for this is the following function, which returns a predicate.
I have the borrow infront of the dyn, to make the type Sized (so it can be used in the closure).

fn cmp<P:AsRef<Path>>(name:P) -> impl Fn(&dyn AsRef<Path>)->bool
{
    move |s| {
        return name.as_ref().ends_with(s)
    }
}

To call it, I need to do the following.
The first borrow should be obvious. The second is due to AsRef.

cmp("test")(&&"test".to_string())
cmp("test")(&"test")//This works, because str and Path are both not Sized, right?

Please correct me, but I do not think, that I can write this any better right now (without inlining the function). I

@SNCPlay42
Copy link
Contributor

Stating, that &str does not have a known size at compile time is just wrong.

The message does not state that &str does not implement Sized, it states that str doesn't, which is true. And it does say why it needs this ("required for the cast to the object type dyn ToString"): only types of known size can be coerced into trait objects. This is because the representation of &dyn Trait provides no way to discern the length of the str being pointed to.

@C0D3-M4513R
Copy link
Author

C0D3-M4513R commented Apr 2, 2022

Stating, that &str does not have a known size at compile time is just wrong.

The message does not state that &str does not implement Sized, it states that str doesn't, which is true. And it does say why it needs this ("required for the cast to the object type dyn ToString"): only types of known size can be coerced into trait objects. This is because the representation of &dyn Trait provides no way to discern the length of the str being pointed to.

Yes, but the whole message is missing the point, of what is actually happening. The Message is very unclear in what is says. Take the error message, without the hints. Misses completely what is going on.

  --> src/main.rs:10:9
   |
10 |     cmp("test");
   |     --- ^^^^^^ doesn't have a size known at compile-time
   |     |
   |     required by a bound introduced by this call

Also can you please explain to me, why str needs to be Sized in the second call eventhough AsRef specifically lifts this requirement?

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 4, 2022
…yn, r=nagisa

Suggest borrowing when trying to coerce unsized type into `dyn Trait`

A helpful error in response to rust-lang#95598, since we can't coerce e.g. `&str` into `&dyn Display`, but we can coerce `&&str` into `&dyn Display` :)

Not sure if the suggestion message needs some help. Let me know, and I can refine this PR.
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants