Skip to content

Confusing error message when calling f(AsRef<...>, &str) with (str, &str) #66258

Closed
@DevJac

Description

@DevJac

The following code produces a confusing error message:

use std::convert::AsRef;
use std::path::Path;

fn main() {
    foo("bar", &"baz");
    //         ^^^^^^ doesn't have a size known at compile-time???
    // Error message will clearly show that the second argument is the problem,
    // but the first argument is the problem.
}

fn foo(_bar: &dyn AsRef<Path>, _baz: &str) {}

Rust Playground link

The confusing error when compiling the above code will be:

error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/main.rs:5:16
  |
5 |     foo("bar", &"baz");
  |                ^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `str`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: required for the cast to the object type `dyn std::convert::AsRef<std::path::Path>`

I first observed this error message behavior with Rust 1.39.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.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