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

Nonsensical error message with const generics and std::mem::sized_of #62645

Closed
Aaron1011 opened this issue Jul 13, 2019 · 7 comments
Closed

Nonsensical error message with const generics and std::mem::sized_of #62645

Aaron1011 opened this issue Jul 13, 2019 · 7 comments
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` F-on_unimplemented Error messages that can be tackled with `#[rustc_on_unimplemented]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Aaron1011
Copy link
Member

The following code:

#![feature(const_generics)]

trait IsZst {
    type ZST;
}

impl<T: Sized> IsZst for T where T: Sized {
    type ZST = ZstHelper<{std::mem::size_of::<T>() == 0}>;
}

struct ZstHelper<const T: bool>;

produces the following error:

error[E0277]: the size for values of type `T` cannot be known at compilation time
 --> src/lib.rs:8:27
  |
8 |     type ZST = ZstHelper<{std::mem::size_of::<T>() == 0}>;
  |                           ^^^^^^^^^^^^^^^^^^^^^^ 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>
  = help: consider adding a `where T: std::marker::Sized` bound
  = note: required by `std::mem::size_of`

despite the fact that we have that T: Sized. If there's another reason why this code shouldn't compile, the error message should clearly state it.

@jonas-schievink jonas-schievink added A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 14, 2019
@estebank estebank added the F-on_unimplemented Error messages that can be tackled with `#[rustc_on_unimplemented]` label Aug 4, 2019
@jonas-schievink jonas-schievink added F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. labels Aug 6, 2019
@the8472
Copy link
Member

the8472 commented Nov 15, 2019

I have run into this issue several times, here's a case I wouldn't even consider const generics, it is a const eval problem in a generic context instead

const fn batch_size<T: Sized>() -> usize {
    32 / std::mem::size_of::<T>()
}

fn x<T: Sized>() {
    [1; batch_size::<T>()];
}
error[E0277]: the size for values of type `T` cannot be known at compilation time
 --> src/main.rs:9:22
  |
4 | const fn batch_size<T: Sized>() -> usize {
  |          ---------- - required by this bound in `batch_size`
...
8 | fn x<T: Sized>() {
  |      -- help: consider further restricting this bound: `T: std::marker::Sized +`
9 |     [1; batch_size::<T>()];
  |                      ^ doesn't have a size known at compile-time
  |

@golddranks
Copy link
Contributor

@comex
Copy link
Contributor

comex commented Dec 8, 2019

Cool! Although if I extend your example to actually test the associated type:

fn needs_zst<T>(_: T) where T: IsZst<ZST=ZstHelper<{true}>> {}

I get some nice errors:

error[E0391]: cycle detected when const-evaluating + checking `needs_zst::{{constant}}#0`
  --> src/lib.rs:13:52
   |
13 | fn needs_zst<T>(q: T) where T: IsZst<ZST=ZstHelper<{true}>> {}
   |                                                    ^^^^^^
   |
note: ...which requires const-evaluating + checking `needs_zst::{{constant}}#0`...
  --> src/lib.rs:13:52
   |
13 | fn needs_zst<T>(q: T) where T: IsZst<ZST=ZstHelper<{true}>> {}
   |                                                    ^^^^^^
note: ...which requires const-evaluating `needs_zst::{{constant}}#0`...
  --> src/lib.rs:13:52
   |
13 | fn needs_zst<T>(q: T) where T: IsZst<ZST=ZstHelper<{true}>> {}
   |                                                    ^^^^^^
note: ...which requires processing `needs_zst::{{constant}}#0`...
  --> src/lib.rs:13:52
   |
13 | fn needs_zst<T>(q: T) where T: IsZst<ZST=ZstHelper<{true}>> {}
   |                                                    ^^^^^^
note: ...which requires processing `needs_zst::{{constant}}#0`...
  --> src/lib.rs:13:52
   |
13 | fn needs_zst<T>(q: T) where T: IsZst<ZST=ZstHelper<{true}>> {}
   |                                                    ^^^^^^
   = note: ...which again requires const-evaluating + checking `needs_zst::{{constant}}#0`, completing the cycle
note: cycle used when processing `needs_zst`
  --> src/lib.rs:13:1
   |
13 | fn needs_zst<T>(q: T) where T: IsZst<ZST=ZstHelper<{true}>> {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@Aaron1011
Copy link
Member Author

Aaron1011 commented Dec 8, 2019

The cycle appears to be due to eger normalization when constructing the ParamEnv during wf-checking of the impl.

@the8472
Copy link
Member

the8472 commented Dec 8, 2019

The example in OP now gets the cycle error, but my case still fails with the nonsensical one.

@ExpHP
Copy link
Contributor

ExpHP commented Dec 13, 2019

Isn't this just a duplicate of #43408?

(I mean, yeah, that issue is about this not working, while this issue is about the terrible error message; but many of the closed duplicates are also about the error message. The fact that this error message is still here gives me the impression that fixing the message must be as difficult as making it compile!)

@Dylan-DPC-zz
Copy link

I agree with @ExpHP here, since when those issues are fixed the error message is going to change. Closing this as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` F-on_unimplemented Error messages that can be tackled with `#[rustc_on_unimplemented]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants