@@ -1389,7 +1389,6 @@ pub struct MethodSig {
13891389 pub abi : Abi ,
13901390 pub decl : P < FnDecl > ,
13911391 pub generics : Generics ,
1392- pub explicit_self : ExplicitSelf ,
13931392}
13941393
13951394/// Represents an item declaration within a trait declaration,
@@ -1640,6 +1639,8 @@ pub enum TyKind {
16401639 /// TyKind::Infer means the type should be inferred instead of it having been
16411640 /// specified. This can appear anywhere in a type.
16421641 Infer ,
1642+ /// Inferred type of a `self` or `&self` argument in a method.
1643+ ImplicitSelf ,
16431644 // A macro in the type position.
16441645 Mac ( Mac ) ,
16451646}
@@ -1679,81 +1680,65 @@ pub struct Arg {
16791680 pub id : NodeId ,
16801681}
16811682
1682- /// Represents the kind of 'self' associated with a method.
1683- /// String representation of `Ident` here is always "self", but hygiene contexts may differ.
1683+ /// Alternative representation for `Arg`s describing `self` parameter of methods.
16841684#[ derive( Clone , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug ) ]
16851685pub enum SelfKind {
1686- /// No self
1687- Static ,
16881686 /// `self`, `mut self`
1689- Value ( Ident ) ,
1687+ Value ( Mutability ) ,
16901688 /// `&'lt self`, `&'lt mut self`
1691- Region ( Option < Lifetime > , Mutability , Ident ) ,
1689+ Region ( Option < Lifetime > , Mutability ) ,
16921690 /// `self: TYPE`, `mut self: TYPE`
1693- Explicit ( P < Ty > , Ident ) ,
1691+ Explicit ( P < Ty > , Mutability ) ,
16941692}
16951693
16961694pub type ExplicitSelf = Spanned < SelfKind > ;
16971695
16981696impl Arg {
1699- #[ unstable( feature = "rustc_private" , issue = "27812" ) ]
1700- #[ rustc_deprecated( since = "1.10.0" , reason = "use `from_self` instead" ) ]
1701- pub fn new_self ( span : Span , mutability : Mutability , self_ident : Ident ) -> Arg {
1702- let path = Spanned { span : span, node : self_ident} ;
1703- Arg {
1704- // HACK(eddyb) fake type for the self argument.
1705- ty : P ( Ty {
1706- id : DUMMY_NODE_ID ,
1707- node : TyKind :: Infer ,
1708- span : DUMMY_SP ,
1709- } ) ,
1710- pat : P ( Pat {
1711- id : DUMMY_NODE_ID ,
1712- node : PatKind :: Ident ( BindingMode :: ByValue ( mutability) , path, None ) ,
1713- span : span
1714- } ) ,
1715- id : DUMMY_NODE_ID
1716- }
1717- }
1718-
17191697 pub fn to_self ( & self ) -> Option < ExplicitSelf > {
1720- if let PatKind :: Ident ( _ , ident, _) = self . pat . node {
1698+ if let PatKind :: Ident ( BindingMode :: ByValue ( mutbl ) , ident, _) = self . pat . node {
17211699 if ident. node . name == keywords:: SelfValue . name ( ) {
17221700 return match self . ty . node {
1723- TyKind :: Infer => Some ( respan ( self . pat . span , SelfKind :: Value ( ident . node ) ) ) ,
1724- TyKind :: Rptr ( lt, MutTy { ref ty, mutbl} ) if ty. node == TyKind :: Infer => {
1725- Some ( respan ( self . pat . span , SelfKind :: Region ( lt, mutbl, ident . node ) ) )
1701+ TyKind :: ImplicitSelf => Some ( respan ( self . pat . span , SelfKind :: Value ( mutbl ) ) ) ,
1702+ TyKind :: Rptr ( lt, MutTy { ref ty, mutbl} ) if ty. node == TyKind :: ImplicitSelf => {
1703+ Some ( respan ( self . pat . span , SelfKind :: Region ( lt, mutbl) ) )
17261704 }
17271705 _ => Some ( respan ( mk_sp ( self . pat . span . lo , self . ty . span . hi ) ,
1728- SelfKind :: Explicit ( self . ty . clone ( ) , ident . node ) ) ) ,
1706+ SelfKind :: Explicit ( self . ty . clone ( ) , mutbl ) ) ) ,
17291707 }
17301708 }
17311709 }
17321710 None
17331711 }
17341712
1735- pub fn from_self ( eself : ExplicitSelf , ident_sp : Span , mutbl : Mutability ) -> Arg {
1736- let pat = |ident, span| P ( Pat {
1737- id : DUMMY_NODE_ID ,
1738- node : PatKind :: Ident ( BindingMode :: ByValue ( mutbl) , respan ( ident_sp, ident) , None ) ,
1739- span : span,
1740- } ) ;
1713+ pub fn is_self ( & self ) -> bool {
1714+ if let PatKind :: Ident ( _, ident, _) = self . pat . node {
1715+ ident. node . name == keywords:: SelfValue . name ( )
1716+ } else {
1717+ false
1718+ }
1719+ }
1720+
1721+ pub fn from_self ( eself : ExplicitSelf , eself_ident : SpannedIdent ) -> Arg {
17411722 let infer_ty = P ( Ty {
17421723 id : DUMMY_NODE_ID ,
1743- node : TyKind :: Infer ,
1724+ node : TyKind :: ImplicitSelf ,
17441725 span : DUMMY_SP ,
17451726 } ) ;
1746- let arg = |ident, ty, span| Arg {
1747- pat : pat ( ident, span) ,
1727+ let arg = |mutbl, ty, span| Arg {
1728+ pat : P ( Pat {
1729+ id : DUMMY_NODE_ID ,
1730+ node : PatKind :: Ident ( BindingMode :: ByValue ( mutbl) , eself_ident, None ) ,
1731+ span : span,
1732+ } ) ,
17481733 ty : ty,
17491734 id : DUMMY_NODE_ID ,
17501735 } ;
17511736 match eself. node {
1752- SelfKind :: Static => panic ! ( "bug: `Arg::from_self` is called \
1753- with `SelfKind::Static` argument" ) ,
1754- SelfKind :: Explicit ( ty , ident ) => arg ( ident , ty , mk_sp ( eself . span . lo , ident_sp . hi ) ) ,
1755- SelfKind :: Value ( ident ) => arg ( ident , infer_ty, eself. span ) ,
1756- SelfKind :: Region ( lt, mutbl, ident ) => arg ( ident , P ( Ty {
1737+ SelfKind :: Explicit ( ty , mutbl ) => {
1738+ arg ( mutbl , ty , mk_sp ( eself . span . lo , eself_ident . span . hi ) )
1739+ }
1740+ SelfKind :: Value ( mutbl ) => arg ( mutbl , infer_ty, eself. span ) ,
1741+ SelfKind :: Region ( lt, mutbl) => arg ( Mutability :: Immutable , P ( Ty {
17571742 id : DUMMY_NODE_ID ,
17581743 node : TyKind :: Rptr ( lt, MutTy { ty : infer_ty, mutbl : mutbl } ) ,
17591744 span : DUMMY_SP ,
@@ -1770,6 +1755,15 @@ pub struct FnDecl {
17701755 pub variadic : bool
17711756}
17721757
1758+ impl FnDecl {
1759+ pub fn get_self ( & self ) -> Option < ExplicitSelf > {
1760+ self . inputs . get ( 0 ) . and_then ( Arg :: to_self)
1761+ }
1762+ pub fn has_self ( & self ) -> bool {
1763+ self . inputs . get ( 0 ) . map ( Arg :: is_self) . unwrap_or ( false )
1764+ }
1765+ }
1766+
17731767#[ derive( Copy , Clone , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug ) ]
17741768pub enum Unsafety {
17751769 Unsafe ,
0 commit comments