Skip to content

Commit 5bf68cc

Browse files
Uplift TypeAndMut
1 parent c495dfe commit 5bf68cc

File tree

8 files changed

+55
-30
lines changed

8 files changed

+55
-30
lines changed

compiler/rustc_middle/src/mir/type_foldable.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ TrivialTypeTraversalImpls! {
1515
UserTypeAnnotationIndex,
1616
BorrowKind,
1717
CastKind,
18-
hir::Movability,
1918
BasicBlock,
2019
SwitchTargets,
2120
CoroutineKind,

compiler/rustc_middle/src/ty/context.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::ty::{
2828
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Const, ConstData, GenericParamDefKind,
2929
ImplPolarity, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig, Predicate,
3030
PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid,
31-
TypeAndMut, Visibility,
31+
Visibility,
3232
};
3333
use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
3434
use rustc_ast::{self as ast, attr};
@@ -88,7 +88,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
8888
type Term = ty::Term<'tcx>;
8989

9090
type Binder<T> = Binder<'tcx, T>;
91-
type TypeAndMut = TypeAndMut<'tcx>;
9291
type CanonicalVars = CanonicalVarInfos<'tcx>;
9392

9493
type Ty = Ty<'tcx>;
@@ -128,12 +127,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
128127
type CoercePredicate = ty::CoercePredicate<'tcx>;
129128
type ClosureKind = ty::ClosureKind;
130129

131-
fn ty_and_mut_to_parts(
132-
TypeAndMut { ty, mutbl }: TypeAndMut<'tcx>,
133-
) -> (Self::Ty, ty::Mutability) {
134-
(ty, mutbl)
135-
}
136-
137130
fn mk_canonical_var_infos(self, infos: &[ty::CanonicalVarInfo<Self>]) -> Self::CanonicalVars {
138131
self.mk_canonical_var_infos(infos)
139132
}

compiler/rustc_middle/src/ty/print/pretty.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2754,6 +2754,10 @@ forward_display_to_print! {
27542754
define_print! {
27552755
(self, cx):
27562756

2757+
ty::TypeAndMut<'tcx> {
2758+
p!(write("{}", self.mutbl.prefix_str()), print(self.ty))
2759+
}
2760+
27572761
ty::ClauseKind<'tcx> {
27582762
match *self {
27592763
ty::ClauseKind::Trait(ref data) => {
@@ -2799,10 +2803,6 @@ define_print_and_forward_display! {
27992803
p!("{{", comma_sep(self.iter()), "}}")
28002804
}
28012805

2802-
ty::TypeAndMut<'tcx> {
2803-
p!(write("{}", self.mutbl.prefix_str()), print(self.ty))
2804-
}
2805-
28062806
ty::ExistentialTraitRef<'tcx> {
28072807
// Use a type that can't appear in defaults of type parameters.
28082808
let dummy_self = Ty::new_fresh(cx.tcx(),0);

compiler/rustc_middle/src/ty/structural_impls.rs

-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,6 @@ TrivialTypeTraversalImpls! {
440440
// interners).
441441
TrivialTypeTraversalAndLiftImpls! {
442442
::rustc_hir::def_id::DefId,
443-
::rustc_hir::Mutability,
444443
::rustc_hir::Unsafety,
445444
::rustc_target::spec::abi::Abi,
446445
crate::ty::ClosureKind,

compiler/rustc_middle/src/ty/sty.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,18 @@ use rustc_type_ir::PredicateKind as IrPredicateKind;
4242
use rustc_type_ir::RegionKind as IrRegionKind;
4343
use rustc_type_ir::TyKind as IrTyKind;
4444
use rustc_type_ir::TyKind::*;
45+
use rustc_type_ir::TypeAndMut as IrTypeAndMut;
4546

4647
use super::GenericParamDefKind;
4748

48-
// Re-export the `TyKind` from `rustc_type_ir` here for convenience
49+
// Re-export and re-parameterize some `I = TyCtxt<'tcx>` types here
4950
#[rustc_diagnostic_item = "TyKind"]
5051
pub type TyKind<'tcx> = IrTyKind<TyCtxt<'tcx>>;
5152
pub type RegionKind<'tcx> = IrRegionKind<TyCtxt<'tcx>>;
5253
pub type ConstKind<'tcx> = IrConstKind<TyCtxt<'tcx>>;
5354
pub type PredicateKind<'tcx> = IrPredicateKind<TyCtxt<'tcx>>;
5455
pub type ClauseKind<'tcx> = IrClauseKind<TyCtxt<'tcx>>;
55-
56-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
57-
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
58-
pub struct TypeAndMut<'tcx> {
59-
pub ty: Ty<'tcx>,
60-
pub mutbl: hir::Mutability,
61-
}
56+
pub type TypeAndMut<'tcx> = IrTypeAndMut<TyCtxt<'tcx>>;
6257

6358
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, TyEncodable, TyDecodable, Copy)]
6459
#[derive(HashStable)]

compiler/rustc_type_ir/src/interner.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::fmt::Debug;
33
use std::hash::Hash;
44

55
use crate::{
6-
BoundVar, CanonicalVarInfo, ConstKind, DebruijnIndex, DebugWithInfcx, Mutability, RegionKind,
7-
TyKind, UniverseIndex,
6+
BoundVar, CanonicalVarInfo, ConstKind, DebruijnIndex, DebugWithInfcx, RegionKind, TyKind,
7+
UniverseIndex,
88
};
99

1010
pub trait Interner: Sized {
@@ -20,7 +20,6 @@ pub trait Interner: Sized {
2020
type Term: Copy + Debug + Hash + Ord;
2121

2222
type Binder<T>;
23-
type TypeAndMut: Copy + Debug + Hash + Ord;
2423
type CanonicalVars: Copy + Debug + Hash + Eq + IntoIterator<Item = CanonicalVarInfo<Self>>;
2524

2625
// Kinds of tys
@@ -81,8 +80,6 @@ pub trait Interner: Sized {
8180
type CoercePredicate: Copy + Debug + Hash + Eq;
8281
type ClosureKind: Copy + Debug + Hash + Eq;
8382

84-
fn ty_and_mut_to_parts(ty_and_mut: Self::TypeAndMut) -> (Self::Ty, Mutability);
85-
8683
fn mk_canonical_var_infos(self, infos: &[CanonicalVarInfo<Self>]) -> Self::CanonicalVars;
8784

8885
// FIXME: We should not have all these constructors on `Interner`, but as functions on some trait.

compiler/rustc_type_ir/src/macros.rs

+2
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ TrivialTypeTraversalImpls! {
5151
crate::DebruijnIndex,
5252
crate::AliasRelationDirection,
5353
crate::UniverseIndex,
54+
crate::Mutability,
55+
crate::Movability,
5456
}

compiler/rustc_type_ir/src/ty_kind.rs

+43-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
44
use rustc_data_structures::unify::{EqUnifyValue, UnifyKey};
55
use std::fmt;
66

7+
use crate::fold::{FallibleTypeFolder, TypeFoldable};
8+
use crate::visit::{TypeVisitable, TypeVisitor};
79
use crate::Interner;
810
use crate::{DebruijnIndex, DebugWithInfcx, InferCtxtLike, WithInfcx};
911

@@ -158,7 +160,7 @@ pub enum TyKind<I: Interner> {
158160
Slice(I::Ty),
159161

160162
/// A raw pointer. Written as `*mut T` or `*const T`
161-
RawPtr(I::TypeAndMut),
163+
RawPtr(TypeAndMut<I>),
162164

163165
/// A reference; a pointer with an associated lifetime. Written as
164166
/// `&'a mut T` or `&'a T`.
@@ -410,8 +412,7 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
410412
Str => write!(f, "str"),
411413
Array(t, c) => write!(f, "[{:?}; {:?}]", &this.wrap(t), &this.wrap(c)),
412414
Slice(t) => write!(f, "[{:?}]", &this.wrap(t)),
413-
RawPtr(p) => {
414-
let (ty, mutbl) = I::ty_and_mut_to_parts(*p);
415+
RawPtr(TypeAndMut { ty, mutbl }) => {
415416
match mutbl {
416417
Mutability::Mut => write!(f, "*mut "),
417418
Mutability::Not => write!(f, "*const "),
@@ -831,3 +832,42 @@ impl<I: Interner> DebugWithInfcx<I> for InferTy {
831832
}
832833
}
833834
}
835+
836+
#[derive(derivative::Derivative)]
837+
#[derivative(
838+
Clone(bound = ""),
839+
Copy(bound = ""),
840+
PartialOrd(bound = ""),
841+
Ord(bound = ""),
842+
PartialEq(bound = ""),
843+
Eq(bound = ""),
844+
Hash(bound = ""),
845+
Debug(bound = "")
846+
)]
847+
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable, HashStable_NoContext))]
848+
pub struct TypeAndMut<I: Interner> {
849+
pub ty: I::Ty,
850+
pub mutbl: Mutability,
851+
}
852+
853+
impl<I: Interner> TypeFoldable<I> for TypeAndMut<I>
854+
where
855+
I::Ty: TypeFoldable<I>,
856+
{
857+
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
858+
Ok(TypeAndMut {
859+
ty: self.ty.try_fold_with(folder)?,
860+
mutbl: self.mutbl.try_fold_with(folder)?,
861+
})
862+
}
863+
}
864+
865+
impl<I: Interner> TypeVisitable<I> for TypeAndMut<I>
866+
where
867+
I::Ty: TypeVisitable<I>,
868+
{
869+
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> std::ops::ControlFlow<V::BreakTy> {
870+
self.ty.visit_with(visitor)?;
871+
self.mutbl.visit_with(visitor)
872+
}
873+
}

0 commit comments

Comments
 (0)