@@ -1389,7 +1389,6 @@ pub struct MethodSig {
1389
1389
pub abi : Abi ,
1390
1390
pub decl : P < FnDecl > ,
1391
1391
pub generics : Generics ,
1392
- pub explicit_self : ExplicitSelf ,
1393
1392
}
1394
1393
1395
1394
/// Represents an item declaration within a trait declaration,
@@ -1640,6 +1639,8 @@ pub enum TyKind {
1640
1639
/// TyKind::Infer means the type should be inferred instead of it having been
1641
1640
/// specified. This can appear anywhere in a type.
1642
1641
Infer ,
1642
+ /// Inferred type of a `self` or `&self` argument in a method.
1643
+ ImplicitSelf ,
1643
1644
// A macro in the type position.
1644
1645
Mac ( Mac ) ,
1645
1646
}
@@ -1679,81 +1680,65 @@ pub struct Arg {
1679
1680
pub id : NodeId ,
1680
1681
}
1681
1682
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.
1684
1684
#[ derive( Clone , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug ) ]
1685
1685
pub enum SelfKind {
1686
- /// No self
1687
- Static ,
1688
1686
/// `self`, `mut self`
1689
- Value ( Ident ) ,
1687
+ Value ( Mutability ) ,
1690
1688
/// `&'lt self`, `&'lt mut self`
1691
- Region ( Option < Lifetime > , Mutability , Ident ) ,
1689
+ Region ( Option < Lifetime > , Mutability ) ,
1692
1690
/// `self: TYPE`, `mut self: TYPE`
1693
- Explicit ( P < Ty > , Ident ) ,
1691
+ Explicit ( P < Ty > , Mutability ) ,
1694
1692
}
1695
1693
1696
1694
pub type ExplicitSelf = Spanned < SelfKind > ;
1697
1695
1698
1696
impl 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
-
1719
1697
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 {
1721
1699
if ident. node . name == keywords:: SelfValue . name ( ) {
1722
1700
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) ) )
1726
1704
}
1727
1705
_ => 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 ) ) ) ,
1729
1707
}
1730
1708
}
1731
1709
}
1732
1710
None
1733
1711
}
1734
1712
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 {
1741
1722
let infer_ty = P ( Ty {
1742
1723
id : DUMMY_NODE_ID ,
1743
- node : TyKind :: Infer ,
1724
+ node : TyKind :: ImplicitSelf ,
1744
1725
span : DUMMY_SP ,
1745
1726
} ) ;
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
+ } ) ,
1748
1733
ty : ty,
1749
1734
id : DUMMY_NODE_ID ,
1750
1735
} ;
1751
1736
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 {
1757
1742
id : DUMMY_NODE_ID ,
1758
1743
node : TyKind :: Rptr ( lt, MutTy { ty : infer_ty, mutbl : mutbl } ) ,
1759
1744
span : DUMMY_SP ,
@@ -1770,6 +1755,15 @@ pub struct FnDecl {
1770
1755
pub variadic : bool
1771
1756
}
1772
1757
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
+
1773
1767
#[ derive( Copy , Clone , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug ) ]
1774
1768
pub enum Unsafety {
1775
1769
Unsafe ,
0 commit comments