Skip to content

Commit 5660a00

Browse files
committed
Remove ExplicitSelf from AST
1 parent 5229e0e commit 5660a00

File tree

15 files changed

+155
-261
lines changed

15 files changed

+155
-261
lines changed

src/librustc/hir/lowering.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,10 +788,10 @@ impl<'a> LoweringContext<'a> {
788788

789789
fn lower_method_sig(&mut self, sig: &MethodSig) -> hir::MethodSig {
790790
// Check for `self: _` and `self: &_`
791-
if let SelfKind::Explicit(ref ty, _) = sig.explicit_self.node {
792-
match sig.decl.inputs.get(0).and_then(Arg::to_self).map(|eself| eself.node) {
791+
if !sig.self_shortcut {
792+
match sig.decl.get_self().map(|eself| eself.node) {
793793
Some(SelfKind::Value(..)) | Some(SelfKind::Region(..)) => {
794-
self.id_assigner.diagnostic().span_err(ty.span,
794+
self.id_assigner.diagnostic().span_err(sig.decl.inputs[0].ty.span,
795795
"the type placeholder `_` is not allowed within types on item signatures");
796796
}
797797
_ => {}

src/librustc/hir/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,9 @@ pub struct FnDecl {
11751175
}
11761176

11771177
impl FnDecl {
1178+
pub fn get_self(&self) -> Option<ExplicitSelf> {
1179+
self.inputs.get(0).and_then(Arg::to_self)
1180+
}
11781181
pub fn has_self(&self) -> bool {
11791182
self.inputs.get(0).map(Arg::is_self).unwrap_or(false)
11801183
}

src/librustc/lint/context.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,11 +1043,6 @@ impl<'a, 'v> ast_visit::Visitor<'v> for EarlyContext<'a> {
10431043
run_lints!(self, check_lifetime_def, early_passes, lt);
10441044
}
10451045

1046-
fn visit_explicit_self(&mut self, es: &ast::ExplicitSelf) {
1047-
run_lints!(self, check_explicit_self, early_passes, es);
1048-
ast_visit::walk_explicit_self(self, es);
1049-
}
1050-
10511046
fn visit_path(&mut self, p: &ast::Path, id: ast::NodeId) {
10521047
run_lints!(self, check_path, early_passes, p, id);
10531048
ast_visit::walk_path(self, p);

src/librustc/lint/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ pub trait LateLintPass: LintPass {
167167
fn check_variant_post(&mut self, _: &LateContext, _: &hir::Variant, _: &hir::Generics) { }
168168
fn check_lifetime(&mut self, _: &LateContext, _: &hir::Lifetime) { }
169169
fn check_lifetime_def(&mut self, _: &LateContext, _: &hir::LifetimeDef) { }
170-
fn check_explicit_self(&mut self, _: &LateContext, _: &hir::ExplicitSelf) { }
171170
fn check_path(&mut self, _: &LateContext, _: &hir::Path, _: ast::NodeId) { }
172171
fn check_path_list_item(&mut self, _: &LateContext, _: &hir::PathListItem) { }
173172
fn check_attribute(&mut self, _: &LateContext, _: &ast::Attribute) { }
@@ -218,7 +217,6 @@ pub trait EarlyLintPass: LintPass {
218217
fn check_variant_post(&mut self, _: &EarlyContext, _: &ast::Variant, _: &ast::Generics) { }
219218
fn check_lifetime(&mut self, _: &EarlyContext, _: &ast::Lifetime) { }
220219
fn check_lifetime_def(&mut self, _: &EarlyContext, _: &ast::LifetimeDef) { }
221-
fn check_explicit_self(&mut self, _: &EarlyContext, _: &ast::ExplicitSelf) { }
222220
fn check_path(&mut self, _: &EarlyContext, _: &ast::Path, _: ast::NodeId) { }
223221
fn check_path_list_item(&mut self, _: &EarlyContext, _: &ast::PathListItem) { }
224222
fn check_attribute(&mut self, _: &EarlyContext, _: &ast::Attribute) { }

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use syntax::codemap::{Span, DUMMY_SP};
3535
use syntax::ast::{Block, Crate, DeclKind};
3636
use syntax::ast::{ForeignItem, ForeignItemKind, Item, ItemKind};
3737
use syntax::ast::{Mutability, PathListItemKind};
38-
use syntax::ast::{SelfKind, Stmt, StmtKind, TraitItemKind};
38+
use syntax::ast::{Stmt, StmtKind, TraitItemKind};
3939
use syntax::ast::{Variant, ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
4040
use syntax::visit::{self, Visitor};
4141

@@ -335,7 +335,7 @@ impl<'b> Resolver<'b> {
335335
let (def, ns) = match item.node {
336336
TraitItemKind::Const(..) => (Def::AssociatedConst(item_def_id), ValueNS),
337337
TraitItemKind::Method(ref sig, _) => {
338-
is_static_method = sig.explicit_self.node == SelfKind::Static;
338+
is_static_method = !sig.decl.has_self();
339339
(Def::Method(item_def_id), ValueNS)
340340
}
341341
TraitItemKind::Type(..) => (Def::AssociatedTy(def_id, item_def_id), TypeNS),

src/librustc_typeck/astconv.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,8 +1833,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
18331833
// lifetime elision, we can determine it in two ways. First (determined
18341834
// here), if self is by-reference, then the implied output region is the
18351835
// region of the self parameter.
1836-
let explicit_self = decl.inputs.get(0).and_then(hir::Arg::to_self);
1837-
let (self_ty, explicit_self_category) = match (opt_untransformed_self_ty, explicit_self) {
1836+
let (self_ty, explicit_self_category) = match (opt_untransformed_self_ty, decl.get_self()) {
18381837
(Some(untransformed_self_ty), Some(explicit_self)) => {
18391838
let self_type = self.determine_self_type(&rb, untransformed_self_ty,
18401839
&explicit_self);

src/libsyntax/ast.rs

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
16831683
pub 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

16941692
pub type ExplicitSelf = Spanned<SelfKind>;
16951693

16961694
impl 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)]
17721766
pub enum Unsafety {
17731767
Unsafe,

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ fn expand_and_rename_method(sig: ast::MethodSig, body: P<ast::Block>,
11281128
(ast::MethodSig {
11291129
generics: fld.fold_generics(sig.generics),
11301130
abi: sig.abi,
1131-
explicit_self: fld.fold_explicit_self(sig.explicit_self),
1131+
self_shortcut: sig.self_shortcut,
11321132
unsafety: sig.unsafety,
11331133
constness: sig.constness,
11341134
decl: rewritten_fn_decl

src/libsyntax/fold.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,6 @@ pub trait Folder : Sized {
179179
// fold::noop_fold_mac(_mac, self)
180180
}
181181

182-
fn fold_explicit_self(&mut self, es: ExplicitSelf) -> ExplicitSelf {
183-
noop_fold_explicit_self(es, self)
184-
}
185-
186-
fn fold_explicit_self_kind(&mut self, es: SelfKind) -> SelfKind {
187-
noop_fold_explicit_self_kind(es, self)
188-
}
189-
190182
fn fold_lifetime(&mut self, l: Lifetime) -> Lifetime {
191183
noop_fold_lifetime(l, self)
192184
}
@@ -523,28 +515,6 @@ pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attr
523515
})
524516
}
525517

526-
pub fn noop_fold_explicit_self_kind<T: Folder>(es: SelfKind, fld: &mut T)
527-
-> SelfKind {
528-
match es {
529-
SelfKind::Static | SelfKind::Value(_) => es,
530-
SelfKind::Region(lifetime, m, ident) => {
531-
SelfKind::Region(fld.fold_opt_lifetime(lifetime), m, ident)
532-
}
533-
SelfKind::Explicit(typ, ident) => {
534-
SelfKind::Explicit(fld.fold_ty(typ), ident)
535-
}
536-
}
537-
}
538-
539-
pub fn noop_fold_explicit_self<T: Folder>(Spanned {span, node}: ExplicitSelf, fld: &mut T)
540-
-> ExplicitSelf {
541-
Spanned {
542-
node: fld.fold_explicit_self_kind(node),
543-
span: fld.new_span(span)
544-
}
545-
}
546-
547-
548518
pub fn noop_fold_mac<T: Folder>(Spanned {node, span}: Mac, fld: &mut T) -> Mac {
549519
Spanned {
550520
node: Mac_ {
@@ -1096,7 +1066,7 @@ pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> Method
10961066
MethodSig {
10971067
generics: folder.fold_generics(sig.generics),
10981068
abi: sig.abi,
1099-
explicit_self: folder.fold_explicit_self(sig.explicit_self),
1069+
self_shortcut: sig.self_shortcut,
11001070
unsafety: sig.unsafety,
11011071
constness: sig.constness,
11021072
decl: folder.fold_fn_decl(sig.decl)

0 commit comments

Comments
 (0)