Make impl_trait_ref into a query also returning more information about the impl

This commit is contained in:
Oli Scherer 2024-02-10 21:26:48 +00:00
parent de4d615e6b
commit 916951efcc
9 changed files with 48 additions and 40 deletions

View File

@ -78,7 +78,7 @@ pub fn provide(providers: &mut Providers) {
trait_def, trait_def,
adt_def, adt_def,
fn_sig, fn_sig,
impl_trait_ref, impl_trait_header,
impl_polarity, impl_polarity,
coroutine_kind, coroutine_kind,
coroutine_for_closure, coroutine_for_closure,
@ -598,7 +598,7 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
hir::ItemKind::Impl { .. } => { hir::ItemKind::Impl { .. } => {
tcx.ensure().generics_of(def_id); tcx.ensure().generics_of(def_id);
tcx.ensure().type_of(def_id); tcx.ensure().type_of(def_id);
tcx.ensure().impl_trait_ref(def_id); tcx.ensure().impl_trait_header(def_id);
tcx.ensure().predicates_of(def_id); tcx.ensure().predicates_of(def_id);
} }
hir::ItemKind::Trait(..) => { hir::ItemKind::Trait(..) => {
@ -1323,19 +1323,20 @@ fn suggest_impl_trait<'tcx>(
None None
} }
fn impl_trait_ref( fn impl_trait_header(
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
def_id: LocalDefId, def_id: LocalDefId,
) -> Option<ty::EarlyBinder<ty::TraitRef<'_>>> { ) -> Option<(ty::EarlyBinder<ty::TraitRef<'_>>, ty::ImplPolarity)> {
let icx = ItemCtxt::new(tcx, def_id); let icx = ItemCtxt::new(tcx, def_id);
let impl_ = tcx.hir().expect_item(def_id).expect_impl(); let item = tcx.hir().expect_item(def_id);
let impl_ = item.expect_impl();
impl_ impl_
.of_trait .of_trait
.as_ref() .as_ref()
.map(|ast_trait_ref| { .map(|ast_trait_ref| {
let selfty = tcx.type_of(def_id).instantiate_identity(); let selfty = tcx.type_of(def_id).instantiate_identity();
if let Some(ErrorGuaranteed { .. }) = check_impl_constness( let impl_trait_ref = if let Some(ErrorGuaranteed { .. }) = check_impl_constness(
tcx, tcx,
tcx.is_const_trait_impl_raw(def_id.to_def_id()), tcx.is_const_trait_impl_raw(def_id.to_def_id()),
ast_trait_ref, ast_trait_ref,
@ -1360,9 +1361,9 @@ fn impl_trait_ref(
icx.astconv().instantiate_mono_trait_ref(trait_ref, selfty) icx.astconv().instantiate_mono_trait_ref(trait_ref, selfty)
} else { } else {
icx.astconv().instantiate_mono_trait_ref(ast_trait_ref, selfty) icx.astconv().instantiate_mono_trait_ref(ast_trait_ref, selfty)
} };
(ty::EarlyBinder::bind(impl_trait_ref), polarity_of_impl(tcx, def_id, impl_, item.span))
}) })
.map(ty::EarlyBinder::bind)
} }
fn check_impl_constness( fn check_impl_constness(
@ -1391,42 +1392,38 @@ fn check_impl_constness(
} }
fn impl_polarity(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::ImplPolarity { fn impl_polarity(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::ImplPolarity {
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
let item = tcx.hir().expect_item(def_id); let item = tcx.hir().expect_item(def_id);
match &item.kind { polarity_of_impl(tcx, def_id, item.expect_impl(), item.span)
hir::ItemKind::Impl(hir::Impl { }
polarity: hir::ImplPolarity::Negative(span),
of_trait, fn polarity_of_impl(
.. tcx: TyCtxt<'_>,
}) => { def_id: LocalDefId,
impl_: &hir::Impl<'_>,
span: Span,
) -> ty::ImplPolarity {
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
match &impl_ {
hir::Impl { polarity: hir::ImplPolarity::Negative(span), of_trait, .. } => {
if is_rustc_reservation { if is_rustc_reservation {
let span = span.to(of_trait.as_ref().map_or(*span, |t| t.path.span)); let span = span.to(of_trait.as_ref().map_or(*span, |t| t.path.span));
tcx.dcx().span_err(span, "reservation impls can't be negative"); tcx.dcx().span_err(span, "reservation impls can't be negative");
} }
ty::ImplPolarity::Negative ty::ImplPolarity::Negative
} }
hir::ItemKind::Impl(hir::Impl { hir::Impl { polarity: hir::ImplPolarity::Positive, of_trait: None, .. } => {
polarity: hir::ImplPolarity::Positive,
of_trait: None,
..
}) => {
if is_rustc_reservation { if is_rustc_reservation {
tcx.dcx().span_err(item.span, "reservation impls can't be inherent"); tcx.dcx().span_err(span, "reservation impls can't be inherent");
} }
ty::ImplPolarity::Positive ty::ImplPolarity::Positive
} }
hir::ItemKind::Impl(hir::Impl { hir::Impl { polarity: hir::ImplPolarity::Positive, of_trait: Some(_), .. } => {
polarity: hir::ImplPolarity::Positive,
of_trait: Some(_),
..
}) => {
if is_rustc_reservation { if is_rustc_reservation {
ty::ImplPolarity::Reservation ty::ImplPolarity::Reservation
} else { } else {
ty::ImplPolarity::Positive ty::ImplPolarity::Positive
} }
} }
item => bug!("impl_polarity: {:?} not an impl", item),
} }
} }

View File

@ -63,7 +63,7 @@ const BASE_HIR: &[&str] = &[
/// `impl` implementation of struct/trait /// `impl` implementation of struct/trait
const BASE_IMPL: &[&str] = const BASE_IMPL: &[&str] =
&[label_strs::associated_item_def_ids, label_strs::generics_of, label_strs::impl_trait_ref]; &[label_strs::associated_item_def_ids, label_strs::generics_of, label_strs::impl_trait_header];
/// DepNodes for mir_built/Optimized, which is relevant in "executable" /// DepNodes for mir_built/Optimized, which is relevant in "executable"
/// code, i.e., functions+methods /// code, i.e., functions+methods

View File

@ -215,7 +215,7 @@ provide! { tcx, def_id, other, cdata,
variances_of => { table } variances_of => { table }
fn_sig => { table } fn_sig => { table }
codegen_fn_attrs => { table } codegen_fn_attrs => { table }
impl_trait_ref => { table } impl_trait_header => { table }
const_param_default => { table } const_param_default => { table }
object_lifetime_default => { table } object_lifetime_default => { table }
thir_abstract_const => { table } thir_abstract_const => { table }

View File

@ -1971,8 +1971,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
self.tables.defaultness.set_some(def_id.index, tcx.defaultness(def_id)); self.tables.defaultness.set_some(def_id.index, tcx.defaultness(def_id));
self.tables.impl_polarity.set_some(def_id.index, tcx.impl_polarity(def_id)); self.tables.impl_polarity.set_some(def_id.index, tcx.impl_polarity(def_id));
if of_trait && let Some(trait_ref) = tcx.impl_trait_ref(def_id) { if of_trait && let Some(header) = tcx.impl_trait_header(def_id) {
record!(self.tables.impl_trait_ref[def_id] <- trait_ref); record!(self.tables.impl_trait_header[def_id] <- header);
let (trait_ref, _polarity) = header;
let trait_ref = trait_ref.instantiate_identity(); let trait_ref = trait_ref.instantiate_identity();
let simplified_self_ty = fast_reject::simplify_type( let simplified_self_ty = fast_reject::simplify_type(

View File

@ -423,7 +423,7 @@ define_tables! {
variances_of: Table<DefIndex, LazyArray<ty::Variance>>, variances_of: Table<DefIndex, LazyArray<ty::Variance>>,
fn_sig: Table<DefIndex, LazyValue<ty::EarlyBinder<ty::PolyFnSig<'static>>>>, fn_sig: Table<DefIndex, LazyValue<ty::EarlyBinder<ty::PolyFnSig<'static>>>>,
codegen_fn_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>, codegen_fn_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>,
impl_trait_ref: Table<DefIndex, LazyValue<ty::EarlyBinder<ty::TraitRef<'static>>>>, impl_trait_header: Table<DefIndex, LazyValue<(ty::EarlyBinder<ty::TraitRef<'static>>, ty::ImplPolarity)>>,
const_param_default: Table<DefIndex, LazyValue<ty::EarlyBinder<rustc_middle::ty::Const<'static>>>>, const_param_default: Table<DefIndex, LazyValue<ty::EarlyBinder<rustc_middle::ty::Const<'static>>>>,
object_lifetime_default: Table<DefIndex, LazyValue<ObjectLifetimeDefault>>, object_lifetime_default: Table<DefIndex, LazyValue<ObjectLifetimeDefault>>,
optimized_mir: Table<DefIndex, LazyValue<mir::Body<'static>>>, optimized_mir: Table<DefIndex, LazyValue<mir::Body<'static>>>,

View File

@ -177,8 +177,9 @@ impl EraseType for Option<mir::DestructuredConstant<'_>> {
type Result = [u8; size_of::<Option<mir::DestructuredConstant<'static>>>()]; type Result = [u8; size_of::<Option<mir::DestructuredConstant<'static>>>()];
} }
impl EraseType for Option<ty::EarlyBinder<ty::TraitRef<'_>>> { impl EraseType for Option<(ty::EarlyBinder<ty::TraitRef<'_>>, ty::ImplPolarity)> {
type Result = [u8; size_of::<Option<ty::EarlyBinder<ty::TraitRef<'static>>>>()]; type Result =
[u8; size_of::<Option<(ty::EarlyBinder<ty::TraitRef<'static>>, ty::ImplPolarity)>>()];
} }
impl EraseType for Option<ty::EarlyBinder<Ty<'_>>> { impl EraseType for Option<ty::EarlyBinder<Ty<'_>>> {

View File

@ -846,9 +846,9 @@ rustc_queries! {
cache_on_disk_if { true } cache_on_disk_if { true }
} }
/// Given an `impl_id`, return the trait it implements. /// Given an `impl_id`, return the trait it implements along with some header information.
/// Return `None` if this is an inherent impl. /// Return `None` if this is an inherent impl.
query impl_trait_ref(impl_id: DefId) -> Option<ty::EarlyBinder<ty::TraitRef<'tcx>>> { query impl_trait_header(impl_id: DefId) -> Option<(ty::EarlyBinder<ty::TraitRef<'tcx>>, ty::ImplPolarity)> {
desc { |tcx| "computing trait implemented by `{}`", tcx.def_path_str(impl_id) } desc { |tcx| "computing trait implemented by `{}`", tcx.def_path_str(impl_id) }
cache_on_disk_if { impl_id.is_local() } cache_on_disk_if { impl_id.is_local() }
separate_provide_extern separate_provide_extern

View File

@ -2308,6 +2308,15 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn module_children_local(self, def_id: LocalDefId) -> &'tcx [ModChild] { pub fn module_children_local(self, def_id: LocalDefId) -> &'tcx [ModChild] {
self.resolutions(()).module_children.get(&def_id).map_or(&[], |v| &v[..]) self.resolutions(()).module_children.get(&def_id).map_or(&[], |v| &v[..])
} }
/// Given an `impl_id`, return the trait it implements.
/// Return `None` if this is an inherent impl.
pub fn impl_trait_ref(
self,
def_id: impl IntoQueryParam<DefId>,
) -> Option<ty::EarlyBinder<ty::TraitRef<'tcx>>> {
Some(self.impl_trait_header(def_id)?.0)
}
} }
/// Parameter attributes that can only be determined by examining the body of a function instead /// Parameter attributes that can only be determined by examining the body of a function instead

View File

@ -462,9 +462,9 @@ impl AddTypeParameterToImpl<u32> for Bar<u32> {
} }
#[cfg(not(any(cfail1,cfail4)))] #[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(except="opt_hir_owner_nodes,generics_of,impl_trait_ref", cfg="cfail2")] #[rustc_clean(except="opt_hir_owner_nodes,generics_of,impl_trait_header", cfg="cfail2")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
#[rustc_clean(except="opt_hir_owner_nodes,generics_of,impl_trait_ref", cfg="cfail5")] #[rustc_clean(except="opt_hir_owner_nodes,generics_of,impl_trait_header", cfg="cfail5")]
#[rustc_clean(cfg="cfail6")] #[rustc_clean(cfg="cfail6")]
impl<TTT> AddTypeParameterToImpl<TTT> for Bar<TTT> { impl<TTT> AddTypeParameterToImpl<TTT> for Bar<TTT> {
#[rustc_clean( #[rustc_clean(
@ -493,9 +493,9 @@ impl ChangeSelfTypeOfImpl for u32 {
} }
#[cfg(not(any(cfail1,cfail4)))] #[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(except="opt_hir_owner_nodes,impl_trait_ref", cfg="cfail2")] #[rustc_clean(except="opt_hir_owner_nodes,impl_trait_header", cfg="cfail2")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
#[rustc_clean(except="opt_hir_owner_nodes,impl_trait_ref", cfg="cfail5")] #[rustc_clean(except="opt_hir_owner_nodes,impl_trait_header", cfg="cfail5")]
#[rustc_clean(cfg="cfail6")] #[rustc_clean(cfg="cfail6")]
impl ChangeSelfTypeOfImpl for u64 { impl ChangeSelfTypeOfImpl for u64 {
#[rustc_clean(except="fn_sig,typeck,optimized_mir", cfg="cfail2")] #[rustc_clean(except="fn_sig,typeck,optimized_mir", cfg="cfail2")]