Skip to content
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

make Parser::parse_foreign_item() return a foreign item or error #54833

Merged
merged 1 commit into from
Oct 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 1 addition & 3 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,9 +1008,7 @@ impl<'a> Parser<'a> {
AstFragmentKind::ForeignItems => {
let mut items = SmallVec::new();
while self.token != token::Eof {
if let Some(item) = self.parse_foreign_item()? {
items.push(item);
}
items.push(self.parse_foreign_item()?);
}
AstFragment::ForeignItems(items)
}
Expand Down
23 changes: 11 additions & 12 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6718,10 +6718,9 @@ impl<'a> Parser<'a> {
attrs.extend(self.parse_inner_attributes()?);

let mut foreign_items = vec![];
while let Some(item) = self.parse_foreign_item()? {
foreign_items.push(item);
while !self.eat(&token::CloseDelim(token::Brace)) {
foreign_items.push(self.parse_foreign_item()?);
}
self.expect(&token::CloseDelim(token::Brace))?;

let prev_span = self.prev_span;
let m = ast::ForeignMod {
Expand Down Expand Up @@ -7305,8 +7304,8 @@ impl<'a> Parser<'a> {
}

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

let attrs = self.parse_outer_attributes()?;
let lo = self.span;
Expand All @@ -7326,20 +7325,20 @@ impl<'a> Parser<'a> {
).emit();
}
self.bump(); // `static` or `const`
return Ok(Some(self.parse_item_foreign_static(visibility, lo, attrs)?));
return Ok(self.parse_item_foreign_static(visibility, lo, attrs)?);
}
// FOREIGN FUNCTION ITEM
if self.check_keyword(keywords::Fn) {
return Ok(Some(self.parse_item_foreign_fn(visibility, lo, attrs)?));
return Ok(self.parse_item_foreign_fn(visibility, lo, attrs)?);
}
// FOREIGN TYPE ITEM
if self.check_keyword(keywords::Type) {
return Ok(Some(self.parse_item_foreign_type(visibility, lo, attrs)?));
return Ok(self.parse_item_foreign_type(visibility, lo, attrs)?);
}

match self.parse_assoc_macro_invoc("extern", Some(&visibility), &mut false)? {
Some(mac) => {
Ok(Some(
Ok(
ForeignItem {
ident: keywords::Invalid.ident(),
span: lo.to(self.prev_span),
Expand All @@ -7348,14 +7347,14 @@ impl<'a> Parser<'a> {
vis: visibility,
node: ForeignItemKind::Macro(mac),
}
))
)
}
None => {
if !attrs.is_empty() {
if !attrs.is_empty() {
self.expected_item_err(&attrs);
}

Ok(None)
self.unexpected()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/parse-fail/duplicate-visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// compile-flags: -Z parse-only

// error-pattern:expected one of `(`, `fn`, `static`, `type`, or `}` here
// error-pattern:expected one of `(`, `fn`, `static`, or `type`
extern {
pub pub fn foo();
}
13 changes: 13 additions & 0 deletions src/test/ui/macros/issue-54441.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![feature(macros_in_extern)]

macro_rules! m {
() => {
let //~ ERROR expected
};
}

extern "C" {
m!();
}

fn main() {}
14 changes: 14 additions & 0 deletions src/test/ui/macros/issue-54441.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: expected one of `crate`, `fn`, `pub`, `static`, or `type`, found `let`
--> $DIR/issue-54441.rs:5:9
|
LL | #![feature(macros_in_extern)]
| - expected one of `crate`, `fn`, `pub`, `static`, or `type` here
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is the "expected" line pointing to line 1 of the source. However, it looks to be an existing bug in Parser::expect_one_of() WRT macros: https://play.rust-lang.org/?gist=1865a38a912b5aaed3e9a23931ace87c&version=stable&mode=debug&edition=2015

I can fix it here or in a different PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can fix it here or in a different PR.

Whatever is more convenient for you.

...
LL | let //~ ERROR expected
| ^^^ unexpected token
...
LL | m!();
| ----- in this macro invocation

error: aborting due to previous error