Introduce indexvec macro.

This commit is contained in:
Camille Gillot
2025-10-14 16:42:08 +00:00
parent 51275e82c9
commit 0156eaf0d1
3 changed files with 19 additions and 14 deletions

View File

@@ -50,3 +50,13 @@ macro_rules! static_assert_size {
const _: (usize, usize) = ($size, ::std::mem::size_of::<$ty>());
};
}
#[macro_export]
macro_rules! indexvec {
($expr:expr; $n:expr) => {
IndexVec::from_raw(vec![$expr; $n])
};
($($expr:expr),* $(,)?) => {
IndexVec::from_raw(vec![$($expr),*])
};
}

View File

@@ -67,7 +67,7 @@ use rustc_hir as hir;
use rustc_hir::lang_items::LangItem;
use rustc_hir::{CoroutineDesugaring, CoroutineKind};
use rustc_index::bit_set::{BitMatrix, DenseBitSet, GrowableBitSet};
use rustc_index::{Idx, IndexVec};
use rustc_index::{Idx, IndexVec, indexvec};
use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::*;
use rustc_middle::ty::util::Discr;
@@ -289,7 +289,7 @@ impl<'tcx> TransformVisitor<'tcx> {
let poll_def_id = self.tcx.require_lang_item(LangItem::Poll, source_info.span);
let args = self.tcx.mk_args(&[self.old_ret_ty.into()]);
let (variant_idx, operands) = if is_return {
(ZERO, IndexVec::from_raw(vec![val])) // Poll::Ready(val)
(ZERO, indexvec![val]) // Poll::Ready(val)
} else {
(ONE, IndexVec::new()) // Poll::Pending
};
@@ -301,7 +301,7 @@ impl<'tcx> TransformVisitor<'tcx> {
let (variant_idx, operands) = if is_return {
(ZERO, IndexVec::new()) // None
} else {
(ONE, IndexVec::from_raw(vec![val])) // Some(val)
(ONE, indexvec![val]) // Some(val)
};
make_aggregate_adt(option_def_id, variant_idx, args, operands)
}
@@ -337,12 +337,7 @@ impl<'tcx> TransformVisitor<'tcx> {
} else {
ZERO // CoroutineState::Yielded(val)
};
make_aggregate_adt(
coroutine_state_def_id,
variant_idx,
args,
IndexVec::from_raw(vec![val]),
)
make_aggregate_adt(coroutine_state_def_id, variant_idx, args, indexvec![val])
}
};
@@ -1122,7 +1117,7 @@ fn return_poll_ready_assign<'tcx>(tcx: TyCtxt<'tcx>, source_info: SourceInfo) ->
}));
let ready_val = Rvalue::Aggregate(
Box::new(AggregateKind::Adt(poll_def_id, VariantIdx::from_usize(0), args, None, None)),
IndexVec::from_raw(vec![val]),
indexvec![val],
);
Statement::new(source_info, StatementKind::Assign(Box::new((Place::return_place(), ready_val))))
}

View File

@@ -6,7 +6,7 @@
//! contents, we do not need this any more on runtime MIR.
use rustc_abi::{FieldIdx, VariantIdx};
use rustc_index::IndexVec;
use rustc_index::{IndexVec, indexvec};
use rustc_middle::mir::visit::MutVisitor;
use rustc_middle::mir::*;
use rustc_middle::span_bug;
@@ -127,7 +127,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'a, 'tcx> {
nonnull.into(),
Rvalue::Aggregate(
adt_kind(self.nonnull_def, args),
IndexVec::from_raw(vec![Operand::Move(constptr.into())]),
indexvec![Operand::Move(constptr.into())],
),
);
@@ -139,7 +139,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'a, 'tcx> {
unique.into(),
Rvalue::Aggregate(
adt_kind(self.unique_def, args),
IndexVec::from_raw(vec![Operand::Move(nonnull.into()), zst(phantomdata_ty)]),
indexvec![Operand::Move(nonnull.into()), zst(phantomdata_ty)],
),
);
@@ -147,7 +147,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'a, 'tcx> {
box_adt.non_enum_variant().fields[FieldIdx::ONE].ty(tcx, box_args);
*rvalue = Rvalue::Aggregate(
adt_kind(*box_adt, box_args),
IndexVec::from_raw(vec![Operand::Move(unique.into()), zst(global_alloc_ty)]),
indexvec![Operand::Move(unique.into()), zst(global_alloc_ty)],
);
}
}