Skip to content

Commit bea94aa

Browse files
committed
Refactor functions used by HasEscapingVarsVisitor
1 parent 75a0be9 commit bea94aa

File tree

9 files changed

+52
-25
lines changed

9 files changed

+52
-25
lines changed

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ impl<'tcx> Const<'tcx> {
239239
}
240240
}
241241

242+
impl<'tcx> ty::BoundIndex for Const<'tcx> {
243+
fn bound_index(&self) -> Option<ty::DebruijnIndex> {
244+
if let ty::ConstKind::Bound(debruijn, _) = self.kind() { Some(debruijn) } else { None }
245+
}
246+
}
247+
242248
pub fn const_param_default(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<Const<'_>> {
243249
let default_def_id = match tcx.hir().get_by_def_id(def_id.expect_local()) {
244250
hir::Node::GenericParam(hir::GenericParam {

compiler/rustc_middle/src/ty/flags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::ty::subst::{GenericArg, GenericArgKind};
2-
use crate::ty::{self, InferConst, Ty, TypeFlags};
2+
use crate::ty::{self, InferConst, OuterExclusiveBinder, Ty, TypeFlags};
33
use std::slice;
44

55
#[derive(Debug)]

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,6 @@ impl<'tcx> Predicate<'tcx> {
479479
self.0.flags
480480
}
481481

482-
#[inline(always)]
483-
pub fn outer_exclusive_binder(self) -> DebruijnIndex {
484-
self.0.outer_exclusive_binder
485-
}
486-
487482
/// Flips the polarity of a Predicate.
488483
///
489484
/// Given `T: Trait` predicate it returns `T: !Trait` and given `T: !Trait` returns `T: Trait`.
@@ -557,6 +552,13 @@ impl<'tcx> Predicate<'tcx> {
557552
}
558553
}
559554

555+
impl<'tcx> ty::OuterExclusiveBinder for Predicate<'tcx> {
556+
#[inline(always)]
557+
fn outer_exclusive_binder(self) -> DebruijnIndex {
558+
self.0.outer_exclusive_binder
559+
}
560+
}
561+
560562
impl rustc_errors::IntoDiagnosticArg for Predicate<'_> {
561563
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
562564
rustc_errors::DiagnosticArgValue::Str(std::borrow::Cow::Owned(self.to_string()))

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use crate::mir::{Field, ProjectionKind};
77
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable};
88
use crate::ty::print::{with_no_trimmed_paths, FmtPrinter, Printer};
99
use crate::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
10-
use crate::ty::{self, AliasTy, InferConst, Lift, Term, TermKind, Ty, TyCtxt};
10+
use crate::ty::{
11+
self, AliasTy, InferConst, Lift, OuterExclusiveBinder, Term, TermKind, Ty, TyCtxt,
12+
};
1113
use rustc_data_structures::functor::IdFunctor;
1214
use rustc_hir::def::Namespace;
1315
use rustc_index::vec::{Idx, IndexVec};

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,14 +1646,6 @@ impl<'tcx> Region<'tcx> {
16461646
matches!(*self, ty::RePlaceholder(..))
16471647
}
16481648

1649-
#[inline]
1650-
pub fn bound_at_or_above_binder(self, index: ty::DebruijnIndex) -> bool {
1651-
match *self {
1652-
ty::ReLateBound(debruijn, _) => debruijn >= index,
1653-
_ => false,
1654-
}
1655-
}
1656-
16571649
pub fn type_flags(self) -> TypeFlags {
16581650
let mut flags = TypeFlags::empty();
16591651

@@ -1738,6 +1730,16 @@ impl<'tcx> Region<'tcx> {
17381730
}
17391731
}
17401732

1733+
impl<'tcx> ty::BoundAtOrAboveBinder for Region<'tcx> {
1734+
#[inline]
1735+
fn bound_at_or_above_binder(self, index: ty::DebruijnIndex) -> bool {
1736+
match *self {
1737+
ty::ReLateBound(debruijn, _) => debruijn >= index,
1738+
_ => false,
1739+
}
1740+
}
1741+
}
1742+
17411743
/// Type utilities
17421744
impl<'tcx> Ty<'tcx> {
17431745
#[inline(always)]

compiler/rustc_middle/src/ty/util.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,9 +1157,11 @@ impl<'tcx> Ty<'tcx> {
11571157
}
11581158
ty
11591159
}
1160+
}
11601161

1162+
impl<'tcx> ty::OuterExclusiveBinder for Ty<'tcx> {
11611163
#[inline]
1162-
pub fn outer_exclusive_binder(self) -> ty::DebruijnIndex {
1164+
fn outer_exclusive_binder(self) -> ty::DebruijnIndex {
11631165
self.0.outer_exclusive_binder
11641166
}
11651167
}

compiler/rustc_middle/src/ty/visit.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
//! - ty.super_visit_with(visitor)
3939
//! - u.visit_with(visitor)
4040
//! ```
41-
use crate::ty::{self, flags::FlagComputation, Binder, Ty, TyCtxt, TypeFlags};
41+
use crate::ty::{
42+
self, flags::FlagComputation, Binder, BoundAtOrAboveBinder, BoundIndex, OuterExclusiveBinder,
43+
Ty, TyCtxt, TypeFlags,
44+
};
4245
use rustc_errors::ErrorGuaranteed;
4346

4447
use rustc_data_structures::fx::FxHashSet;
@@ -537,10 +540,8 @@ impl<'tcx> TypeVisitor<'tcx> for HasEscapingVarsVisitor {
537540
// otherwise we do want to remember to visit the rest of the
538541
// const, as it has types/regions embedded in a lot of other
539542
// places.
540-
match ct.kind() {
541-
ty::ConstKind::Bound(debruijn, _) if debruijn >= self.outer_index => {
542-
ControlFlow::Break(FoundEscapingVars)
543-
}
543+
match ct.bound_index() {
544+
Some(debruijn) if debruijn >= self.outer_index => ControlFlow::Break(FoundEscapingVars),
544545
_ => ct.super_visit_with(self),
545546
}
546547
}

compiler/rustc_trait_selection/src/traits/query/normalize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
1313
use rustc_infer::traits::Normalized;
1414
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable};
1515
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable};
16-
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitor};
16+
use rustc_middle::ty::{self, OuterExclusiveBinder, Ty, TyCtxt, TypeVisitor};
1717
use rustc_span::DUMMY_SP;
1818

1919
use std::ops::ControlFlow;

compiler/rustc_type_ir/src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ pub trait Interner {
3232
type AdtDef: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
3333
type SubstsRef: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
3434
type DefId: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
35-
type Ty: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
36-
type Const: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
37-
type Region: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
35+
type Ty: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord + OuterExclusiveBinder;
36+
type Const: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord + BoundIndex;
37+
type Region: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord + BoundAtOrAboveBinder;
3838
type TypeAndMut: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
3939
type Mutability: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
4040
type Movability: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
@@ -836,3 +836,15 @@ impl UniverseIndex {
836836
self.private < other.private
837837
}
838838
}
839+
840+
pub trait OuterExclusiveBinder {
841+
fn outer_exclusive_binder(self) -> DebruijnIndex;
842+
}
843+
844+
pub trait BoundAtOrAboveBinder {
845+
fn bound_at_or_above_binder(self, index: DebruijnIndex) -> bool;
846+
}
847+
848+
pub trait BoundIndex {
849+
fn bound_index(&self) -> Option<DebruijnIndex>;
850+
}

0 commit comments

Comments
 (0)