|
| 1 | +//@ known-bug: unknown |
| 2 | +#![feature(reborrow)] |
| 3 | +#![allow(dead_code)] |
| 4 | + |
| 5 | +use std::marker::{CoerceShared, Reborrow}; |
| 6 | + |
| 7 | +// This test combines alias/projection normalization with the leaf `&mut T` to `&T` |
| 8 | +// shared reborrow path. |
| 9 | + |
| 10 | +struct InnerMut<'a, T> { |
| 11 | + value: &'a mut T, |
| 12 | +} |
| 13 | + |
| 14 | +impl<'a, T> Reborrow for InnerMut<'a, T> {} |
| 15 | + |
| 16 | +struct InnerRef<'a, T> { |
| 17 | + value: &'a T, |
| 18 | +} |
| 19 | + |
| 20 | +impl<'a, T> Clone for InnerRef<'a, T> { |
| 21 | + fn clone(&self) -> Self { |
| 22 | + *self |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl<'a, T> Copy for InnerRef<'a, T> {} |
| 27 | + |
| 28 | +impl<'a, T> CoerceShared<InnerRef<'a, T>> for InnerMut<'a, T> {} |
| 29 | + |
| 30 | +type DirectInnerRef<'a, T> = InnerRef<'a, T>; |
| 31 | + |
| 32 | +trait RefFamily<'a, T> { |
| 33 | + type Ref; |
| 34 | +} |
| 35 | + |
| 36 | +struct Projected; |
| 37 | + |
| 38 | +impl<'a, T: 'a> RefFamily<'a, T> for Projected { |
| 39 | + type Ref = InnerRef<'a, T>; |
| 40 | +} |
| 41 | + |
| 42 | +type ProjectedInnerRef<'a, T> = <Projected as RefFamily<'a, T>>::Ref; |
| 43 | + |
| 44 | +struct OuterMut<'a, T> { |
| 45 | + inner: InnerMut<'a, T>, |
| 46 | + tag: usize, |
| 47 | +} |
| 48 | + |
| 49 | +impl<'a, T> Reborrow for OuterMut<'a, T> {} |
| 50 | + |
| 51 | +struct OuterAliasRef<'a, T> { |
| 52 | + inner: DirectInnerRef<'a, T>, |
| 53 | + tag: usize, |
| 54 | +} |
| 55 | + |
| 56 | +impl<'a, T> Clone for OuterAliasRef<'a, T> { |
| 57 | + fn clone(&self) -> Self { |
| 58 | + *self |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl<'a, T> Copy for OuterAliasRef<'a, T> {} |
| 63 | + |
| 64 | +impl<'a, T> CoerceShared<OuterAliasRef<'a, T>> for OuterMut<'a, T> {} |
| 65 | + |
| 66 | +struct OuterProjectionRef<'a, T: 'a> { |
| 67 | + inner: ProjectedInnerRef<'a, T>, |
| 68 | + tag: usize, |
| 69 | +} |
| 70 | + |
| 71 | +impl<'a, T: 'a> Clone for OuterProjectionRef<'a, T> { |
| 72 | + fn clone(&self) -> Self { |
| 73 | + *self |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl<'a, T: 'a> Copy for OuterProjectionRef<'a, T> {} |
| 78 | + |
| 79 | +impl<'a, T: 'a> CoerceShared<OuterProjectionRef<'a, T>> for OuterMut<'a, T> {} |
| 80 | + |
| 81 | +fn read_alias<'a>(outer: OuterAliasRef<'a, u32>) -> (&'a u32, usize) { |
| 82 | + (outer.inner.value, outer.tag) |
| 83 | +} |
| 84 | + |
| 85 | +fn read_projection<'a>(outer: OuterProjectionRef<'a, u32>) -> (&'a u32, usize) { |
| 86 | + (outer.inner.value, outer.tag) |
| 87 | +} |
| 88 | + |
| 89 | +const fn const_accept_projection(_outer: OuterProjectionRef<'_, u32>) {} |
| 90 | + |
| 91 | +const fn consteval_projection_reborrow() { |
| 92 | + let mut value = 11; |
| 93 | + const_accept_projection(OuterMut { |
| 94 | + inner: InnerMut { value: &mut value }, |
| 95 | + tag: 5, |
| 96 | + }); |
| 97 | +} |
| 98 | + |
| 99 | +fn main() { |
| 100 | + const { consteval_projection_reborrow(); } |
| 101 | + |
| 102 | + let mut value = 22; |
| 103 | + let outer = OuterMut { inner: InnerMut { value: &mut value }, tag: 7 }; |
| 104 | + |
| 105 | + let (alias_value, alias_tag) = read_alias(outer); |
| 106 | + assert_eq!((*alias_value, alias_tag), (22, 7)); |
| 107 | + |
| 108 | + let (projection_value, projection_tag) = read_projection(outer); |
| 109 | + assert_eq!((*projection_value, projection_tag), (22, 7)); |
| 110 | +} |
0 commit comments