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

Lines changed: 120 additions & 138 deletions
Large diffs are not rendered by default.

src/librustc_parse/parser/mod.rs

Lines changed: 5 additions & 0 deletions
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

Lines changed: 6 additions & 12 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 7 additions & 9 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

0 commit comments

Comments
 (0)