From 7e80867f3c270f360a64294e421d4a9b010e28f8 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 13 Feb 2024 15:53:15 +0000 Subject: [PATCH] Move visitable bounds up into interner --- compiler/rustc_middle/src/ty/context.rs | 4 +- .../src/canonicalizer.rs | 10 +-- compiler/rustc_type_ir/src/interner.rs | 16 ++-- compiler/rustc_type_ir/src/visit.rs | 84 +++++-------------- 4 files changed, 37 insertions(+), 77 deletions(-) diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 0b1050a0307..61e449b8b56 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -28,7 +28,7 @@ use crate::ty::{ self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Const, ConstData, GenericParamDefKind, ImplPolarity, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid, - Visibility, + TypeVisitable, Visibility, }; use crate::ty::{GenericArg, GenericArgs, GenericArgsRef}; use rustc_ast::{self as ast, attr}; @@ -87,7 +87,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { type GenericArg = ty::GenericArg<'tcx>; type Term = ty::Term<'tcx>; - type Binder = Binder<'tcx, T>; + type Binder>> = Binder<'tcx, T>; type BoundVars = &'tcx List; type BoundVar = ty::BoundVariableKind; type CanonicalVars = CanonicalVarInfos<'tcx>; diff --git a/compiler/rustc_next_trait_solver/src/canonicalizer.rs b/compiler/rustc_next_trait_solver/src/canonicalizer.rs index 75b0fc39140..cd434fecce2 100644 --- a/compiler/rustc_next_trait_solver/src/canonicalizer.rs +++ b/compiler/rustc_next_trait_solver/src/canonicalizer.rs @@ -1,7 +1,7 @@ use std::cmp::Ordering; use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable}; -use rustc_type_ir::visit::{Flags, TypeVisitableExt}; +use rustc_type_ir::visit::TypeVisitableExt; use rustc_type_ir::{ self as ty, Canonical, CanonicalTyVarKind, CanonicalVarInfo, CanonicalVarKind, ConstTy, InferCtxtLike, Interner, IntoKind, PlaceholderLike, @@ -45,13 +45,7 @@ pub struct Canonicalizer<'a, Infcx: InferCtxtLike, I: Interner> { binder_index: ty::DebruijnIndex, } -impl<'a, Infcx: InferCtxtLike, I: Interner> Canonicalizer<'a, Infcx, I> -where - I::Ty: Flags, - I::Region: Flags, - I::Const: Flags, - I::Predicate: Flags, -{ +impl<'a, Infcx: InferCtxtLike, I: Interner> Canonicalizer<'a, Infcx, I> { pub fn canonicalize>( infcx: &'a Infcx, canonicalize_mode: CanonicalizeMode, diff --git a/compiler/rustc_type_ir/src/interner.rs b/compiler/rustc_type_ir/src/interner.rs index ce82e91f9c6..7728ee0e842 100644 --- a/compiler/rustc_type_ir/src/interner.rs +++ b/compiler/rustc_type_ir/src/interner.rs @@ -2,6 +2,7 @@ use smallvec::SmallVec; use std::fmt::Debug; use std::hash::Hash; +use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable}; use crate::{ BoundVar, BoundVars, CanonicalVarInfo, ConstKind, DebruijnIndex, DebugWithInfcx, RegionKind, TyKind, UniverseIndex, @@ -19,7 +20,7 @@ pub trait Interner: Sized { type GenericArg: Copy + DebugWithInfcx + Hash + Ord; type Term: Copy + Debug + Hash + Ord; - type Binder: BoundVars; + type Binder>: BoundVars + TypeSuperVisitable; type BoundVars: IntoIterator; type BoundVar; @@ -31,7 +32,9 @@ pub trait Interner: Sized { + Hash + Ord + Into - + IntoKind>; + + IntoKind> + + TypeSuperVisitable + + Flags; type Tys: Copy + Debug + Hash + Ord + IntoIterator; type AliasTy: Copy + DebugWithInfcx + Hash + Ord; type ParamTy: Copy + Debug + Hash + Ord; @@ -51,7 +54,9 @@ pub trait Interner: Sized { + Ord + Into + IntoKind> - + ConstTy; + + ConstTy + + TypeSuperVisitable + + Flags; type AliasConst: Copy + DebugWithInfcx + Hash + Ord; type PlaceholderConst: Copy + Debug + Hash + Ord + PlaceholderLike; type ParamConst: Copy + Debug + Hash + Ord; @@ -65,7 +70,8 @@ pub trait Interner: Sized { + Hash + Ord + Into - + IntoKind>; + + IntoKind> + + Flags; type EarlyParamRegion: Copy + Debug + Hash + Ord; type LateParamRegion: Copy + Debug + Hash + Ord; type BoundRegion: Copy + Debug + Hash + Ord; @@ -73,7 +79,7 @@ pub trait Interner: Sized { type PlaceholderRegion: Copy + Debug + Hash + Ord + PlaceholderLike; // Predicates - type Predicate: Copy + Debug + Hash + Eq; + type Predicate: Copy + Debug + Hash + Eq + TypeSuperVisitable + Flags; type TraitPredicate: Copy + Debug + Hash + Eq; type RegionOutlivesPredicate: Copy + Debug + Hash + Eq; type TypeOutlivesPredicate: Copy + Debug + Hash + Eq; diff --git a/compiler/rustc_type_ir/src/visit.rs b/compiler/rustc_type_ir/src/visit.rs index 5f7c507e739..638fb9f7fa9 100644 --- a/compiler/rustc_type_ir/src/visit.rs +++ b/compiler/rustc_type_ir/src/visit.rs @@ -87,38 +87,28 @@ pub trait TypeVisitor: Sized { #[cfg(not(feature = "nightly"))] type BreakTy; - fn visit_binder>(&mut self, t: &I::Binder) -> ControlFlow - where - I::Binder: TypeSuperVisitable, - { + fn visit_binder>( + &mut self, + t: &I::Binder, + ) -> ControlFlow { t.super_visit_with(self) } - fn visit_ty(&mut self, t: I::Ty) -> ControlFlow - where - I::Ty: TypeSuperVisitable, - { + fn visit_ty(&mut self, t: I::Ty) -> ControlFlow { t.super_visit_with(self) } // The default region visitor is a no-op because `Region` is non-recursive - // and has no `super_visit_with` method to call. That also explains the - // lack of `I::Region: TypeSuperVisitable` bound. + // and has no `super_visit_with` method to call. fn visit_region(&mut self, _r: I::Region) -> ControlFlow { ControlFlow::Continue(()) } - fn visit_const(&mut self, c: I::Const) -> ControlFlow - where - I::Const: TypeSuperVisitable, - { + fn visit_const(&mut self, c: I::Const) -> ControlFlow { c.super_visit_with(self) } - fn visit_predicate(&mut self, p: I::Predicate) -> ControlFlow - where - I::Predicate: TypeSuperVisitable, - { + fn visit_predicate(&mut self, p: I::Predicate) -> ControlFlow { p.super_visit_with(self) } } @@ -327,13 +317,7 @@ pub trait TypeVisitableExt: TypeVisitable { } } -impl> TypeVisitableExt for T -where - I::Ty: Flags, - I::Region: Flags, - I::Const: Flags, - I::Predicate: Flags, -{ +impl> TypeVisitableExt for T { fn has_type_flags(&self, flags: TypeFlags) -> bool { let res = self.visit_with(&mut HasTypeFlagsVisitor { flags }) == ControlFlow::Break(FoundFlags); @@ -381,19 +365,13 @@ impl std::fmt::Debug for HasTypeFlagsVisitor { // are present, regardless of whether those bound variables are used. This // is important for anonymization of binders in `TyCtxt::erase_regions`. We // specifically detect this case in `visit_binder`. -impl TypeVisitor for HasTypeFlagsVisitor -where - I::Ty: Flags, - I::Region: Flags, - I::Const: Flags, - I::Predicate: Flags, -{ +impl TypeVisitor for HasTypeFlagsVisitor { type BreakTy = FoundFlags; - fn visit_binder>(&mut self, t: &I::Binder) -> ControlFlow - where - I::Binder: TypeSuperVisitable, - { + fn visit_binder>( + &mut self, + t: &I::Binder, + ) -> ControlFlow { // If we're looking for the HAS_BINDER_VARS flag, check if the // binder has vars. This won't be present in the binder's bound // value, so we need to check here too. @@ -480,19 +458,13 @@ struct HasEscapingVarsVisitor { outer_index: ty::DebruijnIndex, } -impl TypeVisitor for HasEscapingVarsVisitor -where - I::Ty: Flags, - I::Region: Flags, - I::Const: Flags, - I::Predicate: Flags, -{ +impl TypeVisitor for HasEscapingVarsVisitor { type BreakTy = FoundEscapingVars; - fn visit_binder>(&mut self, t: &I::Binder) -> ControlFlow - where - I::Binder: TypeSuperVisitable, - { + fn visit_binder>( + &mut self, + t: &I::Binder, + ) -> ControlFlow { self.outer_index.shift_in(1); let result = t.super_visit_with(self); self.outer_index.shift_out(1); @@ -550,19 +522,10 @@ where struct HasErrorVisitor; -impl TypeVisitor for HasErrorVisitor -where - I::Ty: Flags, - I::Region: Flags, - I::Const: Flags, - I::Predicate: Flags, -{ +impl TypeVisitor for HasErrorVisitor { type BreakTy = I::ErrorGuaranteed; - fn visit_ty(&mut self, t: ::Ty) -> ControlFlow - where - ::Ty: TypeSuperVisitable, - { + fn visit_ty(&mut self, t: ::Ty) -> ControlFlow { if let ty::Error(guar) = t.kind() { ControlFlow::Break(guar) } else { @@ -570,10 +533,7 @@ where } } - fn visit_const(&mut self, c: ::Const) -> ControlFlow - where - ::Const: TypeSuperVisitable, - { + fn visit_const(&mut self, c: ::Const) -> ControlFlow { if let ty::ConstKind::Error(guar) = c.kind() { ControlFlow::Break(guar) } else {