Navigation Menu

Skip to content

Commit

Permalink
Remove ExplicitSelf from AST
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed May 25, 2016
1 parent 5229e0e commit 5660a00
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 261 deletions.
6 changes: 3 additions & 3 deletions src/librustc/hir/lowering.rs
Expand Up @@ -788,10 +788,10 @@ impl<'a> LoweringContext<'a> {

fn lower_method_sig(&mut self, sig: &MethodSig) -> hir::MethodSig {
// Check for `self: _` and `self: &_`
if let SelfKind::Explicit(ref ty, _) = sig.explicit_self.node {
match sig.decl.inputs.get(0).and_then(Arg::to_self).map(|eself| eself.node) {
if !sig.self_shortcut {
match sig.decl.get_self().map(|eself| eself.node) {
Some(SelfKind::Value(..)) | Some(SelfKind::Region(..)) => {
self.id_assigner.diagnostic().span_err(ty.span,
self.id_assigner.diagnostic().span_err(sig.decl.inputs[0].ty.span,
"the type placeholder `_` is not allowed within types on item signatures");
}
_ => {}
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/hir/mod.rs
Expand Up @@ -1175,6 +1175,9 @@ pub struct FnDecl {
}

impl FnDecl {
pub fn get_self(&self) -> Option<ExplicitSelf> {
self.inputs.get(0).and_then(Arg::to_self)
}
pub fn has_self(&self) -> bool {
self.inputs.get(0).map(Arg::is_self).unwrap_or(false)
}
Expand Down
5 changes: 0 additions & 5 deletions src/librustc/lint/context.rs
Expand Up @@ -1043,11 +1043,6 @@ impl<'a, 'v> ast_visit::Visitor<'v> for EarlyContext<'a> {
run_lints!(self, check_lifetime_def, early_passes, lt);
}

fn visit_explicit_self(&mut self, es: &ast::ExplicitSelf) {
run_lints!(self, check_explicit_self, early_passes, es);
ast_visit::walk_explicit_self(self, es);
}

fn visit_path(&mut self, p: &ast::Path, id: ast::NodeId) {
run_lints!(self, check_path, early_passes, p, id);
ast_visit::walk_path(self, p);
Expand Down
2 changes: 0 additions & 2 deletions src/librustc/lint/mod.rs
Expand Up @@ -167,7 +167,6 @@ pub trait LateLintPass: LintPass {
fn check_variant_post(&mut self, _: &LateContext, _: &hir::Variant, _: &hir::Generics) { }
fn check_lifetime(&mut self, _: &LateContext, _: &hir::Lifetime) { }
fn check_lifetime_def(&mut self, _: &LateContext, _: &hir::LifetimeDef) { }
fn check_explicit_self(&mut self, _: &LateContext, _: &hir::ExplicitSelf) { }
fn check_path(&mut self, _: &LateContext, _: &hir::Path, _: ast::NodeId) { }
fn check_path_list_item(&mut self, _: &LateContext, _: &hir::PathListItem) { }
fn check_attribute(&mut self, _: &LateContext, _: &ast::Attribute) { }
Expand Down Expand Up @@ -218,7 +217,6 @@ pub trait EarlyLintPass: LintPass {
fn check_variant_post(&mut self, _: &EarlyContext, _: &ast::Variant, _: &ast::Generics) { }
fn check_lifetime(&mut self, _: &EarlyContext, _: &ast::Lifetime) { }
fn check_lifetime_def(&mut self, _: &EarlyContext, _: &ast::LifetimeDef) { }
fn check_explicit_self(&mut self, _: &EarlyContext, _: &ast::ExplicitSelf) { }
fn check_path(&mut self, _: &EarlyContext, _: &ast::Path, _: ast::NodeId) { }
fn check_path_list_item(&mut self, _: &EarlyContext, _: &ast::PathListItem) { }
fn check_attribute(&mut self, _: &EarlyContext, _: &ast::Attribute) { }
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -35,7 +35,7 @@ use syntax::codemap::{Span, DUMMY_SP};
use syntax::ast::{Block, Crate, DeclKind};
use syntax::ast::{ForeignItem, ForeignItemKind, Item, ItemKind};
use syntax::ast::{Mutability, PathListItemKind};
use syntax::ast::{SelfKind, Stmt, StmtKind, TraitItemKind};
use syntax::ast::{Stmt, StmtKind, TraitItemKind};
use syntax::ast::{Variant, ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
use syntax::visit::{self, Visitor};

Expand Down Expand Up @@ -335,7 +335,7 @@ impl<'b> Resolver<'b> {
let (def, ns) = match item.node {
TraitItemKind::Const(..) => (Def::AssociatedConst(item_def_id), ValueNS),
TraitItemKind::Method(ref sig, _) => {
is_static_method = sig.explicit_self.node == SelfKind::Static;
is_static_method = !sig.decl.has_self();
(Def::Method(item_def_id), ValueNS)
}
TraitItemKind::Type(..) => (Def::AssociatedTy(def_id, item_def_id), TypeNS),
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_typeck/astconv.rs
Expand Up @@ -1833,8 +1833,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
// lifetime elision, we can determine it in two ways. First (determined
// here), if self is by-reference, then the implied output region is the
// region of the self parameter.
let explicit_self = decl.inputs.get(0).and_then(hir::Arg::to_self);
let (self_ty, explicit_self_category) = match (opt_untransformed_self_ty, explicit_self) {
let (self_ty, explicit_self_category) = match (opt_untransformed_self_ty, decl.get_self()) {
(Some(untransformed_self_ty), Some(explicit_self)) => {
let self_type = self.determine_self_type(&rb, untransformed_self_ty,
&explicit_self);
Expand Down
84 changes: 39 additions & 45 deletions src/libsyntax/ast.rs
Expand Up @@ -1387,7 +1387,8 @@ pub struct MethodSig {
pub abi: Abi,
pub decl: P<FnDecl>,
pub generics: Generics,
pub explicit_self: ExplicitSelf,
/// A short form of self argument was used (`self`, `&self` etc, but not `self: TYPE`).
pub self_shortcut: bool,
}

/// Represents an item declaration within a trait declaration,
Expand Down Expand Up @@ -1677,81 +1678,65 @@ pub struct Arg {
pub id: NodeId,
}

/// Represents the kind of 'self' associated with a method.
/// String representation of `Ident` here is always "self", but hygiene contexts may differ.
/// Alternative representation for `Arg`s describing `self` parameter of methods.
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum SelfKind {
/// No self
Static,
/// `self`, `mut self`
Value(Ident),
Value(Mutability),
/// `&'lt self`, `&'lt mut self`
Region(Option<Lifetime>, Mutability, Ident),
Region(Option<Lifetime>, Mutability),
/// `self: TYPE`, `mut self: TYPE`
Explicit(P<Ty>, Ident),
Explicit(P<Ty>, Mutability),
}

pub type ExplicitSelf = Spanned<SelfKind>;

impl Arg {
#[unstable(feature = "rustc_private", issue = "27812")]
#[rustc_deprecated(since = "1.10.0", reason = "use `from_self` instead")]
pub fn new_self(span: Span, mutability: Mutability, self_ident: Ident) -> Arg {
let path = Spanned{span:span,node:self_ident};
Arg {
// HACK(eddyb) fake type for the self argument.
ty: P(Ty {
id: DUMMY_NODE_ID,
node: TyKind::Infer,
span: DUMMY_SP,
}),
pat: P(Pat {
id: DUMMY_NODE_ID,
node: PatKind::Ident(BindingMode::ByValue(mutability), path, None),
span: span
}),
id: DUMMY_NODE_ID
}
}

pub fn to_self(&self) -> Option<ExplicitSelf> {
if let PatKind::Ident(_, ident, _) = self.pat.node {
if let PatKind::Ident(BindingMode::ByValue(mutbl), ident, _) = self.pat.node {
if ident.node.name == keywords::SelfValue.name() {
return match self.ty.node {
TyKind::Infer => Some(respan(self.pat.span, SelfKind::Value(ident.node))),
TyKind::Infer => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
TyKind::Rptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyKind::Infer => {
Some(respan(self.pat.span, SelfKind::Region(lt, mutbl, ident.node)))
Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
}
_ => Some(respan(mk_sp(self.pat.span.lo, self.ty.span.hi),
SelfKind::Explicit(self.ty.clone(), ident.node))),
SelfKind::Explicit(self.ty.clone(), mutbl))),
}
}
}
None
}

pub fn from_self(eself: ExplicitSelf, ident_sp: Span, mutbl: Mutability) -> Arg {
let pat = |ident, span| P(Pat {
id: DUMMY_NODE_ID,
node: PatKind::Ident(BindingMode::ByValue(mutbl), respan(ident_sp, ident), None),
span: span,
});
pub fn is_self(&self) -> bool {
if let PatKind::Ident(_, ident, _) = self.pat.node {
ident.node.name == keywords::SelfValue.name()
} else {
false
}
}

pub fn from_self(eself: ExplicitSelf, eself_ident: SpannedIdent) -> Arg {
let infer_ty = P(Ty {
id: DUMMY_NODE_ID,
node: TyKind::Infer,
span: DUMMY_SP,
});
let arg = |ident, ty, span| Arg {
pat: pat(ident, span),
let arg = |mutbl, ty, span| Arg {
pat: P(Pat {
id: DUMMY_NODE_ID,
node: PatKind::Ident(BindingMode::ByValue(mutbl), eself_ident, None),
span: span,
}),
ty: ty,
id: DUMMY_NODE_ID,
};
match eself.node {
SelfKind::Static => panic!("bug: `Arg::from_self` is called \
with `SelfKind::Static` argument"),
SelfKind::Explicit(ty, ident) => arg(ident, ty, mk_sp(eself.span.lo, ident_sp.hi)),
SelfKind::Value(ident) => arg(ident, infer_ty, eself.span),
SelfKind::Region(lt, mutbl, ident) => arg(ident, P(Ty {
SelfKind::Explicit(ty, mutbl) => {
arg(mutbl, ty, mk_sp(eself.span.lo, eself_ident.span.hi))
}
SelfKind::Value(mutbl) => arg(mutbl, infer_ty, eself.span),
SelfKind::Region(lt, mutbl) => arg(Mutability::Immutable, P(Ty {
id: DUMMY_NODE_ID,
node: TyKind::Rptr(lt, MutTy { ty: infer_ty, mutbl: mutbl }),
span: DUMMY_SP,
Expand All @@ -1768,6 +1753,15 @@ pub struct FnDecl {
pub variadic: bool
}

impl FnDecl {
pub fn get_self(&self) -> Option<ExplicitSelf> {
self.inputs.get(0).and_then(Arg::to_self)
}
pub fn has_self(&self) -> bool {
self.inputs.get(0).map(Arg::is_self).unwrap_or(false)
}
}

#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum Unsafety {
Unsafe,
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/expand.rs
Expand Up @@ -1128,7 +1128,7 @@ fn expand_and_rename_method(sig: ast::MethodSig, body: P<ast::Block>,
(ast::MethodSig {
generics: fld.fold_generics(sig.generics),
abi: sig.abi,
explicit_self: fld.fold_explicit_self(sig.explicit_self),
self_shortcut: sig.self_shortcut,
unsafety: sig.unsafety,
constness: sig.constness,
decl: rewritten_fn_decl
Expand Down
32 changes: 1 addition & 31 deletions src/libsyntax/fold.rs
Expand Up @@ -179,14 +179,6 @@ pub trait Folder : Sized {
// fold::noop_fold_mac(_mac, self)
}

fn fold_explicit_self(&mut self, es: ExplicitSelf) -> ExplicitSelf {
noop_fold_explicit_self(es, self)
}

fn fold_explicit_self_kind(&mut self, es: SelfKind) -> SelfKind {
noop_fold_explicit_self_kind(es, self)
}

fn fold_lifetime(&mut self, l: Lifetime) -> Lifetime {
noop_fold_lifetime(l, self)
}
Expand Down Expand Up @@ -523,28 +515,6 @@ pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attr
})
}

pub fn noop_fold_explicit_self_kind<T: Folder>(es: SelfKind, fld: &mut T)
-> SelfKind {
match es {
SelfKind::Static | SelfKind::Value(_) => es,
SelfKind::Region(lifetime, m, ident) => {
SelfKind::Region(fld.fold_opt_lifetime(lifetime), m, ident)
}
SelfKind::Explicit(typ, ident) => {
SelfKind::Explicit(fld.fold_ty(typ), ident)
}
}
}

pub fn noop_fold_explicit_self<T: Folder>(Spanned {span, node}: ExplicitSelf, fld: &mut T)
-> ExplicitSelf {
Spanned {
node: fld.fold_explicit_self_kind(node),
span: fld.new_span(span)
}
}


pub fn noop_fold_mac<T: Folder>(Spanned {node, span}: Mac, fld: &mut T) -> Mac {
Spanned {
node: Mac_ {
Expand Down Expand Up @@ -1096,7 +1066,7 @@ pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> Method
MethodSig {
generics: folder.fold_generics(sig.generics),
abi: sig.abi,
explicit_self: folder.fold_explicit_self(sig.explicit_self),
self_shortcut: sig.self_shortcut,
unsafety: sig.unsafety,
constness: sig.constness,
decl: folder.fold_fn_decl(sig.decl)
Expand Down

0 comments on commit 5660a00

Please sign in to comment.