]> git.lizzy.rs Git - rust.git/commitdiff
mir constants: type traversing bye bye
authorlcnr <rust@lcnr.de>
Tue, 27 Sep 2022 09:59:25 +0000 (11:59 +0200)
committerlcnr <rust@lcnr.de>
Mon, 17 Oct 2022 08:54:01 +0000 (10:54 +0200)
12 files changed:
compiler/rustc_borrowck/src/renumber.rs
compiler/rustc_infer/src/infer/resolve.rs
compiler/rustc_middle/src/mir/mod.rs
compiler/rustc_middle/src/mir/type_foldable.rs
compiler/rustc_middle/src/mir/type_visitable.rs
compiler/rustc_middle/src/ty/erase_regions.rs
compiler/rustc_middle/src/ty/fold.rs
compiler/rustc_middle/src/ty/normalize_erasing_regions.rs
compiler/rustc_middle/src/ty/subst.rs
compiler/rustc_middle/src/ty/visit.rs
compiler/rustc_monomorphize/src/polymorphize.rs
compiler/rustc_trait_selection/src/traits/query/normalize.rs

index d737432f0ef6897e0f1ac91c20288ec816042a6f..f3023769081f205777cd4a238ae3cf31d24d4755 100644 (file)
@@ -1,8 +1,8 @@
 use rustc_index::vec::IndexVec;
 use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
 use rustc_middle::mir::visit::{MutVisitor, TyContext};
+use rustc_middle::mir::Constant;
 use rustc_middle::mir::{Body, Location, Promoted};
-use rustc_middle::mir::{Constant, ConstantKind};
 use rustc_middle::ty::subst::SubstsRef;
 use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
 
@@ -38,21 +38,6 @@ pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'tcx>, value: T) -> T
     })
 }
 
-// FIXME(valtrees): This function is necessary because `fold_regions`
-// panics for mir constants in the visitor.
-//
-// Once `visit_mir_constant` is removed we can also remove this function
-// and just use `renumber_regions`.
-fn renumber_regions_in_mir_constant<'tcx>(
-    infcx: &InferCtxt<'tcx>,
-    value: ConstantKind<'tcx>,
-) -> ConstantKind<'tcx> {
-    infcx.tcx.super_fold_regions(value, |_region, _depth| {
-        let origin = NllRegionVariableOrigin::Existential { from_forall: false };
-        infcx.next_nll_region_var(origin)
-    })
-}
-
 struct NllVisitor<'a, 'tcx> {
     infcx: &'a InferCtxt<'tcx>,
 }
@@ -64,13 +49,6 @@ fn renumber_regions<T>(&mut self, value: T) -> T
     {
         renumber_regions(self.infcx, value)
     }
-
-    fn renumber_regions_in_mir_constant(
-        &mut self,
-        value: ConstantKind<'tcx>,
-    ) -> ConstantKind<'tcx> {
-        renumber_regions_in_mir_constant(self.infcx, value)
-    }
 }
 
 impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
@@ -103,7 +81,7 @@ fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
     #[instrument(skip(self), level = "debug")]
     fn visit_constant(&mut self, constant: &mut Constant<'tcx>, _location: Location) {
         let literal = constant.literal;
-        constant.literal = self.renumber_regions_in_mir_constant(literal);
+        constant.literal = self.renumber_regions(literal);
         debug!("constant: {:#?}", constant);
     }
 }
index 069f96000918faf082b30f5d44be94e9f4a1c5ae..4db4ff2388d77e6e24efd3447c54571b11290cfa 100644 (file)
@@ -1,6 +1,5 @@
 use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
 use super::{FixupError, FixupResult, InferCtxt, Span};
-use rustc_middle::mir;
 use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable};
 use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitor};
 use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable, TypeVisitable};
@@ -48,10 +47,6 @@ fn fold_const(&mut self, ct: Const<'tcx>) -> Const<'tcx> {
             ct.super_fold_with(self)
         }
     }
-
-    fn fold_mir_const(&mut self, constant: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
-        constant.super_fold_with(self)
-    }
 }
 
 /// The opportunistic region resolver opportunistically resolves regions
index c022ea9e5b4708597a4b44ef3763cf2ab2e81e41..f94c447e8fb9e6265528272a58336eb120c7c5d8 100644 (file)
@@ -7,9 +7,9 @@
 };
 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 @@ pub fn from_const(c: ty::Const<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
 
 /// 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<DefId>,
     pub substs: SubstsRef<'tcx>,
index 8e18cad442ed9f46a3c6cf6cdcbcaf2a065d025f..4d7d464d7cd90e2bf9e484dca6a2bd4d914bf701 100644 (file)
@@ -3,7 +3,6 @@
 use rustc_ast::InlineAsmTemplatePiece;
 
 use super::*;
-use crate::mir;
 use crate::ty;
 
 TrivialTypeTraversalAndLiftImpls! {
@@ -51,43 +50,8 @@ fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F
     }
 }
 
