Skip to content

Commit e0767d9

Browse files
committed
Refactor functions used by HasTypeFlagsVisitor
1 parent bea94aa commit e0767d9

File tree

9 files changed

+85
-61
lines changed

9 files changed

+85
-61
lines changed

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::infer::InferCtxt;
1212
use rustc_middle::ty::flags::FlagComputation;
1313
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
1414
use rustc_middle::ty::subst::GenericArg;
15-
use rustc_middle::ty::{self, BoundVar, InferConst, List, Ty, TyCtxt, TypeFlags};
15+
use rustc_middle::ty::{self, BoundVar, Flags, InferConst, List, Ty, TyCtxt, TypeFlags};
1616
use std::sync::atomic::Ordering;
1717

1818
use rustc_data_structures::fx::FxHashMap;

compiler/rustc_infer/src/infer/opaque_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::ty::error::{ExpectedFound, TypeError};
1212
use rustc_middle::ty::fold::BottomUpFolder;
1313
use rustc_middle::ty::GenericArgKind;
1414
use rustc_middle::ty::{
15-
self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable,
15+
self, Flags, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable,
1616
TypeVisitable, TypeVisitor,
1717
};
1818
use rustc_span::Span;

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::Flags for Const<'tcx> {
243+
fn flags(self) -> ty::TypeFlags {
244+
ty::flags::FlagComputation::for_const(self)
245+
}
246+
}
247+
242248
impl<'tcx> ty::BoundIndex for Const<'tcx> {
243249
fn bound_index(&self) -> Option<ty::DebruijnIndex> {
244250
if let ty::ConstKind::Bound(debruijn, _) = self.kind() { Some(debruijn) } else { None }

compiler/rustc_middle/src/ty/flags.rs

Lines changed: 2 additions & 2 deletions
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, OuterExclusiveBinder, Ty, TypeFlags};
2+
use crate::ty::{self, Flags, InferConst, OuterExclusiveBinder, Ty, TypeFlags};
33
use std::slice;
44

55
#[derive(Debug)]
@@ -302,7 +302,7 @@ impl FlagComputation {
302302
}
303303

304304
fn add_region(&mut self, r: ty::Region<'_>) {
305-
self.add_flags(r.type_flags());
305+
self.add_flags(r.flags());
306306
if let ty::ReLateBound(debruijn, _) = *r {
307307
self.add_bound_var(debruijn);
308308
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,6 @@ impl<'tcx> Predicate<'tcx> {
474474
self.0.internee
475475
}
476476

477-
#[inline(always)]
478-
pub fn flags(self) -> TypeFlags {
479-
self.0.flags
480-
}
481-
482477
/// Flips the polarity of a Predicate.
483478
///
484479
/// Given `T: Trait` predicate it returns `T: !Trait` and given `T: !Trait` returns `T: Trait`.
@@ -552,6 +547,13 @@ impl<'tcx> Predicate<'tcx> {
552547
}
553548
}
554549

550+
impl<'tcx> ty::Flags for Predicate<'tcx> {
551+
#[inline(always)]
552+
fn flags(self) -> TypeFlags {
553+
self.0.flags
554+
}
555+
}
556+
555557
impl<'tcx> ty::OuterExclusiveBinder for Predicate<'tcx> {
556558
#[inline(always)]
557559
fn outer_exclusive_binder(self) -> DebruijnIndex {

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ 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};
1010
use crate::ty::{
11-
self, AliasTy, InferConst, Lift, OuterExclusiveBinder, Term, TermKind, Ty, TyCtxt,
11+
self, AliasTy, Flags, InferConst, Lift, OuterExclusiveBinder, Term, TermKind, Ty, TyCtxt,
1212
};
1313
use rustc_data_structures::functor::IdFunctor;
1414
use rustc_hir::def::Namespace;

compiler/rustc_middle/src/ty/sty.rs

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

1649-
pub fn type_flags(self) -> TypeFlags {
1650-
let mut flags = TypeFlags::empty();
1651-
1652-
match *self {
1653-
ty::ReVar(..) => {
1654-
flags = flags | TypeFlags::HAS_FREE_REGIONS;
1655-
flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
1656-
flags = flags | TypeFlags::HAS_RE_INFER;
1657-
}
1658-
ty::RePlaceholder(..) => {
1659-
flags = flags | TypeFlags::HAS_FREE_REGIONS;
1660-
flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
1661-
flags = flags | TypeFlags::HAS_RE_PLACEHOLDER;
1662-
}
1663-
ty::ReEarlyBound(..) => {
1664-
flags = flags | TypeFlags::HAS_FREE_REGIONS;
1665-
flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
1666-
flags = flags | TypeFlags::HAS_RE_PARAM;
1667-
}
1668-
ty::ReFree { .. } => {
1669-
flags = flags | TypeFlags::HAS_FREE_REGIONS;
1670-
flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
1671-
}
1672-
ty::ReStatic => {
1673-
flags = flags | TypeFlags::HAS_FREE_REGIONS;
1674-
}
1675-
ty::ReLateBound(..) => {
1676-
flags = flags | TypeFlags::HAS_RE_LATE_BOUND;
1677-
}
1678-
ty::ReErased => {
1679-
flags = flags | TypeFlags::HAS_RE_ERASED;
1680-
}
1681-
}
1682-
1683-
debug!("type_flags({:?}) = {:?}", self, flags);
1684-
1685-
flags
1686-
}
1687-
16881649
/// Given an early-bound or free region, returns the `DefId` where it was bound.
16891650
/// For example, consider the regions in this snippet of code:
16901651
///
@@ -1740,18 +1701,54 @@ impl<'tcx> ty::BoundAtOrAboveBinder for Region<'tcx> {
17401701
}
17411702
}
17421703

1704+
impl<'tcx> ty::Flags for Region<'tcx> {
1705+
fn flags(self) -> TypeFlags {
1706+
let mut flags = TypeFlags::empty();
1707+
1708+
match *self {
1709+
ty::ReVar(..) => {
1710+
flags = flags | TypeFlags::HAS_FREE_REGIONS;
1711+
flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
1712+
flags = flags | TypeFlags::HAS_RE_INFER;
1713+
}
1714+
ty::RePlaceholder(..) => {
1715+
flags = flags | TypeFlags::HAS_FREE_REGIONS;
1716+
flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
1717+
flags = flags | TypeFlags::HAS_RE_PLACEHOLDER;
1718+
}
1719+
ty::ReEarlyBound(..) => {
1720+
flags = flags | TypeFlags::HAS_FREE_REGIONS;
1721+
flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
1722+
flags = flags | TypeFlags::HAS_RE_PARAM;
1723+
}
1724+
ty::ReFree { .. } => {
1725+
flags = flags | TypeFlags::HAS_FREE_REGIONS;
1726+
flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
1727+
}
1728+
ty::ReStatic => {
1729+
flags = flags | TypeFlags::HAS_FREE_REGIONS;
1730+
}
1731+
ty::ReLateBound(..) => {
1732+
flags = flags | TypeFlags::HAS_RE_LATE_BOUND;
1733+
}
1734+
ty::ReErased => {
1735+
flags = flags | TypeFlags::HAS_RE_ERASED;
1736+
}
1737+
}
1738+
1739+
debug!("type_flags({:?}) = {:?}", self, flags);
1740+
1741+
flags
1742+
}
1743+
}
1744+
17431745
/// Type utilities
17441746
impl<'tcx> Ty<'tcx> {
17451747
#[inline(always)]
17461748
pub fn kind(self) -> &'tcx TyKind<'tcx> {
17471749
&self.0.0
17481750
}
17491751

1750-
#[inline(always)]
1751-
pub fn flags(self) -> TypeFlags {
1752-
self.0.0.flags
1753-
}
1754-
17551752
#[inline]
17561753
pub fn is_unit(self) -> bool {
17571754
match self.kind() {
@@ -2442,6 +2439,13 @@ impl<'tcx> Ty<'tcx> {
24422439
}
24432440
}
24442441

2442+
impl<'tcx> ty::Flags for Ty<'tcx> {
2443+
#[inline(always)]
2444+
fn flags(self) -> TypeFlags {
2445+
self.0.0.flags
2446+
}
2447+
}
2448+
24452449
/// Extra information about why we ended up with a particular variance.
24462450
/// This is only used to add more information to error messages, and
24472451
/// has no effect on soundness. While choosing the 'wrong' `VarianceDiagInfo`

compiler/rustc_middle/src/ty/visit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
//! - u.visit_with(visitor)
4040
//! ```
4141
use crate::ty::{
42-
self, flags::FlagComputation, Binder, BoundAtOrAboveBinder, BoundIndex, OuterExclusiveBinder,
43-
Ty, TyCtxt, TypeFlags,
42+
self, Binder, BoundAtOrAboveBinder, BoundIndex, Flags, OuterExclusiveBinder, Ty, TyCtxt,
43+
TypeFlags,
4444
};
4545
use rustc_errors::ErrorGuaranteed;
4646

@@ -585,7 +585,7 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
585585

586586
#[inline]
587587
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
588-
let flags = r.type_flags();
588+
let flags = r.flags();
589589
if flags.intersects(self.flags) {
590590
ControlFlow::Break(FoundFlags)
591591
} else {
@@ -595,7 +595,7 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
595595

596596
#[inline]
597597
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
598-
let flags = FlagComputation::for_const(c);
598+
let flags = c.flags();
599599
trace!(r.flags=?flags);
600600
if flags.intersects(self.flags) {
601601
ControlFlow::Break(FoundFlags)

compiler/rustc_type_ir/src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,17 @@ 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 + OuterExclusiveBinder;
36-
type Const: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord + BoundIndex;
37-
type Region: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord + BoundAtOrAboveBinder;
35+
type Ty: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord + OuterExclusiveBinder + Flags;
36+
type Const: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord + BoundIndex + Flags;
37+
type Region: Clone
38+
+ Debug
39+
+ Hash
40+
+ PartialEq
41+
+ Eq
42+
+ PartialOrd
43+
+ Ord
44+
+ BoundAtOrAboveBinder
45+
+ Flags;
3846
type TypeAndMut: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
3947
type Mutability: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
4048
type Movability: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
@@ -848,3 +856,7 @@ pub trait BoundAtOrAboveBinder {
848856
pub trait BoundIndex {
849857
fn bound_index(&self) -> Option<DebruijnIndex>;
850858
}
859+
860+
pub trait Flags {
861+
fn flags(self) -> TypeFlags;
862+
}

0 commit comments

Comments
 (0)