Skip to content

Commit

Permalink
Auto merge of #119261 - cjgillot:outlive-def-kind, r=compiler-errors
Browse files Browse the repository at this point in the history
Do not fetch HIR in inferred_outlives_of.

Small simplification allowed by #119248
  • Loading branch information
bors committed Dec 25, 2023
2 parents f8fe517 + af5f7b3 commit e4c626d
Showing 1 changed file with 25 additions and 39 deletions.
64 changes: 25 additions & 39 deletions compiler/rustc_hir_analysis/src/outlives/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use hir::Node;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::LocalDefId;
use rustc_middle::query::Providers;
use rustc_middle::ty::GenericArgKind;
Expand All @@ -17,45 +16,32 @@ pub fn provide(providers: &mut Providers) {
}

fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[(ty::Clause<'_>, Span)] {
let id = tcx.local_def_id_to_hir_id(item_def_id);

if matches!(tcx.def_kind(item_def_id), hir::def::DefKind::AnonConst)
&& tcx.features().generic_const_exprs
{
if tcx.hir().opt_const_param_default_param_def_id(id).is_some() {
// In `generics_of` we set the generics' parent to be our parent's parent which means that
// we lose out on the predicates of our actual parent if we dont return those predicates here.
// (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
//
// struct Foo<'a, 'b, const N: usize = { ... }>(&'a &'b ());
// ^^^ ^^^^^^^ the def id we are calling
// ^^^ inferred_outlives_of on
// parent item we dont have set as the
// parent of generics returned by `generics_of`
//
// In the above code we want the anon const to have predicates in its param env for `'b: 'a`
let item_def_id = tcx.hir().get_parent_item(id);
// In the above code example we would be calling `inferred_outlives_of(Foo)` here
return tcx.inferred_outlives_of(item_def_id);
match tcx.def_kind(item_def_id) {
DefKind::Struct | DefKind::Enum | DefKind::Union => {
let crate_map = tcx.inferred_outlives_crate(());
crate_map.predicates.get(&item_def_id.to_def_id()).copied().unwrap_or(&[])
}
}

match tcx.hir_node(id) {
Node::Item(item) => match item.kind {
hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Union(..) => {
let crate_map = tcx.inferred_outlives_crate(());

let predicates =
crate_map.predicates.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]);

debug!("inferred_outlives_of({:?}) = {:?}", item_def_id, predicates);

predicates
DefKind::AnonConst if tcx.features().generic_const_exprs => {
let id = tcx.local_def_id_to_hir_id(item_def_id);
if tcx.hir().opt_const_param_default_param_def_id(id).is_some() {
// In `generics_of` we set the generics' parent to be our parent's parent which means that
// we lose out on the predicates of our actual parent if we dont return those predicates here.
// (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
//
// struct Foo<'a, 'b, const N: usize = { ... }>(&'a &'b ());
// ^^^ ^^^^^^^ the def id we are calling
// ^^^ inferred_outlives_of on
// parent item we dont have set as the
// parent of generics returned by `generics_of`
//
// In the above code we want the anon const to have predicates in its param env for `'b: 'a`
let item_def_id = tcx.hir().get_parent_item(id);
// In the above code example we would be calling `inferred_outlives_of(Foo)` here
tcx.inferred_outlives_of(item_def_id)
} else {
&[]
}

_ => &[],
},

}
_ => &[],
}
}
Expand Down

0 comments on commit e4c626d

Please sign in to comment.