Skip to content

Commit 212d5d4

Browse files
committed
syntax: Refactor parsing of method declarations
Fix spans and expected token lists, fix #33413 + other cosmetic improvements Add test for #33413 Convert between `Arg` and `ExplicitSelf` precisely Simplify pretty-printing for methods
1 parent d3ec9d4 commit 212d5d4

File tree

6 files changed

+206
-205
lines changed

6 files changed

+206
-205
lines changed

src/libsyntax/ast.rs

+65-18
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ pub use self::ViewPath_::*;
1616
pub use self::PathParameters::*;
1717

1818
use attr::ThinAttributes;
19-
use codemap::{Span, Spanned, DUMMY_SP, ExpnId};
19+
use codemap::{mk_sp, respan, Span, Spanned, DUMMY_SP, ExpnId};
2020
use abi::Abi;
2121
use errors;
2222
use ext::base;
2323
use ext::tt::macro_parser;
24-
use parse::token::InternedString;
25-
use parse::token;
24+
use parse::token::{self, keywords, InternedString};
2625
use parse::lexer;
2726
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
2827
use print::pprust;
@@ -1674,7 +1673,25 @@ pub struct Arg {
16741673
pub id: NodeId,
16751674
}
16761675

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+
16771692
impl Arg {
1693+
#[unstable(feature = "rustc_private", issue = "27812")]
1694+
#[rustc_deprecated(since = "1.10.0", reason = "use `from_self` instead")]
16781695
pub fn new_self(span: Span, mutability: Mutability, self_ident: Ident) -> Arg {
16791696
let path = Spanned{span:span,node:self_ident};
16801697
Arg {
@@ -1692,6 +1709,51 @@ impl Arg {
16921709
id: DUMMY_NODE_ID
16931710
}
16941711
}
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+
}
16951757
}
16961758

16971759
/// Represents the header (not the body) of a function declaration
@@ -1772,21 +1834,6 @@ impl FunctionRetTy {
17721834
}
17731835
}
17741836

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-
17901837
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
17911838
pub struct Mod {
17921839
/// A span from the first token past `{` to the last token until `}`.

0 commit comments

Comments
 (0)