Skip to content

Separate Primitive::Bool and make #[repr($int)] enums always use the ABI of $int - #165481

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
asquared31415:enum-int-repr
Nov 23, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
asquared31415:enum-int-repr

Conversation

@asquared31415

@asquared31415 asquared31415 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

This properly gives bool its own ABI, shared only with bool-like enums that have no explicit repr. #[repr($int)] enums now always use the ABI of $int.

This PR makes no language guarantees, however it will help facilitate such guarantees, if they're decided on: rust-lang/unsafe-code-guidelines#628

Additionally, it seems to give LLVM slightly more information about the tags of certain enums, especially when it comes to variants with different sizes.

Previously, bools were identified for the purposes of ABI as being any u8 primitive that had a valid range of 0..=1. An enum with #[repr(u8)] and with discriminants 0 and 1 would satisfy that condition and be treated like a bool. This is somewhat sketchy, and while no miscompilation has been found, I would not be surprised if the i1 Extension Situation could cause problems on some targets.

fixes #165026

cc #165800. this PR doesn't make that situation any worse, but it is bool ABI related, and this PR adds a commented-out test related to this pre-existing situation.

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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. labels Nov 5, 2026
@rust-log-analyzer

This comment has been minimized.

@mu001999

mu001999 commented Nov 5, 2026

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 5, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 5, 2026
Separate `Primitive::Bool` and make `#[repr($int)]` enums always use the ABI of $int
@rust-log-analyzer

This comment has been minimized.

@asquared31415

This comment was marked as resolved.

@rust-bors

rust-bors Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 309247f (309247f2d061081730fed50fb52ae20610509cc8)
Base parent: 2d20b42 (2d20b42b883dcfc014f3c2c56081eeb6091c2179)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (309247f): 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)

Results (primary 3.2%, secondary 0.2%)

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

mean range count
Regressions ❌
(primary)
3.2% [2.1%, 3.8%] 3
Regressions ❌
(secondary)
2.5% [2.1%, 3.2%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.7% [-5.3%, -2.5%] 3
All ❌✅ (primary) 3.2% [2.1%, 3.8%] 3

Cycles

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

Binary size

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

Bootstrap: 480.462s -> 480.743s (0.06%)
Artifact size: 406.22 MiB -> 406.31 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 5, 2026
@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. label Nov 6, 2026

@asquared31415 asquared31415 Nov 6, 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.

This file is one of two meaningful test changes, the rest are debug layouts changing as expected.

This function and the corresponding function converting the other way are "regressed" in that now there will be an explicit trunc or zext. They were testing for the bad behavior (a repr(u8) that's actually a i1).

The trunc/ext should go away at the assembly level though.

View changes since the review

@asquared31415 asquared31415 Nov 6, 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.

This file is the second meaningful test change.

The changes here are scary because I'm not quite sure of the requirements, but I asked on zulip for a second opinion.

I believe this is a correct change, with LLVM using more knowledge about the enum tag to generate a different instruction.

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, this looks fine to me. It makes sense that LLVM now exploits the known 0..=1 range of the discriminant.

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.

Thank you!

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "nightly", derive(StableHash))]
pub enum Primitive {
Bool,

@workingjubilee workingjubilee Nov 7, 2026 •

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.

I had been wondering when this would happen. Thanks.

View changes since the review

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.

I'm still not so sure about it, between the number of things that were silently non-exhaustively matching on Primitive for the integer type and discarding the rest, and the fact that bool really is a Fancy Integer, I almost want to try and integrate it into the Integer variant.

But also, bool clearly acts differently in several places, so it sort of is special.

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. I have not scrutinized this PR closely. I think putting it in Integer would also make sense to me, but I'm going to wildly guess that it actually goes less 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.

It, at the very least, implies the existence of (bool, is_signed), which im sure explodes things if that accidentally leaks through. Integer also seems to be about being the byte-level "storage" type, so that probably isn't the best idea.

There probably exists a better version of Primitive, with a complete refactor, but this PR seems to at least disentangle bool Enough(tm) to get it to be its own ABI. We can delegate further things to "Future Work".

@asquared31415
asquared31415 marked this pull request as ready for review November 7, 2026 16:38
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 7, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

💔 Test for b4f7fae failed: CI. Failed job:

@workingjubilee

Copy link
Copy Markdown
Member

despair!

@workingjubilee

Copy link
Copy Markdown
Member

@bors try jobs=test-x86_64-gnu-nopt,test-various,test-i686-msvc,test-armhf-gnu

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
Separate `Primitive::Bool` and make `#[repr($int)]` enums always use the ABI of $int


try-job: test-x86_64-gnu-nopt
try-job: test-various
try-job: test-i686-msvc
try-job: test-armhf-gnu
@rust-log-analyzer

This comment has been minimized.

@workingjubilee

Copy link
Copy Markdown
Member

hrm.

@rust-bors

rust-bors Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

💔 Test for acfbdd5 failed: CI. Failed job:

@asquared31415

Copy link
Copy Markdown
Contributor Author

ah right, the range is also not emitted in nopt

@asquared31415

Copy link
Copy Markdown
Contributor Author

Okay I think this should really actually for realsies pass gnu-nopt. i hope.

@RalfJung

Copy link
Copy Markdown
Member

I think that's a thing?
@bors delegate=try

@rust-bors

rust-bors Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

✌️ @asquared31415, you can now perform try builds on this pull request!

Warning

You used the legacy format of the delegate command.
The new format is @bors delegate[=<username>] [<try|review>].

You can now post @bors try to start a try build.

@asquared31415

Copy link
Copy Markdown
Contributor Author

@bors try jobs=test-x86_64-gnu-nopt,test-various,test-i686-msvc,test-armhf-gnu

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
Separate `Primitive::Bool` and make `#[repr($int)]` enums always use the ABI of $int


try-job: test-x86_64-gnu-nopt
try-job: test-various
try-job: test-i686-msvc
try-job: test-armhf-gnu
@rust-bors

rust-bors Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: be0d1c7 (be0d1c7de27cd8cc3c365353f24c57265d8702d1)
Base parent: 01afe53 (01afe534803b93889b85f6028117251cfbd431da)

@workingjubilee

Copy link
Copy Markdown
Member

Nice. @bors r+

@rust-bors

rust-bors Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 30aba32 has been approved by workingjubilee

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 23, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 23, 2026
Rollup of 5 pull requests

Successful merges:

