Skip to content

Generate valid_ranges for enums sign-agnostically#159509

Open
scottmcm wants to merge 5 commits into
rust-lang:mainfrom
scottmcm:redo-valid-range
Open

Generate valid_ranges for enums sign-agnostically#159509
scottmcm wants to merge 5 commits into
rust-lang:mainfrom
scottmcm:redo-valid-range

Conversation

@scottmcm

@scottmcm scottmcm commented Jul 18, 2026

Copy link
Copy Markdown
Member

Prompted by #159438 (comment), this reworks the generation of the tag layout information for enums.

Having gone down the wrong path at first here, I think the core trouble this is having is that the current calculation is trying to serve two incompatible desires:

  • When picking the value: Primitive, we need to respect the signedness of the original discriminant numbers as typed in Rust.
  • When picking the valid_range: WrappingRange, we want to ignore the signedness (and size!) of the discriminants and look only at the bit values (and size) of the tags themselves.

This thus splits the handling in layout_of_enum into those two parts, handled differently.

  • When looking at discriminants (to eventually call discr_range_of_repr), we obey the signedness of the inputs to find the smallest negative discriminant and largest positive discriminant -- notably without any wraparound logic.
  • Then after having used the discriminants to pick a tag size, then we run a wrapping-aware check to pick the best WrappingRange to represent those tags (which can be a different range than we'd have picked on the discriminants, especially for repr(Rust) where the discriminants are isize).

With apologies for sending this when you'd said you'd look at it, but I got inspired 😅
r? @oli-obk

Fixes #159438


I have a bunch of commits in here; it's probably best reviewed one at a time.

  1. Just moving WrappingRange to its own file, since the lib.rs is over 2000 lines already so if I was going to add stuff I figured some reorganizing could help.
  2. Adding a new constructor to WrappingRange that takes an iterator of values and returns a range containing those values. Dealing just in u128 + Size, like WrappingRange does in other places, it doesn't even have signedness information so structurally is prevented from having any "why are repr(u8) and `repr(i8) picking different ranges?" issues. I found it helpful to have this split out from the layout code, for example that it let me put some examples on the doc-comment. Nothing actually uses the new function in this commit though.
  3. Stop giving layout discriminants as i128 since https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/util/struct.Discr.html#structfield.val points out that they're generated as u128 in the same way that WrappingRange uses already -- even with the field doc that -1_i8 is generated as 255_u128, so the as i128ing was probably just making everything more confusing here than it needed to be.
  4. Adding some tests looking at the generated Primitive and valid_range, along with rustc support for #[rustc_dump_layout(largest_niche)] to help have these tests include specific ERROR annotations rather than just the stderr. The tests pass at this point, so the next commit can update them to show the change.
  5. This one actually updates layout code, including using the WrappingRange constructor added earlier.

@rustbot

rustbot commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) 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 Jul 18, 2026
@rust-log-analyzer

This comment has been minimized.

Comment thread tests/ui/layout/hexagon-enum.stderr Outdated
@scottmcm
scottmcm force-pushed the redo-valid-range branch 2 times, most recently from 89681fc to fc374f1 Compare July 18, 2026 11:04
@scottmcm

This comment was marked as outdated.

@scottmcm
scottmcm marked this pull request as draft July 18, 2026 11:14
@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 Jul 18, 2026
@rust-log-analyzer

This comment has been minimized.

@scottmcm

Copy link
Copy Markdown
Member Author

Ok, the first version I'd misunderstood how we need to pick tag signedness, so redid that and added a bunch of new tests because there were disturbingly few failures when I'd probably broken a ton of stuff (albeit subtly).

OP updated as well to describe the fixed approach.
@rustbot ready

@scottmcm
scottmcm marked this pull request as ready for review July 18, 2026 21:44
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 18, 2026
@scottmcm

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 18, 2026
rust-bors Bot pushed a commit that referenced this pull request Jul 18, 2026
Generate `valid_range`s for enums sign-agnostically
@rust-bors

rust-bors Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 8d18ada (8d18adad4abcffe08e2c9c56e062fe6826106354)
Base parent: eff8269 (eff8269f797067c30555e77f160ec84c0ed15cd9)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (8d18ada): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

This perf run didn't have relevant results for this metric.

Cycles

Results (primary -2.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.4% [-2.4%, -2.4%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.4% [-2.4%, -2.4%] 1

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 489.126s -> 483.723s (-1.10%)
Artifact size: 389.52 MiB -> 389.47 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Size of Option of 128-variant repr(i8) enum has size 2 bytes instead of 1.

5 participants