Skip to content

Commit e87cd7e

Browse files
committed
Auto merge of #33505 - petrochenkov:self, r=nrc
Remove ExplicitSelf from HIR `self` argument is already kept in the argument list and can be retrieved from there if necessary, so there's no need for the duplication. The same changes can be applied to AST, I'll make them in the next breaking batch. The first commit also improves parsing of method declarations and fixes #33413. r? @eddyb
2 parents e90307d + a62a690 commit e87cd7e

File tree

23 files changed

+430
-636
lines changed

23 files changed

+430
-636
lines changed

src/librustc/hir/fold.rs

-32
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,6 @@ pub trait Folder : Sized {
158158
noop_fold_local(l, self)
159159
}
160160

161-
fn fold_explicit_self(&mut self, es: ExplicitSelf) -> ExplicitSelf {
162-
noop_fold_explicit_self(es, self)
163-
}
164-
165-
fn fold_explicit_self_underscore(&mut self, es: ExplicitSelf_) -> ExplicitSelf_ {
166-
noop_fold_explicit_self_underscore(es, self)
167-
}
168-
169161
fn fold_lifetime(&mut self, l: Lifetime) -> Lifetime {
170162
noop_fold_lifetime(l, self)
171163
}
@@ -495,29 +487,6 @@ pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attr
495487
})
496488
}
497489

498-
pub fn noop_fold_explicit_self_underscore<T: Folder>(es: ExplicitSelf_,
499-
fld: &mut T)
500-
-> ExplicitSelf_ {
501-
match es {
502-
SelfStatic | SelfValue(_) => es,
503-
SelfRegion(lifetime, m, name) => {
504-
SelfRegion(fld.fold_opt_lifetime(lifetime), m, name)
505-
}
506-
SelfExplicit(typ, name) => {
507-
SelfExplicit(fld.fold_ty(typ), name)
508-
}
509-
}
510-
}
511-
512-
pub fn noop_fold_explicit_self<T: Folder>(Spanned { span, node }: ExplicitSelf,
513-
fld: &mut T)
514-
-> ExplicitSelf {
515-
Spanned {
516-
node: fld.fold_explicit_self_underscore(node),
517-
span: fld.new_span(span),
518-
}
519-
}
520-
521490
pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> {
522491
mi.map(|Spanned { node, span }| {
523492
Spanned {
@@ -941,7 +910,6 @@ pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> Method
941910
MethodSig {
942911
generics: folder.fold_generics(sig.generics),
943912
abi: sig.abi,
944-
explicit_self: folder.fold_explicit_self(sig.explicit_self),
945913
unsafety: sig.unsafety,
946914
constness: sig.constness,
947915
decl: folder.fold_fn_decl(sig.decl),

src/librustc/hir/intravisit.rs

-22
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,6 @@ pub trait Visitor<'v> : Sized {
180180
fn visit_lifetime_def(&mut self, lifetime: &'v LifetimeDef) {
181181
walk_lifetime_def(self, lifetime)
182182
}
183-
fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
184-
walk_explicit_self(self, es)
185-
}
186183
fn visit_path(&mut self, path: &'v Path, _id: NodeId) {
187184
walk_path(self, path)
188185
}
@@ -258,23 +255,6 @@ pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V, lifetime_def: &'v
258255
walk_list!(visitor, visit_lifetime, &lifetime_def.bounds);
259256
}
260257

261-
pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V, explicit_self: &'v ExplicitSelf) {
262-
match explicit_self.node {
263-
SelfStatic => {}
264-
SelfValue(name) => {
265-
visitor.visit_name(explicit_self.span, name)
266-
}
267-
SelfRegion(ref opt_lifetime, _, name) => {
268-
visitor.visit_name(explicit_self.span, name);
269-
walk_list!(visitor, visit_lifetime, opt_lifetime);
270-
}
271-
SelfExplicit(ref typ, name) => {
272-
visitor.visit_name(explicit_self.span, name);
273-
visitor.visit_ty(typ)
274-
}
275-
}
276-
}
277-
278258
pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
279259
trait_ref: &'v PolyTraitRef,
280260
_modifier: &'v TraitBoundModifier)
@@ -620,7 +600,6 @@ pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'
620600
}
621601
FnKind::Method(_, sig, _, _) => {
622602
visitor.visit_generics(&sig.generics);
623-
visitor.visit_explicit_self(&sig.explicit_self);
624603
}
625604
FnKind::Closure(_) => {}
626605
}
@@ -645,7 +624,6 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
645624
walk_list!(visitor, visit_expr, default);
646625
}
647626
MethodTraitItem(ref sig, None) => {
648-
visitor.visit_explicit_self(&sig.explicit_self);
649627
visitor.visit_generics(&sig.generics);
650628
walk_fn_decl(visitor, &sig.decl);
651629
}

