1
Fork 0

Use &IndexSlice instead of &IndexVec where possible

All the same reasons as for `[T]`: more general, less pointer chasing, and `&mut IndexSlice` emphasizes that it doesn't change *length*.
This commit is contained in:
Scott McMurray 2023-03-31 00:32:44 -07:00
parent a93bcdc307
commit a2ee7592d6
42 changed files with 168 additions and 118 deletions

View file

@ -6,7 +6,7 @@ use rustc_data_structures::graph;
use rustc_data_structures::graph::dominators::{dominators, Dominators};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::OnceCell;
use rustc_index::vec::IndexVec;
use rustc_index::vec::{IndexSlice, IndexVec};
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use smallvec::SmallVec;
@ -124,10 +124,10 @@ impl<'tcx> BasicBlocks<'tcx> {
}
impl<'tcx> std::ops::Deref for BasicBlocks<'tcx> {
type Target = IndexVec<BasicBlock, BasicBlockData<'tcx>>;
type Target = IndexSlice<BasicBlock, BasicBlockData<'tcx>>;
#[inline]
fn deref(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
fn deref(&self) -> &IndexSlice<BasicBlock, BasicBlockData<'tcx>> {
&self.basic_blocks
}
}

View file

@ -27,7 +27,7 @@ use polonius_engine::Atom;
pub use rustc_ast::Mutability;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::graph::dominators::Dominators;
use rustc_index::vec::{Idx, IndexVec};
use rustc_index::vec::{Idx, IndexSlice, IndexVec};
use rustc_serialize::{Decodable, Encodable};
use rustc_span::symbol::Symbol;
use rustc_span::{Span, DUMMY_SP};
@ -70,12 +70,19 @@ pub use self::pretty::{
};
/// Types for locals
pub type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
pub type LocalDecls<'tcx> = IndexSlice<Local, LocalDecl<'tcx>>;
pub trait HasLocalDecls<'tcx> {
fn local_decls(&self) -> &LocalDecls<'tcx>;
}
impl<'tcx> HasLocalDecls<'tcx> for IndexVec<Local, LocalDecl<'tcx>> {
#[inline]
fn local_decls(&self) -> &LocalDecls<'tcx> {
self
}
}
impl<'tcx> HasLocalDecls<'tcx> for LocalDecls<'tcx> {
#[inline]
fn local_decls(&self) -> &LocalDecls<'tcx> {
@ -250,7 +257,7 @@ pub struct Body<'tcx> {
/// The first local is the return value pointer, followed by `arg_count`
/// locals for the function arguments, followed by any user-declared
/// variables and temporaries.
pub local_decls: LocalDecls<'tcx>,
pub local_decls: IndexVec<Local, LocalDecl<'tcx>>,
/// User type annotations.
pub user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
@ -311,7 +318,7 @@ impl<'tcx> Body<'tcx> {
source: MirSource<'tcx>,
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
local_decls: LocalDecls<'tcx>,
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
arg_count: usize,
var_debug_info: Vec<VarDebugInfo<'tcx>>,
@ -1779,7 +1786,7 @@ impl SourceScope {
/// from the function that was inlined instead of the function call site.
pub fn lint_root(
self,
source_scopes: &IndexVec<SourceScope, SourceScopeData<'_>>,
source_scopes: &IndexSlice<SourceScope, SourceScopeData<'_>>,
) -> Option<HirId> {
let mut data = &source_scopes[self];
// FIXME(oli-obk): we should be able to just walk the `inlined_parent_scope`, but it
@ -1799,7 +1806,7 @@ impl SourceScope {
#[inline]
pub fn inlined_instance<'tcx>(
self,
source_scopes: &IndexVec<SourceScope, SourceScopeData<'tcx>>,
source_scopes: &IndexSlice<SourceScope, SourceScopeData<'tcx>>,
) -> Option<ty::Instance<'tcx>> {
let scope_data = &source_scopes[self];
if let Some((inlined_instance, _)) = scope_data.inlined {

View file

@ -116,7 +116,7 @@ impl<'tcx> PlaceTy<'tcx> {
}
impl<'tcx> Place<'tcx> {
pub fn ty_from<D>(
pub fn ty_from<D: ?Sized>(
local: Local,
projection: &[PlaceElem<'tcx>],
local_decls: &D,
@ -132,7 +132,7 @@ impl<'tcx> Place<'tcx> {
})
}
pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> PlaceTy<'tcx>
pub fn ty<D: ?Sized>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> PlaceTy<'tcx>
where
D: HasLocalDecls<'tcx>,
{
@ -141,7 +141,7 @@ impl<'tcx> Place<'tcx> {
}
impl<'tcx> PlaceRef<'tcx> {
pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> PlaceTy<'tcx>
pub fn ty<D: ?Sized>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> PlaceTy<'tcx>
where
D: HasLocalDecls<'tcx>,
{
@ -155,7 +155,7 @@ pub enum RvalueInitializationState {
}
impl<'tcx> Rvalue<'tcx> {
pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
pub fn ty<D: ?Sized>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
where
D: HasLocalDecls<'tcx>,
{
@ -217,7 +217,7 @@ impl<'tcx> Rvalue<'tcx> {
}
impl<'tcx> Operand<'tcx> {
pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
pub fn ty<D: ?Sized>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
where
D: HasLocalDecls<'tcx>,
{

View file

@ -101,7 +101,7 @@ impl<'a, 'tcx> Iterator for Preorder<'a, 'tcx> {
///
/// A Postorder traversal of this graph is `D B C A` or `D C B A`
pub struct Postorder<'a, 'tcx> {
basic_blocks: &'a IndexVec<BasicBlock, BasicBlockData<'tcx>>,
basic_blocks: &'a IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
visited: BitSet<BasicBlock>,
visit_stack: Vec<(BasicBlock, Successors<'a>)>,
root_is_start_block: bool,
@ -109,7 +109,7 @@ pub struct Postorder<'a, 'tcx> {
impl<'a, 'tcx> Postorder<'a, 'tcx> {
pub fn new(
basic_blocks: &'a IndexVec<BasicBlock, BasicBlockData<'tcx>>,
basic_blocks: &'a IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
root: BasicBlock,
) -> Postorder<'a, 'tcx> {
let mut po = Postorder {

View file

@ -10,7 +10,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_index::vec::IndexVec;
use rustc_index::vec::{IndexSlice, IndexVec};
use rustc_query_system::ich::StableHashingContext;
use rustc_session::DataTypeKind;
use rustc_span::symbol::sym;
@ -168,7 +168,7 @@ impl<'tcx> AdtDef<'tcx> {
}
#[inline]
pub fn variants(self) -> &'tcx IndexVec<VariantIdx, VariantDef> {
pub fn variants(self) -> &'tcx IndexSlice<VariantIdx, VariantDef> {
&self.0.0.variants
}