Skip to content

Commit 8ee0954

Browse files
committed
Impl Copy for almost all HIR types
This simplifies the invocation of the `arena_types` macro and probably makes working with HIR nicer in general.
1 parent 3709ec1 commit 8ee0954

File tree

40 files changed

+511
-253
lines changed

40 files changed

+511
-253
lines changed

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{ImplTraitContext, ImplTraitPosition, LoweringContext};
22
use rustc_ast::{Block, BlockCheckMode, Local, LocalKind, Stmt, StmtKind};
33
use rustc_hir as hir;
4+
use rustc_data_structures::thin_slice::ThinSlice;
45

56
use smallvec::SmallVec;
67

@@ -27,7 +28,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2728
fn lower_stmts(
2829
&mut self,
2930
mut ast_stmts: &[Stmt],
30-
) -> (&'hir [hir::Stmt<'hir>], Option<&'hir hir::Expr<'hir>>) {
31+
) -> (&'hir ThinSlice<hir::Stmt<'hir>>, Option<&'hir hir::Expr<'hir>>) {
3132
let mut stmts = SmallVec::<[hir::Stmt<'hir>; 8]>::new();
3233
let mut expr = None;
3334
while let [s, tail @ ..] = ast_stmts {
@@ -78,7 +79,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
7879
}
7980
ast_stmts = &ast_stmts[1..];
8081
}
81-
(self.arena.alloc_from_iter(stmts), expr)
82+
(self.arena.allocate_thin_from_iter(stmts), expr)
8283
}
8384

8485
fn lower_local(&mut self, l: &Local) -> &'hir hir::Local<'hir> {

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_ast::attr;
1111
use rustc_ast::ptr::P as AstP;
1212
use rustc_ast::*;
1313
use rustc_data_structures::stack::ensure_sufficient_stack;
14+
use rustc_data_structures::thin_slice::ThinSlice;
1415
use rustc_hir as hir;
1516
use rustc_hir::def::Res;
1617
use rustc_hir::definitions::DefPathData;
@@ -25,6 +26,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
2526
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
2627
}
2728

29+
fn lower_exprs_thin(&mut self, exprs: &[AstP<Expr>]) -> &'hir ThinSlice<hir::Expr<'hir>> {
30+
self.arena.allocate_thin_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
31+
}
32+
2833
pub(super) fn lower_expr(&mut self, e: &Expr) -> &'hir hir::Expr<'hir> {
2934
self.arena.alloc(self.lower_expr_mut(e))
3035
}
@@ -100,7 +105,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
100105
self.lower_legacy_const_generics((**f).clone(), args.clone(), &legacy_args)
101106
} else {
102107
let f = self.lower_expr(f);
103-
hir::ExprKind::Call(f, self.lower_exprs(args))
108+
hir::ExprKind::Call(f, self.lower_exprs_thin(args))
104109
}
105110
}
106111
ExprKind::MethodCall(box MethodCall { seg, receiver, args, span }) => {
@@ -112,8 +117,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
112117
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
113118
));
114119
let receiver = self.lower_expr(receiver);
115-
let args =
116-
self.arena.alloc_from_iter(args.iter().map(|x| self.lower_expr_mut(x)));
120+
let args = self
121+
.arena
122+
.allocate_thin_from_iter(args.iter().map(|x| self.lower_expr_mut(x)));
117123
hir::ExprKind::MethodCall(hir_seg, receiver, args, self.lower_span(*span))
118124
}
119125
ExprKind::Binary(binop, lhs, rhs) => {
@@ -407,7 +413,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
407413

408414
// Now lower everything as normal.
409415
let f = self.lower_expr(&f);
410-
hir::ExprKind::Call(f, self.lower_exprs(&real_args))
416+
hir::ExprKind::Call(f, self.lower_exprs_thin(&real_args))
411417
}
412418

413419
fn lower_expr_if(
@@ -494,7 +500,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
494500
let then = self.lower_block_expr(body);
495501
let expr_break = self.expr_break(span);
496502
let stmt_break = self.stmt_expr(span, expr_break);
497-
let else_blk = self.block_all(span, arena_vec![self; stmt_break], None);
503+
let else_blk = self.block_all(span, arena_thin_vec![self; stmt_break], None);
498504
let else_expr = self.arena.alloc(self.expr_block(else_blk));
499505
let if_kind = hir::ExprKind::If(lowered_cond, self.arena.alloc(then), Some(else_expr));
500506
let if_expr = self.expr(span, if_kind);
@@ -554,7 +560,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
554560
overall_span: Span,
555561
) -> &'hir hir::Expr<'hir> {
556562
let constructor = self.arena.alloc(self.expr_lang_item_path(method_span, lang_item, None));
557-
self.expr_call(overall_span, constructor, std::slice::from_ref(expr))
563+
self.expr_call(overall_span, constructor, arena_thin_vec![self; *expr])
558564
}
559565

560566
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
@@ -653,7 +659,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
653659
def_id: self.local_def_id(closure_node_id),
654660
binder: hir::ClosureBinder::Default,
655661
capture_clause,
656-
bound_generic_params: &[],
662+
bound_generic_params: ThinSlice::empty(),
657663
fn_decl,
658664
body,
659665
fn_decl_span: self.lower_span(span),
@@ -755,19 +761,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
755761
let new_unchecked = self.expr_call_lang_item_fn_mut(
756762
span,
757763
hir::LangItem::PinNewUnchecked,
758-
arena_vec![self; ref_mut_awaitee],
764+
arena_thin_vec![self; ref_mut_awaitee],
759765
Some(expr_hir_id),
760766
);
761767
let get_context = self.expr_call_lang_item_fn_mut(
762768
gen_future_span,
763769
hir::LangItem::GetContext,
764-
arena_vec![self; task_context],
770+
arena_thin_vec![self; task_context],
765771
Some(expr_hir_id),
766772
);
767773
let call = self.expr_call_lang_item_fn(
768774
span,
769775
hir::LangItem::FuturePoll,
770-
arena_vec![self; new_unchecked, get_context],
776+
arena_thin_vec![self; new_unchecked, get_context],
771777
Some(expr_hir_id),
772778
);
773779
self.arena.alloc(self.expr_unsafe(call))
@@ -800,7 +806,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
800806
let pending_pat = self.pat_lang_item_variant(
801807
span,
802808
hir::LangItem::PollPending,
803-
&[],
809+
ThinSlice::empty(),
804810
Some(expr_hir_id),
805811
);
806812
let empty_block = self.expr_block_empty(span);
@@ -838,7 +844,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
838844
}
839845
};
840846

841-
let loop_block = self.block_all(span, arena_vec![self; inner_match_stmt, yield_stmt], None);
847+
let loop_block =
848+
self.block_all(span, arena_thin_vec![self; inner_match_stmt, yield_stmt], None);
842849

843850
// loop { .. }
844851
let loop_expr = self.arena.alloc(hir::Expr {
@@ -864,7 +871,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
864871
let into_future_expr = self.expr_call_lang_item_fn(
865872
into_future_span,
866873
hir::LangItem::IntoFutureIntoFuture,
867-
arena_vec![self; expr],
874+
arena_thin_vec![self; expr],
868875
Some(expr_hir_id),
869876
);
870877

@@ -1092,9 +1099,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
10921099
);
10931100

10941101
// `a = lhs1; b = lhs2;`.
1095-
let stmts = self
1096-
.arena
1097-
.alloc_from_iter(std::iter::once(destructure_let).chain(assignments.into_iter()));
1102+
let stmts = self.arena.allocate_thin_from_iter(
1103+
std::iter::once(destructure_let).chain(assignments.into_iter()),
1104+
);
10981105

10991106
// Wrap everything in a block.
11001107
hir::ExprKind::Block(&self.block_all(whole_span, stmts, None), None)
@@ -1167,7 +1174,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11671174
let (pats, rest) =
11681175
self.destructure_sequence(elements, "slice", eq_sign_span, assignments);
11691176
let slice_pat = if let Some((i, span)) = rest {
1170-
let (before, after) = pats.split_at(i);
1177+
let (before, after): (&[hir::Pat<'_>], &[hir::Pat<'_>]) = pats.split_at(i);
11711178
hir::PatKind::Slice(
11721179
before,
11731180
Some(self.arena.alloc(self.pat_without_dbm(span, hir::PatKind::Wild))),
@@ -1220,7 +1227,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12201227
}
12211228
// Structs.
12221229
ExprKind::Struct(se) => {
1223-
let field_pats = self.arena.alloc_from_iter(se.fields.iter().map(|f| {
1230+
let field_pats = self.arena.allocate_thin_from_iter(se.fields.iter().map(|f| {
12241231
let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments);
12251232
hir::PatField {
12261233
hir_id: self.next_id(),
@@ -1260,7 +1267,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
12601267
ExprKind::Paren(e) => {
12611268
// We special-case `(..)` for consistency with patterns.
12621269
if let ExprKind::Range(None, None, RangeLimits::HalfOpen) = e.kind {
1263-
let tuple_pat = hir::PatKind::Tuple(&[], hir::DotDotPos::new(Some(0)));
1270+
let tuple_pat =
1271+
hir::PatKind::Tuple(ThinSlice::empty(), hir::DotDotPos::new(Some(0)));
12641272
return self.pat_without_dbm(lhs.span, tuple_pat);
12651273
} else {
12661274
return self.destructure_assign_mut(e, eq_sign_span, assignments);
@@ -1290,10 +1298,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
12901298
ctx: &str,
12911299
eq_sign_span: Span,
12921300
assignments: &mut Vec<hir::Stmt<'hir>>,
1293-
) -> (&'hir [hir::Pat<'hir>], Option<(usize, Span)>) {
1301+
) -> (&'hir ThinSlice<hir::Pat<'hir>>, Option<(usize, Span)>) {
12941302
let mut rest = None;
12951303
let elements =
1296-
self.arena.alloc_from_iter(elements.iter().enumerate().filter_map(|(i, e)| {
1304+
self.arena.allocate_thin_from_iter(elements.iter().enumerate().filter_map(|(i, e)| {
12971305
// Check for `..` pattern.
12981306
if let ExprKind::Range(None, None, RangeLimits::HalfOpen) = e.kind {
12991307
if let Some((_, prev_span)) = rest {
@@ -1316,7 +1324,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13161324
let fn_path =
13171325
hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, self.lower_span(span), None);
13181326
let fn_expr = self.arena.alloc(self.expr(span, hir::ExprKind::Path(fn_path)));
1319-
hir::ExprKind::Call(fn_expr, arena_vec![self; e1, e2])
1327+
hir::ExprKind::Call(fn_expr, arena_thin_vec![self; e1, e2])
13201328
}
13211329

13221330
fn lower_expr_range(
@@ -1512,7 +1520,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15121520
let next_expr = self.expr_call_lang_item_fn(
15131521
head_span,
15141522
hir::LangItem::IteratorNext,
1515-
arena_vec![self; ref_mut_iter],
1523+
arena_thin_vec![self; ref_mut_iter],
15161524
None,
15171525
);
15181526
let arms = arena_vec![self; none_arm, some_arm];
@@ -1521,7 +1529,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15211529
};
15221530
let match_stmt = self.stmt_expr(for_span, match_expr);
15231531

1524-
let loop_block = self.block_all(for_span, arena_vec![self; match_stmt], None);
1532+
let loop_block = self.block_all(for_span, arena_thin_vec![self; match_stmt], None);
15251533

15261534
// `[opt_ident]: loop { ... }`
15271535
let kind = hir::ExprKind::Loop(
@@ -1541,7 +1549,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15411549
self.expr_call_lang_item_fn(
15421550
head_span,
15431551
hir::LangItem::IntoIterIntoIter,
1544-
arena_vec![self; head],
1552+
arena_thin_vec![self; head],
15451553
None,
15461554
)
15471555
};
@@ -1597,7 +1605,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15971605
self.expr_call_lang_item_fn(
15981606
unstable_span,
15991607
hir::LangItem::TryTraitBranch,
1600-
arena_vec![self; sub_expr],
1608+
arena_thin_vec![self; sub_expr],
16011609
None,
16021610
)
16031611
};
@@ -1784,7 +1792,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17841792
&mut self,
17851793
span: Span,
17861794
e: &'hir hir::Expr<'hir>,
1787-
args: &'hir [hir::Expr<'hir>],
1795+
args: &'hir ThinSlice<hir::Expr<'hir>>,
17881796
) -> hir::Expr<'hir> {
17891797
self.expr(span, hir::ExprKind::Call(e, args))
17901798
}
@@ -1793,7 +1801,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17931801
&mut self,
17941802
span: Span,
17951803
e: &'hir hir::Expr<'hir>,
1796-
args: &'hir [hir::Expr<'hir>],
1804+
args: &'hir ThinSlice<hir::Expr<'hir>>,
17971805
) -> &'hir hir::Expr<'hir> {
17981806
self.arena.alloc(self.expr_call_mut(span, e, args))
17991807
}
@@ -1802,7 +1810,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18021810
&mut self,
18031811
span: Span,
18041812
lang_item: hir::LangItem,
1805-
args: &'hir [hir::Expr<'hir>],
1813+
args: &'hir ThinSlice<hir::Expr<'hir>>,
18061814
hir_id: Option<hir::HirId>,
18071815
) -> hir::Expr<'hir> {
18081816
let path = self.arena.alloc(self.expr_lang_item_path(span, lang_item, hir_id));
@@ -1813,7 +1821,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18131821
&mut self,
18141822
span: Span,
18151823
lang_item: hir::LangItem,
1816-
args: &'hir [hir::Expr<'hir>],
1824+
args: &'hir ThinSlice<hir::Expr<'hir>>,
18171825
hir_id: Option<hir::HirId>,
18181826
) -> &'hir hir::Expr<'hir> {
18191827
self.arena.alloc(self.expr_call_lang_item_fn_mut(span, lang_item, args, hir_id))
@@ -1874,7 +1882,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18741882
self.arena.alloc(hir::Path {
18751883
span: self.lower_span(span),
18761884
res,
1877-
segments: arena_vec![self; hir::PathSegment::new(ident, hir_id, res)],
1885+
segments: arena_thin_vec![self; hir::PathSegment::new(ident, hir_id, res)],
18781886
}),
18791887
));
18801888

@@ -1888,7 +1896,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18881896
span,
18891897
hir::ExprKind::Block(
18901898
self.arena.alloc(hir::Block {
1891-
stmts: &[],
1899+
stmts: ThinSlice::empty(),
18921900
expr: Some(expr),
18931901
hir_id,
18941902
rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated),
@@ -1901,7 +1909,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19011909
}
19021910

19031911
fn expr_block_empty(&mut self, span: Span) -> &'hir hir::Expr<'hir> {
1904-
let blk = self.block_all(span, &[], None);
1912+
let blk = self.block_all(span, ThinSlice::empty(), None);
19051913
let expr = self.expr_block(blk);
19061914
self.arena.alloc(expr)
19071915
}

compiler/rustc_ast_lowering/src/format.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_ast as ast;
33
use rustc_ast::visit::{self, Visitor};
44
use rustc_ast::*;
55
use rustc_data_structures::fx::FxIndexSet;
6+
use rustc_data_structures::thin_slice::ThinSlice;
67
use rustc_hir as hir;
78
use rustc_span::{
89
sym,
@@ -212,7 +213,7 @@ fn make_argument<'hir>(
212213
Usize => sym::from_usize,
213214
},
214215
));
215-
ctx.expr_call_mut(sp, new_fn, std::slice::from_ref(arg))
216+
ctx.expr_call_mut(sp, new_fn, arena_thin_vec![ctx; *arg])
216217
}
217218

218219
/// Generate a hir expression for a format_args Count.
@@ -247,7 +248,7 @@ fn make_count<'hir>(
247248
hir::LangItem::FormatCount,
248249
sym::Is,
249250
));
250-
let value = ctx.arena.alloc_from_iter([ctx.expr_usize(sp, *n)]);
251+
let value = ctx.arena.allocate_thin_from_iter([ctx.expr_usize(sp, *n)]);
251252
ctx.expr_call_mut(sp, count_is, value)
252253
}
253254
Some(FormatCount::Argument(arg)) => {
@@ -258,7 +259,7 @@ fn make_count<'hir>(
258259
hir::LangItem::FormatCount,
259260
sym::Param,
260261
));
261-
let value = ctx.arena.alloc_from_iter([ctx.expr_usize(sp, i)]);
262+
let value = ctx.arena.allocate_thin_from_iter([ctx.expr_usize(sp, i)]);
262263
ctx.expr_call_mut(sp, count_param, value)
263264
} else {
264265
ctx.expr(
@@ -340,7 +341,7 @@ fn make_format_spec<'hir>(
340341
hir::LangItem::FormatPlaceholder,
341342
sym::new,
342343
));
343-
let args = ctx.arena.alloc_from_iter([position, fill, align, flags, precision, width]);
344+
let args = ctx.arena.allocate_thin_from_iter([position, fill, align, flags, precision, width]);
344345
ctx.expr_call_mut(sp, format_placeholder_new, args)
345346
}
346347

