Repository navigation
structural match for TypeId - #166205
structural match for TypeId#166205soqb wants to merge 7 commits into
TypeId#166205Conversation
|
Some changes occurred in compiler/rustc_sanitizers cc @rcvalle Some changes occurred in match checking cc @Nadrieril Some changes occurred in match lowering cc @Nadrieril Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri Some changes occurred to the CTFE machinery changes to the core type system cc @lcnr |
|
rustbot has assigned @dingxiangfei2009. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
|
||
| let s: &[u8] = &[0; { | ||
| match TypeId::of::<u8>() { | ||
| A => 1, |
There was a problem hiding this comment.
The recommended pattern for non-structural-eq types is:
| A => 1, | |
| id if id == A => 1, |
There was a problem hiding this comment.
sorry, i'm not sure what's being suggested here. as of this PR, TypeId: StructuralPartialEq so this match is legal.
There was a problem hiding this comment.
I am suggesting an alternative that avoids the need for this PR. :)
There was a problem hiding this comment.
oh, i see! well ConstParamTy_: StructuralPartialEq so that doesn't itself elide this PR.
There was a problem hiding this comment.
although it's true that the unstable impls and const-patterns changes are somewhat orthogonal to the valtree change, since one can just feature(const_param_ty_unchecked) everything, and so it's possible this could be split out into two PRs.
|
Thanks for the PR! However, a new ValTreeKind is too heavy a hammer for something "silly", I think. |
| /// | ||
| /// Also stores an inner type which transparently contains some representation of the bits of the `TypeId`. | ||
| /// Currently, this is just `(u128,)` but this is not to be relied upon. | ||
| TypeId(I::Ty, I::Consts), |
There was a problem hiding this comment.
The 2nd field is redundant, so if we did this I think we should not have a 2nd field here.
There was a problem hiding this comment.
unfortunately, this is not true since symbol mangling (among other things) wants something tangible. i would love not to need this, but i haven't found a working alternative.
Is there a better way to preserve provenance through valtrees? I'm not massively comfortable with this solution either. I completely agree that we shouldn't merge a PR based on the whim of one user (especially for something "silly"), but I have been imagining the feature has value in its own right. Consider that the hammer is heavy, but the diff is small. |
This comment has been minimized.
This comment has been minimized.
0ec6c78 to
1178d3f
Compare
|
While an RFC isn't required, I do think that this probably should go through the ACP process. |
|
This is not a libs feature, so ACP doesn't apply. We could consider MCP. |
|
😄 fun! cc @yara-blue @Tess4552 I'll think about this next week |
| macro_rules! arbitrary_const_value { | ||
| () => { | ||
| const { | ||
| enum Arbitrary {} | ||
| Value { disambiguator: TypeId::of::<Arbitrary>() } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
I do agree this is very cute, but this is also less powerful than I initially thought: since we get one fresh type per macro invocation, this is basically a glorified hash of the span of the macro call. One could hash the output of core::panic::Location::caller with basically the same results. So I wonder what you can actually use this for?
There was a problem hiding this comment.
One could hash the output of
core::panic::Location::callerwith basically the same results.
not really... it should be called unique_const_value, i guess. for my use case, the equivalent of arbitrary_const_value is publicly-accessible and its uniqueness is relied upon for soundness: they are used to represent local variables, so two invocations being unexpectedly equal would introduce aliasing problems.
how would you implement the following soundly with Location::caller?
macro_rules! thing {
() => {
if const { arbitrary_const_value!() == arbitrary_const_value!() } {
unsafe { unreachable_unchecked() }
}
);
}There was a problem hiding this comment.
the best i can conceive of without this feature is a proc macro which spits out a random number on every invocation, which uhh. no thanks.
There was a problem hiding this comment.
how would you implement the following soundly with Location::caller?
Are you talking about hash collisions, or? I would just have arbitrary_const_value!() expand to a const block that calls Location::caller and hashes the result (well Hash is a trait so I guess this needs const traits oops). In fact you could hash your TypeId::of<LocalEnum>() too. Proc macro that returns a random value is also semantically equivalent as far as I'm concerned.
My question remains: what would you use this API for? I understand wanting a unique token generator when the token is unique every time a function is called; I'm less sure about a token that's unique per source location.
There was a problem hiding this comment.
the Location version would reach the unreachable_unchecked expression since they would both evaluate to the same call site. that is not true of the TypeId version since the types would be distinct.
the only sound way to hash a TypeId is with Hash which is very much not const.
There was a problem hiding this comment.
no worries! i take the point about PartialEq not saving you, it seems no-one is sure whether that's a reasonable concern or not. although note that here, it would be fine since the CTFE version of TypeId::eq just compares the actual compiler Ty values so the api in the playground above would be totally sound!
There was a problem hiding this comment.
Well fair, though PartialEq doesn't fully save you either since at runtime it's using a hash too. With your PR though it's using real types so you get true soundness guarantees, which is pleasant.
At the same time it is highly suspicious, because it means that valtree equality does not agree with PartialEq. So far that was a hard constraint that we imposed.
There was a problem hiding this comment.
it means that valtree equality does not agree with
PartialEq
TypeId::eq defers to an intrinsic which, during const eval, will dig up and compare the Tys directly. this implementation is the only way valtree equality can exactly match PartialEq.
There was a problem hiding this comment.
The problem is that it does not match runtime PartialEq.
There was a problem hiding this comment.
It does in practice tho? If there is a hash collision at codegen time, the build ices, so hash equality and type_id_eq are the same in behaviour
|
As In regards to adding back |
you mean the hash from out of the
i don't think so. we can commit to the comment you mention is from #77125 (aug '22), the (deliberate) removal of Footnotes
|
This comment has been minimized.
This comment has been minimized.
1178d3f to
46dfb64
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
A, I see, yes. Agree that adding a new variant to the |
View all comments
Supports
StructuralPartialEqandConstParamTy_forstd::any::TypeId, behindfeature(type_id_structural_match).Background
Needed this while doing something silly & experimental. See
tests/ui/const-generics/min_adt_const_params/type-id-arbitrary.rsfor why I found this useful.Not sure if this is really RFC-worthy. I am happy to write one quickly, but since I already had the branch ready, I thought I might as well make the PR. Happy to close this now if that is more appropriate.
Solution
The problem with putting
TypeIdinto valtrees is provenance. Currently,TypeIdstores pointers which are nothing more than integers at runtime, but are annotated with special type-specific provenance during CTFE, so that comptime shenanigans can recover theTyfrom aTypeId.To solve this, I just put a new variant on
ValTreeKind, which keeps track of theTyit is for. This was surprisingly easy to do, but is probably not a great precedent to set. If we do ever wantTypeIdin valtrees, I think this is all but unavoidable.For valtree-to-pattern, the same approach as
stris taken. Make references to the scrutinee and pattern constant and then compare them withPartialEq::eq.The actual library impls are gated behind
unstable_feature_bound. I don't think that causes problems here, but I'm not very familiar with how it works.Problems
I'm not sure that I took the correct approach to symbol mangling. Currently, the bits of the hash are printed out as a
u128, but that might not be appropriate stability-wise. I suppose the only other thing we can do is print out the actual type it represents, but I ran into issues with demangling that I had no idea how to solve.