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

Bogus T:Sized suggestion when using size_of::<T> as a const #69228

Closed
bugaevc opened this issue Feb 17, 2020 · 2 comments · Fixed by #95593
Closed

Bogus T:Sized suggestion when using size_of::<T> as a const #69228

bugaevc opened this issue Feb 17, 2020 · 2 comments · Fixed by #95593
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-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bugaevc
Copy link

bugaevc commented Feb 17, 2020

Example:

use std::mem::size_of;

fn foo<T>() {
    let arr: [u8; size_of::<T>()];
}

playground

Results in the following error:

error[E0277]: the size for values of type `T` cannot be known at compilation time
   --> src/lib.rs:4:29
    |
3   | fn foo<T>() {
    |        - help: consider restricting this bound: `T: std::marker::Sized`
4   |     let arr: [u8; size_of::<T>()];
    |                             ^ doesn't have a size known at compile-time
    |
    = help: the trait `std::marker::Sized` is not implemented for `T`
    = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

of course, T is Sized by default unless you say T: ?Sized, so the error and the suggestion are clearly wrong. And if you do say T: Sized explicitly,

// Of course, an explicit Sized constraint doesn't help:
fn bar<T: Sized>() {
    let arr: [u8; size_of::<T>()];
}

you get

error[E0277]: the size for values of type `T` cannot be known at compilation time
   --> src/lib.rs:9:29
    |
8   | fn bar<T: Sized>() {
    |        -- help: consider further restricting this bound: `T: std::marker::Sized +`
9   |     let arr: [u8; size_of::<T>()];
    |                             ^ doesn't have a size known at compile-time
    |
    = help: the trait `std::marker::Sized` is not implemented for `T`
    = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

Note T: std::marker::Sized + being suggested, which is not even valid syntax apparently valid syntax, but not something you would write.

I expected to see this happen:

  • Either this compiles cleanly
  • Or the compiler emits a different error that makes sense

Meta

rustc --version --verbose:

rustc 1.41.0 (5e1a79984 2020-01-27)
binary: rustc
commit-hash: 5e1a799842ba6ed4a57e91f7ab9435947482f7d8
commit-date: 2020-01-27
host: x86_64-unknown-linux-gnu
release: 1.41.0
LLVM version: 9.0

(Seems to happen on nightly too.)

@bugaevc bugaevc added the C-bug Category: This is a bug. label Feb 17, 2020
@jonas-schievink jonas-schievink added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 17, 2020
@Centril
Copy link
Contributor

Centril commented Feb 17, 2020

Duplicate of #43408.

@Centril Centril closed this as completed Feb 17, 2020
@tmandry
Copy link
Member

tmandry commented Feb 25, 2020

Reopening to track the error message, which is not tracked by #43408.

the size for values of type T cannot be known at compilation time is just wrong.

@tmandry tmandry reopened this Feb 25, 2020
@tmandry tmandry added A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels Feb 25, 2020
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 2, 2022
…t-context, r=compiler-errors

diagnostics: add test case for bogus T:Sized suggestion

Closes rust-lang#69228
@bors bors closed this as completed in 7620a5f Apr 2, 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-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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