Skip to content

Commit

Permalink
Rollup merge of rust-lang#34355 - jseyfried:paren_expression_ids_nonu…
Browse files Browse the repository at this point in the history
…nique, r=nrc

Give `ast::ExprKind::Paren` no-op expressions the same ids as their children.

Having `ast::ExprKind::Paren` expressions share ids with their children
 - reduces the number of unused `NodeId`s in the hir map and
 - guarantees that `tcx.map.expect_expr(ast_expr.id)` is the hir corresponding to `ast_expr`.

This fixes the bug from rust-lang#34327, which was introduced in rust-lang#33296 when I assumed the above guarantee.

r? @nrc
  • Loading branch information
Manishearth committed Jun 29, 2016
2 parents 366de83 + 8557a2e commit f74d0fb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
13 changes: 11 additions & 2 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,6 @@ pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {

pub fn noop_fold_expr<T: Folder>(Expr {id, node, span, attrs}: Expr, folder: &mut T) -> Expr {
Expr {
id: folder.new_id(id),
node: match node {
ExprKind::Box(e) => {
ExprKind::Box(folder.fold_expr(e))
Expand Down Expand Up @@ -1270,9 +1269,19 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span, attrs}: Expr, folder: &mu
fields.move_map(|x| folder.fold_field(x)),
maybe_expr.map(|x| folder.fold_expr(x)))
},
ExprKind::Paren(ex) => ExprKind::Paren(folder.fold_expr(ex)),
ExprKind::Paren(ex) => {
let sub_expr = folder.fold_expr(ex);
return Expr {
// Nodes that are equal modulo `Paren` sugar no-ops should have the same ids.
id: sub_expr.id,
node: ExprKind::Paren(sub_expr),
span: folder.new_span(span),
attrs: fold_attrs(attrs.into(), folder).into(),
};
}
ExprKind::Try(ex) => ExprKind::Try(folder.fold_expr(ex)),
},
id: folder.new_id(id),
span: folder.new_span(span),
attrs: fold_attrs(attrs.into(), folder).into(),
}
Expand Down
6 changes: 1 addition & 5 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,6 @@ impl<'a> Parser<'a> {
self.expect_one_of(edible, inedible)
}

pub fn commit_stmt_expecting(&mut self, edible: token::Token) -> PResult<'a, ()> {
self.commit_stmt(&[edible], &[])
}

/// returns the span of expr, if it was not interpolated or the span of the interpolated token
fn interpolated_or_expr_span(&self,
expr: PResult<'a, P<Expr>>)
Expand Down Expand Up @@ -4122,7 +4118,7 @@ impl<'a> Parser<'a> {
_ => { // all other kinds of statements:
let mut hi = span.hi;
if classify::stmt_ends_with_semi(&node) {
self.commit_stmt_expecting(token::Semi)?;
self.commit_stmt(&[token::Semi], &[])?;
hi = self.last_span.hi;
}

Expand Down

0 comments on commit f74d0fb

Please sign in to comment.