Skip to content

Commit c559cf6

Browse files
committed
Simplify attribute handling in rustc_ast_lowering
Given that attributes is stored in a separate BTreeMap, it's not necessary to pass it in when constructing `hir::Expr`. We can just construct `hir::Expr` and then call `self.lower_attrs` later if it needs attributes. As most desugaring code don't use attributes, this allows some code cleanup.
1 parent 8e440b0 commit c559cf6

File tree

3 files changed

+45
-106
lines changed

3 files changed

+45
-106
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+39-94
Original file line numberDiff line numberDiff line change
@@ -436,18 +436,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
436436
let lhs = self.lower_cond(lhs);
437437
let rhs = self.lower_cond(rhs);
438438

439-
self.arena.alloc(self.expr(
440-
cond.span,
441-
hir::ExprKind::Binary(op, lhs, rhs),
442-
AttrVec::new(),
443-
))
439+
self.arena.alloc(self.expr(cond.span, hir::ExprKind::Binary(op, lhs, rhs)))
444440
}
445441
ExprKind::Let(..) => self.lower_expr(cond),
446442
_ => {
447443
let cond = self.lower_expr(cond);
448444
let reason = DesugaringKind::CondTemporary;
449445
let span_block = self.mark_span_with_reason(reason, cond.span, None);
450-
self.expr_drop_temps(span_block, cond, AttrVec::new())
446+
self.expr_drop_temps(span_block, cond)
451447
}
452448
}
453449
}
@@ -477,12 +473,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
477473
) -> hir::ExprKind<'hir> {
478474
let lowered_cond = self.with_loop_condition_scope(|t| t.lower_cond(cond));
479475
let then = self.lower_block_expr(body);
480-
let expr_break = self.expr_break(span, AttrVec::new());
476+
let expr_break = self.expr_break(span);
481477
let stmt_break = self.stmt_expr(span, expr_break);
482478
let else_blk = self.block_all(span, arena_vec![self; stmt_break], None);
483-
let else_expr = self.arena.alloc(self.expr_block(else_blk, AttrVec::new()));
479+
let else_expr = self.arena.alloc(self.expr_block(else_blk));
484480
let if_kind = hir::ExprKind::If(lowered_cond, self.arena.alloc(then), Some(else_expr));
485-
let if_expr = self.expr(span, if_kind, AttrVec::new());
481+
let if_expr = self.expr(span, if_kind);
486482
let block = self.block_expr(self.arena.alloc(if_expr));
487483
let span = self.lower_span(span.with_hi(cond.span.hi()));
488484
let opt_label = self.lower_label(opt_label);
@@ -538,12 +534,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
538534
expr: &'hir hir::Expr<'hir>,
539535
overall_span: Span,
540536
) -> &'hir hir::Expr<'hir> {
541-
let constructor = self.arena.alloc(self.expr_lang_item_path(
542-
method_span,
543-
lang_item,
544-
AttrVec::new(),
545-
None,
546-
));
537+
let constructor = self.arena.alloc(self.expr_lang_item_path(method_span, lang_item, None));
547538
self.expr_call(overall_span, constructor, std::slice::from_ref(expr))
548539
}
549540

@@ -694,12 +685,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
694685
// E0700 in src/test/ui/self/self_lifetime-async.rs
695686

696687
// `future::identity_future`:
697-
let identity_future = self.expr_lang_item_path(
698-
unstable_span,
699-
hir::LangItem::IdentityFuture,
700-
AttrVec::new(),
701-
None,
702-
);
688+
let identity_future =
689+
self.expr_lang_item_path(unstable_span, hir::LangItem::IdentityFuture, None);
703690

704691
// `future::identity_future(generator)`:
705692
hir::ExprKind::Call(self.arena.alloc(identity_future), arena_vec![self; generator])
@@ -802,7 +789,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
802789
let break_x = self.with_loop_scope(loop_node_id, move |this| {
803790
let expr_break =
804791
hir::ExprKind::Break(this.lower_loop_destination(None), Some(x_expr));
805-
this.arena.alloc(this.expr(gen_future_span, expr_break, AttrVec::new()))
792+
this.arena.alloc(this.expr(gen_future_span, expr_break))
806793
});
807794
self.arm(ready_pat, break_x)
808795
};
@@ -835,17 +822,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
835822
let yield_expr = self.expr(
836823
span,
837824
hir::ExprKind::Yield(unit, hir::YieldSource::Await { expr: Some(expr_hir_id) }),
838-
AttrVec::new(),
839825
);
840826
let yield_expr = self.arena.alloc(yield_expr);
841827

842828
if let Some(task_context_hid) = self.task_context {
843829
let lhs = self.expr_ident(span, task_context_ident, task_context_hid);
844-
let assign = self.expr(
845-
span,
846-
hir::ExprKind::Assign(lhs, yield_expr, self.lower_span(span)),
847-
AttrVec::new(),
848-
);
830+
let assign =
831+
self.expr(span, hir::ExprKind::Assign(lhs, yield_expr, self.lower_span(span)));
849832
self.stmt_expr(span, assign)
850833
} else {
851834
// Use of `await` outside of an async context. Return `yield_expr` so that we can
@@ -1029,7 +1012,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10291012
hir::AsyncGeneratorKind::Closure,
10301013
|this| this.with_new_scopes(|this| this.lower_expr_mut(body)),
10311014
);
1032-
this.expr(fn_decl_span, async_body, AttrVec::new())
1015+
this.expr(fn_decl_span, async_body)
10331016
});
10341017
body_id
10351018
});
@@ -1289,7 +1272,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12891272
let ident = self.expr_ident(lhs.span, ident, binding);
12901273
let assign =
12911274
hir::ExprKind::Assign(self.lower_expr(lhs), ident, self.lower_span(eq_sign_span));
1292-
let expr = self.expr(lhs.span, assign, AttrVec::new());
1275+
let expr = self.expr(lhs.span, assign);
12931276
assignments.push(self.stmt_expr(lhs.span, expr));
12941277
pat
12951278
}
@@ -1330,8 +1313,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13301313
let e2 = self.lower_expr_mut(e2);
13311314
let fn_path =
13321315
hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, self.lower_span(span), None);
1333-
let fn_expr =
1334-
self.arena.alloc(self.expr(span, hir::ExprKind::Path(fn_path), AttrVec::new()));
1316+
let fn_expr = self.arena.alloc(self.expr(span, hir::ExprKind::Path(fn_path)));
13351317
hir::ExprKind::Call(fn_expr, arena_vec![self; e1, e2])
13361318
}
13371319

@@ -1503,8 +1485,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15031485

15041486
// `None => break`
15051487
let none_arm = {
1506-
let break_expr =
1507-
self.with_loop_scope(e.id, |this| this.expr_break_alloc(for_span, AttrVec::new()));
1488+
let break_expr = self.with_loop_scope(e.id, |this| this.expr_break_alloc(for_span));
15081489
let pat = self.pat_none(for_span);
15091490
self.arm(pat, break_expr)
15101491
};
@@ -1513,7 +1494,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15131494
let some_arm = {
15141495
let some_pat = self.pat_some(pat_span, pat);
15151496
let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
1516-
let body_expr = self.arena.alloc(self.expr_block(body_block, AttrVec::new()));
1497+
let body_expr = self.arena.alloc(self.expr_block(body_block));
15171498
self.arm(some_pat, body_expr)
15181499
};
15191500

@@ -1576,7 +1557,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
15761557
// surrounding scope of the `match` since the `match` is not a terminating scope.
15771558
//
15781559
// Also, add the attributes to the outer returned expr node.
1579-
self.expr_drop_temps_mut(for_span, match_expr, e.attrs.clone())
1560+
let expr = self.expr_drop_temps_mut(for_span, match_expr);
1561+
self.lower_attrs(expr.hir_id, &e.attrs);
1562+
expr
15801563
}
15811564

15821565
/// Desugar `ExprKind::Try` from: `<expr>?` into:
@@ -1631,12 +1614,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
16311614
let continue_arm = {
16321615
let val_ident = Ident::with_dummy_span(sym::val);
16331616
let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
1634-
let val_expr = self.arena.alloc(self.expr_ident_with_attrs(
1635-
span,
1636-
val_ident,
1637-
val_pat_nid,
1638-
attrs.clone(),
1639-
));
1617+
let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
1618+
self.lower_attrs(val_expr.hir_id, &attrs);
16401619
let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
16411620
self.arm(continue_pat, val_expr)
16421621
};
@@ -1662,15 +1641,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
16621641
hir::Destination { label: None, target_id },
16631642
Some(from_residual_expr),
16641643
),
1665-
attrs,
16661644
))
16671645
} else {
1668-
self.arena.alloc(self.expr(
1669-
try_span,
1670-
hir::ExprKind::Ret(Some(from_residual_expr)),
1671-
attrs,
1672-
))
1646+
self.arena.alloc(self.expr(try_span, hir::ExprKind::Ret(Some(from_residual_expr))))
16731647
};
1648+
self.lower_attrs(ret_expr.hir_id, &attrs);
16741649

