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

Rustc help tips create a loop with borrowing #72742

Closed
NotWearingPants opened this issue May 29, 2020 · 2 comments · Fixed by #106223
Closed

Rustc help tips create a loop with borrowing #72742

NotWearingPants opened this issue May 29, 2020 · 2 comments · Fixed by #106223
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. F-on_unimplemented Error messages that can be tackled with `#[rustc_on_unimplemented]` F-unsized_locals `#![feature(unsized_locals)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@NotWearingPants
Copy link
Contributor

NotWearingPants commented May 29, 2020

I tried compiling this code:

fn main() {
    let x: [u8] = vec!(1, 2, 3)[..];
}

which resulted in the following output of rustc:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/main.rs:2:9
  |
2 |     let x: [u8] = vec!(1, 2, 3)[..];
  |         ^         ----------------- help: consider borrowing here: `&vec!(1, 2, 3)[..]`
  |         |
  |         doesn't have a size known at compile-time
  |
  = help: 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: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

error: aborting due to previous error

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

So the compiler says help: consider borrowing here: &vec!(1, 2, 3)[..]
So I tried adding a borrow and recompiling the code:

fn main() {
    let x: [u8] = &vec!(1, 2, 3)[..];
}

which resulted in:

error[E0308]: mismatched types
 --> src/main.rs:2:19
  |
2 |     let x: [u8] = &vec!(1, 2, 3)[..];
  |            ----   ^^^^^^^^^^^^^^^^^^
  |            |      |
  |            |      expected slice `[u8]`, found `&[{integer}]`
  |            |      help: consider removing the borrow: `vec!(1, 2, 3)[..]`
  |            expected due to this

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/main.rs:2:9
  |
2 |     let x: [u8] = &vec!(1, 2, 3)[..];
  |         ^ doesn't have a size known at compile-time
  |
  = help: 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: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.

so the compiler says help: consider removing the borrow: vec!(1, 2, 3)[..]

and so I'm stuck in a loop. The compiler can't decide whether I should borrow or not.

As a newbie to Rust, this is very frustrating and the opposite of helpful, it just confuses me further.
Please fix this indecisiveness :)

Meta

rustc --version --verbose:

rustc 1.43.1 (8d69840ab 2020-05-04)
binary: rustc
commit-hash: 8d69840ab92ea7f4d323420088dd8c9775f180cd
commit-date: 2020-05-04
host: x86_64-pc-windows-msvc
release: 1.43.1
LLVM version: 9.0
@NotWearingPants NotWearingPants added the C-bug Category: This is a bug. label May 29, 2020
@csmoe csmoe added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels May 30, 2020
@estebank estebank added the F-on_unimplemented Error messages that can be tackled with `#[rustc_on_unimplemented]` label May 31, 2020
@estebank
Copy link
Contributor

For reference, what you likely want to do is one of the following

use std::convert::TryInto;

fn main() {
    let x: [u8; 3] = vec!(1, 2, 3)[..].try_into().unwrap(); // sized array
    let y: &[u8] = &vec!(1, 2, 3)[..]; // `u8` slice
}

Bare slices ([_]) are !Sized (unsized) which means they cannot be the type of a local binding. You can give a binding a slice type (&[_]), because that's morally equivalent to a pointer (with sizing info), which has a compile time determined size, as all borrows are Sized.

@estebank estebank added the F-unsized_locals `#![feature(unsized_locals)]` label May 31, 2020
@crlf0710 crlf0710 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 11, 2020
@estebank
Copy link
Contributor

Current output:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/test/ui/unsized-locals/suggest-borrow.rs:2:9
  |