 - #164537 (Parser: Clean up `parse_expr_dot_or_call_with` & simplify `parse_expr_prefix`)
 - #165481 (Separate `Primitive::Bool` and make `#[repr($int)]` enums always use the ABI of $int)
 - #166290 (Require the next-solver for unsafe binders)
 - #165865 (Move more rustc lint attributes to lint helpers.)
 - #166221 (Unstably implement `StaticAllocator` on fundamental types)
@rust-bors
rust-bors Bot merged commit 2da9287 into rust-lang:main Nov 23, 2026
15 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 23, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 23, 2026
Rollup merge of #165481 - asquared31415:enum-int-repr, r=workingjubilee

Separate `Primitive::Bool` and make `#[repr($int)]` enums always use the ABI of $int

This properly gives `bool` its own ABI, shared only with bool-like `enum`s that have **no explicit repr**. `#[repr($int)]` `enum`s now always use the ABI of `$int`.

This PR makes **no language guarantees**, however it will help facilitate such guarantees, if they're decided on: rust-lang/unsafe-code-guidelines#628

Additionally, it seems to give LLVM slightly more information about the tags of certain enums, especially when it comes to variants with different sizes.

Previously, bools were identified for the purposes of ABI as being any `u8` primitive that had a valid range of `0..=1`. An enum with `#[repr(u8)]` and with discriminants `0` and `1` would satisfy that condition and be treated like a bool. This is somewhat sketchy, and while no miscompilation has been found, I would not be surprised if the i1 Extension Situation could cause problems on some targets.

fixes #165026

cc #165800. this PR doesn't make that situation any worse, but it is bool ABI related, and this PR adds a commented-out test related to this pre-existing situation.
bjorn3 pushed a commit to rust-lang/rustc_codegen_cranelift that referenced this pull request Nov 24, 2026
Rollup of 5 pull requests

Successful merges:

 - rust-lang/rust#164537 (Parser: Clean up `parse_expr_dot_or_call_with` & simplify `parse_expr_prefix`)
 - rust-lang/rust#165481 (Separate `Primitive::Bool` and make `#[repr($int)]` enums always use the ABI of $int)
 - rust-lang/rust#166290 (Require the next-solver for unsafe binders)
 - rust-lang/rust#165865 (Move more rustc lint attributes to lint helpers.)
 - rust-lang/rust#166221 (Unstably implement `StaticAllocator` on fundamental types)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect / dubious LLVM-level ABI for two-variant #[repr(u8)] enums: lowered to i1

9 participants