Skip to content

Constify TypeId ordering impls - #101698

Merged
bors merged 1 commit into
rust-lang:masterfrom
raldone01:feat/const_cmp_typeid
Jan 17, 2023
Merged

Constify TypeId ordering impls#101698
bors merged 1 commit into
rust-lang:masterfrom
raldone01:feat/const_cmp_typeid

Conversation

@raldone01

@raldone01raldone01 commented Sep 11, 2022

Copy link
Copy Markdown
Contributor

Tracking issue: #101871

Adding const ordering to TypeId allows rtti crates to optimize some casting scenarios (without transmuting to u64). This would also prevent these crates from breaking if the underlying type is changed from u64 to something different.

Feature gate: #![feature(const_cmp_type_id)]

@rustbotrustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 11, 2022
@rustbot

Copy link
Copy Markdown
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive

Copy link
Copy Markdown
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @scottmcm (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 11, 2022
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

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.

Isn't this a bit weird? “impl defined here but it is not const” but it is const as shown?

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.

Yes that is because this test doesn't use the const_trait_impl feature flag.
That always leads to this error if used this way.

The same error happens in src/test/ui/consts/const_cmp_type_id.rs if #![feature(const_trait_impl)] is removed, this is probably more of an issue in const_trait_impl than here.

@raldone01raldone01 changed the title Consitfy TypeId ordering implsConstify TypeId ordering implsSep 11, 2022
@onestacked

Copy link
Copy Markdown
Contributor

Makes public stable implementations of Ordering traits unstably const, so

@rustbot label +T-libs-api -T-libs

@rustbotrustbot added T-libs-api [DEPRECATED; DO NOT USE] and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 14, 2022

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

Diff LGTM.

@scottmcmscottmcm added I-libs-api-nominated [DEPRECATED; DO NOT USE] I-lang-nominated Nominated for discussion during a lang team meeting. labels Sep 24, 2022
@scottmcm

Copy link
Copy Markdown
Member

Nominating for team discussions. The ordering of TypeIds becoming const-visible in particular feels potentially scary to me.

@raldone01

raldone01 commented Sep 24, 2022

Copy link
Copy Markdown
ContributorAuthor

Some additional background that may help in the discussion:

I have created a crate called trait_cast_rs. It provides a custom Any type. That allows casts to dyn Traits.

Internally it has a const slice of function pointers with their associated TypeIds. Making TypeId const comparable would allow the crate to sort the slice at compile time. When downcasting at runtime a binary_search is performed to find the correct function pointer.

This has the advantage that the array must not be sorted on every program launch and the lookup slice can be a constant.

I currently use transmute<_, u64>(type_id) to sort the slice at compile time.

Comment threadlibrary/core/src/any.rs Outdated
Comment threadlibrary/core/src/any.rs Outdated

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.

Per T-libs (see #102371 (comment)) we're going to wait on derive_const (or similar) before accepting more changes like this to library.

@rustbot label +S-Blocked

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.

Please do not make TypeId "structural match" - instead, that needs to be removed - as per e.g. #77125 (comment) - sadly the one PR trying to do it was rejected and I'm not seeing a replacement just removing "structural match":

@onestackedonestackedOct 13, 2022

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.

the impl StructualPartialEq was added because the test for issue-73976 failed as it used matches!.
It could be changed to a normal == though, but removing the structural match would be a breaking change (nightly only since const_type_id isn't stable yet).

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.

I've added a commit that does those changes.

@rustbotrustbot added the S-blocked Status: Blocked on something else such as an RFC or other implementation work. label Sep 28, 2022
@scottmcm

Copy link
Copy Markdown
Member

We discussed this in the lang meeting this week. It didn't result in anything that needs to block this for nightly, but I've added a note to the tracking issue that this precludes some possible implementations, and thus stabilizing it would need to be considered very carefully.

@scottmcmscottmcm removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Sep 28, 2022
@m-ou-sem-ou-se added the I-compiler-nominated Nominated for discussion during a compiler team meeting. label Oct 4, 2022
@m-ou-se

Copy link
Copy Markdown
Member

Nominating this for the compiler team for input, since the compiler is the one generating the type ids. So question for the compiler team: will this get in the way of any future plans there might be with future typeid implementations? (And if so, is that a problem?)

@apiraino

Copy link
Copy Markdown
Contributor

Issue was discussed in T-compiler meeting, I'm leaving here some notes; more will probably follow-up in this issue to reach a wider audience.

Leaving I-compiler-nominated for the moment.

@apiraino

Copy link
Copy Markdown
Contributor

@scottmcm during T-compiler meeting (see previous comment), some questions arised (here, for example). Can you provide a bit of feedback to complement the discussion there? thanks

@rustbot label -i-compiler-nominated

@apirainoapiraino removed the I-compiler-nominated Nominated for discussion during a compiler team meeting. label Oct 13, 2022
@eddyb

Copy link
Copy Markdown
Contributor

Adding const ordering to TypeId allows rtti crates to optimize some casting scenarios (without transmuting to u64).

If you want the hash, you should have never transmuted, but instead written a Hasher that is most efficient for write_u64 (ideally it should accept any data then truncate and/or mix but we found out the hard way that we can't really break people requiringwrite_u64 because it's kind of common in code that doesn't misuse transmute).

You can do this on stable Rust, and it should work all the way back to 1.0 AFAIK, so it's a good option to consider.

@borsbors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jan 16, 2023
@onestacked

Copy link
Copy Markdown
Contributor

Why did the CI not run?

@scottmcm

Copy link
Copy Markdown
Member

Hmm, I'll try to re-kick it.

@bors r-

@borsbors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 16, 2023
@scottmcmscottmcm reopened this Jan 16, 2023
@WaffleLapkin

Copy link
Copy Markdown
Member

@bors r=scottmcm
github is/was having some issues with github actions

@bors

bors commented Jan 16, 2023

Copy link
Copy Markdown
Collaborator

📌 Commit 73f8dc788b0e63d3ed4fd88fbbfd9ffa70c8acb9 has been approved by scottmcm

It is now in the queue for this repository.

@borsbors 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 Jan 16, 2023
@onestacked

Copy link
Copy Markdown
Contributor

Should probably r- untill CI passes

@scottmcm

Copy link
Copy Markdown
Member

@bors r-

@borsbors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 16, 2023
@onestacked

Copy link
Copy Markdown
Contributor

@scottmcm can you r+?

@WaffleLapkin

Copy link
Copy Markdown
Member

@bors r=scottmcm

@bors

bors commented Jan 17, 2023

Copy link
Copy Markdown
Collaborator

📌 Commit 7355ab3 has been approved by scottmcm

It is now in the queue for this repository.

@borsbors 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 Jan 17, 2023
@Dylan-DPCDylan-DPC mentioned this pull request Jan 17, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 17, 2023
Rollup of 5 pull requests
Successful merges:
- rust-lang#101698 (Constify `TypeId` ordering impls)
- rust-lang#106148 (Fix unused_parens issue for higher ranked function pointers)
- rust-lang#106922 (Avoid unsafe code in `to_ascii_[lower/upper]case()`)
- rust-lang#106951 (Remove ineffective run of SimplifyConstCondition)
- rust-lang#106962 (Fix use suggestion span)
Failed merges:
r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit 09faa26 into rust-lang:masterJan 17, 2023
@rustbotrustbot added this to the 1.68.0 milestone Jan 17, 2023
@onestacked
onestacked deleted the feat/const_cmp_typeid branch January 18, 2023 09:06
yvt added a commit to r3-os/r3 that referenced this pull request Mar 18, 2023
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request May 26, 2023
…atch, r=dtolnay
Remove structural match from `TypeId`
As per rust-lang#99189 (comment).
> Removing the structural equality might make sense, but is a breaking change that'd require a libs-api FCP.
rust-lang#99189 (comment)
> Landing this PR now (well, mainly the "remove structural equality" part) would unblock `const fn` `TypeId::of`, since we only postponed that because we were guaranteeing too much.
See also rust-lang#99189, rust-lang#101698
bors added a commit to rust-lang-ci/rust that referenced this pull request May 26, 2023
…ch, r=dtolnay
Remove structural match from `TypeId`
As per rust-lang#99189 (comment).
> Removing the structural equality might make sense, but is a breaking change that'd require a libs-api FCP.
rust-lang#99189 (comment)
> Landing this PR now (well, mainly the "remove structural equality" part) would unblock `const fn` `TypeId::of`, since we only postponed that because we were guaranteeing too much.
See also rust-lang#99189, rust-lang#101698
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-libs-api[DEPRECATED; DO NOT USE]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

14 participants

@raldone01@rustbot@rust-highfive@rust-log-analyzer@onestacked@scottmcm@m-ou-se@apiraino@eddyb@pnkfelix@bors@WaffleLapkin@jhpratt@fmease