Uplift TypeAndMut
This commit is contained in:
parent
b8ea6e686f
commit
9f0849f9e0
9 changed files with 58 additions and 33 deletions
|
@ -15,7 +15,6 @@ TrivialTypeTraversalImpls! {
|
|||
UserTypeAnnotationIndex,
|
||||
BorrowKind,
|
||||
CastKind,
|
||||
hir::Movability,
|
||||
BasicBlock,
|
||||
SwitchTargets,
|
||||
CoroutineKind,
|
||||
|
|
|
@ -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,
|
||||
TypeAndMut, Visibility,
|
||||
Visibility,
|
||||
};
|
||||
use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
|
||||
use rustc_ast::{self as ast, attr};
|
||||
|
@ -88,7 +88,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
|
|||
type Term = ty::Term<'tcx>;
|
||||
|
||||
type Binder<T> = Binder<'tcx, T>;
|
||||
type TypeAndMut = TypeAndMut<'tcx>;
|
||||
type CanonicalVars = CanonicalVarInfos<'tcx>;
|
||||
|
||||
type Ty = Ty<'tcx>;
|
||||
|
@ -128,12 +127,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
|
|||
type CoercePredicate = ty::CoercePredicate<'tcx>;
|
||||
type ClosureKind = ty::ClosureKind;
|
||||
|
||||
fn ty_and_mut_to_parts(
|
||||
TypeAndMut { ty, mutbl }: TypeAndMut<'tcx>,
|
||||
) -> (Self::Ty, ty::Mutability) {
|
||||
(ty, mutbl)
|
||||
}
|
||||
|
||||
fn mk_canonical_var_infos(self, infos: &[ty::CanonicalVarInfo<Self>]) -> Self::CanonicalVars {
|
||||
self.mk_canonical_var_infos(infos)
|
||||
}
|
||||
|
|
|
@ -2754,6 +2754,10 @@ forward_display_to_print! {
|
|||
define_print! {
|
||||
(self, cx):
|
||||
|
||||
ty::TypeAndMut<'tcx> {
|
||||
p!(write("{}", self.mutbl.prefix_str()), print(self.ty))
|
||||
}
|
||||
|
||||
ty::ClauseKind<'tcx> {
|
||||
match *self {
|
||||
ty::ClauseKind::Trait(ref data) => {
|
||||
|
@ -2799,10 +2803,6 @@ define_print_and_forward_display! {
|
|||
p!("{{", comma_sep(self.iter()), "}}")
|
||||
}
|
||||
|
||||
ty::TypeAndMut<'tcx> {
|
||||
p!(write("{}", self.mutbl.prefix_str()), print(self.ty))
|
||||
}
|
||||
|
||||
ty::ExistentialTraitRef<'tcx> {
|
||||
// Use a type that can't appear in defaults of type parameters.
|
||||
let dummy_self = Ty::new_fresh(cx.tcx(),0);
|
||||
|
|
|
@ -440,7 +440,6 @@ TrivialTypeTraversalImpls! {
|
|||
// interners).
|
||||
TrivialTypeTraversalAndLiftImpls! {
|
||||
::rustc_hir::def_id::DefId,
|
||||
::rustc_hir::Mutability,
|
||||
::rustc_hir::Unsafety,
|
||||
::rustc_target::spec::abi::Abi,
|
||||
crate::ty::ClosureKind,
|
||||
|
|
|
@ -42,23 +42,18 @@ use rustc_type_ir::PredicateKind as IrPredicateKind;
|
|||
use rustc_type_ir::RegionKind as IrRegionKind;
|
||||
use rustc_type_ir::TyKind as IrTyKind;
|
||||
use rustc_type_ir::TyKind::*;
|
||||
use rustc_type_ir::TypeAndMut as IrTypeAndMut;
|
||||
|
||||
use super::GenericParamDefKind;
|
||||
|
||||
// Re-export the `TyKind` from `rustc_type_ir` here for convenience
|
||||
// Re-export and re-parameterize some `I = TyCtxt<'tcx>` types here
|
||||
#[rustc_diagnostic_item = "TyKind"]
|
||||
pub type TyKind<'tcx> = IrTyKind<TyCtxt<'tcx>>;
|
||||
pub type RegionKind<'tcx> = IrRegionKind<TyCtxt<'tcx>>;
|
||||
pub type ConstKind<'tcx> = IrConstKind<TyCtxt<'tcx>>;
|
||||
pub type PredicateKind<'tcx> = IrPredicateKind<TyCtxt<'tcx>>;
|
||||
pub type ClauseKind<'tcx> = IrClauseKind<TyCtxt<'tcx>>;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
|
||||
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
|
||||
pub struct TypeAndMut<'tcx> {
|
||||
pub ty: Ty<'tcx>,
|
||||
pub mutbl: hir::Mutability,
|
||||
}
|
||||
pub type TypeAndMut<'tcx> = IrTypeAndMut<TyCtxt<'tcx>>;
|
||||
|
||||
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, TyEncodable, TyDecodable, Copy)]
|
||||
#[derive(HashStable)]
|
||||
|
|
|
@ -3,8 +3,8 @@ use std::fmt::Debug;
|
|||
use std::hash::Hash;
|
||||
|
||||
use crate::{
|
||||
BoundVar, CanonicalVarInfo, ConstKind, DebruijnIndex, DebugWithInfcx, Mutability, RegionKind,
|
||||
TyKind, UniverseIndex,
|
||||
BoundVar, CanonicalVarInfo, ConstKind, DebruijnIndex, DebugWithInfcx, RegionKind, TyKind,
|
||||
UniverseIndex,
|
||||
};
|
||||
|
||||
pub trait Interner: Sized {
|
||||
|
@ -20,7 +20,6 @@ pub trait Interner: Sized {
|
|||
type Term: Copy + Debug + Hash + Ord;
|
||||
|
||||
type Binder<T>;
|
||||
type TypeAndMut: Copy + Debug + Hash + Ord;
|
||||
type CanonicalVars: Copy + Debug + Hash + Eq + IntoIterator<Item = CanonicalVarInfo<Self>>;
|
||||
|
||||
// Kinds of tys
|
||||
|
@ -81,8 +80,6 @@ pub trait Interner: Sized {
|
|||
type CoercePredicate: Copy + Debug + Hash + Eq;
|
||||
type ClosureKind: Copy + Debug + Hash + Eq;
|
||||
|
||||
fn ty_and_mut_to_parts(ty_and_mut: Self::TypeAndMut) -> (Self::Ty, Mutability);
|
||||
|
||||
fn mk_canonical_var_infos(self, infos: &[CanonicalVarInfo<Self>]) -> Self::CanonicalVars;
|
||||
|
||||
// FIXME: We should not have all these constructors on `Interner`, but as functions on some trait.
|
||||
|
|
|
@ -51,4 +51,6 @@ TrivialTypeTraversalImpls! {
|
|||
crate::DebruijnIndex,
|
||||
crate::AliasRelationDirection,
|
||||
crate::UniverseIndex,
|
||||
crate::Mutability,
|
||||
crate::Movability,
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
|||
use rustc_data_structures::unify::{EqUnifyValue, UnifyKey};
|
||||
use std::fmt;
|
||||
|
||||
use crate::fold::{FallibleTypeFolder, TypeFoldable};
|
||||
use crate::visit::{TypeVisitable, TypeVisitor};
|
||||
use crate::Interner;
|
||||
use crate::{DebruijnIndex, DebugWithInfcx, InferCtxtLike, WithInfcx};
|
||||
|
||||
|
@ -158,7 +160,7 @@ pub enum TyKind<I: Interner> {
|
|||
Slice(I::Ty),
|
||||
|
||||
/// A raw pointer. Written as `*mut T` or `*const T`
|
||||
RawPtr(I::TypeAndMut),
|
||||
RawPtr(TypeAndMut<I>),
|
||||
|
||||
/// A reference; a pointer with an associated lifetime. Written as
|
||||
/// `&'a mut T` or `&'a T`.
|
||||
|
@ -410,8 +412,7 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
|
|||
Str => write!(f, "str"),
|
||||
Array(t, c) => write!(f, "[{:?}; {:?}]", &this.wrap(t), &this.wrap(c)),
|
||||
Slice(t) => write!(f, "[{:?}]", &this.wrap(t)),
|
||||
RawPtr(p) => {
|
||||
let (ty, mutbl) = I::ty_and_mut_to_parts(*p);
|
||||
RawPtr(TypeAndMut { ty, mutbl }) => {
|
||||
match mutbl {
|
||||
Mutability::Mut => write!(f, "*mut "),
|
||||
Mutability::Not => write!(f, "*const "),
|
||||
|
@ -831,3 +832,42 @@ impl<I: Interner> DebugWithInfcx<I> for InferTy {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(derivative::Derivative)]
|
||||
#[derivative(
|
||||
Clone(bound = ""),
|
||||
Copy(bound = ""),
|
||||
PartialOrd(bound = ""),
|
||||
Ord(bound = ""),
|
||||
PartialEq(bound = ""),
|
||||
Eq(bound = ""),
|
||||
Hash(bound = ""),
|
||||
Debug(bound = "")
|
||||
)]
|
||||
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable, HashStable_NoContext))]
|
||||
pub struct TypeAndMut<I: Interner> {
|
||||
pub ty: I::Ty,
|
||||
pub mutbl: Mutability,
|
||||
}
|
||||
|
||||
impl<I: Interner> TypeFoldable<I> for TypeAndMut<I>
|
||||
where
|
||||
I::Ty: TypeFoldable<I>,
|
||||
{
|
||||
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
|
||||
Ok(TypeAndMut {
|
||||
ty: self.ty.try_fold_with(folder)?,
|
||||
mutbl: self.mutbl.try_fold_with(folder)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Interner> TypeVisitable<I> for TypeAndMut<I>
|
||||
where
|
||||
I::Ty: TypeVisitable<I>,
|
||||
{
|
||||
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> std::ops::ControlFlow<V::BreakTy> {
|
||||
self.ty.visit_with(visitor)?;
|
||||
self.mutbl.visit_with(visitor)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,8 +10,8 @@ use super::AS_PTR_CAST_MUT;
|
|||
|
||||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to: Ty<'_>) {
|
||||
if let ty::RawPtr(
|
||||
ptrty @ TypeAndMut {
|
||||
mutbl: Mutability::Mut, ..
|
||||
TypeAndMut {
|
||||
mutbl: Mutability::Mut, ty: ptrty,
|
||||
},
|
||||
) = cast_to.kind()
|
||||
&& let ty::RawPtr(TypeAndMut {
|
||||
|
@ -34,7 +34,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
|
|||
cx,
|
||||
AS_PTR_CAST_MUT,
|
||||
expr.span,
|
||||
&format!("casting the result of `as_ptr` to *{ptrty}"),
|
||||
&format!("casting the result of `as_ptr` to *mut {ptrty}"),
|
||||
"replace with",
|
||||
format!("{recv}.as_mut_ptr()"),
|
||||
applicability,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue