diff options
| author | Frank King <frankking1729@gmail.com> | 2025-03-05 22:13:22 +0800 |
|---|---|---|
| committer | Frank King <frankking1729@gmail.com> | 2025-03-05 22:50:27 +0800 |
| commit | 52cd8746bab900aa38210d24c62352170b9f51f8 (patch) | |
| tree | 68ea946752c2ab91bfbad3e9df1306fde1c41007 /compiler/rustc_parse/src | |
| parent | cb7d687e96151adc81c55fc48734497a2b48e79c (diff) | |
| download | rust-52cd8746bab900aa38210d24c62352170b9f51f8.tar.gz rust-52cd8746bab900aa38210d24c62352170b9f51f8.zip | |
Simplify `parse_self_param`
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/parser/item.rs | 63 |
1 files changed, 20 insertions, 43 deletions
diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 68a16a1c66b..c3b1956ad2e 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -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(); // & - 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` + 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(); // & - self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span); - self.bump(); // pin + let lifetime = has_lifetime.then(|| self.expect_lifetime()); 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` + SelfKind::Region(lifetime, Mutability::Mut) + } else if is_isolated_pin_const_self(self, skip_lifetime_count + 1) { + // `&{'lt} pin const self` self.bump(); // & - let lt = self.expect_lifetime(); + 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(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` + SelfKind::Pinned(lifetime, Mutability::Not) + } else if is_isolated_pin_mut_self(self, skip_lifetime_count + 1) { + // `&{'lt} pin mut self` self.bump(); // & - let lt = self.expect_lifetime(); + 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(Some(lt), Mutability::Mut) + SelfKind::Pinned(lifetime, Mutability::Mut) } else { // `¬_self` return Ok(None); |
