Rollup merge of #70970 - eddyb:trait-vs-impl-mismatch, r=oli-obk

Detect mistyped associated consts in `Instance::resolve`.

*Based on #71049 to prevent redundant/misleading downstream errors.*

Fixes #70942 by refusing to resolve an associated `const` if it doesn't have the same type in the `impl` that it does in the `trait` (which we assume had errored, and `delay_span_bug` guards against bugs).
This commit is contained in:
Dylan DPC 2020-04-22 12:18:31 +02:00 committed by GitHub
commit 707004c552
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 157 additions and 67 deletions

View file

@ -0,0 +1,14 @@
trait Nat {
const VALUE: usize;
}
struct Zero;
impl Nat for Zero {
const VALUE: i32 = 0;
//~^ ERROR implemented const `VALUE` has an incompatible type for trait
}
fn main() {
let _: [i32; Zero::VALUE] = [];
}

View file

@ -0,0 +1,12 @@
error[E0326]: implemented const `VALUE` has an incompatible type for trait
--> $DIR/issue-70942-trait-vs-impl-mismatch.rs:8:18
|
LL | const VALUE: usize;
| ----- type in trait
...
LL | const VALUE: i32 = 0;
| ^^^ expected `usize`, found `i32`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0326`.

View file

@ -19,5 +19,4 @@ impl TraitB for B { //~ ERROR not all trait items implemented, missing: `MyA`
fn main() {
let _ = [0; B::VALUE];
//~^ ERROR constant expression depends on a generic parameter
}

View file

@ -13,15 +13,7 @@ LL | type MyA: TraitA;
LL | impl TraitB for B {
| ^^^^^^^^^^^^^^^^^ missing `MyA` in implementation
error: constant expression depends on a generic parameter
--> $DIR/issue-69602-type-err-during-codegen-ice.rs:21:17
|
LL | let _ = [0; B::VALUE];
| ^^^^^^^^
|
= note: this may fail depending on what value the parameter takes
error: aborting due to 3 previous errors
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0046, E0437.
For more information about an error, try `rustc --explain E0046`.