Skip to content

Cannot coerce dyn AsRef #61977

@clarfonthey

Description

@clarfonthey

Really weird example:

use std::path::Path;
fn main() {
    let boxed_path: Box<Path> = Path::new("test").to_path_buf().into_boxed_path();
    let as_ref_works: &Path = boxed_path.as_ref();
    //let lots_of_question_marks: Box<dyn AsRef<Path>> = boxed_path;
}

Uncommenting the last line gives:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/main.rs:5:56
  |
5 |     let lots_of_question_marks: Box<dyn AsRef<Path>> = boxed_path;
  |                                                        ^^^^^^^^^^ borrow the `Path` instead
  |
  = help: within `std::path::Path`, the trait `std::marker::Sized` is not implemented for `[u8]`
  = 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 because it appears within the type `std::path::Path`
  = note: required for the cast to the object type `dyn std::convert::AsRef<std::path::Path>`

error: aborting due to previous error

I genuinely don't know what's going on here, but this seems to be a bug in both coercion and diagnostics.

This is on the latest nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsA-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions