Skip to content

Commit 0492302

Browse files
committed
syntax refactor parse_self_param (5)
1 parent 4306d00 commit 0492302

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,15 +1197,12 @@ impl<'a> Parser<'a> {
11971197
/// Evaluates the closure with restrictions in place.
11981198
///
11991199
/// Afters the closure is evaluated, restrictions are reset.
1200-
fn with_res<F, T>(&mut self, r: Restrictions, f: F) -> T
1201-
where F: FnOnce(&mut Self) -> T
1202-
{
1200+
fn with_res<T>(&mut self, res: Restrictions, f: impl FnOnce(&mut Self) -> T) -> T {
12031201
let old = self.restrictions;
1204-
self.restrictions = r;
1205-
let r = f(self);
1202+
self.restrictions = res;
1203+
let res = f(self);
12061204
self.restrictions = old;
1207-
return r;
1208-
1205+
res
12091206
}
12101207

12111208
fn parse_fn_params(
@@ -1275,6 +1272,11 @@ impl<'a> Parser<'a> {
12751272
&& self.look_ahead(n + 1, |t| t != &token::ModSep)
12761273
}
12771274

1275+
fn is_isolated_mut_self(&self, n: usize) -> bool {
1276+
self.is_keyword_ahead(n, &[kw::Mut])
1277+
&& self.is_isolated_self(n + 1)
1278+
}
1279+
12781280
fn expect_self_ident(&mut self) -> Ident {
12791281
match self.token.kind {
12801282
// Preserve hygienic context.
@@ -1320,34 +1322,31 @@ impl<'a> Parser<'a> {
13201322
let eself_lo = self.token.span;
13211323
let (eself, eself_ident, eself_hi) = match self.token.kind {
13221324
token::BinOp(token::And) => {
1323-
// `&self`
1324-
// `&mut self`
1325-
// `&'lt self`
1326-
// `&'lt mut self`
1327-
// `&not_self`
1328-
(if self.is_isolated_self(1) {
1325+
let eself = if self.is_isolated_self(1) {
1326+
// `&self`
13291327
self.bump();
13301328
SelfKind::Region(None, Mutability::Immutable)
1331-
} else if self.is_keyword_ahead(1, &[kw::Mut]) &&
1332-
self.is_isolated_self(2) {
1329+
} else if self.is_isolated_mut_self(1) {
1330+
// `&mut self`
13331331
self.bump();
13341332
self.bump();
13351333
SelfKind::Region(None, Mutability::Mutable)
1336-
} else if self.look_ahead(1, |t| t.is_lifetime()) &&
1337-
self.is_isolated_self(2) {
1334+
} else if self.look_ahead(1, |t| t.is_lifetime()) && self.is_isolated_self(2) {
1335+
// `&'lt self`
13381336
self.bump();
13391337
let lt = self.expect_lifetime();
13401338
SelfKind::Region(Some(lt), Mutability::Immutable)
1341-
} else if self.look_ahead(1, |t| t.is_lifetime()) &&
1342-
self.is_keyword_ahead(2, &[kw::Mut]) &&
1343-
self.is_isolated_self(3) {
1339+
} else if self.look_ahead(1, |t| t.is_lifetime()) && self.is_isolated_mut_self(2) {
1340+
// `&'lt mut self`
13441341
self.bump();
13451342
let lt = self.expect_lifetime();
13461343
self.bump();
13471344
SelfKind::Region(Some(lt), Mutability::Mutable)
13481345
} else {
1346+
// `&not_self`
13491347
return Ok(None);
1350-
}, self.expect_self_ident(), self.prev_span)
1348+
};
1349+
(eself, self.expect_self_ident(), self.prev_span)
13511350
}
13521351
// `*self`
13531352
token::BinOp(token::Star) if self.is_isolated_self(1) => {
@@ -1368,7 +1367,7 @@ impl<'a> Parser<'a> {
13681367
self.parse_self_possibly_typed(Mutability::Immutable)?
13691368
}
13701369
// `mut self` and `mut self: TYPE`
1371-
token::Ident(..) if self.token.is_keyword(kw::Mut) && self.is_isolated_self(1) => {
1370+
token::Ident(..) if self.is_isolated_mut_self(0) => {
13721371
self.bump();
13731372
self.parse_self_possibly_typed(Mutability::Mutable)?
13741373
}

0 commit comments

Comments
 (0)