Skip to content

Prefer -1 for None#155473

Draft
scottmcm wants to merge 1 commit intorust-lang:mainfrom
scottmcm:tweak_niche_assignment
Draft

Prefer -1 for None#155473
scottmcm wants to merge 1 commit intorust-lang:mainfrom
scottmcm:tweak_niche_assignment

Conversation

@scottmcm
Copy link
Copy Markdown
Member

@scottmcm scottmcm commented Apr 18, 2026

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)
Copy link
Copy Markdown
Member Author

@scottmcm scottmcm Apr 18, 2026

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
Copy link
Copy Markdown
Collaborator

Awaiting bors try build completion.

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

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

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.
@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
Copy link
Copy Markdown
Collaborator

This pull request is already queued and waiting for a try build to finish.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Apr 18, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 18, 2026

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

@rust-timer
Copy link
Copy Markdown
Collaborator

Queued ed76e05 with parent 2f201bc, future comparison URL.
There is currently 1 preceding artifact in the queue.
It will probably take at least ~1.7 hours until the benchmark run finishes.

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. S-waiting-on-perf Status: Waiting on a perf run to be completed. 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.

4 participants