src/librustc/hir/lowering.rs

+10-23
Original file line numberDiff line numberDiff line change
@@ -388,35 +388,13 @@ impl<'a> LoweringContext<'a> {
388388
})
389389
}
390390

391-
fn lower_explicit_self_underscore(&mut self, es: &SelfKind) -> hir::ExplicitSelf_ {
392-
match *es {
393-
SelfKind::Static => hir::SelfStatic,
394-
SelfKind::Value(v) => hir::SelfValue(v.name),
395-
SelfKind::Region(ref lifetime, m, ident) => {
396-
hir::SelfRegion(self.lower_opt_lifetime(lifetime),
397-
self.lower_mutability(m),
398-
ident.name)
399-
}
400-
SelfKind::Explicit(ref typ, ident) => {
401-
hir::SelfExplicit(self.lower_ty(typ), ident.name)
402-
}
403-
}
404-
}
405-
406391
fn lower_mutability(&mut self, m: Mutability) -> hir::Mutability {
407392
match m {
408393
Mutability::Mutable => hir::MutMutable,
409394
Mutability::Immutable => hir::MutImmutable,
410395
}
411396
}
412397

413-
fn lower_explicit_self(&mut self, s: &ExplicitSelf) -> hir::ExplicitSelf {
414-
Spanned {
415-
node: self.lower_explicit_self_underscore(&s.node),
416-
span: s.span,
417-
}
418-
}
419-
420398
fn lower_arg(&mut self, arg: &Arg) -> hir::Arg {
421399
hir::Arg {
422400
id: arg.id,
@@ -797,10 +775,19 @@ impl<'a> LoweringContext<'a> {
797775
}
798776

799777
fn lower_method_sig(&mut self, sig: &MethodSig) -> hir::MethodSig {
778+
// Check for `self: _` and `self: &_`
779+
if let SelfKind::Explicit(ref ty, _) = sig.explicit_self.node {
780+
match sig.decl.inputs.get(0).and_then(Arg::to_self).map(|eself| eself.node) {
781+
Some(SelfKind::Value(..)) | Some(SelfKind::Region(..)) => {
782+
self.id_assigner.diagnostic().span_err(ty.span,
783+
"the type placeholder `_` is not allowed within types on item signatures");
784+
}
785+
_ => {}
786+
}
787+
}
800788
hir::MethodSig {
801789
generics: self.lower_generics(&sig.generics),
802790
abi: sig.abi,
803-
explicit_self: self.lower_explicit_self(&sig.explicit_self),
804791
unsafety: self.lower_unsafety(sig.unsafety),
805792
constness: self.lower_constness(sig.constness),
806793
decl: self.lower_fn_decl(&sig.decl),

src/librustc/hir/mod.rs

+42-37
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub use self::BinOp_::*;
1515
pub use self::BlockCheckMode::*;
1616
pub use self::CaptureClause::*;
1717
pub use self::Decl_::*;
18-
pub use self::ExplicitSelf_::*;
1918
pub use self::Expr_::*;
2019
pub use self::FunctionRetTy::*;
2120
pub use self::ForeignItem_::*;
@@ -37,12 +36,12 @@ use hir::def::Def;
3736
use hir::def_id::DefId;
3837
use util::nodemap::{NodeMap, FnvHashSet};
3938

40-
use syntax::codemap::{self, Span, Spanned, DUMMY_SP, ExpnId};
39+
use syntax::codemap::{self, mk_sp, respan, Span, Spanned, ExpnId};
4140
use syntax::abi::Abi;
4241
use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, TokenTree, AsmDialect};
4342
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
4443
use syntax::attr::{ThinAttributes, ThinAttributesExt};
45-
use syntax::parse::token::InternedString;
44+
use syntax::parse::token::{keywords, InternedString};
4645
use syntax::ptr::P;
4746

4847
use std::collections::BTreeMap;
@@ -1055,7 +1054,6 @@ pub struct MethodSig {
10551054
pub abi: Abi,
10561055
pub decl: P<FnDecl>,
10571056
pub generics: Generics,
1058-
pub explicit_self: ExplicitSelf,
10591057
}
10601058

10611059
/// Represents an item declaration within a trait declaration,
@@ -1196,25 +1194,41 @@ pub struct Arg {
11961194
pub id: NodeId,
11971195
}
11981196

1197+
/// Alternative representation for `Arg`s describing `self` parameter of methods.
1198+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1199+
pub enum SelfKind {
1200+
/// `self`, `mut self`
1201+
Value(Mutability),
1202+
/// `&'lt self`, `&'lt mut self`
1203+
Region(Option<Lifetime>, Mutability),
1204+
/// `self: TYPE`, `mut self: TYPE`
1205+
Explicit(P<Ty>, Mutability),
1206+
}
1207+
1208+
pub type ExplicitSelf = Spanned<SelfKind>;
1209+
11991210
impl Arg {
1200-
pub fn new_self(span: Span, mutability: Mutability, self_ident: Ident) -> Arg {
1201-
let path = Spanned {
1202-
span: span,
1203-
node: self_ident,
1204-
};
1205-
Arg {
1206-
// HACK(eddyb) fake type for the self argument.
1207-
ty: P(Ty {
1208-
id: DUMMY_NODE_ID,
1209-
node: TyInfer,
1210-
span: DUMMY_SP,
1211-
}),
1212-
pat: P(Pat {
1213-
id: DUMMY_NODE_ID,
1214-
node: PatKind::Ident(BindByValue(mutability), path, None),
1215-
span: span,
1216-
}),
1217-
id: DUMMY_NODE_ID,
1211+
pub fn to_self(&self) -> Option<ExplicitSelf> {
1212+
if let PatKind::Ident(BindByValue(mutbl), ident, _) = self.pat.node {
1213+
if ident.node.unhygienic_name == keywords::SelfValue.name() {
1214+
return match self.ty.node {
1215+
TyInfer => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
1216+
TyRptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyInfer => {
1217+
Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
1218+
}
1219+
_ => Some(respan(mk_sp(self.pat.span.lo, self.ty.span.hi),
1220+
SelfKind::Explicit(self.ty.clone(), mutbl)))
1221+
}
1222+
}
1223+
}
1224+
None
1225+
}
1226+
1227+
pub fn is_self(&self) -> bool {
1228+
if let PatKind::Ident(_, ident, _) = self.pat.node {
1229+
ident.node.unhygienic_name == keywords::SelfValue.name()
1230+
} else {
1231+
false
12181232
}
12191233
}
12201234
}
@@ -1227,6 +1241,12 @@ pub struct FnDecl {
12271241
pub variadic: bool,
12281242
}
12291243

1244+
impl FnDecl {
1245+
pub fn has_self(&self) -> bool {
1246+
self.inputs.get(0).map(Arg::is_self).unwrap_or(false)
1247+
}
1248+
}
1249+
12301250
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
12311251
pub enum Unsafety {
12321252
Unsafe,
@@ -1308,21 +1328,6 @@ impl FunctionRetTy {
13081328
}
13091329
}
13101330

1311-
/// Represents the kind of 'self' associated with a method
1312-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1313-
pub enum ExplicitSelf_ {
1314-
/// No self
1315-
SelfStatic,
1316-
/// `self`
1317-
SelfValue(Name),
1318-
/// `&'lt self`, `&'lt mut self`
1319-
SelfRegion(Option<Lifetime>, Mutability, Name),
1320-
/// `self: TYPE`
1321-
SelfExplicit(P<Ty>, Name),
1322-
}
1323-
1324-
pub type ExplicitSelf = Spanned<ExplicitSelf_>;
1325-
13261331
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
13271332
pub struct Mod {
13281333
/// A span from the first token past `{` to the last token until `}`.

0 commit comments

Comments
 (0)