Skip to content

Improve const array diagnostics (points 1 and 3 of #162851) - #165976

Open
Absobel wants to merge 2 commits into
rust-lang:mainfrom
Absobel:valtree-print-arrays
Open

Absobel wants to merge 2 commits into
rust-lang:mainfrom
Absobel:valtree-print-arrays

Conversation

@Absobel

@Absobel Absobel commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Point 1 of #162851

I just deleted the match arm that made [u8; N] display as *b"..."
The only "regression" is that consts written as byte strings also display as arrays but well, this seems like a smaller use case than arrays? I thought about conditionally writing it as a byte string if the bytes were ASCII but [42] would then display as *b"*" which is not very readable either.

Also, for consistency, I also made u8 slices display as lists. This had the additional consequence of fixing a bug where a mismatch in slice content would display an error exposing some internal structs:

struct S<const A: &'static[u32]>;

fn main() {
    let a: S<{ &[1, 2, 3] }> = S::<{ &[4, 5, 6] }>;
}
error[E0308]: mismatched types
 --> src/main.rs:6:32
  |
6 |     let a: S<{ &[1, 2, 3] }> = S::<{ &[4, 5, 6] }>;
  |            -----------------   ^^^^^^^^^^^^^^^^^^^ expected `&Branch([1_u32, 2_u32, 3_u32])`, found `&Branch([4_u32, 5_u32, 6_u32])`
  |            |
  |            expected due to this
  |
  = note: expected struct `S<&Branch([1_u32, 2_u32, 3_u32])>`
             found struct `S<&Branch([4_u32, 5_u32, 6_u32])>`

Vs now:

error[E0308]: mismatched types
 --> src/main.rs:6:32
  |
6 |     let a: S<{ &[1, 2, 3] }> = S::<{ &[4, 5, 6] }>;
  |            -----------------   ^^^^^^^^^^^^^^^^^^^ expected `&[1, 2, 3]`, found `&[4, 5, 6]`
  |            |
  |            expected due to this
  |
  = note: expected struct `S<&[1, 2, 3]>`
             found struct `S<&[4, 5, 6]>`

r? khyperia

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 14, 2026
@rustbot

rustbot commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

khyperia is not on the review rotation at the moment.
They may take a while to respond.

@rustbot

This comment has been minimized.

@Absobel
Absobel force-pushed the valtree-print-arrays branch from f03b9bb to 7c6adb3 Compare November 14, 2026 20:56
@rust-log-analyzer

This comment has been minimized.

- `[u8; N]` now print as `[1, 2, 3]` instead of `*b"..."`
- For consistency, `&[u8]` now prints as `&[1, 2, 3]` too. This fixed
  internal structs being displayed for non `u8` slices.
@Absobel
Absobel force-pushed the valtree-print-arrays branch from 7c6adb3 to 75fb503 Compare November 15, 2026 00:08
@Absobel Absobel changed the title Print u8 array and slice const valtrees as lists, not byte strings Improve const array diagnostics (points 1 and 3 of #162851) Nov 15, 2026
@Absobel

Absobel commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Covered point 3 of #162851 too

I just made the new error head be "array lenght mismatch" and made the error hold a TypeError:ArraySize with the expected and found lengths (though the case without the gca features make it return a E0308 mismatched types error, so maybe this whole branch should do so too?)

@bit-aloo bit-aloo left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes looks good to me. Some suggestions. Thanks for working on this

View changes since this review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this into the else block below? That's the only place it's used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

let (msg, terr) = if let ty::Array(ct_elem_ty, ct_len) = ct_ty.kind()
&& let ty::Array(expected_elem_ty, expected_len) = expected_ty.kind()
&& ct_elem_ty == expected_elem_ty
&& ct_len != expected_len

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed

Suggested change
&& ct_len != expected_len

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and done


const X4: &[u8; usize::MAX] = b"";
//~^ ERROR the constant `&*b""` is not of type `&'static [u8; usize::MAX]`
//~^ ERROR the constant `&[]` is not of type `&'static [u8; usize::MAX]`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... we are not covering ref to arrays. Lets do it.

@Absobel Absobel Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does just calling peel_ref() on both work? or is it better to verify they have the same number of references, so that comparing &[u8; 2] and [u8; 3] wouldn't complain about the length of the array first? (though that seems like a niche case, but i prefer asking)

I'll do the peel ref for now in case it's the right call (for CI, might as well)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvmd i thought it was giving me the array length mismatch error when I compared a ref and an array but not at all. So, it works fine

@Absobel
Absobel force-pushed the valtree-print-arrays branch 2 times, most recently from 3c82515 to f1b4f6e Compare November 15, 2026 16:37
@Absobel
Absobel force-pushed the valtree-print-arrays branch from f1b4f6e to 899fb90 Compare November 15, 2026 18:23
@khyperia

Copy link
Copy Markdown
Member

thanks for the PR!

I thought about conditionally writing it as a byte string if the bytes were ASCII but [42] would then display as *b"*" which is not very readable either.

not sure if you saw what oli said in zulip, but:

https://rust-lang.zulipchat.com/#narrow/channel/260443-project-const-generics/topic/casual.20chat.20and.20support/near/641270414

I do like having byte strs printed, but I also see how annoying it is. My preference would be to just look at the value and if it is alphanumeric + common sigils that are fine to print, then print it as a str, otherwise as an array. But that's just a "would be nice if we had some good UX", so throw it out if there are reasons not to

that seems reasonable to me

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 23, 2026
@rustbot

rustbot commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants