Skip to content

Commit f688f8a

Browse files
committed
syntax refactor parse_self_param (2)
1 parent 40dc9da commit f688f8a

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,17 +1264,22 @@ impl<'a> Parser<'a> {
12641264
&& self.look_ahead(n + 1, |t| t != &token::ModSep)
12651265
}
12661266

1267+
fn expect_self_ident(&mut self) -> Ident {
1268+
match self.token.kind {
1269+
// Preserve hygienic context.
1270+
token::Ident(name, _) => {
1271+
let span = self.token.span;
1272+
self.bump();
1273+
Ident::new(name, span)
1274+
}
1275+
_ => unreachable!(),
1276+
}
1277+
}
1278+
12671279
/// Returns the parsed optional self parameter and whether a self shortcut was used.
12681280
///
12691281
/// See `parse_self_param_with_attrs` to collect attributes.
12701282
fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
1271-
let expect_ident = |this: &mut Self| match this.token.kind {
1272-
// Preserve hygienic context.
1273-
token::Ident(name, _) =>
1274-
{ let span = this.token.span; this.bump(); Ident::new(name, span) }
1275-
_ => unreachable!()
1276-
};
1277-
12781283
// Parse optional `self` parameter of a method.
12791284
// Only a limited set of initial token sequences is considered `self` parameters; anything
12801285
// else is parsed as a normal function parameter list, so some lookahead is required.
@@ -1308,7 +1313,7 @@ impl<'a> Parser<'a> {
13081313
SelfKind::Region(Some(lt), Mutability::Mutable)
13091314
} else {
13101315
return Ok(None);
1311-
}, expect_ident(self), self.prev_span)
1316+
}, self.expect_self_ident(), self.prev_span)
13121317
}
13131318
token::BinOp(token::Star) => {
13141319
// `*self`
@@ -1333,13 +1338,13 @@ impl<'a> Parser<'a> {
13331338
SelfKind::Value(Mutability::Immutable)
13341339
} else {
13351340
return Ok(None);
1336-
}, expect_ident(self), self.prev_span)
1341+
}, self.expect_self_ident(), self.prev_span)
13371342
}
13381343
token::Ident(..) => {
13391344
if self.is_isolated_self(0) {
13401345
// `self`
13411346
// `self: TYPE`
1342-
let eself_ident = expect_ident(self);
1347+
let eself_ident = self.expect_self_ident();
13431348
let eself_hi = self.prev_span;
13441349
(if self.eat(&token::Colon) {
13451350
let ty = self.parse_ty()?;
@@ -1352,7 +1357,7 @@ impl<'a> Parser<'a> {
13521357
// `mut self`
13531358
// `mut self: TYPE`
13541359
self.bump();
1355-
let eself_ident = expect_ident(self);
1360+
let eself_ident = self.expect_self_ident();
13561361
let eself_hi = self.prev_span;
13571362
(if self.eat(&token::Colon) {
13581363
let ty = self.parse_ty()?;

0 commit comments

Comments
 (0)