Skip to content

Commit 7905452

Browse files
committed
Rollup merge of rust-lang#33644 - petrochenkov:selfast, r=nrc
The AST part of rust-lang#33505. rust-lang#33505 isn't landed yet, so this PR is based on top of it. r? @nrc plugin-[breaking-change] cc rust-lang#31645 @Manishearth
2 parents 3578571 + dcea4b2 commit 7905452

File tree

16 files changed

+166
-282
lines changed

16 files changed

+166
-282
lines changed

src/librustc/hir/lowering.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl<'a> LoweringContext<'a> {
273273
P(hir::Ty {
274274
id: t.id,
275275
node: match t.node {
276-
Infer => hir::TyInfer,
276+
Infer | ImplicitSelf => hir::TyInfer,
277277
Vec(ref ty) => hir::TyVec(self.lower_ty(ty)),
278278
Ptr(ref mt) => hir::TyPtr(self.lower_mt(mt)),
279279
Rptr(ref region, ref mt) => {
@@ -791,23 +791,24 @@ impl<'a> LoweringContext<'a> {
791791
}
792792

793793
fn lower_method_sig(&mut self, sig: &MethodSig) -> hir::MethodSig {
794-
// Check for `self: _` and `self: &_`
795-
if let SelfKind::Explicit(ref ty, _) = sig.explicit_self.node {
796-
match sig.decl.inputs.get(0).and_then(Arg::to_self).map(|eself| eself.node) {
797-
Some(SelfKind::Value(..)) | Some(SelfKind::Region(..)) => {
798-
self.id_assigner.diagnostic().span_err(ty.span,
799-
"the type placeholder `_` is not allowed within types on item signatures");
800-
}
801-
_ => {}
802-
}
803-
}
804-
hir::MethodSig {
794+
let hir_sig = hir::MethodSig {
805795
generics: self.lower_generics(&sig.generics),
806796
abi: sig.abi,
807797
unsafety: self.lower_unsafety(sig.unsafety),
808798
constness: self.lower_constness(sig.constness),
809799
decl: self.lower_fn_decl(&sig.decl),
800+
};
801+
// Check for `self: _` and `self: &_`
802+
if let Some(SelfKind::Explicit(..)) = sig.decl.get_self().map(|eself| eself.node) {
803+
match hir_sig.decl.get_self().map(|eself| eself.node) {
804+
Some(hir::SelfKind::Value(..)) | Some(hir::SelfKind::Region(..)) => {
805+
self.id_assigner.diagnostic().span_err(sig.decl.inputs[0].ty.span,
806+
"the type placeholder `_` is not allowed within types on item signatures");
807+
}
808+
_ => {}
809+
}
810810
}
811+
hir_sig
811812
}
812813

813814
fn lower_unsafety(&mut self, u: Unsafety) -> hir::Unsafety {

src/librustc/hir/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,9 @@ pub struct FnDecl {
11771177
}
11781178

11791179
impl FnDecl {
1180+
pub fn get_self(&self) -> Option<ExplicitSelf> {
1181+
self.inputs.get(0).and_then(Arg::to_self)
1182+
}
11801183
pub fn has_self(&self) -> bool {
11811184
self.inputs.get(0).map(Arg::is_self).unwrap_or(false)
11821185
}

src/librustc/lint/context.rs

-5
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

-2
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

+2-2
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_resolve/lib.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use syntax::ast::{Arm, BindingMode, Block, Crate, Expr, ExprKind};
6767
use syntax::ast::{FnDecl, ForeignItem, ForeignItemKind, Generics};
6868
use syntax::ast::{Item, ItemKind, ImplItem, ImplItemKind};
6969
use syntax::ast::{Local, Pat, PatKind, Path};
70-
use syntax::ast::{PathSegment, PathParameters, SelfKind, TraitItemKind, TraitRef, Ty, TyKind};
70+
use syntax::ast::{PathSegment, PathParameters, TraitItemKind, TraitRef, Ty, TyKind};
7171

7272
use std::collections::{HashMap, HashSet};
7373
use std::cell::{Cell, RefCell};
@@ -607,7 +607,7 @@ impl<'a, 'v> Visitor<'v> for Resolver<'a> {
607607
}
608608
FnKind::Method(_, sig, _) => {
609609
self.visit_generics(&sig.generics);
610-
MethodRibKind(sig.explicit_self.node == SelfKind::Static)
610+
MethodRibKind(!sig.decl.has_self())
611611
}
612612
FnKind::Closure => ClosureRibKind(node_id),
613613
};
@@ -1676,9 +1676,7 @@ impl<'a> Resolver<'a> {
16761676
let type_parameters =
16771677
HasTypeParameters(&sig.generics,
16781678
FnSpace,
1679-
MethodRibKind(
1680-
sig.explicit_self.node ==
1681-
SelfKind::Static));
1679+
MethodRibKind(!sig.decl.has_self()));
16821680
this.with_type_parameter_rib(type_parameters, |this| {
16831681
visit::walk_trait_item(this, trait_item)
16841682
});
@@ -2007,9 +2005,7 @@ impl<'a> Resolver<'a> {
20072005
let type_parameters =
20082006
HasTypeParameters(&sig.generics,
20092007
FnSpace,
2010-
MethodRibKind(
2011-
sig.explicit_self.node ==
2012-
SelfKind::Static));
2008+
MethodRibKind(!sig.decl.has_self()));
20132009
this.with_type_parameter_rib(type_parameters, |this| {
20142010
visit::walk_impl_item(this, impl_item);
20152011
});

src/librustc_typeck/astconv.rs

+1-2
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

+41-47
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,6 @@ pub struct MethodSig {
13891389
pub abi: Abi,
13901390
pub decl: P<FnDecl>,
13911391
pub generics: Generics,
1392-
pub explicit_self: ExplicitSelf,
13931392
}
13941393

13951394
/// Represents an item declaration within a trait declaration,
@@ -1640,6 +1639,8 @@ pub enum TyKind {
16401639
/// TyKind::Infer means the type should be inferred instead of it having been
16411640
/// specified. This can appear anywhere in a type.
16421641
Infer,
1642+
/// Inferred type of a `self` or `&self` argument in a method.
1643+
ImplicitSelf,
16431644
// A macro in the type position.
16441645
Mac(Mac),
16451646
}
@@ -1679,81 +1680,65 @@ pub struct Arg {
16791680
pub id: NodeId,
16801681
}
16811682

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

16961694
pub type ExplicitSelf = Spanned<SelfKind>;
16971695

16981696
impl Arg {
1699-
#[unstable(feature = "rustc_private", issue = "27812")]
1700-
#[rustc_deprecated(since = "1.10.0", reason = "use `from_self` instead")]
1701-
pub fn new_self(span: Span, mutability: Mutability, self_ident: Ident) -> Arg {
1702-
let path = Spanned{span:span,node:self_ident};
1703-
Arg {
1704-
// HACK(eddyb) fake type for the self argument.
1705-
ty: P(Ty {
1706-
id: DUMMY_NODE_ID,
1707-
node: TyKind::Infer,
1708-
span: DUMMY_SP,
1709-
}),
1710-
pat: P(Pat {
1711-
id: DUMMY_NODE_ID,
1712-
node: PatKind::Ident(BindingMode::ByValue(mutability), path, None),
1713-
span: span
1714-
}),
1715-
id: DUMMY_NODE_ID
1716-
}
1717-
}
1718-
17191697
pub fn to_self(&self) -> Option<ExplicitSelf> {
1720-
if let PatKind::Ident(_, ident, _) = self.pat.node {
1698+
if let PatKind::Ident(BindingMode::ByValue(mutbl), ident, _) = self.pat.node {
17211699
if ident.node.name == keywords::SelfValue.name() {
17221700
return match self.ty.node {
1723-
TyKind::Infer => Some(respan(self.pat.span, SelfKind::Value(ident.node))),
1724-
TyKind::Rptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyKind::Infer => {
1725-
Some(respan(self.pat.span, SelfKind::Region(lt, mutbl, ident.node)))
1701+
TyKind::ImplicitSelf => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
1702+
TyKind::Rptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyKind::ImplicitSelf => {
1703+
Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
17261704
}
17271705
_ => Some(respan(mk_sp(self.pat.span.lo, self.ty.span.hi),
1728-
SelfKind::Explicit(self.ty.clone(), ident.node))),
1706+
SelfKind::Explicit(self.ty.clone(), mutbl))),
17291707
}
17301708
}
17311709
}
17321710
None
17331711
}
17341712

1735-
pub fn from_self(eself: ExplicitSelf, ident_sp: Span, mutbl: Mutability) -> Arg {
1736-
let pat = |ident, span| P(Pat {
1737-
id: DUMMY_NODE_ID,
1738-
node: PatKind::Ident(BindingMode::ByValue(mutbl), respan(ident_sp, ident), None),
1739-
span: span,
1740-
});
1713+
pub fn is_self(&self) -> bool {
1714+
if let PatKind::Ident(_, ident, _) = self.pat.node {
1715+
ident.node.name == keywords::SelfValue.name()
1716+
} else {
1717+
false
1718+
}
1719+
}
1720+
1721+
pub fn from_self(eself: ExplicitSelf, eself_ident: SpannedIdent) -> Arg {
17411722
let infer_ty = P(Ty {
17421723
id: DUMMY_NODE_ID,
1743-
node: TyKind::Infer,
1724+
node: TyKind::ImplicitSelf,
17441725
span: DUMMY_SP,
17451726
});
1746-
let arg = |ident, ty, span| Arg {
1747-
pat: pat(ident, span),
1727+
let arg = |mutbl, ty, span| Arg {
1728+
pat: P(Pat {
1729+
id: DUMMY_NODE_ID,
1730+
node: PatKind::Ident(BindingMode::ByValue(mutbl), eself_ident, None),
1731+
span: span,
1732+
}),
17481733
ty: ty,
17491734
id: DUMMY_NODE_ID,
17501735
};
17511736
match eself.node {
1752-
SelfKind::Static => panic!("bug: `Arg::from_self` is called \
1753-
with `SelfKind::Static` argument"),
1754-
SelfKind::Explicit(ty, ident) => arg(ident, ty, mk_sp(eself.span.lo, ident_sp.hi)),
1755-
SelfKind::Value(ident) => arg(ident, infer_ty, eself.span),
1756-
SelfKind::Region(lt, mutbl, ident) => arg(ident, P(Ty {
1737+
SelfKind::Explicit(ty, mutbl) => {
1738+
arg(mutbl, ty, mk_sp(eself.span.lo, eself_ident.span.hi))
1739+
}
1740+
SelfKind::Value(mutbl) => arg(mutbl, infer_ty, eself.span),
1741+
SelfKind::Region(lt, mutbl) => arg(Mutability::Immutable, P(Ty {
17571742
id: DUMMY_NODE_ID,
17581743
node: TyKind::Rptr(lt, MutTy { ty: infer_ty, mutbl: mutbl }),
17591744
span: DUMMY_SP,
@@ -1770,6 +1755,15 @@ pub struct FnDecl {
17701755
pub variadic: bool
17711756
}
17721757

1758+
impl FnDecl {
1759+
pub fn get_self(&self) -> Option<ExplicitSelf> {
1760+
self.inputs.get(0).and_then(Arg::to_self)
1761+
}
1762+
pub fn has_self(&self) -> bool {
1763+
self.inputs.get(0).map(Arg::is_self).unwrap_or(false)
1764+
}
1765+
}
1766+
17731767
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
17741768
pub enum Unsafety {
17751769
Unsafe,

src/libsyntax/ext/expand.rs

-1
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,6 @@ fn expand_and_rename_method(sig: ast::MethodSig, body: P<ast::Block>,
950950
(ast::MethodSig {
951951
generics: fld.fold_generics(sig.generics),
952952
abi: sig.abi,
953-
explicit_self: fld.fold_explicit_self(sig.explicit_self),
954953
unsafety: sig.unsafety,
955954
constness: sig.constness,
956955
decl: rewritten_fn_decl

src/libsyntax/fold.rs

+1-32
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
}
@@ -383,7 +375,7 @@ pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
383375
t.map(|Ty {id, node, span}| Ty {
384376
id: fld.new_id(id),
385377
node: match node {
386-
TyKind::Infer => node,
378+
TyKind::Infer | TyKind::ImplicitSelf => node,
387379
TyKind::Vec(ty) => TyKind::Vec(fld.fold_ty(ty)),
388380
TyKind::Ptr(mt) => TyKind::Ptr(fld.fold_mt(mt)),
389381
TyKind::Rptr(region, mt) => {
@@ -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,6 @@ 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),
11001069
unsafety: sig.unsafety,
11011070
constness: sig.constness,
11021071
decl: folder.fold_fn_decl(sig.decl)

0 commit comments

Comments
 (0)