Simplify parse_self_param

This commit is contained in:
Frank King 2025-03-05 22:13:22 +08:00
parent cb7d687e96
commit 52cd8746ba

View file

@ -2954,6 +2954,8 @@ impl<'a> Parser<'a> {
}
_ => unreachable!(),
};
// is lifetime `n` tokens ahead?
let is_lifetime = |this: &Self, n| this.look_ahead(n, |t| t.is_lifetime());
// Is `self` `n` tokens ahead?
let is_isolated_self = |this: &Self, n| {
this.is_keyword_ahead(n, &[kw::SelfLower])
@ -3023,60 +3025,35 @@ impl<'a> Parser<'a> {
let eself_lo = self.token.span;
let (eself, eself_ident, eself_hi) = match self.token.uninterpolate().kind {
token::And => {
let eself = if is_isolated_self(self, 1) {
// `&self`
self.bump();
SelfKind::Region(None, Mutability::Not)
} else if is_isolated_mut_self(self, 1) {
// `&mut self`
self.bump();
self.bump();
SelfKind::Region(None, Mutability::Mut)
} else if is_isolated_pin_const_self(self, 1) {
// `&pin const self`
let has_lifetime = is_lifetime(self, 1);
let skip_lifetime_count = has_lifetime as usize;
let eself = if is_isolated_self(self, skip_lifetime_count + 1) {
// `&{'lt} self`
self.bump(); // &
let lifetime = has_lifetime.then(|| self.expect_lifetime());
SelfKind::Region(lifetime, Mutability::Not)
} else if is_isolated_mut_self(self, skip_lifetime_count + 1) {
// `&{'lt} mut self`
self.bump(); // &
let lifetime = has_lifetime.then(|| self.expect_lifetime());
self.bump(); // mut
SelfKind::Region(lifetime, Mutability::Mut)
} else if is_isolated_pin_const_self(self, skip_lifetime_count + 1) {
// `&{'lt} pin const self`
self.bump(); // &
let lifetime = has_lifetime.then(|| self.expect_lifetime());
self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
self.bump(); // pin
self.bump(); // const
SelfKind::Pinned(None, Mutability::Not)
} else if is_isolated_pin_mut_self(self, 1) {
// `&pin mut self`
SelfKind::Pinned(lifetime, Mutability::Not)
} else if is_isolated_pin_mut_self(self, skip_lifetime_count + 1) {
// `&{'lt} pin mut self`
self.bump(); // &
let lifetime = has_lifetime.then(|| self.expect_lifetime());
self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
self.bump(); // pin
self.bump(); // mut
SelfKind::Pinned(None, Mutability::Mut)
} else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_self(self, 2) {
// `&'lt self`
self.bump();
let lt = self.expect_lifetime();
SelfKind::Region(Some(lt), Mutability::Not)
} else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_mut_self(self, 2) {
// `&'lt mut self`
self.bump();
let lt = self.expect_lifetime();
self.bump();
SelfKind::Region(Some(lt), Mutability::Mut)
} else if self.look_ahead(1, |t| t.is_lifetime())
&& is_isolated_pin_const_self(self, 2)
{
// `&'lt pin const self`
self.bump(); // &
let lt = self.expect_lifetime();
self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
self.bump(); // pin
self.bump(); // const
SelfKind::Pinned(Some(lt), Mutability::Not)
} else if self.look_ahead(1, |t| t.is_lifetime())
&& is_isolated_pin_mut_self(self, 2)
{
// `&'lt pin mut self`
self.bump(); // &
let lt = self.expect_lifetime();
self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
self.bump(); // pin
self.bump(); // mut
SelfKind::Pinned(Some(lt), Mutability::Mut)
SelfKind::Pinned(lifetime, Mutability::Mut)
} else {
// `&not_self`
return Ok(None);