Skip to content

Clean up parse_bottom_expr to use list parsing utility #64105

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

Closed
wants to merge 2 commits into from
Closed
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
99 changes: 39 additions & 60 deletions src/libsyntax/parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,29 @@ impl<'a> Parser<'a> {
self.parse_expr_res(Restrictions::empty(), None)
}

fn parse_expr_catch_underscore(&mut self) -> PResult<'a, P<Expr>> {
match self.parse_expr() {
Ok(expr) => Ok(expr),
Err(mut err) => match self.token.kind {
token::Ident(name, false)
if name == kw::Underscore && self.look_ahead(1, |t| {
t == &token::Comma
}) => {
// Special-case handling of `foo(_, _, _)`
err.emit();
let sp = self.token.span;
self.bump();
Ok(self.mk_expr(sp, ExprKind::Err, ThinVec::new()))
}
_ => Err(err),
},
}
}

/// Parses a sequence of expressions bounded by parentheses.
fn parse_paren_expr_seq(&mut self) -> PResult<'a, Vec<P<Expr>>> {
self.parse_paren_comma_seq(|p| {
match p.parse_expr() {
Ok(expr) => Ok(expr),
Err(mut err) => match p.token.kind {
token::Ident(name, false)
if name == kw::Underscore && p.look_ahead(1, |t| {
t == &token::Comma
}) => {
// Special-case handling of `foo(_, _, _)`
err.emit();
let sp = p.token.span;
p.bump();
Ok(p.mk_expr(sp, ExprKind::Err, ThinVec::new()))
}
_ => Err(err),
},
}
p.parse_expr_catch_underscore()
}).map(|(r, _)| r)
}

Expand Down Expand Up @@ -786,51 +791,25 @@ impl<'a> Parser<'a> {
parse_lit!()
}
token::OpenDelim(token::Paren) => {
self.bump();
Copy link
Contributor

Choose a reason for hiding this comment

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

The order matters here. You've changed the parser to accept ( inner_attr* expr_0, ..., expr_n ) to accepting inner_attr* ( expr_0, ..., expr_n ).

To preserve the semantics you'll need to use parse_paren_comma_seq instead and then conditionally do parse_inner_attributes() on the first element. To avoid duplication, you'll also need to refactor the match p.parse_expr() { ... } bit in parse_paren_expr_seq into a different function.

To catch similar mistakes in the future, let's also add a test:

#![feature(stmt_expr_attributes)]

fn main() {
    let x = #![allow(warnings)] (1, 2);
    //~^ ERROR an inner attribute is not permitted in this context
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I have a test and a proper fix now.

My original refactoring of parse_paren_expr_seq is now unnecessary, and currently just contributes extra noise. I'd like to undo it. But if I back it out locally and force push I believe the current review comments will be lost -- is that ok? Or should I do something else?

Copy link
Contributor

Choose a reason for hiding this comment

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

The comments themselves and the code snippet remain, it should be fine.


attrs.extend(self.parse_inner_attributes()?);

// `(e)` is parenthesized `e`.
// `(e,)` is a tuple with only one field, `e`.
let mut es = vec![];
let mut trailing_comma = false;
let mut recovered = false;
while self.token != token::CloseDelim(token::Paren) {
es.push(match self.parse_expr() {
Ok(es) => es,
Err(mut err) => {
// Recover from parse error in tuple list.
match self.token.kind {
token::Ident(name, false)
if name == kw::Underscore && self.look_ahead(1, |t| {
t == &token::Comma
}) => {
// Special-case handling of `Foo<(_, _, _)>`
err.emit();
let sp = self.token.span;
self.bump();
self.mk_expr(sp, ExprKind::Err, ThinVec::new())
}
_ => return Ok(
self.recover_seq_parse_error(token::Paren, lo, Err(err)),
),
}
}
});
recovered = self.expect_one_of(
&[],
&[token::Comma, token::CloseDelim(token::Paren)],
)?;
if self.eat(&token::Comma) {
trailing_comma = true;
} else {
trailing_comma = false;
break;
let mut first = true;
let parse_leading_attr_expr = |this: &mut Parser<'a>| {
if first {
attrs.extend(this.parse_inner_attributes()?);
first = false;
}
}
if !recovered {
self.bump();
}
this.parse_expr_catch_underscore()
};

// (e) is parenthesized e
// (e,) is a tuple with only one field, e
let (es, trailing_comma) =
match self.parse_paren_comma_seq(parse_leading_attr_expr)
{
Ok(x) => x,
Err(err) => return Ok(
self.recover_seq_parse_error(token::Paren, lo, Err(err)),
),
};

hi = self.prev_span;
ex = if es.len() == 1 && !trailing_comma {
Expand Down
22 changes: 22 additions & 0 deletions src/test/ui/parser/stmt_expr_attrs_placement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![feature(stmt_expr_attributes)]

// Test that various placements of the inner attribute are parsed correctly,
// or not.

fn main() {
let a = #![allow(warnings)] (1, 2);
//~^ ERROR an inner attribute is not permitted in this context

let b = (#![allow(warnings)] 1, 2);

let c = {
#![allow(warnings)]
(#![allow(warnings)] 1, 2)
};

let d = {
#![allow(warnings)]
let e = (#![allow(warnings)] 1, 2);
e
};
}
10 changes: 10 additions & 0 deletions src/test/ui/parser/stmt_expr_attrs_placement.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: an inner attribute is not permitted in this context
--> $DIR/stmt_expr_attrs_placement.rs:7:13
|
LL | let a = #![allow(warnings)] (1, 2);
| ^^^^^^^^^^^^^^^^^^^
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes, like `#[test]`, annotate the item following them.

error: aborting due to previous error