Stabilize const_type_id feature#72488
Merged
bors merged 4 commits intorust-lang:masterfrom Jul 29, 2020
KodrAus:stabilize/const_type_id
Merged
Stabilize const_type_id feature#72488bors merged 4 commits intorust-lang:masterfrom KodrAus:stabilize/const_type_id
bors merged 4 commits intorust-lang:masterfrom
KodrAus:stabilize/const_type_id
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The tracking issue for
const_type_idpoints to the ill-fated #41875. So I'm re-energizingTypeIdshenanigans by opening this one up to see if there's anything blocking us from stabilizing the constification of type ids.Will wait for CI before pinging teams/groups.
This PR stabilizes the
const_type_idfeature, which allowsTypeId::of(and the underlying unstable intrinsic) to be called in constant contexts.There are some sanity tests that demonstrate its usage, but I’ve included some more below.
As a simple example, you could create a constant item that contains some type ids:
Type ids can also now appear in associated constants. You could create a trait that associates each type with its constant type id:
TypeId::ofis generic, which we saw above in the way the genericSelfargument was used. This has some implications for const evaluation. It means we can make trait impls evaluate differently depending on information that wasn't directly passed through the trait system. This violates the parametricity property, which requires all instances of a generic function to behave the same way with respect to its generic parameters. That's not unique toTypeId::of, other generic const functions based on compiler intrinsics likemem::align_ofcan also violate parametricity. In practice Rust doesn't really have type parametricity anyway since it monomorphizes generics into concrete functions, so violating it using type ids isn’t new.As an example of how impls can behave differently, you could combine constant type ids with the
const_if_matchfeature to dispatch calls based on the type id of the genericSelf, rather than based on information aboutSelfthat was threaded through trait bounds. It's like a rough-and-ready form of specialization:Type ids have some edges that this stabilization exposes to more contexts. It's possible for type ids to collide (but this is a bug). Since they can change between compiler versions, it's never valid to cast a type id to its underlying value.