Skip to content

Commit 9da428d

Browse files
committed
make Parser::parse_foreign_item() return a foreign item or error
closes #54441
1 parent a57f1c9 commit 9da428d

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

src/libsyntax/ext/expand.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1008,9 +1008,7 @@ impl<'a> Parser<'a> {
10081008
AstFragmentKind::ForeignItems => {
10091009
let mut items = SmallVec::new();
10101010
while self.token != token::Eof {
1011-
if let Some(item) = self.parse_foreign_item()? {
1012-
items.push(item);
1013-
}
1011+
items.push(self.parse_foreign_item()?);
10141012
}
10151013
AstFragment::ForeignItems(items)
10161014
}

src/libsyntax/parse/parser.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -6718,10 +6718,9 @@ impl<'a> Parser<'a> {
67186718
attrs.extend(self.parse_inner_attributes()?);
67196719

67206720
let mut foreign_items = vec![];
6721-
while let Some(item) = self.parse_foreign_item()? {
6722-
foreign_items.push(item);
6721+
while !self.eat(&token::CloseDelim(token::Brace)) {
6722+
foreign_items.push(self.parse_foreign_item()?);
67236723
}
6724-
self.expect(&token::CloseDelim(token::Brace))?;
67256724

67266725
let prev_span = self.prev_span;
67276726
let m = ast::ForeignMod {
@@ -7305,8 +7304,8 @@ impl<'a> Parser<'a> {
73057304
}
73067305

73077306
/// Parse a foreign item.
7308-
crate fn parse_foreign_item(&mut self) -> PResult<'a, Option<ForeignItem>> {
7309-
maybe_whole!(self, NtForeignItem, |ni| Some(ni));
7307+
crate fn parse_foreign_item(&mut self) -> PResult<'a, ForeignItem> {
7308+
maybe_whole!(self, NtForeignItem, |ni| ni);
73107309

73117310
let attrs = self.parse_outer_attributes()?;
73127311
let lo = self.span;
@@ -7326,20 +7325,20 @@ impl<'a> Parser<'a> {
73267325
).emit();
73277326
}
73287327
self.bump(); // `static` or `const`
7329-
return Ok(Some(self.parse_item_foreign_static(visibility, lo, attrs)?));
7328+
return Ok(self.parse_item_foreign_static(visibility, lo, attrs)?);
73307329
}
73317330
// FOREIGN FUNCTION ITEM
73327331
if self.check_keyword(keywords::Fn) {
7333-
return Ok(Some(self.parse_item_foreign_fn(visibility, lo, attrs)?));
7332+
return Ok(self.parse_item_foreign_fn(visibility, lo, attrs)?);
73347333
}
73357334
// FOREIGN TYPE ITEM
73367335
if self.check_keyword(keywords::Type) {
7337-
return Ok(Some(self.parse_item_foreign_type(visibility, lo, attrs)?));
7336+
return Ok(self.parse_item_foreign_type(visibility, lo, attrs)?);
73387337
}
73397338

73407339
match self.parse_assoc_macro_invoc("extern", Some(&visibility), &mut false)? {
73417340
Some(mac) => {
7342-
Ok(Some(
7341+
Ok(
73437342
ForeignItem {
73447343
ident: keywords::Invalid.ident(),
73457344
span: lo.to(self.prev_span),
@@ -7348,14 +7347,14 @@ impl<'a> Parser<'a> {
73487347
vis: visibility,
73497348
node: ForeignItemKind::Macro(mac),
73507349
}
7351-
))
7350+
)
73527351
}
73537352
None => {
7354-
if !attrs.is_empty() {
7353+
if !attrs.is_empty() {
73557354
self.expected_item_err(&attrs);
73567355
}
73577356

7358-
Ok(None)
7357+
self.unexpected()
73597358
}
73607359
}
73617360
}

src/test/parse-fail/duplicate-visibility.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// compile-flags: -Z parse-only
1212

13-
// error-pattern:expected one of `(`, `fn`, `static`, `type`, or `}` here
13+
// error-pattern:expected one of `(`, `fn`, `static`, or `type`
1414
extern {
1515
pub pub fn foo();
1616
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(macros_in_extern)]
2+
3+
macro_rules! m {
4+
() => {
5+
let //~ ERROR expected
6+
};
7+
}
8+
9+
extern "C" {
10+
m!();
11+
}
12+
13+
fn main() {}

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: expected one of `crate`, `fn`, `pub`, `static`, or `type`, found `let`
2+
--> $DIR/issue-54441.rs:5:9
3+
|
4+
LL | #![feature(macros_in_extern)]
5+
| - expected one of `crate`, `fn`, `pub`, `static`, or `type` here
6+
...
7+
LL | let //~ ERROR expected
8+
| ^^^ unexpected token
9+
...
10+
LL | m!();
11+
| ----- in this macro invocation
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)