16751650
let break_pat = self.pat_cf_break(try_span, residual_local);
16761651
self.arm(break_pat, ret_expr)
@@ -1735,18 +1710,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
17351710
&mut self,
17361711
span: Span,
17371712
expr: &'hir hir::Expr<'hir>,
1738-
attrs: AttrVec,
17391713
) -> &'hir hir::Expr<'hir> {
1740-
self.arena.alloc(self.expr_drop_temps_mut(span, expr, attrs))
1714+
self.arena.alloc(self.expr_drop_temps_mut(span, expr))
17411715
}
17421716

17431717
pub(super) fn expr_drop_temps_mut(
17441718
&mut self,
17451719
span: Span,
17461720
expr: &'hir hir::Expr<'hir>,
1747-
attrs: AttrVec,
17481721
) -> hir::Expr<'hir> {
1749-
self.expr(span, hir::ExprKind::DropTemps(expr), attrs)
1722+
self.expr(span, hir::ExprKind::DropTemps(expr))
17501723
}
17511724

17521725
fn expr_match(
@@ -1756,29 +1729,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
17561729
arms: &'hir [hir::Arm<'hir>],
17571730
source: hir::MatchSource,
17581731
) -> hir::Expr<'hir> {
1759-
self.expr(span, hir::ExprKind::Match(arg, arms, source), AttrVec::new())
1732+
self.expr(span, hir::ExprKind::Match(arg, arms, source))
17601733
}
17611734

1762-
fn expr_break(&mut self, span: Span, attrs: AttrVec) -> hir::Expr<'hir> {
1735+
fn expr_break(&mut self, span: Span) -> hir::Expr<'hir> {
17631736
let expr_break = hir::ExprKind::Break(self.lower_loop_destination(None), None);
1764-
self.expr(span, expr_break, attrs)
1737+
self.expr(span, expr_break)
17651738
}
17661739

1767-
fn expr_break_alloc(&mut self, span: Span, attrs: AttrVec) -> &'hir hir::Expr<'hir> {
1768-
let expr_break = self.expr_break(span, attrs);
1740+
fn expr_break_alloc(&mut self, span: Span) -> &'hir hir::Expr<'hir> {
1741+
let expr_break = self.expr_break(span);
17691742
self.arena.alloc(expr_break)
17701743
}
17711744

17721745
fn expr_mut_addr_of(&mut self, span: Span, e: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
1773-
self.expr(
1774-
span,
1775-
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e),
1776-
AttrVec::new(),
1777-
)
1746+
self.expr(span, hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e))
17781747
}
17791748

17801749
fn expr_unit(&mut self, sp: Span) -> &'hir hir::Expr<'hir> {
1781-
self.arena.alloc(self.expr(sp, hir::ExprKind::Tup(&[]), AttrVec::new()))
1750+
self.arena.alloc(self.expr(sp, hir::ExprKind::Tup(&[])))
17821751
}
17831752

17841753
fn expr_call_mut(
@@ -1787,7 +1756,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17871756
e: &'hir hir::Expr<'hir>,
17881757
args: &'hir [hir::Expr<'hir>],
17891758
) -> hir::Expr<'hir> {
1790-
self.expr(span, hir::ExprKind::Call(e, args), AttrVec::new())
1759+
self.expr(span, hir::ExprKind::Call(e, args))
17911760
}
17921761

17931762
fn expr_call(
@@ -1806,8 +1775,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18061775
args: &'hir [hir::Expr<'hir>],
18071776
hir_id: Option<hir::HirId>,
18081777
) -> hir::Expr<'hir> {
1809-
let path =
1810-
self.arena.alloc(self.expr_lang_item_path(span, lang_item, AttrVec::new(), hir_id));
1778+
let path = self.arena.alloc(self.expr_lang_item_path(span, lang_item, hir_id));
18111779
self.expr_call_mut(span, path, args)
18121780
}
18131781

@@ -1825,13 +1793,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
18251793
&mut self,
18261794
span: Span,
18271795
lang_item: hir::LangItem,
1828-
attrs: AttrVec,
18291796
hir_id: Option<hir::HirId>,
18301797
) -> hir::Expr<'hir> {
18311798
self.expr(
18321799
span,
18331800
hir::ExprKind::Path(hir::QPath::LangItem(lang_item, self.lower_span(span), hir_id)),
1834-
attrs,
18351801
)
18361802
}
18371803

