Description
On type mismatchses when using trait objects, rust emits error E0277, saying that the trait does not have a constant size known at compile-time, and that all local variables must have a statically known type. In fact, the problem lies elsewhere, and the program can be fixed without addressing the E0277 error at all.
I wrote this code:
trait T {}
struct A;
impl T for A {}
impl A {
fn new() -> Self {
Self {}
}
}
fn main() {
let (a, b, c) = (A::new(), A::new()); // This tuple is 2 elements, should be three
let ts: Vec<&T> = vec![&a, &b, &c];
println!("{}", ts.len());
}
The marked tuple has 2 A::new()
copies, when it should have 3. If this is fixed, the code compiles. I would expect to see a single error pointing out this fact. Instead, four errors are returned:
Compiling bug v0.1.0 (file:///home/isaac/prog/rust/bug)
error[E0308]: mismatched types
--> src/main.rs:12:9
|
12 | let (a, b, c) = (A::new(), A::new());
| ^^^^^^^^^ expected a tuple with 2 elements, found one with 3 elements
|
= note: expected type `(A, A)`
found type `(_, _, _)`
error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
--> src/main.rs:12:10
|
12 | let (a, b, c) = (A::new(), A::new());
| ^ `T` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= note: all local variables must have a statically known size
error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
--> src/main.rs:12:13
|
12 | let (a, b, c) = (A::new(), A::new());
| ^ `T` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= note: all local variables must have a statically known size
error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
--> src/main.rs:12:16
|
12 | let (a, b, c) = (A::new(), A::new());
| ^ `T` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= note: all local variables must have a statically known size
error: aborting due to 4 previous errors
error: Could not compile `bug`.
Only the first of these errors is related to the bug - the other three are all false and/or irrelevant.
Meta
rustc --version --verbose
:
rustc 1.25.0 (84203ca 2018-03-25)
binary: rustc
commit-hash: 84203ca
commit-date: 2018-03-25
host: x86_64-unknown-linux-gnu
release: 1.25.0
LLVM version: 6.0
Also produced on nightly 1.27.0