@@ -647,8 +647,10 @@ impl Pat {
647647 PatKind :: MacCall ( mac) => TyKind :: MacCall ( mac. clone ( ) ) ,
648648 // `&mut? P` can be reinterpreted as `&mut? T` where `T` is `P` reparsed as a type.
649649 PatKind :: Ref ( pat, pinned, mutbl) => pat. to_ty ( ) . map ( |ty| match pinned {
650- Pinnedness :: Not => TyKind :: Ref ( None , MutTy { ty, mutbl : * mutbl } ) ,
651- Pinnedness :: Pinned => TyKind :: PinnedRef ( None , MutTy { ty, mutbl : * mutbl } ) ,
650+ Pinnedness :: Not => TyKind :: Ref ( None , MutTy { ty, mutbl : * mutbl } , ViewKind :: Full ) ,
651+ Pinnedness :: Pinned => {
652+ TyKind :: PinnedRef ( None , MutTy { ty, mutbl : * mutbl } , ViewKind :: Full )
653+ }
652654 } ) ?,
653655 // A slice/array pattern `[P]` can be reparsed as `[T]`, an unsized array,
654656 // when `P` can be reparsed as a type `T`.
@@ -1511,9 +1513,9 @@ impl Expr {
15111513
15121514 ExprKind :: Paren ( expr) => expr. to_ty ( ) . map ( TyKind :: Paren ) ?,
15131515
1514- ExprKind :: AddrOf ( BorrowKind :: Ref , mutbl, expr) => {
1515- expr . to_ty ( ) . map ( |ty| TyKind :: Ref ( None , MutTy { ty , mutbl : * mutbl } ) ) ?
1516- }
1516+ ExprKind :: AddrOf ( BorrowKind :: Ref , mutbl, expr) => expr
1517+ . to_ty ( )
1518+ . map ( |ty| TyKind :: Ref ( None , MutTy { ty , mutbl : * mutbl } , ViewKind :: Full ) ) ? ,
15171519
15181520 ExprKind :: Repeat ( expr, expr_len) => {
15191521 expr. to_ty ( ) . map ( |ty| TyKind :: Array ( ty, expr_len. clone ( ) ) ) ?
@@ -2474,7 +2476,8 @@ impl From<Box<Ty>> for Ty {
24742476impl Ty {
24752477 pub fn peel_refs ( & self ) -> & Self {
24762478 let mut final_ty = self ;
2477- while let TyKind :: Ref ( _, MutTy { ty, .. } ) | TyKind :: Ptr ( MutTy { ty, .. } ) = & final_ty. kind
2479+ while let TyKind :: Ref ( _, MutTy { ty, .. } , _) | TyKind :: Ptr ( MutTy { ty, .. } ) =
2480+ & final_ty. kind
24782481 {
24792482 final_ty = ty;
24802483 }
@@ -2519,11 +2522,11 @@ pub enum TyKind {
25192522 /// A raw pointer (`*const T` or `*mut T`).
25202523 Ptr ( MutTy ) ,
25212524 /// A reference (`&'a T` or `&'a mut T`).
2522- Ref ( #[ visitable( extra = LifetimeCtxt :: Ref ) ] Option < Lifetime > , MutTy ) ,
2525+ Ref ( #[ visitable( extra = LifetimeCtxt :: Ref ) ] Option < Lifetime > , MutTy , ViewKind ) ,
25232526 /// A pinned reference (`&'a pin const T` or `&'a pin mut T`).
25242527 ///
25252528 /// Desugars into `Pin<&'a T>` or `Pin<&'a mut T>`.
2526- PinnedRef ( #[ visitable( extra = LifetimeCtxt :: Ref ) ] Option < Lifetime > , MutTy ) ,
2529+ PinnedRef ( #[ visitable( extra = LifetimeCtxt :: Ref ) ] Option < Lifetime > , MutTy , ViewKind ) ,
25272530 /// A function pointer type (e.g., `fn(usize) -> bool`).
25282531 FnPtr ( Box < FnPtrTy > ) ,
25292532 /// An unsafe existential lifetime binder (e.g., `unsafe<'a> &'a ()`).
@@ -2661,6 +2664,19 @@ pub enum TraitObjectSyntax {
26612664 None = 1 ,
26622665}
26632666
2667+ /// Represents how a reference is viewed.
2668+ #[ derive( Clone , Encodable , Decodable , Debug , Walkable ) ]
2669+ pub enum ViewKind {
2670+ /// `&mut T`. All the fields can be observed.
2671+ Full ,
2672+ /// `&mut T.{ foo, bar }`. Only `foo` and `bar` can be observed.
2673+ Partial {
2674+ span : Span ,
2675+ #[ visitable( ignore) ]
2676+ fields : ThinVec < Ident > ,
2677+ } ,
2678+ }
2679+
26642680/// SAFETY: `TraitObjectSyntax` only has 3 data-less variants which means
26652681/// it can be represented with a `u2`. We use `repr(u8)` to guarantee the
26662682/// discriminants of the variants are no greater than `3`.
@@ -2934,21 +2950,21 @@ pub struct Param {
29342950pub enum SelfKind {
29352951 /// `self`, `mut self`
29362952 Value ( Mutability ) ,
2937- /// `&'lt self`, `&'lt mut self`
2938- Region ( Option < Lifetime > , Mutability ) ,
2939- /// `&'lt pin const self`, `&'lt pin mut self`
2940- Pinned ( Option < Lifetime > , Mutability ) ,
2953+ /// `&'lt self`, `&'lt mut self`, `&'lt mut self.{ a, b }`
2954+ Region ( Option < Lifetime > , Mutability , ViewKind ) ,
2955+ /// `&'lt pin const self`, `&'lt pin mut self`, `&'lt pin mut self.{ a, b }`.
2956+ Pinned ( Option < Lifetime > , Mutability , ViewKind ) ,
29412957 /// `self: TYPE`, `mut self: TYPE`
29422958 Explicit ( Box < Ty > , Mutability ) ,
29432959}
29442960
29452961impl SelfKind {
29462962 pub fn to_ref_suggestion ( & self ) -> String {
29472963 match self {
2948- SelfKind :: Region ( None , mutbl) => mutbl. ref_prefix_str ( ) . to_string ( ) ,
2949- SelfKind :: Region ( Some ( lt) , mutbl) => format ! ( "&{lt} {}" , mutbl. prefix_str( ) ) ,
2950- SelfKind :: Pinned ( None , mutbl) => format ! ( "&pin {}" , mutbl. ptr_str( ) ) ,
2951- SelfKind :: Pinned ( Some ( lt) , mutbl) => format ! ( "&{lt} pin {}" , mutbl. ptr_str( ) ) ,
2964+ SelfKind :: Region ( None , mutbl, _ ) => mutbl. ref_prefix_str ( ) . to_string ( ) ,
2965+ SelfKind :: Region ( Some ( lt) , mutbl, _ ) => format ! ( "&{lt} {}" , mutbl. prefix_str( ) ) ,
2966+ SelfKind :: Pinned ( None , mutbl, _ ) => format ! ( "&pin {}" , mutbl. ptr_str( ) ) ,
2967+ SelfKind :: Pinned ( Some ( lt) , mutbl, _ ) => format ! ( "&{lt} pin {}" , mutbl. ptr_str( ) ) ,
29522968 SelfKind :: Value ( _) | SelfKind :: Explicit ( _, _) => {
29532969 unreachable ! ( "if we had an explicit self, we wouldn't be here" )
29542970 }
@@ -2965,13 +2981,18 @@ impl Param {
29652981 if ident. name == kw:: SelfLower {
29662982 return match self . ty . kind {
29672983 TyKind :: ImplicitSelf => Some ( respan ( self . pat . span , SelfKind :: Value ( mutbl) ) ) ,
2968- TyKind :: Ref ( lt, MutTy { ref ty, mutbl } ) if ty. kind . is_implicit_self ( ) => {
2969- Some ( respan ( self . pat . span , SelfKind :: Region ( lt, mutbl) ) )
2984+ TyKind :: Ref ( lt, MutTy { ref ty, mutbl } , ref view_fields)
2985+ if ty. kind . is_implicit_self ( ) =>
2986+ {
2987+ Some ( respan (
2988+ self . pat . span ,
2989+ SelfKind :: Region ( lt, mutbl, view_fields. clone ( ) ) ,
2990+ ) )
29702991 }
2971- TyKind :: PinnedRef ( lt, MutTy { ref ty, mutbl } )
2992+ TyKind :: PinnedRef ( lt, MutTy { ref ty, mutbl } , ref view )
29722993 if ty. kind . is_implicit_self ( ) =>
29732994 {
2974- Some ( respan ( self . pat . span , SelfKind :: Pinned ( lt, mutbl) ) )
2995+ Some ( respan ( self . pat . span , SelfKind :: Pinned ( lt, mutbl, view . clone ( ) ) ) )
29752996 }
29762997 _ => Some ( respan (
29772998 self . pat . span . to ( self . ty . span ) ,
@@ -3004,20 +3025,20 @@ impl Param {
30043025 let ( mutbl, ty) = match eself. node {
30053026 SelfKind :: Explicit ( ty, mutbl) => ( mutbl, ty) ,
30063027 SelfKind :: Value ( mutbl) => ( mutbl, infer_ty) ,
3007- SelfKind :: Region ( lt, mutbl) => (
3028+ SelfKind :: Region ( lt, mutbl, views ) => (
30083029 Mutability :: Not ,
30093030 Box :: new ( Ty {
30103031 id : DUMMY_NODE_ID ,
3011- kind : TyKind :: Ref ( lt, MutTy { ty : infer_ty, mutbl } ) ,
3032+ kind : TyKind :: Ref ( lt, MutTy { ty : infer_ty, mutbl } , views ) ,
30123033 span,
30133034 tokens : None ,
30143035 } ) ,
30153036 ) ,
3016- SelfKind :: Pinned ( lt, mutbl) => (
3037+ SelfKind :: Pinned ( lt, mutbl, view ) => (
30173038 mutbl,
30183039 Box :: new ( Ty {
30193040 id : DUMMY_NODE_ID ,
3020- kind : TyKind :: PinnedRef ( lt, MutTy { ty : infer_ty, mutbl } ) ,
3041+ kind : TyKind :: PinnedRef ( lt, MutTy { ty : infer_ty, mutbl } , view ) ,
30213042 span,
30223043 tokens : None ,
30233044 } ) ,
@@ -4341,7 +4362,7 @@ mod size_asserts {
43414362 static_assert_size ! ( Stmt , 32 ) ;
43424363 static_assert_size ! ( StmtKind , 16 ) ;
43434364 static_assert_size ! ( TraitImplHeader , 72 ) ;
4344- static_assert_size ! ( Ty , 64 ) ;
4345- static_assert_size ! ( TyKind , 40 ) ;
4365+ static_assert_size ! ( Ty , 80 ) ;
4366+ static_assert_size ! ( TyKind , 56 ) ;
43464367 // tidy-alphabetical-end
43474368}
0 commit comments