Skip to content

Commit 1a1de5b

Browse files
committed
Add a new AST-only type variant ImplicitSelf
1 parent 5660a00 commit 1a1de5b

File tree

8 files changed

+34
-40
lines changed

8 files changed

+34
-40
lines changed

src/librustc/hir/lowering.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl<'a> LoweringContext<'a> {
269269
P(hir::Ty {
270270
id: t.id,
271271
node: match t.node {
272-
Infer => hir::TyInfer,
272+
Infer | ImplicitSelf => hir::TyInfer,
273273
Vec(ref ty) => hir::TyVec(self.lower_ty(ty)),
274274
Ptr(ref mt) => hir::TyPtr(self.lower_mt(mt)),
275275
Rptr(ref region, ref mt) => {
@@ -787,23 +787,24 @@ impl<'a> LoweringContext<'a> {
787787
}
788788

789789
fn lower_method_sig(&mut self, sig: &MethodSig) -> hir::MethodSig {
790+
let hir_sig = hir::MethodSig {
791+
generics: self.lower_generics(&sig.generics),
792+
abi: sig.abi,
793+
unsafety: self.lower_unsafety(sig.unsafety),
794+
constness: self.lower_constness(sig.constness),
795+
decl: self.lower_fn_decl(&sig.decl),
796+
};
790797
// Check for `self: _` and `self: &_`
791-
if !sig.self_shortcut {
792-
match sig.decl.get_self().map(|eself| eself.node) {
793-
Some(SelfKind::Value(..)) | Some(SelfKind::Region(..)) => {
798+
if let Some(SelfKind::Explicit(..)) = sig.decl.get_self().map(|eself| eself.node) {
799+
match hir_sig.decl.get_self().map(|eself| eself.node) {
800+
Some(hir::SelfKind::Value(..)) | Some(hir::SelfKind::Region(..)) => {
794801
self.id_assigner.diagnostic().span_err(sig.decl.inputs[0].ty.span,
795802
"the type placeholder `_` is not allowed within types on item signatures");
796803
}
797804
_ => {}
798805
}
799806
}
800-
hir::MethodSig {
801-
generics: self.lower_generics(&sig.generics),
802-
abi: sig.abi,
803-
unsafety: self.lower_unsafety(sig.unsafety),
804-
constness: self.lower_constness(sig.constness),
805-
decl: self.lower_fn_decl(&sig.decl),
806-
}
807+
hir_sig
807808
}
808809

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

src/libsyntax/ast.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1387,8 +1387,6 @@ pub struct MethodSig {
13871387
pub abi: Abi,
13881388
pub decl: P<FnDecl>,
13891389
pub generics: Generics,
1390-
/// A short form of self argument was used (`self`, `&self` etc, but not `self: TYPE`).
1391-
pub self_shortcut: bool,
13921390
}
13931391

13941392
/// Represents an item declaration within a trait declaration,
@@ -1639,6 +1637,8 @@ pub enum TyKind {
16391637
/// TyKind::Infer means the type should be inferred instead of it having been
16401638
/// specified. This can appear anywhere in a type.
16411639
Infer,
1640+
/// Inferred type of a `self` or `&self` argument in a method.
1641+
ImplicitSelf,
16421642
// A macro in the type position.
16431643
Mac(Mac),
16441644
}
@@ -1696,8 +1696,8 @@ impl Arg {
16961696
if let PatKind::Ident(BindingMode::ByValue(mutbl), ident, _) = self.pat.node {
16971697
if ident.node.name == keywords::SelfValue.name() {
16981698
return match self.ty.node {
1699-
TyKind::Infer => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
1700-
TyKind::Rptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyKind::Infer => {
1699+
TyKind::ImplicitSelf => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
1700+
TyKind::Rptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyKind::ImplicitSelf => {
17011701
Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
17021702
}
17031703
_ => Some(respan(mk_sp(self.pat.span.lo, self.ty.span.hi),
@@ -1719,7 +1719,7 @@ impl Arg {
17191719
pub fn from_self(eself: ExplicitSelf, eself_ident: SpannedIdent) -> Arg {
17201720
let infer_ty = P(Ty {
17211721
id: DUMMY_NODE_ID,
1722-
node: TyKind::Infer,
1722+
node: TyKind::ImplicitSelf,
17231723
span: DUMMY_SP,
17241724
});
17251725
let arg = |mutbl, ty, span| Arg {

src/libsyntax/ext/expand.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,6 @@ 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-
self_shortcut: sig.self_shortcut,
11321131
unsafety: sig.unsafety,
11331132
constness: sig.constness,
11341133
decl: rewritten_fn_decl

src/libsyntax/fold.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
375375
t.map(|Ty {id, node, span}| Ty {
376376
id: fld.new_id(id),
377377
node: match node {
378-
TyKind::Infer => node,
378+
TyKind::Infer | TyKind::ImplicitSelf => node,
379379
TyKind::Vec(ty) => TyKind::Vec(fld.fold_ty(ty)),
380380
TyKind::Ptr(mt) => TyKind::Ptr(fld.fold_mt(mt)),
381381
TyKind::Rptr(region, mt) => {
@@ -1066,7 +1066,6 @@ pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> Method
10661066
MethodSig {
10671067
generics: folder.fold_generics(sig.generics),
10681068
abi: sig.abi,
1069-
self_shortcut: sig.self_shortcut,
10701069
unsafety: sig.unsafety,
10711070
constness: sig.constness,
10721071
decl: folder.fold_fn_decl(sig.decl)

src/libsyntax/parse/parser.rs

+12-17
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ impl<'a> Parser<'a> {
13101310
let ident = p.parse_ident()?;
13111311
let mut generics = p.parse_generics()?;
13121312

1313-
let (d, self_shortcut) = p.parse_fn_decl_with_self(|p: &mut Parser<'a>|{
1313+
let d = p.parse_fn_decl_with_self(|p: &mut Parser<'a>|{
13141314
// This is somewhat dubious; We don't want to allow
13151315
// argument names to be left off if there is a
13161316
// definition...
@@ -1324,7 +1324,6 @@ impl<'a> Parser<'a> {
13241324
decl: d,
13251325
generics: generics,
13261326
abi: abi,
1327-
self_shortcut: self_shortcut,
13281327
};
13291328

13301329
let body = match p.token {
@@ -4617,7 +4616,7 @@ impl<'a> Parser<'a> {
46174616
}
46184617

46194618
/// Returns the parsed optional self argument and whether a self shortcut was used.
4620-
fn parse_self_arg(&mut self) -> PResult<'a, (Option<Arg>, bool)> {
4619+
fn parse_self_arg(&mut self) -> PResult<'a, Option<Arg>> {
46214620
let expect_ident = |this: &mut Self| match this.token {
46224621
// Preserve hygienic context.
46234622
token::Ident(ident) => { this.bump(); codemap::respan(this.last_span, ident) }
@@ -4656,7 +4655,7 @@ impl<'a> Parser<'a> {
46564655
self.bump();
46574656
(SelfKind::Region(Some(lt), Mutability::Mutable), expect_ident(self))
46584657
} else {
4659-
return Ok((None, false));
4658+
return Ok(None);
46604659
}
46614660
}
46624661
token::BinOp(token::Star) => {
@@ -4676,7 +4675,7 @@ impl<'a> Parser<'a> {
46764675
self.span_err(self.span, "cannot pass `self` by raw pointer");
46774676
(SelfKind::Value(Mutability::Immutable), expect_ident(self))
46784677
} else {
4679-
return Ok((None, false));
4678+
return Ok(None);
46804679
}
46814680
}
46824681
token::Ident(..) => {
@@ -4703,27 +4702,24 @@ impl<'a> Parser<'a> {
47034702
(SelfKind::Value(Mutability::Mutable), eself_ident)
47044703
}
47054704
} else {
4706-
return Ok((None, false));
4705+
return Ok(None);
47074706
}
47084707
}
4709-
_ => return Ok((None, false)),
4708+
_ => return Ok(None),
47104709
};
47114710

4712-
let self_shortcut = if let SelfKind::Explicit(..) = eself { false } else { true };
47134711
let eself = codemap::respan(mk_sp(eself_lo, self.last_span.hi), eself);
4714-
Ok((Some(Arg::from_self(eself, eself_ident)), self_shortcut))
4712+
Ok(Some(Arg::from_self(eself, eself_ident)))
47154713
}
47164714

47174715
/// Parse the parameter list and result type of a function that may have a `self` parameter.
4718-
fn parse_fn_decl_with_self<F>(&mut self,
4719-
parse_arg_fn: F)
4720-
-> PResult<'a, (P<FnDecl>, bool)>
4716+
fn parse_fn_decl_with_self<F>(&mut self, parse_arg_fn: F) -> PResult<'a, P<FnDecl>>
47214717
where F: FnMut(&mut Parser<'a>) -> PResult<'a, Arg>,
47224718
{
47234719
self.expect(&token::OpenDelim(token::Paren))?;
47244720

47254721
// Parse optional self argument
4726-
let (self_arg, self_shortcut) = self.parse_self_arg()?;
4722+
let self_arg = self.parse_self_arg()?;
47274723

47284724
// Parse the rest of the function parameter list.
47294725
let sep = SeqSep::trailing_allowed(token::Comma);
@@ -4745,11 +4741,11 @@ impl<'a> Parser<'a> {
47454741

47464742
// Parse closing paren and return type.
47474743
self.expect(&token::CloseDelim(token::Paren))?;
4748-
Ok((P(FnDecl {
4744+
Ok(P(FnDecl {
47494745
inputs: fn_inputs,
47504746
output: self.parse_ret_ty()?,
47514747
variadic: false
4752-
}), self_shortcut))
4748+
}))
47534749
}
47544750

47554751
// parse the |arg, arg| header on a lambda
@@ -4942,13 +4938,12 @@ impl<'a> Parser<'a> {
49424938
let (constness, unsafety, abi) = self.parse_fn_front_matter()?;
49434939
let ident = self.parse_ident()?;
49444940
let mut generics = self.parse_generics()?;
4945-
let (decl, self_shortcut) = self.parse_fn_decl_with_self(|p| p.parse_arg())?;
4941+
let decl = self.parse_fn_decl_with_self(|p| p.parse_arg())?;
49464942
generics.where_clause = self.parse_where_clause()?;
49474943
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
49484944
Ok((ident, inner_attrs, ast::ImplItemKind::Method(ast::MethodSig {
49494945
generics: generics,
49504946
abi: abi,
4951-
self_shortcut: self_shortcut,
49524947
unsafety: unsafety,
49534948
constness: constness,
49544949
decl: decl

src/libsyntax/print/pprust.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,9 @@ impl<'a> State<'a> {
10301030
ast::TyKind::Infer => {
10311031
word(&mut self.s, "_")?;
10321032
}
1033+
ast::TyKind::ImplicitSelf => {
1034+
unreachable!();
1035+
}
10331036
ast::TyKind::Mac(ref m) => {
10341037
self.print_mac(m, token::Paren)?;
10351038
}

src/libsyntax/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
348348
TyKind::Typeof(ref expression) => {
349349
visitor.visit_expr(expression)
350350
}
351-
TyKind::Infer => {}
351+
TyKind::Infer | TyKind::ImplicitSelf => {}
352352
TyKind::Mac(ref mac) => {
353353
visitor.visit_mac(mac)
354354
}

src/libsyntax_ext/deriving/generic/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -860,8 +860,6 @@ impl<'a> MethodDef<'a> {
860860
// create the generics that aren't for Self
861861
let fn_generics = self.generics.to_generics(cx, trait_.span, type_ident, generics);
862862

863-
// derive doesn't generate `self: TYPE` forms
864-
let self_shortcut = explicit_self.is_some();
865863
let args = {
866864
let self_args = explicit_self.map(|explicit_self| {
867865
ast::Arg::from_self(explicit_self, respan(trait_.span, keywords::SelfValue.ident()))
@@ -894,7 +892,6 @@ impl<'a> MethodDef<'a> {
894892
node: ast::ImplItemKind::Method(ast::MethodSig {
895893
generics: fn_generics,
896894
abi: abi,
897-
self_shortcut: self_shortcut,
898895
unsafety: unsafety,
899896
constness: ast::Constness::NotConst,
900897
decl: fn_decl

0 commit comments

Comments
 (0)