Skip to content
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

Suggest reborrowing unsized value when borrowed trait object wanted #93596

Closed
ijackson opened this issue Feb 2, 2022 · 2 comments · Fixed by #98277
Closed

Suggest reborrowing unsized value when borrowed trait object wanted #93596

ijackson opened this issue Feb 2, 2022 · 2 comments · Fixed by #98277
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-traits Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ijackson
Copy link
Contributor

ijackson commented Feb 2, 2022

Given the following code:

use std::ffi::{OsStr, OsString};
use std::path::Path;

fn check(p: &dyn AsRef<Path>) {
    let m = std::fs::metadata(&p);
    println!("{:?}", &m);
}

fn main() {
    let s: OsString = ".".into();
    let s: &OsStr = &s;
    check(s);
}

The current output is:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
  --> src/main.rs:12:11
   |
12 |     check(s);
   |     ----- ^ doesn't have a size known at compile-time
   |     |
   |     required by a bound introduced by this call
   |
   = help: within `OsStr`, the trait `Sized` is not implemented for `[u8]`
   = note: required because it appears within the type `OsStr`
   = note: required for the cast to the object type `dyn AsRef<Path>`

Ideally the output would suggest borrowing, since this:

    check(&s);

compiles and works just fine.

The problem is that you can't make an object which is a wide pointer directly into a trait object refernce, because it would have to be a triple-sized pointer. But often, you can just reborrow instead.

@ijackson ijackson 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 Feb 2, 2022
@ijackson
Copy link
Contributor Author

ijackson commented Feb 2, 2022

@rustbot modify labels +A-suggestion-diagnostics +A-traits

@rustbot rustbot added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-traits Area: Trait system labels Feb 2, 2022
@ijackson
Copy link
Contributor Author

ijackson commented Feb 2, 2022

@rustbot modify labels +C-enhancement

@rustbot rustbot added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Feb 2, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jun 22, 2022
…bank

Fix trait object reborrow suggestion

Fixes rust-lang#93596

Slightly generalizes the logic we use to suggest fix first implemented in rust-lang#95609, specifically when we have a `Sized` obligation that comes from a struct's unsized tail.
compiler-errors added a commit to compiler-errors/rust that referenced this issue Jun 23, 2022
…bank

Fix trait object reborrow suggestion

Fixes rust-lang#93596

Slightly generalizes the logic we use to suggest fix first implemented in rust-lang#95609, specifically when we have a `Sized` obligation that comes from a struct's unsized tail.
compiler-errors added a commit to compiler-errors/rust that referenced this issue Jun 25, 2022
…bank

Fix trait object reborrow suggestion

Fixes rust-lang#93596

Slightly generalizes the logic we use to suggest fix first implemented in rust-lang#95609, specifically when we have a `Sized` obligation that comes from a struct's unsized tail.
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jun 25, 2022
…bank

Fix trait object reborrow suggestion

Fixes rust-lang#93596

Slightly generalizes the logic we use to suggest fix first implemented in rust-lang#95609, specifically when we have a `Sized` obligation that comes from a struct's unsized tail.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jun 25, 2022
…bank

Fix trait object reborrow suggestion

Fixes rust-lang#93596

Slightly generalizes the logic we use to suggest fix first implemented in rust-lang#95609, specifically when we have a `Sized` obligation that comes from a struct's unsized tail.
@bors bors closed this as completed in 57c3cee Jun 29, 2022
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-traits Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants