Skip to content

Prefer -1 for None#155473

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
scottmcm:tweak_niche_assignment
Apr 26, 2026
Merged

Prefer -1 for None#155473
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
scottmcm:tweak_niche_assignment

Conversation

@scottmcm

@scottmcm scottmcm commented Apr 18, 2026

Copy link
Copy Markdown
Member

View all comments

Currently we pick "weird" numbers like 1114112 for None::<char>. While that's not wrong, it's kinda unnatural -- a human wouldn't make that choice.

This PR instead picks -1 for thinge like None::<char> -- like clang's WEOF -- and None::<bool> and such.

Any enums with more than one niched value (so not Result nor Option) remain as they were before. Also we continue to use 0 when that's possible -- -1 is only preferred when zero isn't possible.


Inspired when someone in discord posted an example like this https://rust.godbolt.org/z/W94s9qdYW and I thought it was odd that we're currently picking -9223372036854775808 to be the value to store to mark an Option<Vec<_>> as None. (Especially since that needs an 8-byte immediate on x64, and writing -1 is only a 4-byte immediate.)

@rustbot rustbot added 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 Apr 18, 2026
Comment on lines 2091 to 2092
// zero is unavailable because wrapping occurs
move_end(v)

@scottmcm scottmcm Apr 18, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note that this arm just kinda gave up before; it never even considered whether putting it next to start might be better.

View changes since the review

@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.

rust-bors Bot pushed a commit that referenced this pull request Apr 18, 2026
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 18, 2026
@rust-log-analyzer

This comment has been minimized.

@scottmcm scottmcm force-pushed the tweak_niche_assignment branch from 302d83d to a146c66 Compare April 18, 2026 06:26
@scottmcm

Copy link
Copy Markdown
Member Author

It helps to remember to git add all the changes 🤦

@rust-bors

This comment was marked as outdated.

@rust-log-analyzer

This comment has been minimized.

@scottmcm scottmcm force-pushed the tweak_niche_assignment branch from a146c66 to 09df883 Compare April 18, 2026 07:26
@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.

rust-bors Bot pushed a commit that referenced this pull request Apr 18, 2026
@rust-bors

rust-bors Bot commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: ed76e05 (ed76e056cbb36453c64da4dd8ffccac4e093f819, parent: 2f201bccb3a7fb5a85b0fcfcc0a020a946d6d58a)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (ed76e05): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.3% [0.3%, 0.3%] 1
Regressions ❌
(secondary)
0.5% [0.0%, 1.5%] 9
Improvements ✅
(primary)
-0.3% [-2.9%, -0.2%] 86
Improvements ✅
(secondary)
-0.3% [-1.8%, -0.0%] 62
All ❌✅ (primary) -0.3% [-2.9%, 0.3%] 87

Max RSS (memory usage)

Results (secondary 1.1%)

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)
6.5% [6.5%, 6.5%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.4% [-4.4%, -4.4%] 1
All ❌✅ (primary) - - 0

Cycles

Results (primary -2.9%, secondary -4.2%)

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)
1.8% [1.8%, 1.8%] 1
Improvements ✅
(primary)
-2.9% [-3.0%, -2.9%] 2
Improvements ✅
(secondary)
-6.2% [-7.6%, -3.4%] 3
All ❌✅ (primary) -2.9% [-3.0%, -2.9%] 2

Binary size

