mgca: const array diagnostics aren't great
Open
Nobody has claimed this yet.
A-diagnostics
F-min_generic_const_args
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
followup from https://github.com/rust-lang/rust/pull/162845#pullrequestreview-5221186941
there's at least three ways that array diagnostics are bad, in my opinion:
[u8]is unconditionally formatted as a*b""
#![feature(min_adt_const_params, min_generic_const_args)]
struct S<const A: [u8; 2]>;
fn main() {
let a: S<core::direct_const_arg!([104, 105])> = S::<core::direct_const_arg!([110, 111])>;
}
error[E0308]: mismatched types
--> /d/rust/tests/ui/asdf.rs:4:53
|
LL | let a: S<core::direct_const_arg!([104, 105])> = S::<core::direct_const_arg!([110, 111])>;
| -------------------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `*b"hi"`, found `*b"no"`
| |
| expected due to this
|
= note: expected struct `S<*b"hi">`
found struct `S<*b"no">`
this is due to
- types are not printed, "expected
[1], found[1]" is confusing in the below message. (unsure if the first message can be silenced or something, if the second message always appears. this is due to https://github.com/rust-lang/rust/pull/162845 and there's probably better ways of handling this, not producing additional errors about a not-wf ty, or something)
#![feature(min_adt_const_params, min_generic_const_args)]
struct S<const A: [u16; 1]>;
fn main() {
let a: S<core::direct_const_arg!([1_u16])> = S::<core::direct_const_arg!([1_u32])>;
}
error[E0308]: mismatched types
--> /d/rust/tests/ui/asdf.rs:4:50
|
LL | let a: S<core::direct_const_arg!([1_u16])> = S::<core::direct_const_arg!([1_u32])>;
| ----------------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `[1]`, found `[1]`
| |
| expected due to this
|
= note: expected struct `S<[1]>`
found struct `S<[1]>`
error: the constant `1` is not of type `u16`
--> /d/rust/tests/ui/asdf.rs:4:78
|
LL | let a: S<core::direct_const_arg!([1_u16])> = S::<core::direct_const_arg!([1_u32])>;
| ^^^^^^^ expected `u16`, found `u32`
- length mismatches print the values of every element in the whole array, they don't just print the length mismatch
#![feature(min_adt_const_params, min_generic_const_args)]
struct S<const A: [u32; 5]>;
fn main() {
let a = S::<core::direct_const_arg!([1, 2, 3, 4])>;
}
error: the constant `[1, 2, 3, 4]` is not of type `[u32; 5]`
--> /d/rust/tests/ui/asdf.rs:4:13
|
LL | let a = S::<core::direct_const_arg!([1, 2, 3, 4])>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `[u32; 5]`, found `[u32; 4]`
|
note: required by a const generic parameter in `S`
--> /d/rust/tests/ui/asdf.rs:2:10
|
LL | struct S<const A: [u32; 5]>;
| ^^^^^^^^^^^^^^^^^ required by this const generic parameter in `S`
fyi @BoxyUwU
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked review and the referenced formatting logic in compiler/rustc_middle/src/ty/print/pretty.rs, then reproduce the three examples from the issue in UI tests. Compare diagnostics for byte-array formatting, element types, and length mismatches; done means the resulting errors distinguish the relevant types and report length mismatches without unnecessarily printing every element.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100