]> git.lizzy.rs Git - rust.git/commitdiff
Use `BottomUpFolder`
authorEsteban Küber <esteban@kuber.com.ar>
Wed, 4 Jan 2023 04:49:47 +0000 (20:49 -0800)
committerEsteban Küber <esteban@kuber.com.ar>
Thu, 5 Jan 2023 16:51:16 +0000 (16:51 +0000)
compiler/rustc_hir_typeck/src/demand.rs
compiler/rustc_infer/src/infer/opaque_types.rs

index 41bbe2d15c16fbf00dc8b15d07cce76f2a403ef7..abff2637519160ca8194804e5d48f822b3cdc181 100644 (file)
 use rustc_middle::middle::stability::EvalResult;
 use rustc_middle::ty::adjustment::AllowTwoPhase;
 use rustc_middle::ty::error::{ExpectedFound, TypeError};
-use rustc_middle::ty::fold::TypeFolder;
+use rustc_middle::ty::fold::{BottomUpFolder, TypeFolder};
 use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
 use rustc_middle::ty::relate::TypeRelation;
-use rustc_middle::ty::{
-    self, Article, AssocItem, Ty, TyCtxt, TypeAndMut, TypeSuperFoldable, TypeVisitable,
-};
+use rustc_middle::ty::{self, Article, AssocItem, Ty, TypeAndMut, TypeVisitable};
 use rustc_span::symbol::{sym, Symbol};
 use rustc_span::{BytePos, Span};
 use rustc_trait_selection::infer::InferCtxtExt as _;
@@ -222,42 +220,8 @@ fn point_at_expr_source_of_inferred_type(
         found: Ty<'tcx>,
         expected: Ty<'tcx>,
     ) -> bool {
-        let tcx = self.tcx;
         let map = self.tcx.hir();
 
-        // Hack to make equality checks on types with inference variables and regions useful.
-        struct TypeEraser<'tcx> {
-            tcx: TyCtxt<'tcx>,
-        }
-        impl<'tcx> TypeFolder<'tcx> for TypeEraser<'tcx> {
-            fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
-                self.tcx
-            }
-            fn fold_region(&mut self, _r: ty::Region<'tcx>) -> ty::Region<'tcx> {
-                self.tcx().lifetimes.re_erased
-            }
-            fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
-                if !t.needs_infer() && !t.has_erasable_regions() {
-                    return t;
-                }
-                match *t.kind() {
-                    ty::Infer(ty::TyVar(_) | ty::FreshTy(_)) => {
-                        self.tcx.mk_ty_infer(ty::TyVar(ty::TyVid::from_u32(0)))
-                    }
-                    ty::Infer(ty::IntVar(_) | ty::FreshIntTy(_)) => {
-                        self.tcx.mk_ty_infer(ty::IntVar(ty::IntVid { index: 0 }))
-                    }
-                    ty::Infer(ty::FloatVar(_) | ty::FreshFloatTy(_)) => {
-                        self.tcx.mk_ty_infer(ty::FloatVar(ty::FloatVid { index: 0 }))
-                    }
-                    _ => t.super_fold_with(self),
-                }
-            }
-            fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
-                ct.super_fold_with(self)
-            }
-        }
-
         let hir::ExprKind::Path(hir::QPath::Resolved(None, p)) = expr.kind else { return false; };
         let [hir::PathSegment { ident, args: None, .. }] = p.segments else { return false; };
         let hir::def::Res::Local(hir_id) = p.res else { return false; };
@@ -298,7 +262,22 @@ fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
         let Some(body_id) = node.body_id() else { return false; };
         let body = map.body(body_id);
         expr_finder.visit_expr(body.value);
-        let mut eraser = TypeEraser { tcx };
+        // Hack to make equality checks on types with inference variables and regions useful.
+        let mut eraser = BottomUpFolder {
+            tcx: self.tcx,
+            lt_op: |_| self.tcx.lifetimes.re_erased,
+            ct_op: |c| c,
+            ty_op: |t| match *t.kind() {
+                ty::Infer(ty::TyVar(vid)) => self.tcx.mk_ty_infer(ty::TyVar(self.root_var(vid))),
+                ty::Infer(ty::IntVar(_)) => {
+                    self.tcx.mk_ty_infer(ty::IntVar(ty::IntVid { index: 0 }))
+                }
+                ty::Infer(ty::FloatVar(_)) => {
+                    self.tcx.mk_ty_infer(ty::FloatVar(ty::FloatVid { index: 0 }))
+                }
+                _ => t,
+            },
+        };
         let mut prev = eraser.fold_ty(ty);
         let mut prev_span = None;
 
index a130fde47ed5c4d7c0ccdae1536387b649f7cc1b..749e960bfd03090876186167b6b6247b5eb44efc 100644 (file)
@@ -61,7 +61,7 @@ pub fn replace_opaque_types_with_inference_vars<T: TypeFoldable<'tcx>>(
                 .as_local()
                 .map_or(false, |def_id| self.opaque_type_origin(def_id, span).is_some())
         };
-        let value = value.fold_with(&mut ty::fold::BottomUpFolder {
+        let value = value.fold_with(&mut BottomUpFolder {
             tcx: self.tcx,
             lt_op: |lt| lt,
             ct_op: |ct| ct,