Auto merge of #119261 - cjgillot:outlive-def-kind, r=compiler-errors

Do not fetch HIR in inferred_outlives_of.

Small simplification allowed by https://github.com/rust-lang/rust/pull/119248
This commit is contained in:
bors 2023-12-25 18:52:48 +00:00
commit e4c626dd9a

View File

@ -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;
@ -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 {
&[]
}
_ => &[],
},
}
_ => &[],
}
}