diff options
| author | bors <bors@rust-lang.org> | 2024-12-13 16:17:34 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-12-13 16:17:34 +0000 |
| commit | e217f949179d7337d0cae303881449360937211f (patch) | |
| tree | 07716a920c13cbc769b533d69afb725655cc0163 /compiler/rustc_middle | |
| parent | 4847d6a9d07d4be9ba3196f6ad444af2d7bdde72 (diff) | |
| parent | 2ffe3b1e70a55867de5c85b9cb24cb1a2a3d6162 (diff) | |
| download | rust-e217f949179d7337d0cae303881449360937211f.tar.gz rust-e217f949179d7337d0cae303881449360937211f.zip | |
Auto merge of #134122 - oli-obk:push-zqnyznxtpnll, r=petrochenkov
Move impl constness into impl trait header This PR is kind of the opposite of the rejected https://github.com/rust-lang/rust/pull/134114 Instead of moving more things into the `constness` query, we want to keep them where their corresponding hir nodes are lowered. So I gave this a spin for impls, which have an obvious place to be (the impl trait header). And surprisingly it's also a perf improvement (likely just slightly better query & cache usage). The issue was that removing anything from the `constness` query makes it just return `NotConst`, which is wrong. So I had to change it to `bug!` out if used wrongly, and only then remove the impl blocks from the `constness` query. I think this change is good in general, because it makes using `constness` more robust (as can be seen by how few sites that had to be changed, so it was almost solely used specifically for the purpose of asking for functions' constness). The main thing where this change was not great was in clippy, which was using the `constness` query as a general DefId -> constness map. I added a `DefKind` filter in front of that. If it becomes a more common pattern we can always move that helper into rustc.
Diffstat (limited to 'compiler/rustc_middle')
| -rw-r--r-- | compiler/rustc_middle/src/query/mod.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/context.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 23 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/util.rs | 2 |
4 files changed, 22 insertions, 10 deletions
diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index fc3d690a8a9..a9efa656a66 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -746,7 +746,10 @@ rustc_queries! { desc { |tcx| "computing drop-check constraints for `{}`", tcx.def_path_str(key) } } - /// Returns `true` if this is a const fn / const impl. + /// Returns the constness of function-like things (tuple struct/variant constructors, functions, + /// methods) + /// + /// Will ICE if used on things that are always const or never const. /// /// **Do not call this function manually.** It is only meant to cache the base data for the /// higher-level functions. Consider using `is_const_fn` or `is_const_trait_impl` instead. diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 2841470d248..db1a479f580 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -3141,7 +3141,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Whether the trait impl is marked const. This does not consider stability or feature gates. pub fn is_const_trait_impl(self, def_id: DefId) -> bool { self.def_kind(def_id) == DefKind::Impl { of_trait: true } - && self.constness(def_id) == hir::Constness::Const + && self.impl_trait_header(def_id).unwrap().constness == hir::Constness::Const } pub fn intrinsic(self, def_id: impl IntoQueryParam<DefId> + Copy) -> Option<ty::IntrinsicDef> { diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 80b11892a42..8051aa01715 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -254,6 +254,7 @@ pub struct ImplTraitHeader<'tcx> { pub trait_ref: ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>>, pub polarity: ImplPolarity, pub safety: hir::Safety, + pub constness: hir::Constness, } #[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable)] @@ -2005,17 +2006,25 @@ impl<'tcx> TyCtxt<'tcx> { let def_id: DefId = def_id.into(); match self.def_kind(def_id) { DefKind::Impl { of_trait: true } => { - self.constness(def_id) == hir::Constness::Const - && self.is_const_trait( - self.trait_id_of_impl(def_id) - .expect("expected trait for trait implementation"), - ) + let header = self.impl_trait_header(def_id).unwrap(); + header.constness == hir::Constness::Const + && self.is_const_trait(header.trait_ref.skip_binder().def_id) } DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn) => { self.constness(def_id) == hir::Constness::Const } DefKind::Trait => self.is_const_trait(def_id), - DefKind::AssocTy | DefKind::AssocFn => { + DefKind::AssocTy => { + let parent_def_id = self.parent(def_id); + match self.def_kind(parent_def_id) { + DefKind::Impl { of_trait: false } => false, + DefKind::Impl { of_trait: true } | DefKind::Trait => { + self.is_conditionally_const(parent_def_id) + } + _ => bug!("unexpected parent item of associated type: {parent_def_id:?}"), + } + } + DefKind::AssocFn => { let parent_def_id = self.parent(def_id); match self.def_kind(parent_def_id) { DefKind::Impl { of_trait: false } => { @@ -2024,7 +2033,7 @@ impl<'tcx> TyCtxt<'tcx> { DefKind::Impl { of_trait: true } | DefKind::Trait => { self.is_conditionally_const(parent_def_id) } - _ => bug!("unexpected parent item of associated item: {parent_def_id:?}"), + _ => bug!("unexpected parent item of associated fn: {parent_def_id:?}"), } } DefKind::OpaqueTy => match self.opaque_ty_origin(def_id) { diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index b9a45ea3c2c..fda9898ed56 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -389,7 +389,7 @@ impl<'tcx> TyCtxt<'tcx> { .delay_as_bug(); } - dtor_candidate = Some((*item_id, self.constness(impl_did))); + dtor_candidate = Some((*item_id, self.impl_trait_header(impl_did).unwrap().constness)); }); let (did, constness) = dtor_candidate?; |
