Skip to content

Commit aaaf0ba

Browse files
committed
parser: misc small item related improvements & cleanups.
1 parent 46d3ef5 commit aaaf0ba

36 files changed

+233
-249
lines changed

src/librustc_parse/parser/item.rs

+120-138
Large diffs are not rendered by default.

src/librustc_parse/parser/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,11 @@ impl<'a> Parser<'a> {
572572
if !self.eat_keyword(kw) { self.unexpected() } else { Ok(()) }
573573
}
574574

575+
/// Is the given keyword `kw` followed by a non-reserved identifier?
576+
fn is_kw_followed_by_ident(&self, kw: Symbol) -> bool {
577+
self.token.is_keyword(kw) && self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
578+
}
579+
575580
fn check_or_expected(&mut self, ok: bool, typ: TokenType) -> bool {
576581
if ok {
577582
true

src/librustc_parse/parser/stmt.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::DirectoryOwnership;
88

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

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

@@ -191,10 +189,6 @@ impl<'a> Parser<'a> {
191189
}
192190
}
193191

194-
fn is_kw_followed_by_ident(&self, kw: Symbol) -> bool {
195-
self.token.is_keyword(kw) && self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
196-
}
197-
198192
fn recover_stmt_local(
199193
&mut self,
200194
lo: Span,

src/test/ui/issues/issue-58856-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | fn how_are_you(&self -> Empty {
77
| | help: `)` may belong here
88
| unclosed delimiter
99

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

src/test/ui/issues/issue-60075.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: expected one of `.`, `;`, `?`, `else`, or an operator, found `}`
44
LL | });
55
| ^ expected one of `.`, `;`, `?`, `else`, or an operator
66

7-
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `;`
7+
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, `}`, or identifier, found `;`
88
--> $DIR/issue-60075.rs:6:11
99
|
1010
LL | fn qux() -> Option<usize> {

src/test/ui/macros/issue-54441.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
macro_rules! m {
2+
//~^ ERROR missing `fn`, `type`, or `static` for extern-item declaration
23
() => {
3-
let //~ ERROR expected
4+
let
45
};
56
}
67

src/test/ui/macros/issue-54441.stderr

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
error: expected one of `async`, `const`, `crate`, `extern`, `fn`, `pub`, `static`, `type`, or `unsafe`, found keyword `let`
2-
--> $DIR/issue-54441.rs:3:9
1+
error: missing `fn`, `type`, or `static` for extern-item declaration
2+
--> $DIR/issue-54441.rs:1:1
33
|
4-
LL | let
5-
| ^^^ expected one of 9 possible tokens
6-
...
7-
LL | m!();
8-
| ----- in this macro invocation
9-
|
10-
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
4+
LL | / macro_rules! m {
5+
LL | |
6+
LL | | () => {
7+
LL | | let
8+
| |________^ missing `fn`, `type`, or `static`
119

1210
error: aborting due to previous error
1311

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: expected item after attributes
2-
--> $DIR/attr-before-eof.rs:3:16
2+
--> $DIR/attr-before-eof.rs:3:1
33
|
44
LL | #[derive(Debug)]
5-
| ^
5+
| ^^^^^^^^^^^^^^^^
66

77
error: aborting due to previous error
88

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: expected item after attributes
2-
--> $DIR/attr-dangling-in-mod.rs:6:14
2+
--> $DIR/attr-dangling-in-mod.rs:6:1
33
|
44
LL | #[foo = "bar"]
5-
| ^
5+
| ^^^^^^^^^^^^^^
66

77
error: aborting due to previous error
88

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: expected item after attributes
2-
--> $DIR/attrs-after-extern-mod.rs:6:19
2+
--> $DIR/attrs-after-extern-mod.rs:6:5
33
|
44
LL | #[cfg(stage37)]
5-
| ^
5+
| ^^^^^^^^^^^^^^^
66

77
error: aborting due to previous error
88

src/test/ui/parser/default.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ impl Foo for u16 {
1919
}
2020

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

2526
fn main() {}

src/test/ui/parser/default.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `async`, `const`, `extern`, `fn`, `type`, or `unsafe`, found keyword `pub`
2-
--> $DIR/default.rs:22:13
1+
error: missing `fn`, `type`, or `const` for associated-item declaration
2+
--> $DIR/default.rs:22:12
33
|
44
LL | default pub fn foo<T: Default>() -> T { T::default() }
5-
| ^^^ expected one of `async`, `const`, `extern`, `fn`, `type`, or `unsafe`
5+
| ^ missing `fn`, `type`, or `const`
66

77
error[E0449]: unnecessary visibility qualifier
88
--> $DIR/default.rs:16:5
+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error: expected item after attributes
2-
--> $DIR/doc-before-attr.rs:4:16
2+
--> $DIR/doc-before-attr.rs:4:1
33
|
4+
LL | /// hi
5+
| ------ other attributes here
46
LL | #[derive(Debug)]
5-
| ^
7+
| ^^^^^^^^^^^^^^^^
68

79
error: aborting due to previous error
810

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// error-pattern: expected one of `(`, `async`, `const`, `extern`, `fn`
2-
31
fn main() {}
42

53
extern {
64
pub pub fn foo();
5+
//~^ ERROR missing `fn`, `type`, or `static` for extern-item declaration
76
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `(`, `async`, `const`, `extern`, `fn`, `static`, `type`, or `unsafe`, found keyword `pub`
2-
--> $DIR/duplicate-visibility.rs:6:9
1+
error: missing `fn`, `type`, or `static` for extern-item declaration
2+
--> $DIR/duplicate-visibility.rs:4:8
33
|
44
LL | pub pub fn foo();
5-
| ^^^ expected one of 8 possible tokens
5+
| ^ missing `fn`, `type`, or `static`
66

77
error: aborting due to previous error
88

src/test/ui/parser/issue-19398.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
trait T {
2-
extern "Rust" unsafe fn foo(); //~ ERROR expected one of `async`, `const`
2+
//~^ ERROR missing `fn`, `type`, or `const` for associated-item declaration
3+
extern "Rust" unsafe fn foo();
34
}
45

56
fn main() {}

src/test/ui/parser/issue-19398.stderr

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found keyword `extern`
2-
--> $DIR/issue-19398.rs:2:5
1+
error: missing `fn`, `type`, or `const` for associated-item declaration
2+
--> $DIR/issue-19398.rs:1:10
33
|
4-
LL | trait T {
5-
| - expected one of 10 possible tokens
6-
LL | extern "Rust" unsafe fn foo();
7-
| ^^^^^^ unexpected token
4+
LL | trait T {
5+
| __________^
6+
LL | |
7+
LL | | extern "Rust" unsafe fn foo();
8+
| |____^ missing `fn`, `type`, or `const`
89

910
error: aborting due to previous error
1011

src/test/ui/parser/issue-20711-2.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ impl Foo {
44
fn foo() {}
55

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

910
fn main() {}
+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, or `unsafe`, found `}`
2-
--> $DIR/issue-20711-2.rs:7:1
1+
error: expected item after attributes
2+
--> $DIR/issue-20711-2.rs:6:5
33
|
44
LL | #[stable(feature = "rust1", since = "1.0.0")]
5-
| - expected one of 9 possible tokens
6-
LL | }
7-
| ^ unexpected token
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86

97
error: aborting due to previous error
108

src/test/ui/parser/issue-20711.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ struct Foo;
22

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

78
fn main() {}

src/test/ui/parser/issue-20711.stderr

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, or `unsafe`, found `}`
2-
--> $DIR/issue-20711.rs:5:1
1+
error: expected item after attributes
2+
--> $DIR/issue-20711.rs:4:5
33
|
44
LL | #[stable(feature = "rust1", since = "1.0.0")]
5-
| - expected one of 9 possible tokens
6-
LL | }
7-
| ^ unexpected token
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86

97
error: aborting due to previous error
108

src/test/ui/parser/issue-32446.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `...`
1+
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, `}`, or identifier, found `...`
22
--> $DIR/issue-32446.rs:4:11
33
|
44
LL | trait T { ... }
5-
| ^^^ expected one of 10 possible tokens
5+
| ^^^ expected one of 11 possible tokens
66

77
error: aborting due to previous error
88

src/test/ui/parser/issue-41155.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `(`, `async`, `const`, `default`, `extern`, `fn`, `type`, or `unsafe`, found `}`
1+
error: expected one of `(`, `async`, `const`, `default`, `extern`, `fn`, `type`, `unsafe`, or identifier, found `}`
22
--> $DIR/issue-41155.rs:5:1
33
|
44
LL | pub
5-
| - expected one of 8 possible tokens
5+
| - expected one of 9 possible tokens
66
LL | }
77
| ^ unexpected token
88

src/test/ui/parser/issue-58094-missing-right-square-bracket.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ LL | #[Ѕ
77
| unclosed delimiter
88

99
error: expected item after attributes
10-
--> $DIR/issue-58094-missing-right-square-bracket.rs:4:4
10+
--> $DIR/issue-58094-missing-right-square-bracket.rs:4:1
1111
|
1212
LL | #[Ѕ
13-
| ^
13+
| ^^^
1414

1515
error: aborting due to 2 previous errors
1616

src/test/ui/parser/macro/pub-item-macro.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// Issue #14660
22

3-
macro_rules! priv_x { () => {
4-
static x: u32 = 0;
5-
}}
3+
macro_rules! priv_x {
4+
() => {
5+
static x: u32 = 0;
6+
};
7+
}
68

79
macro_rules! pub_x { () => {
810
pub priv_x!(); //~ ERROR can't qualify macro invocation with `pub`
9-
//~^ HELP try adjusting the macro to put `pub` inside the invocation
11+
//~^ HELP remove the visibility
12+
//~| HELP try adjusting the macro to put `pub` inside the invocation
1013
}}
1114

1215
mod foo {

src/test/ui/parser/macro/pub-item-macro.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: can't qualify macro invocation with `pub`
2-
--> $DIR/pub-item-macro.rs:8:5
2+
--> $DIR/pub-item-macro.rs:10:5
33
|
44
LL | pub priv_x!();
5-
| ^^^
5+
| ^^^ help: remove the visibility
66
...
77
LL | pub_x!();
88
| --------- in this macro invocation
@@ -11,16 +11,16 @@ LL | pub_x!();
1111
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1212

1313
error[E0603]: static `x` is private
14-
--> $DIR/pub-item-macro.rs:17:23
14+
--> $DIR/pub-item-macro.rs:20:23
1515
|
1616
LL | let y: u32 = foo::x;
1717
| ^ this static is private
1818
|
1919
note: the static `x` is defined here
20-
--> $DIR/pub-item-macro.rs:4:5
20+
--> $DIR/pub-item-macro.rs:5:9
2121
|
22-
LL | static x: u32 = 0;
23-
| ^^^^^^^^^^^^^^^^^^
22+
LL | static x: u32 = 0;
23+
| ^^^^^^^^^^^^^^^^^^
2424
...
2525
LL | pub_x!();
2626
| --------- in this macro invocation

src/test/ui/parser/macro/trait-non-item-macros.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, or `unsafe`, found `2`
1+
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or identifier, found `2`
22
--> $DIR/trait-non-item-macros.rs:2:19
33
|
44
LL | ($a:expr) => ($a)
5-
| ^^ expected one of 9 possible tokens
5+
| ^^ expected one of 10 possible tokens
66
...
77
LL | bah!(2);
88
| -------- in this macro invocation
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
fn main() {}
2+
13
impl T for () { //~ ERROR cannot find trait `T` in this scope
24

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

5-
trait T { //~ ERROR expected one of
8+
trait T {
69
fn foo(&self);
710
}
811

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

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

0 commit comments

Comments
 (0)