@@ -1387,7 +1387,8 @@ pub struct MethodSig {
13871387 pub abi : Abi ,
13881388 pub decl : P < FnDecl > ,
13891389 pub generics : Generics ,
1390- pub explicit_self : ExplicitSelf ,
1390+ /// A short form of self argument was used (`self`, `&self` etc, but not `self: TYPE`).
1391+ pub self_shortcut : bool ,
13911392}
13921393
13931394/// Represents an item declaration within a trait declaration,
@@ -1677,81 +1678,65 @@ pub struct Arg {
16771678 pub id : NodeId ,
16781679}
16791680
1680- /// Represents the kind of 'self' associated with a method.
1681- /// String representation of `Ident` here is always "self", but hygiene contexts may differ.
1681+ /// Alternative representation for `Arg`s describing `self` parameter of methods.
16821682#[ derive( Clone , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug ) ]
16831683pub enum SelfKind {
1684- /// No self
1685- Static ,
16861684 /// `self`, `mut self`
1687- Value ( Ident ) ,
1685+ Value ( Mutability ) ,
16881686 /// `&'lt self`, `&'lt mut self`
1689- Region ( Option < Lifetime > , Mutability , Ident ) ,
1687+ Region ( Option < Lifetime > , Mutability ) ,
16901688 /// `self: TYPE`, `mut self: TYPE`
1691- Explicit ( P < Ty > , Ident ) ,
1689+ Explicit ( P < Ty > , Mutability ) ,
16921690}
16931691
16941692pub type ExplicitSelf = Spanned < SelfKind > ;
16951693
16961694impl Arg {
1697- #[ unstable( feature = "rustc_private" , issue = "27812" ) ]
1698- #[ rustc_deprecated( since = "1.10.0" , reason = "use `from_self` instead" ) ]
1699- pub fn new_self ( span : Span , mutability : Mutability , self_ident : Ident ) -> Arg {
1700- let path = Spanned { span : span, node : self_ident} ;
1701- Arg {
1702- // HACK(eddyb) fake type for the self argument.
1703- ty : P ( Ty {
1704- id : DUMMY_NODE_ID ,
1705- node : TyKind :: Infer ,
1706- span : DUMMY_SP ,
1707- } ) ,
1708- pat : P ( Pat {
1709- id : DUMMY_NODE_ID ,
1710- node : PatKind :: Ident ( BindingMode :: ByValue ( mutability) , path, None ) ,
1711- span : span
1712- } ) ,
1713- id : DUMMY_NODE_ID
1714- }
1715- }
1716-
17171695 pub fn to_self ( & self ) -> Option < ExplicitSelf > {
1718- if let PatKind :: Ident ( _ , ident, _) = self . pat . node {
1696+ if let PatKind :: Ident ( BindingMode :: ByValue ( mutbl ) , ident, _) = self . pat . node {
17191697 if ident. node . name == keywords:: SelfValue . name ( ) {
17201698 return match self . ty . node {
1721- TyKind :: Infer => Some ( respan ( self . pat . span , SelfKind :: Value ( ident . node ) ) ) ,
1699+ TyKind :: Infer => Some ( respan ( self . pat . span , SelfKind :: Value ( mutbl ) ) ) ,
17221700 TyKind :: Rptr ( lt, MutTy { ref ty, mutbl} ) if ty. node == TyKind :: Infer => {
1723- Some ( respan ( self . pat . span , SelfKind :: Region ( lt, mutbl, ident . node ) ) )
1701+ Some ( respan ( self . pat . span , SelfKind :: Region ( lt, mutbl) ) )
17241702 }
17251703 _ => Some ( respan ( mk_sp ( self . pat . span . lo , self . ty . span . hi ) ,
1726- SelfKind :: Explicit ( self . ty . clone ( ) , ident . node ) ) ) ,
1704+ SelfKind :: Explicit ( self . ty . clone ( ) , mutbl ) ) ) ,
17271705 }
17281706 }
17291707 }
17301708 None
17311709 }
17321710
1733- pub fn from_self ( eself : ExplicitSelf , ident_sp : Span , mutbl : Mutability ) -> Arg {
1734- let pat = |ident, span| P ( Pat {
1735- id : DUMMY_NODE_ID ,
1736- node : PatKind :: Ident ( BindingMode :: ByValue ( mutbl) , respan ( ident_sp, ident) , None ) ,
1737- span : span,
1738- } ) ;
1711+ pub fn is_self ( & self ) -> bool {
1712+ if let PatKind :: Ident ( _, ident, _) = self . pat . node {
1713+ ident. node . name == keywords:: SelfValue . name ( )
1714+ } else {
1715+ false
1716+ }
1717+ }
1718+
1719+ pub fn from_self ( eself : ExplicitSelf , eself_ident : SpannedIdent ) -> Arg {
17391720 let infer_ty = P ( Ty {
17401721 id : DUMMY_NODE_ID ,
17411722 node : TyKind :: Infer ,
17421723 span : DUMMY_SP ,
17431724 } ) ;
1744- let arg = |ident, ty, span| Arg {
1745- pat : pat ( ident, span) ,
1725+ let arg = |mutbl, ty, span| Arg {
1726+ pat : P ( Pat {
1727+ id : DUMMY_NODE_ID ,
1728+ node : PatKind :: Ident ( BindingMode :: ByValue ( mutbl) , eself_ident, None ) ,
1729+ span : span,
1730+ } ) ,
17461731 ty : ty,
17471732 id : DUMMY_NODE_ID ,
17481733 } ;
17491734 match eself. node {
1750- SelfKind :: Static => panic ! ( "bug: `Arg::from_self` is called \
1751- with `SelfKind::Static` argument" ) ,
1752- SelfKind :: Explicit ( ty , ident ) => arg ( ident , ty , mk_sp ( eself . span . lo , ident_sp . hi ) ) ,
1753- SelfKind :: Value ( ident ) => arg ( ident , infer_ty, eself. span ) ,
1754- SelfKind :: Region ( lt, mutbl, ident ) => arg ( ident , P ( Ty {
1735+ SelfKind :: Explicit ( ty , mutbl ) => {
1736+ arg ( mutbl , ty , mk_sp ( eself . span . lo , eself_ident . span . hi ) )
1737+ }
1738+ SelfKind :: Value ( mutbl ) => arg ( mutbl , infer_ty, eself. span ) ,
1739+ SelfKind :: Region ( lt, mutbl) => arg ( Mutability :: Immutable , P ( Ty {
17551740 id : DUMMY_NODE_ID ,
17561741 node : TyKind :: Rptr ( lt, MutTy { ty : infer_ty, mutbl : mutbl } ) ,
17571742 span : DUMMY_SP ,
@@ -1768,6 +1753,15 @@ pub struct FnDecl {
17681753 pub variadic : bool
17691754}
17701755
1756+ impl FnDecl {
1757+ pub fn get_self ( & self ) -> Option < ExplicitSelf > {
1758+ self . inputs . get ( 0 ) . and_then ( Arg :: to_self)
1759+ }
1760+ pub fn has_self ( & self ) -> bool {
1761+ self . inputs . get ( 0 ) . map ( Arg :: is_self) . unwrap_or ( false )
1762+ }
1763+ }
1764+
17711765#[ derive( Copy , Clone , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug ) ]
17721766pub enum Unsafety {
17731767 Unsafe ,
0 commit comments