Skip to content

Rollup of 7 pull requests #116605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9683f8a
rustdoc: use let chain in `CacheBuilder::fold_item`
notriddle Mar 20, 2023
3fbfe2b
rustdoc-search: add impl disambiguator to duplicate assoc items
notriddle Mar 20, 2023
3583e86
rustdoc: update test cases for changes to the printing style
notriddle Mar 21, 2023
20b93b9
rustdoc: wait for section to open before trying to highlight
notriddle Mar 21, 2023
2a4c9d0
Update search-result-impl-disambiguation.goml
notriddle Jul 13, 2023
7bb594f
On type error of closure call argument, point at earlier calls that a…
estebank Sep 28, 2023
8c7d232
Update docs for mips target tier demotion.
ehuss Oct 7, 2023
5e1b0cb
add test for const-eval error in dead code during monomorphization
RalfJung Oct 5, 2023
bbc2304
Make BTreeMap::new_in const
Oct 9, 2023
d60b43c
Make BTreeSet::new_in const
Oct 9, 2023
0f27c1b
defids are indexmapped
ouz-a Oct 9, 2023
5f079dd
alloc id is indexmapped
ouz-a Oct 9, 2023
77df2cd
spans are now indexmapped
ouz-a Oct 9, 2023
7962b96
Update books
rustbot Oct 9, 2023
0bcb058
add new wrapper for FxIndexMap
ouz-a Oct 10, 2023
4be9cfa
Rollup merge of #109422 - notriddle:notriddle/impl-disambiguate-searc…
GuillaumeGomez Oct 10, 2023
0e5e04b
Rollup merge of #116250 - estebank:closure-arg-inference-span, r=petr…
GuillaumeGomez Oct 10, 2023
bbaf6bd
Rollup merge of #116444 - RalfJung:broken-unused-const, r=oli-obk
GuillaumeGomez Oct 10, 2023
fccf9ec
Rollup merge of #116503 - ehuss:fix-mips-tier, r=Amanieu
GuillaumeGomez Oct 10, 2023
b72db84
Rollup merge of #116559 - Kritzefitz:btree-new-in-const, r=Amanieu
GuillaumeGomez Oct 10, 2023
100713e
Rollup merge of #116560 - ouz-a:efficient_ids, r=oli-obk
GuillaumeGomez Oct 10, 2023
49dd50b
Rollup merge of #116574 - rustbot:docs-update, r=ehuss
GuillaumeGomez Oct 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add new wrapper for FxIndexMap
  • Loading branch information
ouz-a committed Oct 10, 2023
commit 0bcb058fb13242bf6804e66413315a784484ab09
66 changes: 38 additions & 28 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs
//! until stable MIR is complete.

use std::ops::{ControlFlow, Index};

use crate::rustc_internal;
use crate::rustc_smir::Tables;
use rustc_data_structures::fx;
Expand All @@ -14,14 +12,18 @@ use rustc_middle::mir::interpret::AllocId;
use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::{CrateNum, DefId};
use rustc_span::Span;
use stable_mir::ty::IndexedVal;
use stable_mir::CompilerError;
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::{ControlFlow, Index};

impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
type Output = DefId;

#[inline(always)]
fn index(&self, index: stable_mir::DefId) -> &Self::Output {
&self.def_ids.get_index(index.0).unwrap().0
&self.def_ids[index]
}
}

Expand All @@ -30,7 +32,7 @@ impl<'tcx> Index<stable_mir::ty::Span> for Tables<'tcx> {

#[inline(always)]
fn index(&self, index: stable_mir::ty::Span) -> &Self::Output {
&self.spans.get_index(index.0).unwrap().0
&self.spans[index]
}
}

Expand Down Expand Up @@ -96,33 +98,15 @@ impl<'tcx> Tables<'tcx> {
}

fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
if let Some(i) = self.def_ids.get(&did) {
return *i;
} else {
let id = self.def_ids.len();
self.def_ids.insert(did, stable_mir::DefId(id));
stable_mir::DefId(id)
}
self.def_ids.create_or_fetch(did)
}

fn create_alloc_id(&mut self, aid: AllocId) -> stable_mir::AllocId {
if let Some(i) = self.alloc_ids.get(&aid) {
return *i;
} else {
let id = self.def_ids.len();
self.alloc_ids.insert(aid, stable_mir::AllocId(id));
stable_mir::AllocId(id)
}
self.alloc_ids.create_or_fetch(aid)
}

pub(crate) fn create_span(&mut self, span: Span) -> stable_mir::ty::Span {
if let Some(i) = self.spans.get(&span) {
return *i;
} else {
let id = self.spans.len();
self.spans.insert(span, stable_mir::ty::Span(id));
stable_mir::ty::Span(id)
}
self.spans.create_or_fetch(span)
}
}