2 |     let x: [u8] = vec!(1, 2, 3)[..]; //~ ERROR E0277
  |         ^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[u8]`
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature
help: consider borrowing here
  |
2 |     let x: [u8] = &vec!(1, 2, 3)[..]; //~ ERROR E0277
  |                   +

error[E0308]: mismatched types
 --> src/test/ui/unsized-locals/suggest-borrow.rs:3:20
  |
3 |     let x: &[u8] = vec!(1, 2, 3)[..]; //~ ERROR E0308
  |            -----   ^^^^^^^^^^^^^^^^^
  |            |       |
  |            |       expected `&[u8]`, found slice `[{integer}]`
  |            |       help: consider borrowing here: `&vec!(1, 2, 3)[..]`
  |            expected due to this

error[E0308]: mismatched types
 --> src/test/ui/unsized-locals/suggest-borrow.rs:4:19
  |
4 |     let x: [u8] = &vec!(1, 2, 3)[..]; //~ ERROR E0308
  |            ----   ^^^^^^^^^^^^^^^^^^ expected slice `[u8]`, found `&[{integer}]`
  |            |
  |            expected due to this
  |
help: consider removing the borrow
  |
4 -     let x: [u8] = &vec!(1, 2, 3)[..]; //~ ERROR E0308
4 +     let x: [u8] = vec!(1, 2, 3)[..]; //~ ERROR E0308
  |
help: alternatively, consider changing the type annotation
  |
4 |     let x: &[u8] = &vec!(1, 2, 3)[..]; //~ ERROR E0308
  |            +

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/test/ui/unsized-locals/suggest-borrow.rs:4:9
  |
4 |     let x: [u8] = &vec!(1, 2, 3)[..]; //~ ERROR E0308
  |         ^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[u8]`
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

After #106223:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/test/ui/unsized-locals/suggest-borrow.rs:2:9
  |
2 |     let x: [u8] = vec!(1, 2, 3)[..]; //~ ERROR E0277
  |         ^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[u8]`
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature
help: consider borrowing here
  |
2 |     let x: &[u8] = vec!(1, 2, 3)[..]; //~ ERROR E0277
  |            +

error[E0308]: mismatched types
 --> src/test/ui/unsized-locals/suggest-borrow.rs:3:20
  |
3 |     let x: &[u8] = vec!(1, 2, 3)[..]; //~ ERROR E0308
  |            -----   ^^^^^^^^^^^^^^^^^
  |            |       |
  |            |       expected `&[u8]`, found slice `[{integer}]`
  |            |       help: consider borrowing here: `&vec!(1, 2, 3)[..]`
  |            expected due to this

error[E0308]: mismatched types
 --> src/test/ui/unsized-locals/suggest-borrow.rs:4:19
  |
4 |     let x: [u8] = &vec!(1, 2, 3)[..]; //~ ERROR E0308
  |            ----   ^^^^^^^^^^^^^^^^^^ expected slice `[u8]`, found `&[{integer}]`
  |            |
  |            expected due to this
  |
help: consider removing the borrow
  |
4 -     let x: [u8] = &vec!(1, 2, 3)[..]; //~ ERROR E0308
4 +     let x: [u8] = vec!(1, 2, 3)[..]; //~ ERROR E0308
  |
help: alternatively, consider changing the type annotation
  |
4 |     let x: &[u8] = &vec!(1, 2, 3)[..]; //~ ERROR E0308
  |            +

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/test/ui/unsized-locals/suggest-borrow.rs:4:9
  |
4 |     let x: [u8] = &vec!(1, 2, 3)[..]; //~ ERROR E0308
  |         ^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[u8]`
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature
help: consider borrowing here
  |
4 |     let x: &[u8] = &vec!(1, 2, 3)[..]; //~ ERROR E0308
  |            +

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 29, 2022
…compiler-errors

On unsized locals with explicit types suggest `&`

Fix rust-lang#72742.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 29, 2022
…compiler-errors

On unsized locals with explicit types suggest `&`

Fix rust-lang#72742.
@bors bors closed this as completed in 083eb93 Dec 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` C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. F-on_unimplemented Error messages that can be tackled with `#[rustc_on_unimplemented]` F-unsized_locals `#![feature(unsized_locals)]` 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.

4 participants