-impl<'tcx> TypeFoldable<'tcx> for mir::UnevaluatedConst<'tcx> {
-    fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
-        folder.try_fold_mir_unevaluated(self)
-    }
-}
-
-impl<'tcx> TypeSuperFoldable<'tcx> for mir::UnevaluatedConst<'tcx> {
-    fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
-        self,
-        folder: &mut F,
-    ) -> Result<Self, F::Error> {
-        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<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
-        folder.try_fold_mir_const(self)
-    }
-}
-
-impl<'tcx> TypeSuperFoldable<'tcx> for ConstantKind<'tcx> {
-    fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
-        self,
-        folder: &mut F,
-    ) -> Result<Self, F::Error> {
-        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<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F::Error> {
+        Ok(self)
     }
 }
index a136ca4d8c37f0219a620a838c07b21371ac03d0..fd3773f22649061c110847c0b9defa07ac5c952b 100644 (file)
@@ -1,7 +1,6 @@
 //! `TypeVisitable` implementations for MIR types
 
 use super::*;
-use crate::mir;
 
 impl<'tcx, R: Idx, C: Idx> TypeVisitable<'tcx> for BitMatrix<R, C> {
     fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
@@ -9,33 +8,8 @@ fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy>
     }
 }
 
-impl<'tcx> TypeVisitable<'tcx> for mir::UnevaluatedConst<'tcx> {
-    fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
-        visitor.visit_mir_unevaluated(*self)
-    }
-}
-
-impl<'tcx> TypeSuperVisitable<'tcx> for mir::UnevaluatedConst<'tcx> {
-    fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
-        self.substs.visit_with(visitor)
-    }
-}
-
-impl<'tcx> TypeVisitable<'tcx> for ConstantKind<'tcx> {
-    fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
-        visitor.visit_mir_const(*self)
-    }
-}
-
-impl<'tcx> TypeSuperVisitable<'tcx> for ConstantKind<'tcx> {
-    fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
-        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<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
+        ControlFlow::CONTINUE
     }
 }
index 3226950e79e13258de17a136488a871510ab3634..ffdac93bcd09abefb3d4754ea79a594784f4f9c2 100644 (file)
@@ -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 @@ fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
             _ => self.tcx.lifetimes.re_erased,
         }
     }
-
-    fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
-        c.super_fold_with(self)
-    }
 }
index ce4a46e362d80e231e34d731a113bcc076137c66..f456999ae3eb2e2968329364c06c007c80312afb 100644 (file)
@@ -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 @@ fn fold_ty_unevaluated(
         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 @@ fn try_fold_ty_unevaluated(
         c.try_super_fold_with(self)
     }
 
-    fn try_fold_mir_unevaluated(
-        &mut self,
-        c: mir::UnevaluatedConst<'tcx>,
-    ) -> Result<mir::UnevaluatedConst<'tcx>, Self::Error> {
-        c.try_super_fold_with(self)
-    }
-
     fn try_fold_predicate(
         &mut self,
         p: ty::Predicate<'tcx>,
     ) -> Result<ty::Predicate<'tcx>, Self::Error> {
         p.try_super_fold_with(self)
     }
-
-    fn try_fold_mir_const(
-        &mut self,
-        c: mir::ConstantKind<'tcx>,
-    ) -> Result<mir::ConstantKind<'tcx>, 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 @@ fn try_fold_ty_unevaluated(
         Ok(self.fold_ty_unevaluated(c))
     }
 
-    fn try_fold_mir_unevaluated(
-        &mut self,
-        c: mir::UnevaluatedConst<'tcx>,
-    ) -> Result<mir::UnevaluatedConst<'tcx>, !> {
-        Ok(self.fold_mir_unevaluated(c))
-    }
-
     fn try_fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> Result<ty::Predicate<'tcx>, !> {
         Ok(self.fold_predicate(p))
     }
-
-    fn try_fold_mir_const(
-        &mut self,
-        c: mir::ConstantKind<'tcx>,
-    ) -> Result<mir::ConstantKind<'tcx>, !> {
-        Ok(self.fold_mir_const(c))
-    }
 }
 
 ///////////////////////////////////////////////////////////////////////////
index fe303e21b658d9e89a219ef3e0d70a36f1a30ecc..ee13920d52edda90d2f5ba00fbb49115af4ffaa7 100644 (file)
@@ -214,15 +214,6 @@ fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'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 @@ fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self
             Err(_) => Err(NormalizationError::Const(c)),
         }
     }
-
-    fn try_fold_mir_const(
-        &mut self,
-        c: mir::ConstantKind<'tcx>,
-    ) -> Result<mir::ConstantKind<'tcx>, 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)),
-        }
-    }
 }
index e552d3f1cc551d0edcd26bd7fa6298a179d82a76..c2a83ca9dbb838224178192c63a98f5e255ce2c5 100644 (file)
@@ -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 @@ fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'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> {
index 6e1991b527fe3ed0bf35e64132b10745cf694e10..5ca00a11ab9f482b17d923281cfd920d55d887a2 100644 (file)
@@ -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 @@ fn visit_ty_unevaluated(
         uv.super_visit_with(self)
     }
 
-    fn visit_mir_unevaluated(
-        &mut self,
-        uv: mir::UnevaluatedConst<'tcx>,
-    ) -> ControlFlow<Self::BreakTy> {
-        uv.super_visit_with(self)
-    }
-
     fn visit_predicate(&mut self, p: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
         p.super_visit_with(self)
     }
-
-    fn visit_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> ControlFlow<Self::BreakTy> {
-        c.super_visit_with(self)
-    }
 }
 
 ///////////////////////////////////////////////////////////////////////////
@@ -619,19 +607,6 @@ fn visit_ty_unevaluated(
         }
     }
 
-    fn visit_mir_unevaluated(
-        &mut self,
-        uv: mir::UnevaluatedConst<'tcx>,
-    ) -> ControlFlow<Self::BreakTy> {
-        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<Self::BreakTy> {
index a93f6a60114a6a106eeed73c7dcb776cc932eb21..650076c2213a35f1231e6c1bbb72b01f5ed902df 100644 (file)
@@ -276,9 +276,21 @@ fn visit_constant(&mut self, ct: &Constant<'tcx>, location: Location) {
             ConstantKind::Ty(c) => {
                 c.visit_with(self);
             }
-            ConstantKind::Val(_, ty) | ConstantKind::Unevaluated(_, ty) => {
-                Visitor::visit_ty(self, ty, TyContext::Location(location))
+            ConstantKind::Unevaluated(mir::UnevaluatedConst { def, substs: _, promoted }, ty) => {
+                // Avoid considering `T` unused when constants are of the form:
+                //   `<Self as Foo<T>>::foo::promoted[p]`
+                if let Some(p) = promoted {
+                    if self.def_id == def.did && !self.tcx.generics_of(def.did).has_self {
+                        // If there is a promoted, don't look at the substs - since it will always contain
+                        // the generic parameters, instead, traverse the promoted MIR.
+                        let promoted = self.tcx.promoted_mir(def.did);
+                        self.visit_body(&promoted[p]);
+                    }
+                }
+
+                Visitor::visit_ty(self, ty, TyContext::Location(location));
             }
+            ConstantKind::Val(_, ty) => Visitor::visit_ty(self, ty, TyContext::Location(location)),
         }
     }
 
@@ -310,30 +322,6 @@ fn visit_const(&mut self, c: Const<'tcx>) -> ControlFlow<Self::BreakTy> {
         }
     }
 
-    fn visit_mir_const(&mut self, constant: ConstantKind<'tcx>) -> ControlFlow<Self::BreakTy> {
-        if !constant.has_non_region_param() {
-            return ControlFlow::CONTINUE;
-        }
-
-        match constant {
-            ConstantKind::Ty(ct) => ct.visit_with(self),
-            ConstantKind::Unevaluated(mir::UnevaluatedConst { def, substs: _, promoted: Some(p) }, _)
-                // Avoid considering `T` unused when constants are of the form:
-                //   `<Self as Foo<T>>::foo::promoted[p]`
-                if self.def_id == def.did && !self.tcx.generics_of(def.did).has_self =>
-            {
-                // If there is a promoted, don't look at the substs - since it will always contain
-                // the generic parameters, instead, traverse the promoted MIR.
-                let promoted = self.tcx.promoted_mir(def.did);
-                self.visit_body(&promoted[p]);
-                ControlFlow::CONTINUE
-            }
-            ConstantKind::Val(..) | ConstantKind::Unevaluated(..) => {
-                constant.super_visit_with(self)
-            }
-        }
-    }
-
     #[instrument(level = "debug", skip(self))]
     fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
         if !ty.has_non_region_param() {
index af1029521726a41ccad1ff821ca19e00350c13bd..aa8094a60dd088149692f95187ed1e7117614d9e 100644 (file)
@@ -11,7 +11,6 @@
 use rustc_data_structures::sso::SsoHashMap;
 use rustc_data_structures::stack::ensure_sufficient_stack;
 use rustc_infer::traits::Normalized;
-use rustc_middle::mir;
 use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable};
 use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable};
 use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitor};
@@ -347,13 +346,6 @@ fn try_fold_const(
         ))
     }
 
-    fn try_fold_mir_const(
-        &mut self,
-        constant: mir::ConstantKind<'tcx>,
-    ) -> Result<mir::ConstantKind<'tcx>, Self::Error> {
-        constant.try_super_fold_with(self)
-    }
-
     #[inline]
     fn try_fold_predicate(
         &mut self,