@@ -1845,20 +1811,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
18451811
}
18461812

18471813
pub(super) fn expr_ident_mut(
1848-
&mut self,
1849-
sp: Span,
1850-
ident: Ident,
1851-
binding: hir::HirId,
1852-
) -> hir::Expr<'hir> {
1853-
self.expr_ident_with_attrs(sp, ident, binding, AttrVec::new())
1854-
}
1855-
1856-
fn expr_ident_with_attrs(
18571814
&mut self,
18581815
span: Span,
18591816
ident: Ident,
18601817
binding: hir::HirId,
1861-
attrs: AttrVec,
18621818
) -> hir::Expr<'hir> {
18631819
let hir_id = self.next_id();
18641820
let res = Res::Local(binding);
@@ -1871,7 +1827,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18711827
}),
18721828
));
18731829

1874-
self.expr(span, expr_path, attrs)
1830+
self.expr(span, expr_path)
18751831
}
18761832

18771833
fn expr_unsafe(&mut self, expr: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
@@ -1890,32 +1846,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
18901846
}),
18911847
None,
18921848
),
1893-
AttrVec::new(),
18941849
)
18951850
}
18961851

18971852
fn expr_block_empty(&mut self, span: Span) -> &'hir hir::Expr<'hir> {
18981853
let blk = self.block_all(span, &[], None);
1899-
let expr = self.expr_block(blk, AttrVec::new());
1854+
let expr = self.expr_block(blk);
19001855
self.arena.alloc(expr)
19011856
}
19021857

1903-
pub(super) fn expr_block(
1904-
&mut self,
1905-
b: &'hir hir::Block<'hir>,
1906-
attrs: AttrVec,
1907-
) -> hir::Expr<'hir> {
1908-
self.expr(b.span, hir::ExprKind::Block(b, None), attrs)
1858+
pub(super) fn expr_block(&mut self, b: &'hir hir::Block<'hir>) -> hir::Expr<'hir> {
1859+
self.expr(b.span, hir::ExprKind::Block(b, None))
19091860
}
19101861

1911-
pub(super) fn expr(
1912-
&mut self,
1913-
span: Span,
1914-
kind: hir::ExprKind<'hir>,
1915-
attrs: AttrVec,
1916-
) -> hir::Expr<'hir> {
1862+
pub(super) fn expr(&mut self, span: Span, kind: hir::ExprKind<'hir>) -> hir::Expr<'hir> {
19171863
let hir_id = self.next_id();
1918-
self.lower_attrs(hir_id, &attrs);
19191864
hir::Expr { hir_id, kind, span: self.lower_span(span) }
19201865
}
19211866

compiler/rustc_ast_lowering/src/item.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
796796

797797
/// Construct `ExprKind::Err` for the given `span`.
798798
pub(crate) fn expr_err(&mut self, span: Span) -> hir::Expr<'hir> {
799-
self.expr(span, hir::ExprKind::Err, AttrVec::new())
799+
self.expr(span, hir::ExprKind::Err)
800800
}
801801

802802
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
@@ -1151,11 +1151,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
11511151
// Transform into `drop-temps { <user-body> }`, an expression:
11521152
let desugared_span =
11531153
this.mark_span_with_reason(DesugaringKind::Async, user_body.span, None);
1154-
let user_body = this.expr_drop_temps(
1155-
desugared_span,
1156-
this.arena.alloc(user_body),
1157-
AttrVec::new(),
1158-
);
1154+
let user_body =
1155+
this.expr_drop_temps(desugared_span, this.arena.alloc(user_body));
11591156

11601157
// As noted above, create the final block like
11611158
//
@@ -1172,14 +1169,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
11721169
Some(user_body),
11731170
);
11741171

1175-
this.expr_block(body, AttrVec::new())
1172+
this.expr_block(body)
11761173
},
11771174
);
11781175

1179-
(
1180-
this.arena.alloc_from_iter(parameters),
1181-
this.expr(body.span, async_expr, AttrVec::new()),
1182-
)
1176+
(this.arena.alloc_from_iter(parameters), this.expr(body.span, async_expr))
11831177
})
11841178
}
11851179

compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22912291
/// has no attributes and is not targeted by a `break`.
22922292
fn lower_block_expr(&mut self, b: &Block) -> hir::Expr<'hir> {
22932293
let block = self.lower_block(b, false);
2294-
self.expr_block(block, AttrVec::new())
2294+
self.expr_block(block)
22952295
}
22962296

22972297
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen {

0 commit comments

Comments
 (0)