Skip to content

Commit

Permalink
parser: misc small item related improvements & cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Feb 13, 2020
1 parent 46d3ef5 commit aaaf0ba
Show file tree
Hide file tree
Showing 36 changed files with 233 additions and 249 deletions.
258 changes: 120 additions & 138 deletions src/librustc_parse/parser/item.rs

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/librustc_parse/parser/mod.rs
Expand Up @@ -572,6 +572,11 @@ impl<'a> Parser<'a> {
if !self.eat_keyword(kw) { self.unexpected() } else { Ok(()) }
}

/// Is the given keyword `kw` followed by a non-reserved identifier?
fn is_kw_followed_by_ident(&self, kw: Symbol) -> bool {
self.token.is_keyword(kw) && self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
}

fn check_or_expected(&mut self, ok: bool, typ: TokenType) -> bool {
if ok {
true
Expand Down
18 changes: 6 additions & 12 deletions src/librustc_parse/parser/stmt.rs
Expand Up @@ -8,7 +8,7 @@ use crate::DirectoryOwnership;

use rustc_errors::{Applicability, PResult};
use rustc_span::source_map::{BytePos, Span};
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::symbol::{kw, sym};
use syntax::ast;
use syntax::ast::{AttrStyle, AttrVec, Attribute, Mac, MacStmtStyle};
use syntax::ast::{Block, BlockCheckMode, Expr, ExprKind, Local, Stmt, StmtKind, DUMMY_NODE_ID};
Expand Down Expand Up @@ -55,13 +55,11 @@ impl<'a> Parser<'a> {
return self.recover_stmt_local(lo, attrs.into(), msg, "let");
}

// Starts like a simple path, being careful to avoid contextual keywords
// such as a union items, item with `crate` visibility or auto trait items.
// Our goal here is to parse an arbitrary path `a::b::c` but not something that starts
// like a path (1 token), but it fact not a path.
if self.token.is_path_start()
&& !self.token.is_qpath_start()
&& !self.is_path_start_item() // Confirm we don't steal syntax from `parse_item_`.
// Starts like a simple path, being careful to avoid contextual keywords,
// e.g., `union`, items with `crate` visibility, or `auto trait` items.
// We aim to parse an arbitrary path `a::b` but not something that starts like a path
// (1 token), but it fact not a path. Also, we avoid stealing syntax from `parse_item_`.
if self.token.is_path_start() && !self.token.is_qpath_start() && !self.is_path_start_item()
{
let path = self.parse_path(PathStyle::Expr)?;

Expand Down Expand Up @@ -191,10 +189,6 @@ impl<'a> Parser<'a> {
}
}

fn is_kw_followed_by_ident(&self, kw: Symbol) -> bool {
self.token.is_keyword(kw) && self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
}

fn recover_stmt_local(
&mut self,
lo: Span,
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-58856-2.stderr
Expand Up @@ -7,11 +7,11 @@ LL | fn how_are_you(&self -> Empty {
| | help: `)` may belong here
| unclosed delimiter

error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `)`
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, `}`, or identifier, found `)`
--> $DIR/issue-58856-2.rs:11:1
|
LL | }
| - expected one of 10 possible tokens
| - expected one of 11 possible tokens
LL | }
| ^ unexpected token

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-60075.stderr
Expand Up @@ -4,7 +4,7 @@ error: expected one of `.`, `;`, `?`, `else`, or an operator, found `}`
LL | });
| ^ expected one of `.`, `;`, `?`, `else`, or an operator

error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `;`
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, `}`, or identifier, found `;`
--> $DIR/issue-60075.rs:6:11
|
LL | fn qux() -> Option<usize> {
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/macros/issue-54441.rs
@@ -1,6 +1,7 @@
macro_rules! m {
//~^ ERROR missing `fn`, `type`, or `static` for extern-item declaration
() => {
let //~ ERROR expected
let
};
}

Expand Down
16 changes: 7 additions & 9 deletions src/test/ui/macros/issue-54441.stderr
@@ -1,13 +1,11 @@
error: expected one of `async`, `const`, `crate`, `extern`, `fn`, `pub`, `static`, `type`, or `unsafe`, found keyword `let`
--> $DIR/issue-54441.rs:3:9
error: missing `fn`, `type`, or `static` for extern-item declaration
--> $DIR/issue-54441.rs:1:1
|
LL | let
| ^^^ expected one of 9 possible tokens
...
LL | m!();
| ----- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
LL | / macro_rules! m {
LL | |
LL | | () => {
LL | | let
| |________^ missing `fn`, `type`, or `static`

error: aborting due to previous error

4 changes: 2 additions & 2 deletions src/test/ui/parser/attr-before-eof.stderr
@@ -1,8 +1,8 @@
error: expected item after attributes
--> $DIR/attr-before-eof.rs:3:16
--> $DIR/attr-before-eof.rs:3:1
|
LL | #[derive(Debug)]
| ^
| ^^^^^^^^^^^^^^^^

error: aborting due to previous error

4 changes: 2 additions & 2 deletions src/test/ui/parser/attr-dangling-in-mod.stderr
@@ -1,8 +1,8 @@
error: expected item after attributes
--> $DIR/attr-dangling-in-mod.rs:6:14
--> $DIR/attr-dangling-in-mod.rs:6:1
|
LL | #[foo = "bar"]
| ^
| ^^^^^^^^^^^^^^

error: aborting due to previous error

4 changes: 2 additions & 2 deletions src/test/ui/parser/attrs-after-extern-mod.stderr
@@ -1,8 +1,8 @@
error: expected item after attributes
--> $DIR/attrs-after-extern-mod.rs:6:19
--> $DIR/attrs-after-extern-mod.rs:6:5
|
LL | #[cfg(stage37)]
| ^
| ^^^^^^^^^^^^^^^

error: aborting due to previous error

3 changes: 2 additions & 1 deletion src/test/ui/parser/default.rs
Expand Up @@ -19,7 +19,8 @@ impl Foo for u16 {
}

impl Foo for u32 { //~ ERROR not all trait items implemented, missing: `foo`
default pub fn foo<T: Default>() -> T { T::default() } //~ ERROR expected one of
default pub fn foo<T: Default>() -> T { T::default() }
//~^ ERROR missing `fn`, `type`, or `const` for associated-item declaration
}

fn main() {}
6 changes: 3 additions & 3 deletions src/test/ui/parser/default.stderr
@@ -1,8 +1,8 @@
error: expected one of `async`, `const`, `extern`, `fn`, `type`, or `unsafe`, found keyword `pub`
--> $DIR/default.rs:22:13
error: missing `fn`, `type`, or `const` for associated-item declaration
--> $DIR/default.rs:22:12
|
LL | default pub fn foo<T: Default>() -> T { T::default() }
| ^^^ expected one of `async`, `const`, `extern`, `fn`, `type`, or `unsafe`
| ^ missing `fn`, `type`, or `const`

error[E0449]: unnecessary visibility qualifier
--> $DIR/default.rs:16:5
Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/parser/doc-before-attr.stderr
@@ -1,8 +1,10 @@
error: expected item after attributes
--> $DIR/doc-before-attr.rs:4:16
--> $DIR/doc-before-attr.rs:4:1
|
LL | /// hi
| ------ other attributes here
LL | #[derive(Debug)]
| ^
| ^^^^^^^^^^^^^^^^

error: aborting due to previous error

3 changes: 1 addition & 2 deletions src/test/ui/parser/duplicate-visibility.rs
@@ -1,7 +1,6 @@
// error-pattern: expected one of `(`, `async`, `const`, `extern`, `fn`

fn main() {}

extern {
pub pub fn foo();
//~^ ERROR missing `fn`, `type`, or `static` for extern-item declaration
}
6 changes: 3 additions & 3 deletions src/test/ui/parser/duplicate-visibility.stderr
@@ -1,8 +1,8 @@
error: expected one of `(`, `async`, `const`, `extern`, `fn`, `static`, `type`, or `unsafe`, found keyword `pub`
--> $DIR/duplicate-visibility.rs:6:9
error: missing `fn`, `type`, or `static` for extern-item declaration
--> $DIR/duplicate-visibility.rs:4:8
|
LL | pub pub fn foo();
| ^^^ expected one of 8 possible tokens
| ^ missing `fn`, `type`, or `static`

error: aborting due to previous error

3 changes: 2 additions & 1 deletion src/test/ui/parser/issue-19398.rs
@@ -1,5 +1,6 @@
trait T {
extern "Rust" unsafe fn foo(); //~ ERROR expected one of `async`, `const`
//~^ ERROR missing `fn`, `type`, or `const` for associated-item declaration
extern "Rust" unsafe fn foo();
}

fn main() {}
13 changes: 7 additions & 6 deletions src/test/ui/parser/issue-19398.stderr
@@ -1,10 +1,11 @@
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found keyword `extern`
--> $DIR/issue-19398.rs:2:5
error: missing `fn`, `type`, or `const` for associated-item declaration
--> $DIR/issue-19398.rs:1:10
|
LL | trait T {
| - expected one of 10 possible tokens
LL | extern "Rust" unsafe fn foo();
| ^^^^^^ unexpected token
LL | trait T {
| __________^
LL | |
LL | | extern "Rust" unsafe fn foo();
| |____^ missing `fn`, `type`, or `const`

error: aborting due to previous error

3 changes: 2 additions & 1 deletion src/test/ui/parser/issue-20711-2.rs
Expand Up @@ -4,6 +4,7 @@ impl Foo {
fn foo() {}

#[stable(feature = "rust1", since = "1.0.0")]
} //~ ERROR expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, or
//~^ ERROR expected item after attributes
}

fn main() {}
8 changes: 3 additions & 5 deletions src/test/ui/parser/issue-20711-2.stderr
@@ -1,10 +1,8 @@
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, or `unsafe`, found `}`
--> $DIR/issue-20711-2.rs:7:1
error: expected item after attributes
--> $DIR/issue-20711-2.rs:6:5
|
LL | #[stable(feature = "rust1", since = "1.0.0")]
| - expected one of 9 possible tokens
LL | }
| ^ unexpected token
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

3 changes: 2 additions & 1 deletion src/test/ui/parser/issue-20711.rs
Expand Up @@ -2,6 +2,7 @@ struct Foo;

impl Foo {
#[stable(feature = "rust1", since = "1.0.0")]
} //~ ERROR expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, or
//~^ ERROR expected item after attributes
}

fn main() {}
8 changes: 3 additions & 5 deletions src/test/ui/parser/issue-20711.stderr
@@ -1,10 +1,8 @@
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, or `unsafe`, found `}`
--> $DIR/issue-20711.rs:5:1
error: expected item after attributes
--> $DIR/issue-20711.rs:4:5
|
LL | #[stable(feature = "rust1", since = "1.0.0")]
| - expected one of 9 possible tokens
LL | }
| ^ unexpected token
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

4 changes: 2 additions & 2 deletions src/test/ui/parser/issue-32446.stderr
@@ -1,8 +1,8 @@
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `...`
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, `}`, or identifier, found `...`
--> $DIR/issue-32446.rs:4:11
|
LL | trait T { ... }
| ^^^ expected one of 10 possible tokens
| ^^^ expected one of 11 possible tokens

error: aborting due to previous error

4 changes: 2 additions & 2 deletions src/test/ui/parser/issue-41155.stderr
@@ -1,8 +1,8 @@
error: expected one of `(`, `async`, `const`, `default`, `extern`, `fn`, `type`, or `unsafe`, found `}`
error: expected one of `(`, `async`, `const`, `default`, `extern`, `fn`, `type`, `unsafe`, or identifier, found `}`
--> $DIR/issue-41155.rs:5:1
|
LL | pub
| - expected one of 8 possible tokens
| - expected one of 9 possible tokens
LL | }
| ^ unexpected token

Expand Down
Expand Up @@ -7,10 +7,10 @@ LL | #[Ѕ
| unclosed delimiter

error: expected item after attributes
--> $DIR/issue-58094-missing-right-square-bracket.rs:4:4
--> $DIR/issue-58094-missing-right-square-bracket.rs:4:1
|
LL | #[Ѕ
| ^
| ^^^

error: aborting due to 2 previous errors

11 changes: 7 additions & 4 deletions src/test/ui/parser/macro/pub-item-macro.rs
@@ -1,12 +1,15 @@
// Issue #14660

macro_rules! priv_x { () => {
static x: u32 = 0;
}}
macro_rules! priv_x {
() => {
static x: u32 = 0;
};
}

macro_rules! pub_x { () => {
pub priv_x!(); //~ ERROR can't qualify macro invocation with `pub`
//~^ HELP try adjusting the macro to put `pub` inside the invocation
//~^ HELP remove the visibility
//~| HELP try adjusting the macro to put `pub` inside the invocation
}}

mod foo {
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/parser/macro/pub-item-macro.stderr
@@ -1,8 +1,8 @@
error: can't qualify macro invocation with `pub`
--> $DIR/pub-item-macro.rs:8:5
--> $DIR/pub-item-macro.rs:10:5
|
LL | pub priv_x!();
| ^^^
| ^^^ help: remove the visibility
...
LL | pub_x!();
| --------- in this macro invocation
Expand All @@ -11,16 +11,16 @@ LL | pub_x!();
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0603]: static `x` is private
--> $DIR/pub-item-macro.rs:17:23
--> $DIR/pub-item-macro.rs:20:23
|
LL | let y: u32 = foo::x;
| ^ this static is private
|
note: the static `x` is defined here
--> $DIR/pub-item-macro.rs:4:5
--> $DIR/pub-item-macro.rs:5:9
|
LL | static x: u32 = 0;
| ^^^^^^^^^^^^^^^^^^
LL | static x: u32 = 0;
| ^^^^^^^^^^^^^^^^^^
...
LL | pub_x!();
| --------- in this macro invocation
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/macro/trait-non-item-macros.stderr
@@ -1,8 +1,8 @@
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, or `unsafe`, found `2`
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or identifier, found `2`
--> $DIR/trait-non-item-macros.rs:2:19
|
LL | ($a:expr) => ($a)
| ^^ expected one of 9 possible tokens
| ^^ expected one of 10 possible tokens
...
LL | bah!(2);
| -------- in this macro invocation
Expand Down
@@ -1,12 +1,14 @@
fn main() {}

impl T for () { //~ ERROR cannot find trait `T` in this scope

fn foo(&self) {}
//~^ ERROR missing `fn`, `type`, or `const`

trait T { //~ ERROR expected one of
trait T {
fn foo(&self);
}

pub(crate) struct Bar<T>();

fn main() {}
//~ ERROR this file contains an unclosed delimiter

0 comments on commit aaaf0ba

Please sign in to comment.