Skip to content

Use constant for detecting thin pointer formatting#157933

Open
hanna-kruppe wants to merge 1 commit into
rust-lang:mainfrom
hanna-kruppe:fmt-pointer-const-thin
Open

Use constant for detecting thin pointer formatting#157933
hanna-kruppe wants to merge 1 commit into
rust-lang:mainfrom
hanna-kruppe:fmt-pointer-const-thin

Conversation

@hanna-kruppe

Copy link
Copy Markdown
Contributor

This allows codegen to prune the unnecessary side of the if for each pointer type during monomorphization. In release builds, LLVM can clean it up, but it's better to not emit unnecessary code in the first place.

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

rustbot commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

r? @Darksonn

rustbot has assigned @Darksonn.
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: libs
  • libs expanded to 11 candidates
  • Random selection from 6 candidates

@hanna-kruppe hanna-kruppe changed the title use constant for detecting thin pointer formatting Use constant for detecting thin pointer formatting Jun 15, 2026
This allows codegen to prune the unnecessary side of the `if` for each
pointer type during monomorphization. LLVM optimizations can clean it up
too, but it's better to not emit unnecessary code in the first place.
@hanna-kruppe hanna-kruppe force-pushed the fmt-pointer-const-thin branch from 06557a2 to 38f4f4a Compare June 15, 2026 20:20
Comment thread library/core/src/unit.rs
true
}
// `type_id` erases lifetimes, but that's OK here because "is it ()" never depends on lifetimes
const IS_UNIT: bool = type_id::<Self>() == type_id::<()>();

@hanna-kruppe hanna-kruppe Jun 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.

Note that this replaces use of specialization with use of non-'static TypeId. I'm not sure if that's better or worse as far as using unstable features in std is concerned. The change to the intrinsic that makes this work is relatively recent, and part of the experimental reflection work. On the other hand, non-'static TypeId is implementable on stable.

View changes since the review

@hanna-kruppe

hanna-kruppe commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

let ptr: *const T = *self;
let ptr_addr = ptr.expose_provenance();
if <<T as core::ptr::Pointee>::Metadata as core::unit::IsUnit>::is_unit() {
if <<T as core::ptr::Pointee>::Metadata as core::unit::IsUnit>::IS_UNIT {

@hanna-kruppe hanna-kruppe Jun 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.

I also considered just detecting ZST metadata, since it shouldn't make a difference and SizedTypeProperties::IS_ZST already exists, but detecting () specifically was requested in the PR that added this code: #135080 (comment)

View changes since the review

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

This seems reasonable to me.

@bors r+ rollup

View changes since this review

@rust-bors

rust-bors Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 38f4f4a has been approved by Darksonn

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-review Status: Awaiting review from the assignee but also interested parties. labels Jun 15, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 16, 2026
…n, r=Darksonn

Use constant for detecting thin pointer formatting

This allows codegen to prune the unnecessary side of the `if` for each pointer type during monomorphization. In release builds, LLVM can clean it up, but it's better to not emit unnecessary code in the first place.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants