Skip to content

Misleading Error with &dyn #95598

Closed
Closed
@C0D3-M4513R

Description

@C0D3-M4513R

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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