Closed
Description
I tried this code (playground):
pub trait Max {
const MAX: usize;
}
pub struct Something();
impl Max for Something {
const MAX: usize = 10;
}
struct Data<M: Max> {
storage: [u8; <M as Max>::MAX],
// alternatively you could define `storage` as the following and a similar error will result.
// storage: [u8; M::MAX],
_marker: std::marker::PhantomData<M>,
}
fn main() {
println!("{}", std::mem::size_of::<Data<Something>>());
}
I expected to see this happen: it compile and the size of the type to be 10.
Instead, this happened: it did not compile and it reported the following confusing/wrong error message:
error[E0277]: the trait bound `M: Max` is not satisfied
--> src/main.rs:12:19
|
2 | const MAX: usize;
| ----------------- required by `Max::MAX`
...
12 | storage: [u8; <M as Max>::MAX],
| ^^^^^^^^^^^^^^^ the trait `Max` is not implemented for `M`
|
help: consider further restricting this bound
|
11 | struct Data<M: Max + Max> {
| ^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
It asks for the trait bound that is currently present to be added (and adding it produces the same error again).
Meta
I checked this on the playground and it happens on:
stable
(1.46.0)beta
(1.47.0-beta.5 2020-09-29 9f0e6fa...)nightly
(1.48.0-nightly 2020-09-28 fc2daaa...)
stable
and nightly
also produced the same error on my personal computer.
rustc --version --verbose
:
rustc 1.46.0 (04488afe3 2020-08-24)
binary: rustc
commit-hash: 04488afe34512aa4c33566eb16d8c912a3ae04f9
commit-date: 2020-08-24
host: x86_64-unknown-linux-gnu
release: 1.46.0
LLVM version: 10.0