Skip to content

Commit

Permalink
Rollup merge of rust-lang#120610 - petrochenkov:maybeownogen, r=cjgillot
Browse files Browse the repository at this point in the history
hir: Remove the generic type parameter from `MaybeOwned`

It's only ever used with a reference to `OwnerInfo` as an argument.

Follow up to rust-lang#120346.
  • Loading branch information
matthiaskrgr authored Feb 3, 2024
2 parents 968cff7 + c5eca33 commit 2a8fc94
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 32 deletions.
7 changes: 2 additions & 5 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(super) struct ItemLowerer<'a, 'hir> {
pub(super) tcx: TyCtxt<'hir>,
pub(super) resolver: &'a mut ResolverAstLowering,
pub(super) ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>>,
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<'hir>>,
}

/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
Expand Down Expand Up @@ -64,10 +64,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
}
}

pub(super) fn lower_node(
&mut self,
def_id: LocalDefId,
) -> hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>> {
pub(super) fn lower_node(&mut self, def_id: LocalDefId) -> hir::MaybeOwner<'hir> {
let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
if let hir::MaybeOwner::Phantom = owner {
let node = self.ast_index[def_id];
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ struct LoweringContext<'a, 'hir> {
/// Attributes inside the owner being lowered.
attrs: SortedMap<hir::ItemLocalId, &'hir [Attribute]>,
/// Collect items that were created by lowering the current owner.
children: Vec<(LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>)>,
children: Vec<(LocalDefId, hir::MaybeOwner<'hir>)>,

coroutine_kind: Option<hir::CoroutineKind>,

Expand Down Expand Up @@ -415,7 +415,7 @@ fn index_crate<'a>(
/// This hash will then be part of the crate_hash which is stored in the metadata.
fn compute_hir_hash(
tcx: TyCtxt<'_>,
owners: &IndexSlice<LocalDefId, hir::MaybeOwner<&hir::OwnerInfo<'_>>>,
owners: &IndexSlice<LocalDefId, hir::MaybeOwner<'_>>,
) -> Fingerprint {
let mut hir_body_nodes: Vec<_> = owners
.iter_enumerated()
Expand Down
25 changes: 7 additions & 18 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,34 +894,23 @@ impl<'tcx> OwnerInfo<'tcx> {
}

#[derive(Copy, Clone, Debug, HashStable_Generic)]
pub enum MaybeOwner<T> {
Owner(T),
pub enum MaybeOwner<'tcx> {
Owner(&'tcx OwnerInfo<'tcx>),
NonOwner(HirId),
/// Used as a placeholder for unused LocalDefId.
Phantom,
}

impl<T> MaybeOwner<T> {
pub fn as_owner(self) -> Option<T> {
impl<'tcx> MaybeOwner<'tcx> {
pub fn as_owner(self) -> Option<&'tcx OwnerInfo<'tcx>> {
match self {
MaybeOwner::Owner(i) => Some(i),
MaybeOwner::NonOwner(_) | MaybeOwner::Phantom => None,
}
}

pub fn map<U>(self, f: impl FnOnce(T) -> U) -> MaybeOwner<U> {
match self {
MaybeOwner::Owner(i) => MaybeOwner::Owner(f(i)),
MaybeOwner::NonOwner(hir_id) => MaybeOwner::NonOwner(hir_id),
MaybeOwner::Phantom => MaybeOwner::Phantom,
}
}

pub fn unwrap(self) -> T {
match self {
MaybeOwner::Owner(i) => i,
MaybeOwner::NonOwner(_) | MaybeOwner::Phantom => panic!("Not a HIR owner"),
}
pub fn unwrap(self) -> &'tcx OwnerInfo<'tcx> {
self.as_owner().unwrap_or_else(|| panic!("Not a HIR owner"))
}
}

Expand All @@ -933,7 +922,7 @@ impl<T> MaybeOwner<T> {
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
#[derive(Debug)]
pub struct Crate<'hir> {
pub owners: IndexVec<LocalDefId, MaybeOwner<&'hir OwnerInfo<'hir>>>,
pub owners: IndexVec<LocalDefId, MaybeOwner<'hir>>,
// Only present when incr. comp. is enabled.
pub opt_hir_hash: Option<Fingerprint>,
}
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,11 @@ pub fn provide(providers: &mut Providers) {
providers.hir_crate_items = map::hir_crate_items;
providers.crate_hash = map::crate_hash;
providers.hir_module_items = map::hir_module_items;
providers.opt_local_def_id_to_hir_id = |tcx, id| {
let owner = tcx.hir_crate(()).owners[id].map(|_| ());
Some(match owner {
MaybeOwner::Owner(_) => HirId::make_owner(id),
MaybeOwner::Phantom => bug!("No HirId for {:?}", id),
providers.opt_local_def_id_to_hir_id = |tcx, def_id| {
Some(match tcx.hir_crate(()).owners[def_id] {
MaybeOwner::Owner(_) => HirId::make_owner(def_id),
MaybeOwner::NonOwner(hir_id) => hir_id,
MaybeOwner::Phantom => bug!("No HirId for {:?}", def_id),
})
};
providers.opt_hir_owner_nodes =
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/query/erase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ impl EraseType for Option<ty::EarlyBinder<Ty<'_>>> {
type Result = [u8; size_of::<Option<ty::EarlyBinder<Ty<'static>>>>()];
}

impl<T> EraseType for rustc_hir::MaybeOwner<&'_ T> {
type Result = [u8; size_of::<rustc_hir::MaybeOwner<&'static ()>>()];
impl EraseType for rustc_hir::MaybeOwner<'_> {
type Result = [u8; size_of::<rustc_hir::MaybeOwner<'static>>()];
}

impl<T: EraseType> EraseType for ty::EarlyBinder<T> {
Expand Down

0 comments on commit 2a8fc94

Please sign in to comment.