Skip to content

Commit f74d0fb

Browse files
authored
Rollup merge of rust-lang#34355 - jseyfried:paren_expression_ids_nonunique, 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
2 parents 366de83 + 8557a2e commit f74d0fb

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/libsyntax/fold.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,6 @@ pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
11021102

11031103
pub fn noop_fold_expr<T: Folder>(Expr {id, node, span, attrs}: Expr, folder: &mut T) -> Expr {
11041104
Expr {
1105-
id: folder.new_id(id),
11061105
node: match node {
11071106
ExprKind::Box(e) => {
11081107
ExprKind::Box(folder.fold_expr(e))
@@ -1270,9 +1269,19 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span, attrs}: Expr, folder: &mu
12701269
fields.move_map(|x| folder.fold_field(x)),
12711270
maybe_expr.map(|x| folder.fold_expr(x)))
12721271
},
1273-
ExprKind::Paren(ex) => ExprKind::Paren(folder.fold_expr(ex)),
1272+
ExprKind::Paren(ex) => {
1273+
let sub_expr = folder.fold_expr(ex);
1274+
return Expr {
1275+
// Nodes that are equal modulo `Paren` sugar no-ops should have the same ids.
1276+
id: sub_expr.id,
1277+
node: ExprKind::Paren(sub_expr),
1278+
span: folder.new_span(span),
1279+
attrs: fold_attrs(attrs.into(), folder).into(),
1280+
};
1281+
}
12741282
ExprKind::Try(ex) => ExprKind::Try(folder.fold_expr(ex)),
12751283
},
1284+
id: folder.new_id(id),
12761285
span: folder.new_span(span),
12771286
attrs: fold_attrs(attrs.into(), folder).into(),
12781287
}

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,6 @@ impl<'a> Parser<'a> {
553553
self.expect_one_of(edible, inedible)
554554
}
555555

556-
pub fn commit_stmt_expecting(&mut self, edible: token::Token) -> PResult<'a, ()> {
557-
self.commit_stmt(&[edible], &[])
558-
}
559-
560556
/// returns the span of expr, if it was not interpolated or the span of the interpolated token
561557
fn interpolated_or_expr_span(&self,
562558
expr: PResult<'a, P<Expr>>)
@@ -4122,7 +4118,7 @@ impl<'a> Parser<'a> {
41224118
_ => { // all other kinds of statements:
41234119
let mut hi = span.hi;
41244120
if classify::stmt_ends_with_semi(&node) {
4125-
self.commit_stmt_expecting(token::Semi)?;
4121+
self.commit_stmt(&[token::Semi], &[])?;
41264122
hi = self.last_span.hi;
41274123
}
41284124

0 commit comments

Comments
 (0)