Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ExplicitSelf from HIR #33505

Merged
merged 3 commits into from
May 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions src/librustc/hir/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,6 @@ pub trait Folder : Sized {
noop_fold_local(l, self)
}

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

fn fold_explicit_self_underscore(&mut self, es: ExplicitSelf_) -> ExplicitSelf_ {
noop_fold_explicit_self_underscore(es, self)
}

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

pub fn noop_fold_explicit_self_underscore<T: Folder>(es: ExplicitSelf_,
fld: &mut T)
-> ExplicitSelf_ {
match es {
SelfStatic | SelfValue(_) => es,
SelfRegion(lifetime, m, name) => {
SelfRegion(fld.fold_opt_lifetime(lifetime), m, name)
}
SelfExplicit(typ, name) => {
SelfExplicit(fld.fold_ty(typ), name)
}
}
}

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

pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> {
mi.map(|Spanned { node, span }| {
Spanned {
Expand Down Expand Up @@ -941,7 +910,6 @@ 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),
unsafety: sig.unsafety,
constness: sig.constness,
decl: folder.fold_fn_decl(sig.decl),
Expand Down
22 changes: 0 additions & 22 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ pub trait Visitor<'v> : Sized {
fn visit_lifetime_def(&mut self, lifetime: &'v LifetimeDef) {
walk_lifetime_def(self, lifetime)
}
fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
walk_explicit_self(self, es)
}
fn visit_path(&mut self, path: &'v Path, _id: NodeId) {
walk_path(self, path)
}
Expand Down Expand Up @@ -258,23 +255,6 @@ pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V, lifetime_def: &'v
walk_list!(visitor, visit_lifetime, &lifetime_def.bounds);
}

pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V, explicit_self: &'v ExplicitSelf) {
match explicit_self.node {
SelfStatic => {}
SelfValue(name) => {
visitor.visit_name(explicit_self.span, name)
}
SelfRegion(ref opt_lifetime, _, name) => {
visitor.visit_name(explicit_self.span, name);
walk_list!(visitor, visit_lifetime, opt_lifetime);
}
SelfExplicit(ref typ, name) => {
visitor.visit_name(explicit_self.span, name);
visitor.visit_ty(typ)
}
}
}

pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
trait_ref: &'v PolyTraitRef,
_modifier: &'v TraitBoundModifier)
Expand Down Expand Up @@ -620,7 +600,6 @@ pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'
}
FnKind::Method(_, sig, _, _) => {
visitor.visit_generics(&sig.generics);
visitor.visit_explicit_self(&sig.explicit_self);
}
FnKind::Closure(_) => {}
}
Expand All @@ -645,7 +624,6 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
walk_list!(visitor, visit_expr, default);
}
MethodTraitItem(ref sig, None) => {
visitor.visit_explicit_self(&sig.explicit_self);
visitor.visit_generics(&sig.generics);
walk_fn_decl(visitor, &sig.decl);
}
Expand Down
33 changes: 10 additions & 23 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,35 +388,13 @@ impl<'a> LoweringContext<'a> {
})
}

fn lower_explicit_self_underscore(&mut self, es: &SelfKind) -> hir::ExplicitSelf_ {
match *es {
SelfKind::Static => hir::SelfStatic,
SelfKind::Value(v) => hir::SelfValue(v.name),
SelfKind::Region(ref lifetime, m, ident) => {
hir::SelfRegion(self.lower_opt_lifetime(lifetime),
self.lower_mutability(m),
ident.name)
}
SelfKind::Explicit(ref typ, ident) => {
hir::SelfExplicit(self.lower_ty(typ), ident.name)
}
}
}

fn lower_mutability(&mut self, m: Mutability) -> hir::Mutability {
match m {
Mutability::Mutable => hir::MutMutable,
Mutability::Immutable => hir::MutImmutable,
}
}

fn lower_explicit_self(&mut self, s: &ExplicitSelf) -> hir::ExplicitSelf {
Spanned {
node: self.lower_explicit_self_underscore(&s.node),
span: s.span,
}
}

