Tweak path parsing logic
This commit is contained in:
parent
65ff4ca294
commit
fea630ef9d
10 changed files with 93 additions and 36 deletions
|
@ -3620,7 +3620,7 @@ impl<'a> Parser<'a> {
|
|||
// Parse box pat
|
||||
let subpat = self.parse_pat()?;
|
||||
pat = PatKind::Box(subpat);
|
||||
} else if self.token.is_ident() && self.token.is_path_start() &&
|
||||
} else if self.token.is_ident() && !self.token.is_any_keyword() &&
|
||||
self.look_ahead(1, |t| match *t {
|
||||
token::OpenDelim(token::Paren) | token::OpenDelim(token::Brace) |
|
||||
token::DotDotDot | token::ModSep | token::Not => false,
|
||||
|
@ -3871,6 +3871,11 @@ impl<'a> Parser<'a> {
|
|||
})
|
||||
}
|
||||
|
||||
fn is_union_item(&mut self) -> bool {
|
||||
self.token.is_keyword(keywords::Union) &&
|
||||
self.look_ahead(1, |t| t.is_ident() && !t.is_any_keyword())
|
||||
}
|
||||
|
||||
fn parse_stmt_without_recovery(&mut self,
|
||||
macro_legacy_warnings: bool)
|
||||
-> PResult<'a, Option<Stmt>> {
|
||||
|
@ -3885,10 +3890,10 @@ impl<'a> Parser<'a> {
|
|||
node: StmtKind::Local(self.parse_local(attrs.into())?),
|
||||
span: mk_sp(lo, self.prev_span.hi),
|
||||
}
|
||||
} else if self.token.is_path_start() && self.token != token::Lt && {
|
||||
!self.check_keyword(keywords::Union) ||
|
||||
self.look_ahead(1, |t| *t == token::Not || *t == token::ModSep)
|
||||
} {
|
||||
// Starts like a simple path, but not a union item.
|
||||
} else if self.token.is_path_start() &&
|
||||
!self.token.is_qpath_start() &&
|
||||
!self.is_union_item() {
|
||||
let pth = self.parse_path(PathStyle::Expr)?;
|
||||
|
||||
if !self.eat(&token::Not) {
|
||||
|
@ -4599,6 +4604,10 @@ impl<'a> Parser<'a> {
|
|||
token::Ident(ident) => { this.bump(); codemap::respan(this.prev_span, ident) }
|
||||
_ => unreachable!()
|
||||
};
|
||||
let isolated_self = |this: &mut Self, n| {
|
||||
this.look_ahead(n, |t| t.is_keyword(keywords::SelfValue)) &&
|
||||
this.look_ahead(n + 1, |t| t != &token::ModSep)
|
||||
};
|
||||
|
||||
// Parse optional self parameter of a method.
|
||||
// Only a limited set of initial token sequences is considered self parameters, anything
|
||||
|
@ -4611,22 +4620,22 @@ impl<'a> Parser<'a> {
|
|||
// &'lt self
|
||||
// &'lt mut self
|
||||
// ¬_self
|
||||
if self.look_ahead(1, |t| t.is_keyword(keywords::SelfValue)) {
|
||||
if isolated_self(self, 1) {
|
||||
self.bump();
|
||||
(SelfKind::Region(None, Mutability::Immutable), expect_ident(self))
|
||||
} else if self.look_ahead(1, |t| t.is_keyword(keywords::Mut)) &&
|
||||
self.look_ahead(2, |t| t.is_keyword(keywords::SelfValue)) {
|
||||
isolated_self(self, 2) {
|
||||
self.bump();
|
||||
self.bump();
|
||||
(SelfKind::Region(None, Mutability::Mutable), expect_ident(self))
|
||||
} else if self.look_ahead(1, |t| t.is_lifetime()) &&
|
||||
self.look_ahead(2, |t| t.is_keyword(keywords::SelfValue)) {
|
||||
isolated_self(self, 2) {
|
||||
self.bump();
|
||||
let lt = self.parse_lifetime()?;
|
||||
(SelfKind::Region(Some(lt), Mutability::Immutable), expect_ident(self))
|
||||
} else if self.look_ahead(1, |t| t.is_lifetime()) &&
|
||||
self.look_ahead(2, |t| t.is_keyword(keywords::Mut)) &&
|
||||
self.look_ahead(3, |t| t.is_keyword(keywords::SelfValue)) {
|
||||
isolated_self(self, 3) {
|
||||
self.bump();
|
||||
let lt = self.parse_lifetime()?;
|
||||
self.bump();
|
||||
|
@ -4641,12 +4650,12 @@ impl<'a> Parser<'a> {
|
|||
// *mut self
|
||||
// *not_self
|
||||
// Emit special error for `self` cases.
|
||||
if self.look_ahead(1, |t| t.is_keyword(keywords::SelfValue)) {
|
||||
if isolated_self(self, 1) {
|
||||
self.bump();
|
||||
self.span_err(self.span, "cannot pass `self` by raw pointer");
|
||||
(SelfKind::Value(Mutability::Immutable), expect_ident(self))
|
||||
} else if self.look_ahead(1, |t| t.is_mutability()) &&
|
||||
self.look_ahead(2, |t| t.is_keyword(keywords::SelfValue)) {
|
||||
isolated_self(self, 2) {
|
||||
self.bump();
|
||||
self.bump();
|
||||
self.span_err(self.span, "cannot pass `self` by raw pointer");
|
||||
|
@ -4656,7 +4665,7 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
}
|
||||
token::Ident(..) => {
|
||||
if self.token.is_keyword(keywords::SelfValue) {
|
||||
if isolated_self(self, 0) {
|
||||
// self
|
||||
// self: TYPE
|
||||
let eself_ident = expect_ident(self);
|
||||
|
@ -4667,7 +4676,7 @@ impl<'a> Parser<'a> {
|
|||
(SelfKind::Value(Mutability::Immutable), eself_ident)
|
||||
}
|
||||
} else if self.token.is_keyword(keywords::Mut) &&
|
||||
self.look_ahead(1, |t| t.is_keyword(keywords::SelfValue)) {
|
||||
isolated_self(self, 1) {
|
||||
// mut self
|
||||
// mut self: TYPE
|
||||
self.bump();
|
||||
|
@ -5958,8 +5967,7 @@ impl<'a> Parser<'a> {
|
|||
maybe_append(attrs, extra_attrs));
|
||||
return Ok(Some(item));
|
||||
}
|
||||
if self.check_keyword(keywords::Union) &&
|
||||
self.look_ahead(1, |t| t.is_ident() && !t.is_any_keyword()) {
|
||||
if self.is_union_item() {
|
||||
// UNION ITEM
|
||||
self.bump();
|
||||
let (ident, item_, extra_attrs) = self.parse_item_union()?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue