Skip to content

Commit 8bfd97c

Browse files
committed
Introduce indexvec macro.
1 parent cbd381c commit 8bfd97c

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

compiler/rustc_index/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,13 @@ macro_rules! static_assert_size {
5050
const _: (usize, usize) = ($size, ::std::mem::size_of::<$ty>());
5151
};
5252
}
53+
54+
#[macro_export]
55+
macro_rules! indexvec {
56+
($expr:expr; $n:expr) => {
57+
IndexVec::from_raw(vec![$expr; $n])
58+
};
59+
($($expr:expr),* $(,)?) => {
60+
IndexVec::from_raw(vec![$($expr),*])
61+
};
62+
}

compiler/rustc_mir_transform/src/coroutine.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use rustc_hir as hir;
6767
use rustc_hir::lang_items::LangItem;
6868
use rustc_hir::{CoroutineDesugaring, CoroutineKind};
6969
use rustc_index::bit_set::{BitMatrix, DenseBitSet, GrowableBitSet};
70-
use rustc_index::{Idx, IndexVec};
70+
use rustc_index::{Idx, IndexVec, indexvec};
7171
use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
7272
use rustc_middle::mir::*;
7373
use rustc_middle::ty::util::Discr;
@@ -289,7 +289,7 @@ impl<'tcx> TransformVisitor<'tcx> {
289289
let poll_def_id = self.tcx.require_lang_item(LangItem::Poll, source_info.span);
290290
let args = self.tcx.mk_args(&[self.old_ret_ty.into()]);
291291
let (variant_idx, operands) = if is_return {
292-
(ZERO, IndexVec::from_raw(vec![val])) // Poll::Ready(val)
292+
(ZERO, indexvec![val]) // Poll::Ready(val)
293293
} else {
294294
(ONE, IndexVec::new()) // Poll::Pending
295295
};
@@ -301,7 +301,7 @@ impl<'tcx> TransformVisitor<'tcx> {
301301
let (variant_idx, operands) = if is_return {
302302
(ZERO, IndexVec::new()) // None
303303
} else {
304-
(ONE, IndexVec::from_raw(vec![val])) // Some(val)
304+
(ONE, indexvec![val]) // Some(val)
305305
};
306306
make_aggregate_adt(option_def_id, variant_idx, args, operands)
307307
}
@@ -337,12 +337,7 @@ impl<'tcx> TransformVisitor<'tcx> {
337337
} else {
338338
ZERO // CoroutineState::Yielded(val)
339339
};
340-
make_aggregate_adt(
341-
coroutine_state_def_id,
342-
variant_idx,
343-
args,
344-
IndexVec::from_raw(vec![val]),
345-
)
340+
make_aggregate_adt(coroutine_state_def_id, variant_idx, args, indexvec![val])
346341
}
347342
};
348343

@@ -1122,7 +1117,7 @@ fn return_poll_ready_assign<'tcx>(tcx: TyCtxt<'tcx>, source_info: SourceInfo) ->
11221117
}));
11231118
let ready_val = Rvalue::Aggregate(
11241119
Box::new(AggregateKind::Adt(poll_def_id, VariantIdx::from_usize(0), args, None, None)),
1125-
IndexVec::from_raw(vec![val]),
1120+
indexvec![val],
11261121
);
11271122
Statement::new(source_info, StatementKind::Assign(Box::new((Place::return_place(), ready_val))))
11281123
}

compiler/rustc_mir_transform/src/elaborate_box_derefs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Box is not actually a pointer so it is incorrect to dereference it directly.
44
55
use rustc_abi::{FieldIdx, VariantIdx};
6-
use rustc_index::IndexVec;
6+
use rustc_index::{IndexVec, indexvec};
77
use rustc_middle::mir::visit::MutVisitor;
88
use rustc_middle::mir::*;
99
use rustc_middle::span_bug;
@@ -124,7 +124,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'a, 'tcx> {
124124
nonnull.into(),
125125
Rvalue::Aggregate(
126126
adt_kind(self.nonnull_def, args),
127-
IndexVec::from_raw(vec![Operand::Move(constptr.into())]),
127+
indexvec![Operand::Move(constptr.into())],
128128
),
129129
);
130130

@@ -136,15 +136,15 @@ impl<'a, 'tcx> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'a, 'tcx> {
136136
unique.into(),
137137
Rvalue::Aggregate(
138138
adt_kind(self.unique_def, args),
139-
IndexVec::from_raw(vec![Operand::Move(nonnull.into()), zst(phantomdata_ty)]),
139+
indexvec![Operand::Move(nonnull.into()), zst(phantomdata_ty)],
140140
),
141141
);
142142

143143
let global_alloc_ty =
144144
box_adt.non_enum_variant().fields[FieldIdx::ONE].ty(tcx, box_args);
145145
*rvalue = Rvalue::Aggregate(
146146
adt_kind(*box_adt, box_args),
147-
IndexVec::from_raw(vec![Operand::Move(unique.into()), zst(global_alloc_ty)]),
147+
indexvec![Operand::Move(unique.into()), zst(global_alloc_ty)],
148148
);
149149
}
150150
}

0 commit comments

Comments
 (0)