-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #133367 - compiler-errors:array-len-mismatch, r=BoxyUwU
Simplify array length mismatch error reporting (to not try to turn consts into target usizes) This changes `TypeError::FixedArrayLen` to use `ExpectedFound<ty::Const<'tcx>>` (instead of `ExpectedFound<u64>`), and renames it to `TypeError::ArrayLen`. This allows us to avoid a `try_to_target_usize` call in the type relation, which ICEs when we have a scalar of the wrong bit length (i.e. u8). This also makes `structurally_relate_tys` to always use this type error kind any time we have a const mismatch resulting from relating the array-len part of `[T; N]`. This has the effect of changing the error message we issue for array length mismatches involving non-valtree consts. I actually quite like the change, though, since before: ``` LL | fn test<const N: usize, const M: usize>() -> [u8; M] { | ------- expected `[u8; M]` because of return type LL | [0; N] | ^^^^^^ expected `M`, found `N` | = note: expected array `[u8; M]` found array `[u8; N]` ``` and after, which I think is far less verbose: ``` LL | fn test<const N: usize, const M: usize>() -> [u8; M] { | ------- expected `[u8; M]` because of return type LL | [0; N] | ^^^^^^ expected an array with a size of M, found one with a size of N ``` The only questions I have are: 1. Should we do something about backticks here? Right now we don't backtick either fully evaluated consts like `2`, or rigid consts like `Foo::BAR`.... but maybe we should? It seems kinda verbose to do for numbers -- maybe we could intercept those specifically. 2. I guess we may still run the risk of leaking unevaluated consts into error reporting like `2 + 1`...? r? ``@BoxyUwU`` Fixes #126359 Fixes #131101
- Loading branch information
Showing
18 changed files
with
60 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
struct BadArraySize<const N: u8> { | ||
arr: [i32; N], | ||
//~^ ERROR the constant `N` is not of type `usize` | ||
} | ||
|
||
fn main() { | ||
let _ = BadArraySize::<2> { arr: [0, 0, 0] }; | ||
//~^ ERROR mismatched types | ||
//~| ERROR the constant `2` is not of type `usize` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
error: the constant `N` is not of type `usize` | ||
--> $DIR/bad-array-size-in-type-err.rs:2:10 | ||
| | ||
LL | arr: [i32; N], | ||
| ^^^^^^^^ expected `usize`, found `u8` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/bad-array-size-in-type-err.rs:7:38 | ||
| | ||
LL | let _ = BadArraySize::<2> { arr: [0, 0, 0] }; | ||
| ^^^^^^^^^ expected an array with a size of 2, found one with a size of 3 | ||
|
||
error: the constant `2` is not of type `usize` | ||
--> $DIR/bad-array-size-in-type-err.rs:7:38 | ||
| | ||
LL | let _ = BadArraySize::<2> { arr: [0, 0, 0] }; | ||
| ^^^^^^^^^ expected `usize`, found `u8` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters