From e8150fa60cc445de7a57db634deb0668880be593 Mon Sep 17 00:00:00 2001 From: lcnr Date: Tue, 27 Sep 2022 11:59:25 +0200 Subject: mir constants: type traversing bye bye --- compiler/rustc_middle/src/mir/mod.rs | 8 ++--- compiler/rustc_middle/src/mir/type_foldable.rs | 42 ++-------------------- compiler/rustc_middle/src/mir/type_visitable.rs | 32 ++--------------- compiler/rustc_middle/src/ty/erase_regions.rs | 5 --- compiler/rustc_middle/src/ty/fold.rs | 40 --------------------- .../src/ty/normalize_erasing_regions.rs | 21 ----------- compiler/rustc_middle/src/ty/subst.rs | 6 ---- compiler/rustc_middle/src/ty/visit.rs | 25 ------------- 8 files changed, 10 insertions(+), 169 deletions(-) (limited to 'compiler/rustc_middle/src') diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index c022ea9e5b4..f94c447e8fb 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -7,9 +7,9 @@ use crate::mir::interpret::{ }; use crate::mir::visit::MirVisitable; use crate::ty::codec::{TyDecoder, TyEncoder}; -use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable}; +use crate::ty::fold::{FallibleTypeFolder, TypeFoldable}; use crate::ty::print::{FmtPrinter, Printer}; -use crate::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor}; +use crate::ty::visit::{TypeVisitable, TypeVisitor}; use crate::ty::{self, List, Ty, TyCtxt}; use crate::ty::{AdtDef, InstanceDef, ScalarInt, UserTypeAnnotationIndex}; use crate::ty::{GenericArg, InternalSubsts, SubstsRef}; @@ -2056,7 +2056,7 @@ pub struct Constant<'tcx> { } #[derive(Clone, Copy, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable, Debug)] -#[derive(Lift)] +#[derive(Lift, TypeFoldable, TypeVisitable)] pub enum ConstantKind<'tcx> { /// This constant came from the type system Ty(ty::Const<'tcx>), @@ -2448,7 +2448,7 @@ impl<'tcx> ConstantKind<'tcx> { /// An unevaluated (potentially generic) constant used in MIR. #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Lift)] -#[derive(Hash, HashStable)] +#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)] pub struct UnevaluatedConst<'tcx> { pub def: ty::WithOptConstParam, pub substs: SubstsRef<'tcx>, diff --git a/compiler/rustc_middle/src/mir/type_foldable.rs b/compiler/rustc_middle/src/mir/type_foldable.rs index 8e18cad442e..4d7d464d7cd 100644 --- a/compiler/rustc_middle/src/mir/type_foldable.rs +++ b/compiler/rustc_middle/src/mir/type_foldable.rs @@ -3,7 +3,6 @@ use rustc_ast::InlineAsmTemplatePiece; use super::*; -use crate::mir; use crate::ty; TrivialTypeTraversalAndLiftImpls! { @@ -51,43 +50,8 @@ impl<'tcx, R: Idx, C: Idx> TypeFoldable<'tcx> for BitMatrix { } } -impl<'tcx> TypeFoldable<'tcx> for mir::UnevaluatedConst<'tcx> { - fn try_fold_with>(self, folder: &mut F) -> Result { - folder.try_fold_mir_unevaluated(self) - } -} - -impl<'tcx> TypeSuperFoldable<'tcx> for mir::UnevaluatedConst<'tcx> { - fn try_super_fold_with>( - self, - folder: &mut F, - ) -> Result { - Ok(mir::UnevaluatedConst { - def: self.def, - substs: self.substs.try_fold_with(folder)?, - promoted: self.promoted, - }) - } -} - -impl<'tcx> TypeFoldable<'tcx> for ConstantKind<'tcx> { - #[inline(always)] - fn try_fold_with>(self, folder: &mut F) -> Result { - folder.try_fold_mir_const(self) - } -} - -impl<'tcx> TypeSuperFoldable<'tcx> for ConstantKind<'tcx> { - fn try_super_fold_with>( - self, - folder: &mut F, - ) -> Result { - match self { - ConstantKind::Ty(c) => Ok(ConstantKind::Ty(c.try_fold_with(folder)?)), - ConstantKind::Val(v, t) => Ok(ConstantKind::Val(v, t.try_fold_with(folder)?)), - ConstantKind::Unevaluated(uv, t) => { - Ok(ConstantKind::Unevaluated(uv.try_fold_with(folder)?, t.try_fold_with(folder)?)) - } - } +impl<'tcx> TypeFoldable<'tcx> for ConstValue<'tcx> { + fn try_fold_with>(self, _: &mut F) -> Result { + Ok(self) } } diff --git a/compiler/rustc_middle/src/mir/type_visitable.rs b/compiler/rustc_middle/src/mir/type_visitable.rs index a136ca4d8c3..fd3773f2264 100644 --- a/compiler/rustc_middle/src/mir/type_visitable.rs +++ b/compiler/rustc_middle/src/mir/type_visitable.rs @@ -1,7 +1,6 @@ //! `TypeVisitable` implementations for MIR types use super::*; -use crate::mir; impl<'tcx, R: Idx, C: Idx> TypeVisitable<'tcx> for BitMatrix { fn visit_with>(&self, _: &mut V) -> ControlFlow { @@ -9,33 +8,8 @@ impl<'tcx, R: Idx, C: Idx> TypeVisitable<'tcx> for BitMatrix { } } -impl<'tcx> TypeVisitable<'tcx> for mir::UnevaluatedConst<'tcx> { - fn visit_with>(&self, visitor: &mut V) -> ControlFlow { - visitor.visit_mir_unevaluated(*self) - } -} - -impl<'tcx> TypeSuperVisitable<'tcx> for mir::UnevaluatedConst<'tcx> { - fn super_visit_with>(&self, visitor: &mut V) -> ControlFlow { - self.substs.visit_with(visitor) - } -} - -impl<'tcx> TypeVisitable<'tcx> for ConstantKind<'tcx> { - fn visit_with>(&self, visitor: &mut V) -> ControlFlow { - visitor.visit_mir_const(*self) - } -} - -impl<'tcx> TypeSuperVisitable<'tcx> for ConstantKind<'tcx> { - fn super_visit_with>(&self, visitor: &mut V) -> ControlFlow { - match *self { - ConstantKind::Ty(c) => c.visit_with(visitor), - ConstantKind::Val(_, t) => t.visit_with(visitor), - ConstantKind::Unevaluated(uv, t) => { - uv.visit_with(visitor)?; - t.visit_with(visitor) - } - } +impl<'tcx> TypeVisitable<'tcx> for ConstValue<'tcx> { + fn visit_with>(&self, _: &mut V) -> ControlFlow { + ControlFlow::CONTINUE } } diff --git a/compiler/rustc_middle/src/ty/erase_regions.rs b/compiler/rustc_middle/src/ty/erase_regions.rs index 3226950e79e..ffdac93bcd0 100644 --- a/compiler/rustc_middle/src/ty/erase_regions.rs +++ b/compiler/rustc_middle/src/ty/erase_regions.rs @@ -1,4 +1,3 @@ -use crate::mir; use crate::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable}; use crate::ty::visit::TypeVisitable; use crate::ty::{self, Ty, TyCtxt, TypeFlags}; @@ -67,8 +66,4 @@ impl<'tcx> TypeFolder<'tcx> for RegionEraserVisitor<'tcx> { _ => self.tcx.lifetimes.re_erased, } } - - fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> { - c.super_fold_with(self) - } } diff --git a/compiler/rustc_middle/src/ty/fold.rs b/compiler/rustc_middle/src/ty/fold.rs index ce4a46e362d..f456999ae3e 100644 --- a/compiler/rustc_middle/src/ty/fold.rs +++ b/compiler/rustc_middle/src/ty/fold.rs @@ -42,7 +42,6 @@ //! - ty.super_fold_with(folder) //! - u.fold_with(folder) //! ``` -use crate::mir; use crate::ty::{self, Binder, BoundTy, Ty, TyCtxt, TypeVisitable}; use rustc_data_structures::fx::FxIndexMap; use rustc_hir::def_id::DefId; @@ -134,20 +133,9 @@ pub trait TypeFolder<'tcx>: FallibleTypeFolder<'tcx, Error = !> { uv.super_fold_with(self) } - fn fold_mir_unevaluated( - &mut self, - uv: mir::UnevaluatedConst<'tcx>, - ) -> mir::UnevaluatedConst<'tcx> { - uv.super_fold_with(self) - } - fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> { p.super_fold_with(self) } - - fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> { - bug!("most type folders should not be folding MIR datastructures: {:?}", c) - } } /// This trait is implemented for every folding traversal. There is a fold @@ -188,26 +176,12 @@ pub trait FallibleTypeFolder<'tcx>: Sized { c.try_super_fold_with(self) } - fn try_fold_mir_unevaluated( - &mut self, - c: mir::UnevaluatedConst<'tcx>, - ) -> Result, Self::Error> { - c.try_super_fold_with(self) - } - fn try_fold_predicate( &mut self, p: ty::Predicate<'tcx>, ) -> Result, Self::Error> { p.try_super_fold_with(self) } - - fn try_fold_mir_const( - &mut self, - c: mir::ConstantKind<'tcx>, - ) -> Result, Self::Error> { - bug!("most type folders should not be folding MIR datastructures: {:?}", c) - } } // This blanket implementation of the fallible trait for infallible folders @@ -248,23 +222,9 @@ where Ok(self.fold_ty_unevaluated(c)) } - fn try_fold_mir_unevaluated( - &mut self, - c: mir::UnevaluatedConst<'tcx>, - ) -> Result, !> { - Ok(self.fold_mir_unevaluated(c)) - } - fn try_fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> Result, !> { Ok(self.fold_predicate(p)) } - - fn try_fold_mir_const( - &mut self, - c: mir::ConstantKind<'tcx>, - ) -> Result, !> { - Ok(self.fold_mir_const(c)) - } } /////////////////////////////////////////////////////////////////////////// diff --git a/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs b/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs index fe303e21b65..ee13920d52e 100644 --- a/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs +++ b/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs @@ -214,15 +214,6 @@ impl<'tcx> TypeFolder<'tcx> for NormalizeAfterErasingRegionsFolder<'tcx> { fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> { self.normalize_generic_arg_after_erasing_regions(c.into()).expect_const() } - - #[inline] - fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> { - // FIXME: This *probably* needs canonicalization too! - let arg = self.param_env.and(c); - self.tcx - .try_normalize_mir_const_after_erasing_regions(arg) - .unwrap_or_else(|_| bug!("failed to normalize {:?}", c)) - } } struct TryNormalizeAfterErasingRegionsFolder<'tcx> { @@ -267,16 +258,4 @@ impl<'tcx> FallibleTypeFolder<'tcx> for TryNormalizeAfterErasingRegionsFolder<'t Err(_) => Err(NormalizationError::Const(c)), } } - - fn try_fold_mir_const( - &mut self, - c: mir::ConstantKind<'tcx>, - ) -> Result, Self::Error> { - // FIXME: This *probably* needs canonicalization too! - let arg = self.param_env.and(c); - match self.tcx.try_normalize_mir_const_after_erasing_regions(arg) { - Ok(c) => Ok(c), - Err(_) => Err(NormalizationError::ConstantKind(c)), - } - } } diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index e552d3f1cc5..c2a83ca9dbb 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -1,6 +1,5 @@ // Type substitutions. -use crate::mir; use crate::ty::codec::{TyDecoder, TyEncoder}; use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldable}; use crate::ty::sty::{ClosureSubsts, GeneratorSubsts, InlineConstSubsts}; @@ -662,11 +661,6 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> { c.super_fold_with(self) } } - - #[inline] - fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> { - c.super_fold_with(self) - } } impl<'a, 'tcx> SubstFolder<'a, 'tcx> { diff --git a/compiler/rustc_middle/src/ty/visit.rs b/compiler/rustc_middle/src/ty/visit.rs index 6e1991b527f..5ca00a11ab9 100644 --- a/compiler/rustc_middle/src/ty/visit.rs +++ b/compiler/rustc_middle/src/ty/visit.rs @@ -38,7 +38,6 @@ //! - ty.super_visit_with(visitor) //! - u.visit_with(visitor) //! ``` -use crate::mir; use crate::ty::{self, flags::FlagComputation, Binder, Ty, TyCtxt, TypeFlags}; use rustc_errors::ErrorGuaranteed; @@ -205,20 +204,9 @@ pub trait TypeVisitor<'tcx>: Sized { uv.super_visit_with(self) } - fn visit_mir_unevaluated( - &mut self, - uv: mir::UnevaluatedConst<'tcx>, - ) -> ControlFlow { - uv.super_visit_with(self) - } - fn visit_predicate(&mut self, p: ty::Predicate<'tcx>) -> ControlFlow { p.super_visit_with(self) } - - fn visit_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> ControlFlow { - c.super_visit_with(self) - } } /////////////////////////////////////////////////////////////////////////// @@ -619,19 +607,6 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor { } } - fn visit_mir_unevaluated( - &mut self, - uv: mir::UnevaluatedConst<'tcx>, - ) -> ControlFlow { - let flags = FlagComputation::for_unevaluated_const(uv.shrink()); - trace!(r.flags=?flags); - if flags.intersects(self.flags) { - ControlFlow::Break(FoundFlags) - } else { - ControlFlow::CONTINUE - } - } - #[inline] #[instrument(level = "trace", ret)] fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow { -- cgit 1.4.1-3-g733a5 From face090ef15de41ae7bdb2e681e4ada25210a7cf Mon Sep 17 00:00:00 2001 From: lcnr Date: Thu, 29 Sep 2022 12:39:35 +0200 Subject: rm `try_normalize_mir_const_after_erasing_regions` --- compiler/rustc_middle/src/query/mod.rs | 8 -------- compiler/rustc_traits/src/normalize_erasing_regions.rs | 3 --- 2 files changed, 11 deletions(-) (limited to 'compiler/rustc_middle/src') diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index cfdc21ac234..a0a857a73a2 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1871,14 +1871,6 @@ rustc_queries! { remap_env_constness } - /// Do not call this query directly: invoke `try_normalize_erasing_regions` instead. - query try_normalize_mir_const_after_erasing_regions( - goal: ParamEnvAnd<'tcx, mir::ConstantKind<'tcx>> - ) -> Result, NoSolution> { - desc { "normalizing `{}`", goal.value } - remap_env_constness - } - query implied_outlives_bounds( goal: CanonicalTyGoal<'tcx> ) -> Result< diff --git a/compiler/rustc_traits/src/normalize_erasing_regions.rs b/compiler/rustc_traits/src/normalize_erasing_regions.rs index 58b718ed127..2da64d73d34 100644 --- a/compiler/rustc_traits/src/normalize_erasing_regions.rs +++ b/compiler/rustc_traits/src/normalize_erasing_regions.rs @@ -18,9 +18,6 @@ pub(crate) fn provide(p: &mut Providers) { try_normalize_after_erasing_regions(tcx, goal) }, - try_normalize_mir_const_after_erasing_regions: |tcx, goal| { - try_normalize_after_erasing_regions(tcx, goal) - }, ..*p }; } -- cgit 1.4.1-3-g733a5 From d04bff658330d72e950ccc03618dc64fe5d11b80 Mon Sep 17 00:00:00 2001 From: lcnr Date: Wed, 5 Oct 2022 18:26:48 +0200 Subject: add inline to `TrivialTypeTraversalImpls` --- compiler/rustc_middle/src/macros.rs | 13 +++++++++++-- compiler/rustc_middle/src/mir/type_foldable.rs | 12 ++++++------ compiler/rustc_middle/src/mir/type_visitable.rs | 6 ------ 3 files changed, 17 insertions(+), 14 deletions(-) (limited to 'compiler/rustc_middle/src') diff --git a/compiler/rustc_middle/src/macros.rs b/compiler/rustc_middle/src/macros.rs index 0e85c60a363..01fe72de612 100644 --- a/compiler/rustc_middle/src/macros.rs +++ b/compiler/rustc_middle/src/macros.rs @@ -54,13 +54,22 @@ macro_rules! TrivialTypeTraversalImpls { impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty { fn try_fold_with>( self, - _: &mut F - ) -> ::std::result::Result<$ty, F::Error> { + _: &mut F, + ) -> ::std::result::Result { Ok(self) } + + #[inline] + fn fold_with>( + self, + _: &mut F, + ) -> Self { + self + } } impl<$tcx> $crate::ty::visit::TypeVisitable<$tcx> for $ty { + #[inline] fn visit_with>( &self, _: &mut F) diff --git a/compiler/rustc_middle/src/mir/type_foldable.rs b/compiler/rustc_middle/src/mir/type_foldable.rs index 4d7d464d7cd..4c0974f86fb 100644 --- a/compiler/rustc_middle/src/mir/type_foldable.rs +++ b/compiler/rustc_middle/src/mir/type_foldable.rs @@ -26,6 +26,12 @@ TrivialTypeTraversalAndLiftImpls! { GeneratorSavedLocal, } +TrivialTypeTraversalImpls! { + for <'tcx> { + ConstValue<'tcx>, + } +} + impl<'tcx> TypeFoldable<'tcx> for &'tcx [InlineAsmTemplatePiece] { fn try_fold_with>(self, _folder: &mut F) -> Result { Ok(self) @@ -49,9 +55,3 @@ impl<'tcx, R: Idx, C: Idx> TypeFoldable<'tcx> for BitMatrix { Ok(self) } } - -impl<'tcx> TypeFoldable<'tcx> for ConstValue<'tcx> { - fn try_fold_with>(self, _: &mut F) -> Result { - Ok(self) - } -} diff --git a/compiler/rustc_middle/src/mir/type_visitable.rs b/compiler/rustc_middle/src/mir/type_visitable.rs index fd3773f2264..e7cd497b206 100644 --- a/compiler/rustc_middle/src/mir/type_visitable.rs +++ b/compiler/rustc_middle/src/mir/type_visitable.rs @@ -7,9 +7,3 @@ impl<'tcx, R: Idx, C: Idx> TypeVisitable<'tcx> for BitMatrix { ControlFlow::CONTINUE } } - -impl<'tcx> TypeVisitable<'tcx> for ConstValue<'tcx> { - fn visit_with>(&self, _: &mut V) -> ControlFlow { - ControlFlow::CONTINUE - } -} -- cgit 1.4.1-3-g733a5