Results (primary -0.2%, secondary -0.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.3% [0.0%, 0.6%] 2
Improvements ✅
(primary)
-0.2% [-0.6%, -0.1%] 5
Improvements ✅
(secondary)
-0.9% [-2.1%, -0.3%] 3
All ❌✅ (primary) -0.2% [-0.6%, -0.1%] 5

Bootstrap: 491.618s -> 493.312s (0.34%)
Artifact size: 394.25 MiB -> 396.31 MiB (0.52%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Apr 18, 2026
@scottmcm

scottmcm commented Apr 18, 2026

Copy link
Copy Markdown
Member Author

Wow, those results are way better than I expected 😅

Seems pretty clearly a net win to me.
@rustbot label: +perf-regression-triaged

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

rustbot commented Apr 18, 2026

Copy link
Copy Markdown
Collaborator

r? @chenyukang

rustbot has assigned @chenyukang.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 72 candidates
  • Random selection from 18 candidates

@rust-bors

rust-bors Bot commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: chenyukang,mati865
Duration: 3h 24m 15s
Pushing d4f7856 to main...

@rust-bors rust-bors Bot merged commit d4f7856 into rust-lang:main Apr 26, 2026
13 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor
What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing c7fe5e9 (parent) -> d4f7856 (this PR)

Test differences

Show 2 test diffs

2 doctest diffs were found. These are ignored, as they are noisy.

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard d4f78565699b229227c9e9ca8164efa3d5d7213e --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-x86_64-apple: 2h 29m -> 1h 42m (-31.2%)
  2. x86_64-gnu: 1h 49m -> 2h 18m (+27.1%)
  3. x86_64-gnu-llvm-21: 1h 14m -> 54m 30s (-27.0%)
  4. dist-x86_64-illumos: 1h 24m -> 1h 46m (+26.9%)
  5. x86_64-gnu-llvm-21-3: 1h 27m -> 1h 49m (+24.9%)
  6. dist-arm-linux-musl: 1h 20m -> 1h 38m (+22.6%)
  7. x86_64-gnu-llvm-22-2: 1h 31m -> 1h 12m (-21.5%)
  8. dist-apple-various: 1h 50m -> 1h 33m (-15.1%)
  9. dist-powerpc-linux: 1h 26m -> 1h 13m (-14.4%)
  10. i686-msvc-1: 3h 3m -> 2h 38m (-13.7%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (d4f7856): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.7% [0.2%, 1.9%] 5
Improvements ✅
(primary)
-0.3% [-2.7%, -0.1%] 67
Improvements ✅
(secondary)
-0.3% [-0.8%, -0.1%] 52
All ❌✅ (primary) -0.3% [-2.7%, -0.1%] 67

Max RSS (memory usage)

Results (secondary -1.9%)

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)
- - 0
Improvements ✅
(secondary)
-1.9% [-1.9%, -1.9%] 1
All ❌✅ (primary) - - 0

Cycles

Results (primary -2.5%)

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.5% [-2.5%, -2.5%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.5% [-2.5%, -2.5%] 1

Binary size

Results (primary -0.2%, secondary -0.1%)

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.0% [0.0%, 0.0%] 3
Improvements ✅
(primary)
-0.2% [-0.6%, -0.0%] 25
Improvements ✅
(secondary)
-0.1% [-0.3%, -0.0%] 6
All ❌✅ (primary) -0.2% [-0.6%, -0.0%] 25

Bootstrap: 488.641s -> 488.853s (0.04%)
Artifact size: 393.90 MiB -> 393.54 MiB (-0.09%)

rust-bors Bot pushed a commit that referenced this pull request Apr 27, 2026
Only exclude the #155473 change for 1-byte bool-likes


try-job: x86_64-gnu-llvm-22-1
try-job: x86_64-gnu-llvm-21-1
rust-bors Bot pushed a commit that referenced this pull request Apr 28, 2026
Only exclude the #155473 change for 1-byte bool-likes

That's the thing we handle differently in codegen (see `to_immediate_scalar`) so if the other ones are fine, that helps narrow it down further.

cc #155473
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 28, 2026
…=mati865

Only exclude the rust-lang#155473 change for 1-byte bool-likes

That's the thing we handle differently in codegen (see `to_immediate_scalar`) so if the other ones are fine, that helps narrow it down further.

cc rust-lang#155473
rust-bors Bot pushed a commit that referenced this pull request Apr 28, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #155850 (Only exclude the #155473 change for 1-byte bool-likes)
 - #155923 (Subtree sync for rustc_codegen_cranelift)
 - #151994 (switch to v0 mangling by default on stable)
 - #154325 (Tweak irrefutable let else warning output)
 - #155899 (`dlltool`: Set the working directory to workaround `--temp-prefix` bug)
 - #155273 (Lock stable_crate_ids once in create_crate_num)
 - #155361 (Document that CFI diverges from Rust wrt. ABI-compatibility rules)
 - #155692 (disable naked-dead-code-elimination test if no RET mnemonic is available)
 - #155747 (Update documentation for `wasm32-wali-linux-musl` after integrating n…)
 - #155768 (compiletest: Overhaul the code for running an incremental test revision)
 - #155907 (Handle hkl const closures)
 - #155910 (misc stuff from reading borrowck again :))
 - #155913 (Delete the 12 year old fixme)
 - #155920 (remove review queue triagebot mentions)
rust-bors Bot pushed a commit that referenced this pull request Apr 28, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #155850 (Only exclude the #155473 change for 1-byte bool-likes)
 - #155923 (Subtree sync for rustc_codegen_cranelift)
 - #151994 (switch to v0 mangling by default on stable)
 - #154325 (Tweak irrefutable let else warning output)
 - #155899 (`dlltool`: Set the working directory to workaround `--temp-prefix` bug)
 - #155273 (Lock stable_crate_ids once in create_crate_num)
 - #155361 (Document that CFI diverges from Rust wrt. ABI-compatibility rules)
 - #155692 (disable naked-dead-code-elimination test if no RET mnemonic is available)
 - #155747 (Update documentation for `wasm32-wali-linux-musl` after integrating n…)
 - #155768 (compiletest: Overhaul the code for running an incremental test revision)
 - #155907 (Handle hkl const closures)
 - #155910 (misc stuff from reading borrowck again :))
 - #155913 (Delete the 12 year old fixme)
 - #155920 (remove review queue triagebot mentions)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 28, 2026
…=mati865

Only exclude the rust-lang#155473 change for 1-byte bool-likes

That's the thing we handle differently in codegen (see `to_immediate_scalar`) so if the other ones are fine, that helps narrow it down further.

cc rust-lang#155473
rust-bors Bot pushed a commit that referenced this pull request Apr 28, 2026
…uwer

Rollup of 15 pull requests

Successful merges:

 - #155923 (Subtree sync for rustc_codegen_cranelift)
 - #155930 (Sync from portable simd 2026 04 28)
 - #155850 (Only exclude the #155473 change for 1-byte bool-likes)
 - #151994 (switch to v0 mangling by default on stable)
 - #154325 (Tweak irrefutable let else warning output)
 - #155273 (Lock stable_crate_ids once in create_crate_num)
 - #155361 (Document that CFI diverges from Rust wrt. ABI-compatibility rules)
 - #155692 (disable naked-dead-code-elimination test if no RET mnemonic is available)
 - #155747 (Update documentation for `wasm32-wali-linux-musl` after integrating n…)
 - #155768 (compiletest: Overhaul the code for running an incremental test revision)
 - #155907 (Handle hkl const closures)
 - #155910 (misc stuff from reading borrowck again :))
 - #155913 (Delete the 12 year old fixme)
 - #155920 (remove review queue triagebot mentions)
 - #155936 (Rename `SharedContext::emit_dyn_lint*` into `emit_lint*`)
rust-bors Bot pushed a commit that referenced this pull request Apr 28, 2026
…uwer

Rollup of 15 pull requests

Successful merges:

 - #155923 (Subtree sync for rustc_codegen_cranelift)
 - #155930 (Sync from portable simd 2026 04 28)
 - #155850 (Only exclude the #155473 change for 1-byte bool-likes)
 - #151994 (switch to v0 mangling by default on stable)
 - #154325 (Tweak irrefutable let else warning output)
 - #155273 (Lock stable_crate_ids once in create_crate_num)
 - #155361 (Document that CFI diverges from Rust wrt. ABI-compatibility rules)
 - #155692 (disable naked-dead-code-elimination test if no RET mnemonic is available)
 - #155747 (Update documentation for `wasm32-wali-linux-musl` after integrating n…)
 - #155768 (compiletest: Overhaul the code for running an incremental test revision)
 - #155907 (Handle hkl const closures)
 - #155910 (misc stuff from reading borrowck again :))
 - #155913 (Delete the 12 year old fixme)
 - #155920 (remove review queue triagebot mentions)
 - #155936 (Rename `SharedContext::emit_dyn_lint*` into `emit_lint*`)
rust-timer added a commit that referenced this pull request Apr 28, 2026
Rollup merge of #155850 - scottmcm:tweak_niche_assignment, r=mati865

Only exclude the #155473 change for 1-byte bool-likes

That's the thing we handle differently in codegen (see `to_immediate_scalar`) so if the other ones are fine, that helps narrow it down further.

cc #155473
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Apr 30, 2026
…uwer

Rollup of 15 pull requests

Successful merges:

 - rust-lang/rust#155923 (Subtree sync for rustc_codegen_cranelift)
 - rust-lang/rust#155930 (Sync from portable simd 2026 04 28)
 - rust-lang/rust#155850 (Only exclude the rust-lang/rust#155473 change for 1-byte bool-likes)
 - rust-lang/rust#151994 (switch to v0 mangling by default on stable)
 - rust-lang/rust#154325 (Tweak irrefutable let else warning output)
 - rust-lang/rust#155273 (Lock stable_crate_ids once in create_crate_num)
 - rust-lang/rust#155361 (Document that CFI diverges from Rust wrt. ABI-compatibility rules)
 - rust-lang/rust#155692 (disable naked-dead-code-elimination test if no RET mnemonic is available)
 - rust-lang/rust#155747 (Update documentation for `wasm32-wali-linux-musl` after integrating n…)
 - rust-lang/rust#155768 (compiletest: Overhaul the code for running an incremental test revision)
 - rust-lang/rust#155907 (Handle hkl const closures)
 - rust-lang/rust#155910 (misc stuff from reading borrowck again :))
 - rust-lang/rust#155913 (Delete the 12 year old fixme)
 - rust-lang/rust#155920 (remove review queue triagebot mentions)
 - rust-lang/rust#155936 (Rename `SharedContext::emit_dyn_lint*` into `emit_lint*`)
@apiraino

Copy link
Copy Markdown
Contributor

We are observing some breakage in a number of crates (see linked issues).
There is no layout guarantees so this is technically not a breaking change. This said, should we give some visibility about this in the release notes?

@scottmcm scottmcm added the relnotes Marks issues that should be documented in the release notes of the next release. label May 29, 2026
@scottmcm

Copy link
Copy Markdown
Member Author

@apiraino Agreed, a compatibility note sounds like a good idea.

@BrianIAm

BrianIAm commented Jun 8, 2026

Copy link
Copy Markdown

Just a small note: 1114112 isn't an arbitrary "weird" number, it's 0x110000, the first value above the Unicode codespace (valid scalar values are 0x0000..=0x10FFFF), which makes it a natural niche for char specifically. It's the tightest possible niche that still lies outside the valid range.

That said, -1 is arguably more ergonomic in practice (smaller immediate on x86-64, matches WEOF convention, easier to spot in a debugger), so the change may still be the right call.

If the value does stay as -1, it might be worth a comment explaining why 0x110000 was the previous choice, so the next reader doesn't have to work it out. And if it stays as 0x110000, a comment like // 0x110000 - one past the end of valid Unicode scalar values would prevent the "weird number" confusion from recurring.

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

Labels

merged-by-bors This PR was explicitly merged by bors. relnotes Marks issues that should be documented in the release notes of the next release. 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.