@@ -16,13 +16,12 @@ pub use self::ViewPath_::*;
16
16
pub use self :: PathParameters :: * ;
17
17
18
18
use attr:: ThinAttributes ;
19
- use codemap:: { Span , Spanned , DUMMY_SP , ExpnId } ;
19
+ use codemap:: { mk_sp , respan , Span , Spanned , DUMMY_SP , ExpnId } ;
20
20
use abi:: Abi ;
21
21
use errors;
22
22
use ext:: base;
23
23
use ext:: tt:: macro_parser;
24
- use parse:: token:: InternedString ;
25
- use parse:: token;
24
+ use parse:: token:: { self , keywords, InternedString } ;
26
25
use parse:: lexer;
27
26
use parse:: lexer:: comments:: { doc_comment_style, strip_doc_comment_decoration} ;
28
27
use print:: pprust;
@@ -1674,7 +1673,25 @@ pub struct Arg {
1674
1673
pub id : NodeId ,
1675
1674
}
1676
1675
1676
+ /// Represents the kind of 'self' associated with a method.
1677
+ /// String representation of `Ident` here is always "self", but hygiene contexts may differ.
1678
+ #[ derive( Clone , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug ) ]
1679
+ pub enum SelfKind {
1680
+ /// No self
1681
+ Static ,
1682
+ /// `self`, `mut self`
1683
+ Value ( Ident ) ,
1684
+ /// `&'lt self`, `&'lt mut self`
1685
+ Region ( Option < Lifetime > , Mutability , Ident ) ,
1686
+ /// `self: TYPE`, `mut self: TYPE`
1687
+ Explicit ( P < Ty > , Ident ) ,
1688
+ }
1689
+
1690
+ pub type ExplicitSelf = Spanned < SelfKind > ;
1691
+
1677
1692
impl Arg {
1693
+ #[ unstable( feature = "rustc_private" , issue = "27812" ) ]
1694
+ #[ rustc_deprecated( since = "1.10.0" , reason = "use `from_self` instead" ) ]
1678
1695
pub fn new_self ( span : Span , mutability : Mutability , self_ident : Ident ) -> Arg {
1679
1696
let path = Spanned { span : span, node : self_ident} ;
1680
1697
Arg {
@@ -1692,6 +1709,51 @@ impl Arg {
1692
1709
id : DUMMY_NODE_ID
1693
1710
}
1694
1711
}
1712
+
1713
+ pub fn to_self ( & self ) -> Option < ExplicitSelf > {
1714
+ if let PatKind :: Ident ( _, ident, _) = self . pat . node {
1715
+ if ident. node . name == keywords:: SelfValue . name ( ) {
1716
+ return match self . ty . node {
1717
+ TyKind :: Infer => Some ( respan ( self . pat . span , SelfKind :: Value ( ident. node ) ) ) ,
1718
+ TyKind :: Rptr ( lt, MutTy { ref ty, mutbl} ) if ty. node == TyKind :: Infer => {
1719
+ Some ( respan ( self . pat . span , SelfKind :: Region ( lt, mutbl, ident. node ) ) )
1720
+ }
1721
+ _ => Some ( respan ( mk_sp ( self . pat . span . lo , self . ty . span . hi ) ,
1722
+ SelfKind :: Explicit ( self . ty . clone ( ) , ident. node ) ) ) ,
1723
+ }
1724
+ }
1725
+ }
1726
+ None
1727
+ }
1728
+
1729
+ pub fn from_self ( eself : ExplicitSelf , ident_sp : Span , mutbl : Mutability ) -> Arg {
1730
+ let pat = |ident, span| P ( Pat {
1731
+ id : DUMMY_NODE_ID ,
1732
+ node : PatKind :: Ident ( BindingMode :: ByValue ( mutbl) , respan ( ident_sp, ident) , None ) ,
1733
+ span : span,
1734
+ } ) ;
1735
+ let infer_ty = P ( Ty {
1736
+ id : DUMMY_NODE_ID ,
1737
+ node : TyKind :: Infer ,
1738
+ span : DUMMY_SP ,
1739
+ } ) ;
1740
+ let arg = |ident, ty, span| Arg {
1741
+ pat : pat ( ident, span) ,
1742
+ ty : ty,
1743
+ id : DUMMY_NODE_ID ,
1744
+ } ;
1745
+ match eself. node {
1746
+ SelfKind :: Static => panic ! ( "bug: `Arg::from_self` is called \
1747
+ with `SelfKind::Static` argument") ,
1748
+ SelfKind :: Explicit ( ty, ident) => arg ( ident, ty, mk_sp ( eself. span . lo , ident_sp. hi ) ) ,
1749
+ SelfKind :: Value ( ident) => arg ( ident, infer_ty, eself. span ) ,
1750
+ SelfKind :: Region ( lt, mutbl, ident) => arg ( ident, P ( Ty {
1751
+ id : DUMMY_NODE_ID ,
1752
+ node : TyKind :: Rptr ( lt, MutTy { ty : infer_ty, mutbl : mutbl } ) ,
1753
+ span : DUMMY_SP ,
1754
+ } ) , eself. span ) ,
1755
+ }
1756
+ }
1695
1757
}
1696
1758
1697
1759
/// Represents the header (not the body) of a function declaration
@@ -1772,21 +1834,6 @@ impl FunctionRetTy {
1772
1834
}
1773
1835
}
1774
1836
1775
- /// Represents the kind of 'self' associated with a method
1776
- #[ derive( Clone , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug ) ]
1777
- pub enum SelfKind {
1778
- /// No self
1779
- Static ,
1780
- /// `self`
1781
- Value ( Ident ) ,
1782
- /// `&'lt self`, `&'lt mut self`
1783
- Region ( Option < Lifetime > , Mutability , Ident ) ,
1784
- /// `self: TYPE`
1785
- Explicit ( P < Ty > , Ident ) ,
1786
- }
1787
-
1788
- pub type ExplicitSelf = Spanned < SelfKind > ;
1789
-
1790
1837
#[ derive( Clone , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug ) ]
1791
1838
pub struct Mod {
1792
1839
/// A span from the first token past `{` to the last token until `}`.
0 commit comments