From 916951efcc51bb7c225a0dba676c34adef620f1d Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Sat, 10 Feb 2024 21:26:48 +0000 Subject: [PATCH 1/6] Make impl_trait_ref into a query also returning more information about the impl --- compiler/rustc_hir_analysis/src/collect.rs | 51 +++++++++---------- .../src/persist/dirty_clean.rs | 2 +- .../src/rmeta/decoder/cstore_impl.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 5 +- compiler/rustc_metadata/src/rmeta/mod.rs | 2 +- compiler/rustc_middle/src/query/erase.rs | 5 +- compiler/rustc_middle/src/query/mod.rs | 4 +- compiler/rustc_middle/src/ty/context.rs | 9 ++++ tests/incremental/hashes/trait_impls.rs | 8 +-- 9 files changed, 48 insertions(+), 40 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index f458ff01c10..0f0facdca65 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -78,7 +78,7 @@ pub fn provide(providers: &mut Providers) { trait_def, adt_def, fn_sig, - impl_trait_ref, + impl_trait_header, impl_polarity, coroutine_kind, coroutine_for_closure, @@ -598,7 +598,7 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) { hir::ItemKind::Impl { .. } => { tcx.ensure().generics_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); } hir::ItemKind::Trait(..) => { @@ -1323,19 +1323,20 @@ fn suggest_impl_trait<'tcx>( None } -fn impl_trait_ref( +fn impl_trait_header( tcx: TyCtxt<'_>, def_id: LocalDefId, -) -> Option>> { +) -> Option<(ty::EarlyBinder>, ty::ImplPolarity)> { 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_ .of_trait .as_ref() .map(|ast_trait_ref| { 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.is_const_trait_impl_raw(def_id.to_def_id()), ast_trait_ref, @@ -1360,9 +1361,9 @@ fn impl_trait_ref( icx.astconv().instantiate_mono_trait_ref(trait_ref, selfty) } else { 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( @@ -1391,42 +1392,38 @@ fn check_impl_constness( } 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); - match &item.kind { - hir::ItemKind::Impl(hir::Impl { - polarity: hir::ImplPolarity::Negative(span), - of_trait, - .. - }) => { + polarity_of_impl(tcx, def_id, item.expect_impl(), item.span) +} + +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 { 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"); } ty::ImplPolarity::Negative } - hir::ItemKind::Impl(hir::Impl { - polarity: hir::ImplPolarity::Positive, - of_trait: None, - .. - }) => { + hir::Impl { polarity: hir::ImplPolarity::Positive, of_trait: None, .. } => { 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 } - hir::ItemKind::Impl(hir::Impl { - polarity: hir::ImplPolarity::Positive, - of_trait: Some(_), - .. - }) => { + hir::Impl { polarity: hir::ImplPolarity::Positive, of_trait: Some(_), .. } => { if is_rustc_reservation { ty::ImplPolarity::Reservation } else { ty::ImplPolarity::Positive } } - item => bug!("impl_polarity: {:?} not an impl", item), } } diff --git a/compiler/rustc_incremental/src/persist/dirty_clean.rs b/compiler/rustc_incremental/src/persist/dirty_clean.rs index 14cc8c260e2..8311a735133 100644 --- a/compiler/rustc_incremental/src/persist/dirty_clean.rs +++ b/compiler/rustc_incremental/src/persist/dirty_clean.rs @@ -63,7 +63,7 @@ const BASE_HIR: &[&str] = &[ /// `impl` implementation of struct/trait 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" /// code, i.e., functions+methods diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 988388edfd5..058f28a3ee9 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -215,7 +215,7 @@ provide! { tcx, def_id, other, cdata, variances_of => { table } fn_sig => { table } codegen_fn_attrs => { table } - impl_trait_ref => { table } + impl_trait_header => { table } const_param_default => { table } object_lifetime_default => { table } thir_abstract_const => { table } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 6ca1973396f..361c0a3f87e 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1971,8 +1971,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { 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)); - if of_trait && let Some(trait_ref) = tcx.impl_trait_ref(def_id) { - record!(self.tables.impl_trait_ref[def_id] <- trait_ref); + if of_trait && let Some(header) = tcx.impl_trait_header(def_id) { + record!(self.tables.impl_trait_header[def_id] <- header); + let (trait_ref, _polarity) = header; let trait_ref = trait_ref.instantiate_identity(); let simplified_self_ty = fast_reject::simplify_type( diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 8205e995c19..f2d4ae11105 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -423,7 +423,7 @@ define_tables! { variances_of: Table>, fn_sig: Table>>>, codegen_fn_attrs: Table>, - impl_trait_ref: Table>>>, + impl_trait_header: Table>, ty::ImplPolarity)>>, const_param_default: Table>>>, object_lifetime_default: Table>, optimized_mir: Table>>, diff --git a/compiler/rustc_middle/src/query/erase.rs b/compiler/rustc_middle/src/query/erase.rs index a272a51f327..e15a051b33f 100644 --- a/compiler/rustc_middle/src/query/erase.rs +++ b/compiler/rustc_middle/src/query/erase.rs @@ -177,8 +177,9 @@ impl EraseType for Option> { type Result = [u8; size_of::>>()]; } -impl EraseType for Option>> { - type Result = [u8; size_of::>>>()]; +impl EraseType for Option<(ty::EarlyBinder>, ty::ImplPolarity)> { + type Result = + [u8; size_of::>, ty::ImplPolarity)>>()]; } impl EraseType for Option>> { diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 938fba0ed09..59fb198a3da 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -846,9 +846,9 @@ rustc_queries! { 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. - query impl_trait_ref(impl_id: DefId) -> Option>> { + query impl_trait_header(impl_id: DefId) -> Option<(ty::EarlyBinder>, ty::ImplPolarity)> { desc { |tcx| "computing trait implemented by `{}`", tcx.def_path_str(impl_id) } cache_on_disk_if { impl_id.is_local() } separate_provide_extern diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index b747f0a4fb6..c3b69cee9ed 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2308,6 +2308,15 @@ impl<'tcx> TyCtxt<'tcx> { pub fn module_children_local(self, def_id: LocalDefId) -> &'tcx [ModChild] { 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, + ) -> Option>> { + Some(self.impl_trait_header(def_id)?.0) + } } /// Parameter attributes that can only be determined by examining the body of a function instead diff --git a/tests/incremental/hashes/trait_impls.rs b/tests/incremental/hashes/trait_impls.rs index 2e97a35d36b..e103be0a085 100644 --- a/tests/incremental/hashes/trait_impls.rs +++ b/tests/incremental/hashes/trait_impls.rs @@ -462,9 +462,9 @@ impl AddTypeParameterToImpl for Bar { } #[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(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")] impl AddTypeParameterToImpl for Bar { #[rustc_clean( @@ -493,9 +493,9 @@ impl ChangeSelfTypeOfImpl for u32 { } #[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(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")] impl ChangeSelfTypeOfImpl for u64 { #[rustc_clean(except="fn_sig,typeck,optimized_mir", cfg="cfail2")] From 90a43f14063c601fa6a3791aac092bfc2e094911 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Sat, 10 Feb 2024 21:46:44 +0000 Subject: [PATCH 2/6] Use a struct instead of a tuple --- compiler/rustc_hir_analysis/src/collect.rs | 9 ++++++--- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- compiler/rustc_metadata/src/rmeta/mod.rs | 2 +- compiler/rustc_middle/src/query/erase.rs | 5 ++--- compiler/rustc_middle/src/query/mod.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 2 +- compiler/rustc_middle/src/ty/mod.rs | 6 ++++++ compiler/rustc_middle/src/ty/parameterized.rs | 1 + 8 files changed, 19 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 0f0facdca65..67d5064ff02 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -1326,7 +1326,7 @@ fn suggest_impl_trait<'tcx>( fn impl_trait_header( tcx: TyCtxt<'_>, def_id: LocalDefId, -) -> Option<(ty::EarlyBinder>, ty::ImplPolarity)> { +) -> Option>> { let icx = ItemCtxt::new(tcx, def_id); let item = tcx.hir().expect_item(def_id); let impl_ = item.expect_impl(); @@ -1336,7 +1336,7 @@ fn impl_trait_header( .map(|ast_trait_ref| { let selfty = tcx.type_of(def_id).instantiate_identity(); - let impl_trait_ref = if let Some(ErrorGuaranteed { .. }) = check_impl_constness( + let trait_ref = if let Some(ErrorGuaranteed { .. }) = check_impl_constness( tcx, tcx.is_const_trait_impl_raw(def_id.to_def_id()), ast_trait_ref, @@ -1362,7 +1362,10 @@ fn impl_trait_header( } else { 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)) + ty::EarlyBinder::bind(ty::ImplTraitHeader { + trait_ref, + polarity: polarity_of_impl(tcx, def_id, impl_, item.span) + }) }) } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 361c0a3f87e..5630566676b 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1973,7 +1973,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { if of_trait && let Some(header) = tcx.impl_trait_header(def_id) { record!(self.tables.impl_trait_header[def_id] <- header); - let (trait_ref, _polarity) = header; + let trait_ref = header.map_bound(|h| h.trait_ref); let trait_ref = trait_ref.instantiate_identity(); let simplified_self_ty = fast_reject::simplify_type( diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index f2d4ae11105..88a749da156 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -423,7 +423,7 @@ define_tables! { variances_of: Table>, fn_sig: Table>>>, codegen_fn_attrs: Table>, - impl_trait_header: Table>, ty::ImplPolarity)>>, + impl_trait_header: Table>>>, const_param_default: Table>>>, object_lifetime_default: Table>, optimized_mir: Table>>, diff --git a/compiler/rustc_middle/src/query/erase.rs b/compiler/rustc_middle/src/query/erase.rs index e15a051b33f..6bd73e9b203 100644 --- a/compiler/rustc_middle/src/query/erase.rs +++ b/compiler/rustc_middle/src/query/erase.rs @@ -177,9 +177,8 @@ impl EraseType for Option> { type Result = [u8; size_of::>>()]; } -impl EraseType for Option<(ty::EarlyBinder>, ty::ImplPolarity)> { - type Result = - [u8; size_of::>, ty::ImplPolarity)>>()]; +impl EraseType for Option>> { + type Result = [u8; size_of::>>>()]; } impl EraseType for Option>> { diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 59fb198a3da..2f947f96c26 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -848,7 +848,7 @@ rustc_queries! { /// Given an `impl_id`, return the trait it implements along with some header information. /// Return `None` if this is an inherent impl. - query impl_trait_header(impl_id: DefId) -> Option<(ty::EarlyBinder>, ty::ImplPolarity)> { + query impl_trait_header(impl_id: DefId) -> Option>> { desc { |tcx| "computing trait implemented by `{}`", tcx.def_path_str(impl_id) } cache_on_disk_if { impl_id.is_local() } separate_provide_extern diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index c3b69cee9ed..7c91da292cb 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2315,7 +2315,7 @@ impl<'tcx> TyCtxt<'tcx> { self, def_id: impl IntoQueryParam, ) -> Option>> { - Some(self.impl_trait_header(def_id)?.0) + Some(self.impl_trait_header(def_id)?.map_bound(|h| h.trait_ref)) } } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index c9137f374a2..e41368957c7 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -248,6 +248,12 @@ pub struct ImplHeader<'tcx> { pub predicates: Vec>, } +#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, TyEncodable, TyDecodable, HashStable)] +pub struct ImplTraitHeader<'tcx> { + pub trait_ref: ty::TraitRef<'tcx>, + pub polarity: ImplPolarity, +} + #[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable)] pub enum ImplSubject<'tcx> { Trait(TraitRef<'tcx>), diff --git a/compiler/rustc_middle/src/ty/parameterized.rs b/compiler/rustc_middle/src/ty/parameterized.rs index 47f9d9e61ad..045856dd9cd 100644 --- a/compiler/rustc_middle/src/ty/parameterized.rs +++ b/compiler/rustc_middle/src/ty/parameterized.rs @@ -134,4 +134,5 @@ parameterized_over_tcx! { ty::Predicate, ty::Clause, ty::ClauseKind, + ty::ImplTraitHeader } From a9e0e968be44160eae362ff5272ebf91eba7b214 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Sat, 10 Feb 2024 22:04:46 +0000 Subject: [PATCH 3/6] Unwrap an Option that can only be Some, as inherent impls can't overlap --- compiler/rustc_middle/src/ty/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index e41368957c7..574ca0ee7eb 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1590,12 +1590,12 @@ impl<'tcx> TyCtxt<'tcx> { def_id1: DefId, def_id2: DefId, ) -> Option { - let impl_trait_ref1 = self.impl_trait_ref(def_id1); - let impl_trait_ref2 = self.impl_trait_ref(def_id2); + let impl_trait_ref1 = self.impl_trait_ref(def_id1).unwrap(); + let impl_trait_ref2 = self.impl_trait_ref(def_id2).unwrap(); // If either trait impl references an error, they're allowed to overlap, // as one of them essentially doesn't exist. - if impl_trait_ref1.is_some_and(|tr| tr.instantiate_identity().references_error()) - || impl_trait_ref2.is_some_and(|tr| tr.instantiate_identity().references_error()) + if impl_trait_ref1.instantiate_identity().references_error() + || impl_trait_ref2.instantiate_identity().references_error() { return Some(ImplOverlapKind::Permitted { marker: false }); } @@ -1615,8 +1615,8 @@ impl<'tcx> TyCtxt<'tcx> { }; let is_marker_overlap = { - let is_marker_impl = |trait_ref: Option>>| -> bool { - trait_ref.is_some_and(|tr| self.trait_def(tr.skip_binder().def_id).is_marker) + let is_marker_impl = |trait_ref: EarlyBinder>| -> bool { + self.trait_def(trait_ref.skip_binder().def_id).is_marker }; is_marker_impl(impl_trait_ref1) && is_marker_impl(impl_trait_ref2) }; From ab0e8b314507cdda07a54f8b8de6a7a6a68e6ce4 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Sat, 10 Feb 2024 22:06:58 +0000 Subject: [PATCH 4/6] Eagerly dismiss binder --- compiler/rustc_middle/src/ty/mod.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 574ca0ee7eb..a45cd9aa396 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1590,13 +1590,11 @@ impl<'tcx> TyCtxt<'tcx> { def_id1: DefId, def_id2: DefId, ) -> Option { - let impl_trait_ref1 = self.impl_trait_ref(def_id1).unwrap(); - let impl_trait_ref2 = self.impl_trait_ref(def_id2).unwrap(); + let impl_trait_ref1 = self.impl_trait_ref(def_id1).unwrap().instantiate_identity(); + let impl_trait_ref2 = self.impl_trait_ref(def_id2).unwrap().instantiate_identity(); // If either trait impl references an error, they're allowed to overlap, // as one of them essentially doesn't exist. - if impl_trait_ref1.instantiate_identity().references_error() - || impl_trait_ref2.instantiate_identity().references_error() - { + if impl_trait_ref1.references_error() || impl_trait_ref2.references_error() { return Some(ImplOverlapKind::Permitted { marker: false }); } @@ -1615,9 +1613,8 @@ impl<'tcx> TyCtxt<'tcx> { }; let is_marker_overlap = { - let is_marker_impl = |trait_ref: EarlyBinder>| -> bool { - self.trait_def(trait_ref.skip_binder().def_id).is_marker - }; + let is_marker_impl = + |trait_ref: TraitRef<'_>| -> bool { self.trait_def(trait_ref.def_id).is_marker }; is_marker_impl(impl_trait_ref1) && is_marker_impl(impl_trait_ref2) }; From b43fbe63e7675a3c8d762127f450a62def4f653e Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Sat, 10 Feb 2024 22:27:03 +0000 Subject: [PATCH 5/6] Stop calling `impl_polarity` when `impl_trait_ref` was also called --- .../rustc_hir_analysis/src/astconv/mod.rs | 19 +++++++-------- .../rustc_hir_analysis/src/check/check.rs | 24 +++++++++++-------- .../rustc_hir_analysis/src/check/wfcheck.rs | 15 ++++++------ .../rustc_hir_typeck/src/method/suggest.rs | 14 +++++++---- compiler/rustc_middle/src/ty/mod.rs | 11 +++++---- compiler/rustc_monomorphize/src/collector.rs | 13 +++++----- .../src/solve/trait_goals.rs | 11 +++++---- .../error_reporting/type_err_ctxt_ext.rs | 15 ++++++------ .../src/traits/select/candidate_assembly.rs | 10 ++++---- .../src/traits/select/mod.rs | 18 +++++++------- .../src/traits/specialize/mod.rs | 16 +++++++++---- compiler/rustc_ty_utils/src/ty.rs | 7 +++--- 12 files changed, 96 insertions(+), 77 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 1ae3ebaebbb..657de8ae9ad 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -1671,9 +1671,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { .is_accessible_from(self.item_def_id(), tcx) && tcx.all_impls(*trait_def_id) .any(|impl_def_id| { - let trait_ref = tcx.impl_trait_ref(impl_def_id); - trait_ref.is_some_and(|trait_ref| { - let impl_ = trait_ref.instantiate( + let impl_header = tcx.impl_trait_header(impl_def_id); + impl_header.is_some_and(|header| { + let header = header.instantiate( tcx, infcx.fresh_args_for_item(DUMMY_SP, impl_def_id), ); @@ -1685,11 +1685,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { infcx .can_eq( ty::ParamEnv::empty(), - impl_.self_ty(), + header.trait_ref.self_ty(), value, - ) + ) && header.polarity != ty::ImplPolarity::Negative }) - && tcx.impl_polarity(impl_def_id) != ty::ImplPolarity::Negative }) }) .map(|trait_def_id| tcx.def_path_str(trait_def_id)) @@ -1735,13 +1734,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } else { // Find all the types that have an `impl` for the trait. tcx.all_impls(trait_def_id) - .filter(|impl_def_id| { + .filter_map(|impl_def_id| tcx.impl_trait_header(impl_def_id)) + .filter(|header| { // Consider only accessible traits tcx.visibility(trait_def_id).is_accessible_from(self.item_def_id(), tcx) - && tcx.impl_polarity(impl_def_id) != ty::ImplPolarity::Negative + && header.skip_binder().polarity != ty::ImplPolarity::Negative }) - .filter_map(|impl_def_id| tcx.impl_trait_ref(impl_def_id)) - .map(|impl_| impl_.instantiate_identity().self_ty()) + .map(|header| header.instantiate_identity().trait_ref.self_ty()) // We don't care about blanket impls. .filter(|self_ty| !self_ty.has_non_region_param()) .map(|self_ty| tcx.erase_regions(self_ty).to_string()) diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 7250dc81faf..2bdf8f9326d 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -474,8 +474,12 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) { } DefKind::Fn => {} // entirely within check_item_body DefKind::Impl { of_trait } => { - if of_trait && let Some(impl_trait_ref) = tcx.impl_trait_ref(def_id) { - check_impl_items_against_trait(tcx, def_id, impl_trait_ref.instantiate_identity()); + if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) { + check_impl_items_against_trait( + tcx, + def_id, + impl_trait_header.instantiate_identity(), + ); check_on_unimplemented(tcx, def_id); } } @@ -666,19 +670,19 @@ pub(super) fn check_specialization_validity<'tcx>( fn check_impl_items_against_trait<'tcx>( tcx: TyCtxt<'tcx>, impl_id: LocalDefId, - impl_trait_ref: ty::TraitRef<'tcx>, + impl_trait_header: ty::ImplTraitHeader<'tcx>, ) { // If the trait reference itself is erroneous (so the compilation is going // to fail), skip checking the items here -- the `impl_item` table in `tcx` // isn't populated for such impls. - if impl_trait_ref.references_error() { + if impl_trait_header.references_error() { return; } let impl_item_refs = tcx.associated_item_def_ids(impl_id); // Negative impls are not expected to have any items - match tcx.impl_polarity(impl_id) { + match impl_trait_header.polarity { ty::ImplPolarity::Reservation | ty::ImplPolarity::Positive => {} ty::ImplPolarity::Negative => { if let [first_item_ref, ..] = impl_item_refs { @@ -695,7 +699,7 @@ fn check_impl_items_against_trait<'tcx>( } } - let trait_def = tcx.trait_def(impl_trait_ref.def_id); + let trait_def = tcx.trait_def(impl_trait_header.trait_ref.def_id); for &impl_item in impl_item_refs { let ty_impl_item = tcx.associated_item(impl_item); @@ -714,10 +718,10 @@ fn check_impl_items_against_trait<'tcx>( )); } ty::AssocKind::Fn => { - compare_impl_method(tcx, ty_impl_item, ty_trait_item, impl_trait_ref); + compare_impl_method(tcx, ty_impl_item, ty_trait_item, impl_trait_header.trait_ref); } ty::AssocKind::Type => { - compare_impl_ty(tcx, ty_impl_item, ty_trait_item, impl_trait_ref); + compare_impl_ty(tcx, ty_impl_item, ty_trait_item, impl_trait_header.trait_ref); } } @@ -737,7 +741,7 @@ fn check_impl_items_against_trait<'tcx>( let mut must_implement_one_of: Option<&[Ident]> = trait_def.must_implement_one_of.as_deref(); - for &trait_item_id in tcx.associated_item_def_ids(impl_trait_ref.def_id) { + for &trait_item_id in tcx.associated_item_def_ids(impl_trait_header.trait_ref.def_id) { let leaf_def = ancestors.leaf_def(tcx, trait_item_id); let is_implemented = leaf_def @@ -815,7 +819,7 @@ fn check_impl_items_against_trait<'tcx>( if let Some(missing_items) = must_implement_one_of { let attr_span = tcx - .get_attr(impl_trait_ref.def_id, sym::rustc_must_implement_one_of) + .get_attr(impl_trait_header.trait_ref.def_id, sym::rustc_must_implement_one_of) .map(|attr| attr.span); missing_items_must_implement_one_of_err( diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 646a84b043c..6eba853af30 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -245,9 +245,9 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<() // won't be allowed unless there's an *explicit* implementation of `Send` // for `T` hir::ItemKind::Impl(impl_) => { - let is_auto = tcx - .impl_trait_ref(def_id) - .is_some_and(|trait_ref| tcx.trait_is_auto(trait_ref.skip_binder().def_id)); + let header = tcx.impl_trait_header(def_id); + let is_auto = header + .is_some_and(|header| tcx.trait_is_auto(header.skip_binder().trait_ref.def_id)); let mut res = Ok(()); if let (hir::Defaultness::Default { .. }, true) = (impl_.defaultness, is_auto) { let sp = impl_.of_trait.as_ref().map_or(item.span, |t| t.path.span); @@ -259,11 +259,12 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<() .emit()); } // We match on both `ty::ImplPolarity` and `ast::ImplPolarity` just to get the `!` span. - match tcx.impl_polarity(def_id) { - ty::ImplPolarity::Positive => { + match header.map(|h| h.skip_binder().polarity) { + // `None` means this is an inherent impl + Some(ty::ImplPolarity::Positive) | None => { res = res.and(check_impl(tcx, item, impl_.self_ty, &impl_.of_trait)); } - ty::ImplPolarity::Negative => { + Some(ty::ImplPolarity::Negative) => { let ast::ImplPolarity::Negative(span) = impl_.polarity else { bug!("impl_polarity query disagrees with impl's polarity in AST"); }; @@ -280,7 +281,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<() .emit()); } } - ty::ImplPolarity::Reservation => { + Some(ty::ImplPolarity::Reservation) => { // FIXME: what amount of WF checking do we need for reservation impls? } } diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 7fc51e36a2b..ab5c0f32705 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -3134,12 +3134,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if self .tcx .all_impls(candidate.def_id) - .filter(|imp_did| { - self.tcx.impl_polarity(*imp_did) == ty::ImplPolarity::Negative + .map(|imp_did| { + self.tcx.impl_trait_header(imp_did).expect( + "inherent impls can't be candidates, only trait impls can be", + ) }) - .any(|imp_did| { - let imp = - self.tcx.impl_trait_ref(imp_did).unwrap().instantiate_identity(); + .filter(|header| { + header.skip_binder().polarity == ty::ImplPolarity::Negative + }) + .any(|header| { + let imp = header.instantiate_identity().trait_ref; let imp_simp = simplify_type(self.tcx, imp.self_ty(), TreatParams::ForLookup); imp_simp.is_some_and(|s| s == simp_rcvr_ty) diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index a45cd9aa396..e64ebae255f 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1590,15 +1590,16 @@ impl<'tcx> TyCtxt<'tcx> { def_id1: DefId, def_id2: DefId, ) -> Option { - let impl_trait_ref1 = self.impl_trait_ref(def_id1).unwrap().instantiate_identity(); - let impl_trait_ref2 = self.impl_trait_ref(def_id2).unwrap().instantiate_identity(); + let impl1 = self.impl_trait_header(def_id1).unwrap().instantiate_identity(); + let impl2 = self.impl_trait_header(def_id2).unwrap().instantiate_identity(); + // If either trait impl references an error, they're allowed to overlap, // as one of them essentially doesn't exist. - if impl_trait_ref1.references_error() || impl_trait_ref2.references_error() { + if impl1.references_error() || impl2.references_error() { return Some(ImplOverlapKind::Permitted { marker: false }); } - match (self.impl_polarity(def_id1), self.impl_polarity(def_id2)) { + match (impl1.polarity, impl2.polarity) { (ImplPolarity::Reservation, _) | (_, ImplPolarity::Reservation) => { // `#[rustc_reservation_impl]` impls don't overlap with anything return Some(ImplOverlapKind::Permitted { marker: false }); @@ -1615,7 +1616,7 @@ impl<'tcx> TyCtxt<'tcx> { let is_marker_overlap = { let is_marker_impl = |trait_ref: TraitRef<'_>| -> bool { self.trait_def(trait_ref.def_id).is_marker }; - is_marker_impl(impl_trait_ref1) && is_marker_impl(impl_trait_ref2) + is_marker_impl(impl1.trait_ref) && is_marker_impl(impl2.trait_ref) }; if is_marker_overlap { diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 149e4c2cb08..e34194f0bb0 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -1347,8 +1347,11 @@ fn create_mono_items_for_default_impls<'tcx>( item: hir::ItemId, output: &mut MonoItems<'tcx>, ) { - let polarity = tcx.impl_polarity(item.owner_id); - if matches!(polarity, ty::ImplPolarity::Negative) { + let Some(impl_) = tcx.impl_trait_header(item.owner_id) else { + return; + }; + + if matches!(impl_.skip_binder().polarity, ty::ImplPolarity::Negative) { return; } @@ -1356,10 +1359,6 @@ fn create_mono_items_for_default_impls<'tcx>( return; } - let Some(trait_ref) = tcx.impl_trait_ref(item.owner_id) else { - return; - }; - // Lifetimes never affect trait selection, so we are allowed to eagerly // instantiate an instance of an impl method if the impl (and method, // which we check below) is only parameterized over lifetime. In that case, @@ -1376,7 +1375,7 @@ fn create_mono_items_for_default_impls<'tcx>( } }; let impl_args = GenericArgs::for_item(tcx, item.owner_id.to_def_id(), only_region_params); - let trait_ref = trait_ref.instantiate(tcx, impl_args); + let trait_ref = impl_.instantiate(tcx, impl_args).trait_ref; // Unlike 'lazy' monomorphization that begins by collecting items transitively // called by `main` or other global items, when eagerly monomorphizing impl diff --git a/compiler/rustc_trait_selection/src/solve/trait_goals.rs b/compiler/rustc_trait_selection/src/solve/trait_goals.rs index 044832224e5..42ae82a907f 100644 --- a/compiler/rustc_trait_selection/src/solve/trait_goals.rs +++ b/compiler/rustc_trait_selection/src/solve/trait_goals.rs @@ -39,15 +39,18 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> { ) -> Result, NoSolution> { let tcx = ecx.tcx(); - let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap(); + let impl_trait_header = tcx.impl_trait_header(impl_def_id).unwrap(); let drcx = DeepRejectCtxt { treat_obligation_params: TreatParams::ForLookup }; - if !drcx.args_may_unify(goal.predicate.trait_ref.args, impl_trait_ref.skip_binder().args) { + if !drcx.args_may_unify( + goal.predicate.trait_ref.args, + impl_trait_header.skip_binder().trait_ref.args, + ) { return Err(NoSolution); } - let impl_polarity = tcx.impl_polarity(impl_def_id); // An upper bound of the certainty of this goal, used to lower the certainty // of reservation impl to ambiguous during coherence. + let impl_polarity = impl_trait_header.skip_binder().polarity; let maximal_certainty = match impl_polarity { ty::ImplPolarity::Positive | ty::ImplPolarity::Negative => { match impl_polarity == goal.predicate.polarity { @@ -63,7 +66,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> { ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| { let impl_args = ecx.fresh_args_for_item(impl_def_id); - let impl_trait_ref = impl_trait_ref.instantiate(tcx, impl_args); + let impl_trait_ref = impl_trait_header.instantiate(tcx, impl_args).trait_ref; ecx.eq(goal.param_env, goal.predicate.trait_ref, impl_trait_ref)?; let where_clause_bounds = tcx diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs index 7d14395850b..8fa5cd5c522 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs @@ -1980,13 +1980,13 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { .tcx .all_impls(trait_pred.def_id()) .filter_map(|def_id| { - if self.tcx.impl_polarity(def_id) == ty::ImplPolarity::Negative + let imp = self.tcx.impl_trait_header(def_id).unwrap().skip_binder(); + if imp.polarity == ty::ImplPolarity::Negative || !self.tcx.is_user_visible_dep(def_id.krate) { return None; } - - let imp = self.tcx.impl_trait_ref(def_id).unwrap().skip_binder(); + let imp = imp.trait_ref; self.fuzzy_match_tys(trait_pred.skip_binder().self_ty(), imp.self_ty(), false).map( |similarity| ImplCandidate { trait_ref: imp, similarity, impl_def_id: def_id }, @@ -2165,12 +2165,13 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { .tcx .all_impls(def_id) // Ignore automatically derived impls and `!Trait` impls. - .filter(|&def_id| { - self.tcx.impl_polarity(def_id) != ty::ImplPolarity::Negative + .filter_map(|def_id| self.tcx.impl_trait_header(def_id)) + .map(ty::EarlyBinder::instantiate_identity) + .filter(|header| { + header.polarity != ty::ImplPolarity::Negative || self.tcx.is_automatically_derived(def_id) }) - .filter_map(|def_id| self.tcx.impl_trait_ref(def_id)) - .map(ty::EarlyBinder::instantiate_identity) + .map(|header| header.trait_ref) .filter(|trait_ref| { let self_ty = trait_ref.self_ty(); // Avoid mentioning type parameters. diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index f9a292c2bd7..b6f672bfabe 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -560,20 +560,22 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // Before we create the substitutions and everything, first // consider a "quick reject". This avoids creating more types // and so forth that we need to. - let impl_trait_ref = self.tcx().impl_trait_ref(impl_def_id).unwrap(); - if !drcx.args_may_unify(obligation_args, impl_trait_ref.skip_binder().args) { + let impl_trait_header = self.tcx().impl_trait_header(impl_def_id).unwrap(); + if !drcx + .args_may_unify(obligation_args, impl_trait_header.skip_binder().trait_ref.args) + { return; } if self.reject_fn_ptr_impls( impl_def_id, obligation, - impl_trait_ref.skip_binder().self_ty(), + impl_trait_header.skip_binder().trait_ref.self_ty(), ) { return; } self.infcx.probe(|_| { - if let Ok(_args) = self.match_impl(impl_def_id, impl_trait_ref, obligation) { + if let Ok(_args) = self.match_impl(impl_def_id, impl_trait_header, obligation) { candidates.vec.push(ImplCandidate(impl_def_id)); } }); diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index ac6cfcdeb59..5f11bb600e3 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -2404,8 +2404,8 @@ impl<'tcx> SelectionContext<'_, 'tcx> { impl_def_id: DefId, obligation: &PolyTraitObligation<'tcx>, ) -> Normalized<'tcx, GenericArgsRef<'tcx>> { - let impl_trait_ref = self.tcx().impl_trait_ref(impl_def_id).unwrap(); - match self.match_impl(impl_def_id, impl_trait_ref, obligation) { + let impl_trait_header = self.tcx().impl_trait_header(impl_def_id).unwrap(); + match self.match_impl(impl_def_id, impl_trait_header, obligation) { Ok(args) => args, Err(()) => { // FIXME: A rematch may fail when a candidate cache hit occurs @@ -2438,7 +2438,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> { fn match_impl( &mut self, impl_def_id: DefId, - impl_trait_ref: EarlyBinder>, + impl_trait_header: EarlyBinder>, obligation: &PolyTraitObligation<'tcx>, ) -> Result>, ()> { let placeholder_obligation = @@ -2447,12 +2447,12 @@ impl<'tcx> SelectionContext<'_, 'tcx> { let impl_args = self.infcx.fresh_args_for_item(obligation.cause.span, impl_def_id); - let impl_trait_ref = impl_trait_ref.instantiate(self.tcx(), impl_args); - if impl_trait_ref.references_error() { + let impl_trait_header = impl_trait_header.instantiate(self.tcx(), impl_args); + if impl_trait_header.references_error() { return Err(()); } - debug!(?impl_trait_ref); + debug!(?impl_trait_header); let Normalized { value: impl_trait_ref, obligations: mut nested_obligations } = ensure_sufficient_stack(|| { @@ -2461,7 +2461,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> { obligation.param_env, obligation.cause.clone(), obligation.recursion_depth + 1, - impl_trait_ref, + impl_trait_header.trait_ref, ) }); @@ -2482,9 +2482,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> { })?; nested_obligations.extend(obligations); - if !self.is_intercrate() - && self.tcx().impl_polarity(impl_def_id) == ty::ImplPolarity::Reservation - { + if !self.is_intercrate() && impl_trait_header.polarity == ty::ImplPolarity::Reservation { debug!("reservation impls only apply in intercrate mode"); return Err(()); } diff --git a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs index 8e0fa79c977..6de839f5d98 100644 --- a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs @@ -168,6 +168,8 @@ pub(super) fn specializes(tcx: TyCtxt<'_>, (impl1_def_id, impl2_def_id): (DefId, } } + let impl1_trait_header = tcx.impl_trait_header(impl1_def_id).unwrap().instantiate_identity(); + // We determine whether there's a subset relationship by: // // - replacing bound vars with placeholders in impl1, @@ -181,21 +183,25 @@ pub(super) fn specializes(tcx: TyCtxt<'_>, (impl1_def_id, impl2_def_id): (DefId, // See RFC 1210 for more details and justification. // Currently we do not allow e.g., a negative impl to specialize a positive one - if tcx.impl_polarity(impl1_def_id) != tcx.impl_polarity(impl2_def_id) { + if impl1_trait_header.polarity != tcx.impl_polarity(impl2_def_id) { return false; } // create a parameter environment corresponding to a (placeholder) instantiation of impl1 let penv = tcx.param_env(impl1_def_id); - let impl1_trait_ref = tcx.impl_trait_ref(impl1_def_id).unwrap().instantiate_identity(); // Create an infcx, taking the predicates of impl1 as assumptions: let infcx = tcx.infer_ctxt().build(); // Attempt to prove that impl2 applies, given all of the above. - fulfill_implication(&infcx, penv, impl1_trait_ref, impl1_def_id, impl2_def_id, |_, _| { - ObligationCause::dummy() - }) + fulfill_implication( + &infcx, + penv, + impl1_trait_header.trait_ref, + impl1_def_id, + impl2_def_id, + |_, _| ObligationCause::dummy(), + ) .is_ok() } diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index 60b1bbe8c2a..b725eda33ce 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -257,14 +257,15 @@ fn param_env_reveal_all_normalized(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamE fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option>> { debug!("issue33140_self_ty({:?})", def_id); - let trait_ref = tcx - .impl_trait_ref(def_id) + let impl_ = tcx + .impl_trait_header(def_id) .unwrap_or_else(|| bug!("issue33140_self_ty called on inherent impl {:?}", def_id)) .skip_binder(); + let trait_ref = impl_.trait_ref; debug!("issue33140_self_ty({:?}), trait-ref={:?}", def_id, trait_ref); - let is_marker_like = tcx.impl_polarity(def_id) == ty::ImplPolarity::Positive + let is_marker_like = impl_.polarity == ty::ImplPolarity::Positive && tcx.associated_item_def_ids(trait_ref.def_id).is_empty(); // Check whether these impls would be ok for a marker trait. From 74c9dffac3d7a4e87bd87815f8a28f1f71e52d08 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Sun, 11 Feb 2024 07:11:48 +0000 Subject: [PATCH 6/6] Remove impl_polarity query --- compiler/rustc_hir_analysis/src/collect.rs | 6 ------ compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs | 1 - compiler/rustc_metadata/src/rmeta/encoder.rs | 1 - compiler/rustc_metadata/src/rmeta/mod.rs | 1 - compiler/rustc_middle/src/query/mod.rs | 4 ---- compiler/rustc_middle/src/ty/context.rs | 5 +++++ 6 files changed, 5 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 67d5064ff02..58fc4ceaa4a 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -79,7 +79,6 @@ pub fn provide(providers: &mut Providers) { adt_def, fn_sig, impl_trait_header, - impl_polarity, coroutine_kind, coroutine_for_closure, collect_mod_item_types, @@ -1394,11 +1393,6 @@ fn check_impl_constness( })) } -fn impl_polarity(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::ImplPolarity { - let item = tcx.hir().expect_item(def_id); - polarity_of_impl(tcx, def_id, item.expect_impl(), item.span) -} - fn polarity_of_impl( tcx: TyCtxt<'_>, def_id: LocalDefId, diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 058f28a3ee9..d79d4b226a5 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -234,7 +234,6 @@ provide! { tcx, def_id, other, cdata, unused_generic_params => { cdata.root.tables.unused_generic_params.get(cdata, def_id.index) } def_kind => { cdata.def_kind(def_id.index) } impl_parent => { table } - impl_polarity => { table_direct } defaultness => { table_direct } constness => { table_direct } coerce_unsized_info => { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 5630566676b..4a24c038f7a 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1969,7 +1969,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let def_id = id.owner_id.to_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)); if of_trait && let Some(header) = tcx.impl_trait_header(def_id) { record!(self.tables.impl_trait_header[def_id] <- header); diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 88a749da156..4d0a6cb60ee 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -433,7 +433,6 @@ define_tables! { promoted_mir: Table>>>, thir_abstract_const: Table>>>, impl_parent: Table, - impl_polarity: Table, constness: Table, defaultness: Table, // FIXME(eddyb) perhaps compute this on the fly if cheap enough? diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 2f947f96c26..63db3250c94 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -853,10 +853,6 @@ rustc_queries! { cache_on_disk_if { impl_id.is_local() } separate_provide_extern } - query impl_polarity(impl_id: DefId) -> ty::ImplPolarity { - desc { |tcx| "computing implementation polarity of `{}`", tcx.def_path_str(impl_id) } - separate_provide_extern - } query issue33140_self_ty(key: DefId) -> Option>> { desc { |tcx| "computing Self type wrt issue #33140 `{}`", tcx.def_path_str(key) } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 7c91da292cb..52db913f36e 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2317,6 +2317,11 @@ impl<'tcx> TyCtxt<'tcx> { ) -> Option>> { Some(self.impl_trait_header(def_id)?.map_bound(|h| h.trait_ref)) } + + pub fn impl_polarity(self, def_id: impl IntoQueryParam) -> ty::ImplPolarity { + self.impl_trait_header(def_id) + .map_or(ty::ImplPolarity::Positive, |h| h.skip_binder().polarity) + } } /// Parameter attributes that can only be determined by examining the body of a function instead