8000 syntax: cleanup param, method, and misc parsing by Centril · Pull Request #64910 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

syntax: cleanup param, method, and misc parsing #64910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
syntax refactor parse_self_param (4)
  • Loading branch information
Centril committed Sep 30, 2019
commit 4306d0037e961825abc08bfa39af0b64d1ed52aa
59 changes: 35 additions & 24 deletions src/libsyntax/parse/parser.rs
8000
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,17 @@ impl<'a> Parser<'a> {
}
}

/// Possibly parses mutability (`const` or `mut`).
fn parse_const_or_mut(&mut self) -> Option<Mutability> {
if self.eat_keyword(kw::Mut) {
Some(Mutability::Mutable)
} else if self.eat_keyword(kw::Const) {
Some(Mutability::Immutable)
} else {
None
}
}

fn parse_field_name(&mut self) -> PResult<'a, Ident> {
if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) =
self.token.kind {
Expand Down Expand Up @@ -1276,6 +1287,17 @@ impl<'a> Parser<'a> {
}
}

/// Recover for the grammar `*self`, `*const self`, and `*mut self`.
fn recover_self_ptr(&mut self) -> PResult<'a, (ast::SelfKind, Ident, Span)> {
let msg = "cannot pass `self` by raw pointer";
let span = self.token.span;
self.struct_span_err(span, msg)
.span_label(span, msg)
.emit();

Ok((SelfKind::Value(Mutability::Immutable), self.expect_self_ident(), self.prev_span))
}

/// Parse `self` or `self: TYPE`. We already know the current token is `self`.
fn parse_self_possibly_typed(&mut self, m: Mutability) -> PResult<'a, (SelfKind, Ident, Span)> {
let eself_ident = self.expect_self_ident();
Expand Down Expand Up @@ -1327,30 +1349,19 @@ impl<'a> Parser<'a> {
return Ok(None);
}, self.expect_self_ident(), self.prev_span)
}
token::BinOp(token::Star) => {
// `*self`
// `*const self`
// `*mut self`
// `*not_self`
// Emit special error for `self` ca 1048B ses.
let msg = "cannot pass `self` by raw pointer";
(if self.is_isolated_self(1) {
self.bump();
self.struct_span_err(self.token.span, msg)
.span_label(self.token.span, msg)
.emit();
SelfKind::Value(Mutability::Immutable)
} else if self.look_ahead(1, |t| t.is_mutability()) &&
self.is_isolated_self(2) {
self.bump();
self.bump();
self.struct_span_err(self.token.span, msg)
.span_label(self.token.span, msg)
.emit();
SelfKind::Value(Mutability::Immutable)
} else {
return Ok(None);
}, self.expect_self_ident(), self.prev_span)
// `*self`
token::BinOp(token::Star) if self.is_isolated_self(1) => {
self.bump();
self.recover_self_ptr()?
}
// `*mut self` and `*const self`
token::BinOp(token::Star) if
self.look_ahead(1, |t| t.is_mutability())
&& self.is_isolated_self(2) =>
{
self.bump();
self.bump();
self.recover_self_ptr()?
}
// `self` and `self: TYPE`
token::Ident(..) if self.is_isolated_self(0) => {
Expand Down
8 changes: 2 additions & 6 deletions src/libsyntax/parse/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,15 @@ impl<'a> Parser<'a> {
}

fn parse_ptr(&mut self) -> PResult<'a, MutTy> {
let mutbl = if self.eat_keyword(kw::Mut) {
Mutability::Mutable
} else if self.eat_keyword(kw::Const) {
Mutability::Immutable
} else {
let mutbl = self.parse_const_or_mut().unwrap_or_else(|| {
let span = self.prev_span;
let msg = "expected mut or const in raw pointer type";
self.struct_span_err(span, msg)
.span_label(span, msg)
.help("use `*mut T` or `*const T` as appropriate")
.emit();
Mutability::Immutable
};
});
let t = self.parse_ty_no_plus()?;
Ok(MutTy { ty: t, mutbl })
}
Expand Down
0