fn lower_arg(&mut self, arg: &Arg) -> hir::Arg {
hir::Arg {
id: arg.id,
Expand Down Expand Up @@ -797,10 +775,19 @@ 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) {
Some(SelfKind::Value(..)) | Some(SelfKind::Region(..)) => {
self.id_assigner.diagnostic().span_err(ty.span,
"the type placeholder `_` is not allowed within types on item signatures");
}
_ => {}
}
}
hir::MethodSig {
generics: self.lower_generics(&sig.generics),
abi: sig.abi,
explicit_self: self.lower_explicit_self(&sig.explicit_self),
unsafety: self.lower_unsafety(sig.unsafety),
constness: self.lower_constness(sig.constness),
decl: self.lower_fn_decl(&sig.decl),
Expand Down
79 changes: 42 additions & 37 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub use self::BinOp_::*;
pub use self::BlockCheckMode::*;
pub use self::CaptureClause::*;
pub use self::Decl_::*;
pub use self::ExplicitSelf_::*;
pub use self::Expr_::*;
pub use self::FunctionRetTy::*;
pub use self::ForeignItem_::*;
Expand All @@ -37,12 +36,12 @@ use hir::def::Def;
use hir::def_id::DefId;
use util::nodemap::{NodeMap, FnvHashSet};

use syntax::codemap::{self, Span, Spanned, DUMMY_SP, ExpnId};
use syntax::codemap::{self, mk_sp, respan, Span, Spanned, ExpnId};
use syntax::abi::Abi;
use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, TokenTree, AsmDialect};
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
use syntax::attr::{ThinAttributes, ThinAttributesExt};
use syntax::parse::token::InternedString;
use syntax::parse::token::{keywords, InternedString};
use syntax::ptr::P;

use std::collections::BTreeMap;
Expand Down Expand Up @@ -1055,7 +1054,6 @@ pub struct MethodSig {
pub abi: Abi,
pub decl: P<FnDecl>,
pub generics: Generics,
pub explicit_self: ExplicitSelf,
}

/// Represents an item declaration within a trait declaration,
Expand Down Expand Up @@ -1196,25 +1194,41 @@ pub struct Arg {
pub id: NodeId,
}

/// Alternative representation for `Arg`s describing `self` parameter of methods.
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum SelfKind {
/// `self`, `mut self`
Value(Mutability),
/// `&'lt self`, `&'lt mut self`
Region(Option<Lifetime>, Mutability),
/// `self: TYPE`, `mut self: TYPE`
Explicit(P<Ty>, Mutability),
}

pub type ExplicitSelf = Spanned<SelfKind>;

impl Arg {
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: TyInfer,
span: DUMMY_SP,
}),
pat: P(Pat {
id: DUMMY_NODE_ID,
node: PatKind::Ident(BindByValue(mutability), path, None),
span: span,
}),
id: DUMMY_NODE_ID,
pub fn to_self(&self) -> Option<ExplicitSelf> {
if let PatKind::Ident(BindByValue(mutbl), ident, _) = self.pat.node {
if ident.node.unhygienic_name == keywords::SelfValue.name() {
return match self.ty.node {
TyInfer => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
TyRptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyInfer => {
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(), mutbl)))
}
}
}
None
}

pub fn is_self(&self) -> bool {
if let PatKind::Ident(_, ident, _) = self.pat.node {
ident.node.unhygienic_name == keywords::SelfValue.name()
} else {
false
}
}
}
Expand All @@ -1227,6 +1241,12 @@ pub struct FnDecl {
pub variadic: bool,
}

impl FnDecl {
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 Expand Up @@ -1308,21 +1328,6 @@ impl FunctionRetTy {
}
}

/// Represents the kind of 'self' associated with a method
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum ExplicitSelf_ {
/// No self
SelfStatic,
/// `self`
SelfValue(Name),
/// `&'lt self`, `&'lt mut self`
SelfRegion(Option<Lifetime>, Mutability, Name),
/// `self: TYPE`
SelfExplicit(P<Ty>, Name),
}

pub type ExplicitSelf = Spanned<ExplicitSelf_>;

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct Mod {
/// A span from the first token past `{` to the last token until `}`.
Expand Down
Loading