Introduce CoerceShared lang item and trait, and basic Reborrow tests #146037
+242
−0
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.
Part of #145612: This introduces the
CoerceShared
trait which is theReborrow
equivalent of a&mut T
->&T
coercion. The trait has aTarget
GAT which makes this (currently) unique in thecore/src/marker.rs
; I'm not sure if this can be considered problematic. Maybe this is not the way such things should be done at the marker trait level? Or maybe it is fine.Improtantly, this PR introduces a battery of basic
Reborrow
andCoerceShared
tests. These test the very basics of the feature; custom marker types intended to have exclusive semantics (Custom<'a>(PhantomData<&'a mut ()>)
), custom exclusive reference wrappers, and standard library exclusive reference wrappers (Pin<&mut T>
andOption<&mut T>
). None of these of course work since the implementation forReborrow
andCoerceShared
is entirely missing, but this is the first step towards making these work.Future PRs will introduce more tests, such as "recursive" reborrowing (ie. reborrowing structs that contain multiple reborrowable fields) and checks around the lifetime semantics of reborrowing ie. that a reborrow produces a new type with the same lifetime as the original.