Expand All @@ -134,9 +118,9 @@ pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
stable_mir::run(
Tables {
tcx,
def_ids: fx::FxIndexMap::default(),
alloc_ids: fx::FxIndexMap::default(),
spans: fx::FxIndexMap::default(),
def_ids: rustc_internal::IndexMap { index_map: fx::FxIndexMap::default() },
alloc_ids: rustc_internal::IndexMap { index_map: fx::FxIndexMap::default() },
spans: rustc_internal::IndexMap { index_map: fx::FxIndexMap::default() },
types: vec![],
},
f,
Expand Down Expand Up @@ -201,3 +185,29 @@ where
})
}
}

/// Simmilar to rustc's `FxIndexMap`, `IndexMap` with extra
/// safety features added.
pub struct IndexMap<K, V> {
index_map: fx::FxIndexMap<K, V>,
}

impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> IndexMap<K, V> {
pub fn create_or_fetch(&mut self, key: K) -> V {
let len = self.index_map.len();
let v = self.index_map.entry(key).or_insert(V::to_val(len));
*v
}
}

impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> Index<V>
for IndexMap<K, V>
{
type Output = K;

fn index(&self, index: V) -> &Self::Output {
let (k, v) = self.index_map.get_index(index.to_index()).unwrap();
assert_eq!(*v, index, "Provided value doesn't match with indexed value");
k
}
}
8 changes: 4 additions & 4 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
//!
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.

use crate::rustc_internal::IndexMap;
use crate::rustc_smir::hir::def::DefKind;
use crate::rustc_smir::stable_mir::ty::{BoundRegion, EarlyBoundRegion, Region};
use rustc_data_structures::fx::FxIndexMap;
use rustc_hir as hir;
use rustc_middle::mir;
use rustc_middle::mir::interpret::{alloc_range, AllocId};
Expand Down Expand Up @@ -195,9 +195,9 @@ impl<S, R: PartialEq> PartialEq<R> for MaybeStable<S, R> {

pub struct Tables<'tcx> {
pub tcx: TyCtxt<'tcx>,
pub def_ids: FxIndexMap<DefId, stable_mir::DefId>,
pub alloc_ids: FxIndexMap<AllocId, stable_mir::AllocId>,
pub spans: FxIndexMap<rustc_span::Span, Span>,
pub def_ids: IndexMap<DefId, stable_mir::DefId>,
pub alloc_ids: IndexMap<AllocId, stable_mir::AllocId>,
pub spans: IndexMap<rustc_span::Span, Span>,
pub types: Vec<MaybeStable<stable_mir::ty::TyKind, Ty<'tcx>>>,
}

Expand Down
26 changes: 23 additions & 3 deletions compiler/stable_mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use std::fmt;
use std::fmt::Debug;

use self::ty::{
GenericPredicates, Generics, ImplDef, ImplTrait, Span, TraitDecl, TraitDef, Ty, TyKind,
GenericPredicates, Generics, ImplDef, ImplTrait, IndexedVal, Span, TraitDecl, TraitDef, Ty,
TyKind,
};

#[macro_use]
Expand All @@ -41,7 +42,7 @@ pub type CrateNum = usize;

/// A unique identification number for each item accessible for the current compilation unit.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct DefId(pub usize);
pub struct DefId(usize);

impl Debug for DefId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -52,9 +53,28 @@ impl Debug for DefId {
}
}

impl IndexedVal for DefId {
fn to_val(index: usize) -> Self {
DefId(index)
}

fn to_index(&self) -> usize {
self.0
}
}

/// A unique identification number for each provenance
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct AllocId(pub usize);
pub struct AllocId(usize);

impl IndexedVal for AllocId {
fn to_val(index: usize) -> Self {
AllocId(index)
}
fn to_index(&self) -> usize {
self.0
}
}

/// A list of crate items.
pub type CrateItems = Vec<CrateItem>;
Expand Down
17 changes: 16 additions & 1 deletion compiler/stable_mir/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub struct Placeholder<T> {
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Span(pub usize);
pub struct Span(usize);

impl Debug for Span {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Expand All @@ -86,6 +86,15 @@ impl Debug for Span {
}
}

impl IndexedVal for Span {
fn to_val(index: usize) -> Self {
Span(index)
}
fn to_index(&self) -> usize {
self.0
}
}

#[derive(Clone, Debug)]
pub enum TyKind {
RigidTy(RigidTy),
Expand Down Expand Up @@ -565,3 +574,9 @@ pub enum ImplPolarity {
Negative,
Reservation,
}

pub trait IndexedVal {
fn to_val(index: usize) -> Self;

fn to_index(&self) -> usize;
}