@@ -426,7 +427,7 @@ fn expand_format_args<'hir>(
426427
hir::LangItem::FormatArguments,
427428
sym::new_const,
428429
));
429-
let new_args = ctx.arena.alloc_from_iter([lit_pieces]);
430+
let new_args = ctx.arena.allocate_thin_from_iter([lit_pieces]);
430431
return hir::ExprKind::Call(new, new_args);
431432
}
432433

@@ -530,17 +531,17 @@ fn expand_format_args<'hir>(
530531
hir::LangItem::FormatUnsafeArg,
531532
sym::new,
532533
));
533-
let unsafe_arg_new_call = ctx.expr_call(macsp, unsafe_arg_new, &[]);
534+
let unsafe_arg_new_call = ctx.expr_call(macsp, unsafe_arg_new, ThinSlice::empty());
534535
let hir_id = ctx.next_id();
535536
let unsafe_arg = ctx.expr_block(ctx.arena.alloc(hir::Block {
536-
stmts: &[],
537+
stmts: ThinSlice::empty(),
537538
expr: Some(unsafe_arg_new_call),
538539
hir_id,
539540
rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated),
540541
span: macsp,
541542
targeted_by_break: false,
542543
}));
543-
let args = ctx.arena.alloc_from_iter([lit_pieces, args, format_options, unsafe_arg]);
544+
let args = ctx.arena.allocate_thin_from_iter([lit_pieces, args, format_options, unsafe_arg]);
544545
hir::ExprKind::Call(new_v1_formatted, args)
545546
} else {
546547
// Generate:
@@ -553,7 +554,7 @@ fn expand_format_args<'hir>(
553554
hir::LangItem::FormatArguments,
554555
sym::new_v1,
555556
));
556-
let new_args = ctx.arena.alloc_from_iter([lit_pieces, args]);
557+
let new_args = ctx.arena.allocate_thin_from_iter([lit_pieces, args]);
557558
hir::ExprKind::Call(new_v1, new_args)
558559
}
559560
}

0 commit comments

Comments
 (0)