rust-lang / rust-lang/rust

mgca: const array diagnostics aren't great

Open
#162,851 0 comments 0 reactions 0 assignees View on GitHub

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:

  1. [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

https://github.com/rust-lang/rust/blob/28e8a8c81bf3b37909edac6c2a76e56f30cd492f/compiler/rustc_middle/src/ty/print/pretty.rs#L1910-L1918

  1. 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`
  1. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.