mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-16 17:03:35 +00:00
Make RawPtr take Ty and Mutbl separately
This commit is contained in:
parent
ff0c31e6b9
commit
7be0dbe772
@ -503,10 +503,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
||||
ty::VarianceDiagInfo::None => {}
|
||||
ty::VarianceDiagInfo::Invariant { ty, param_index } => {
|
||||
let (desc, note) = match ty.kind() {
|
||||
ty::RawPtr(ty_mut) => {
|
||||
assert_eq!(ty_mut.mutbl, rustc_hir::Mutability::Mut);
|
||||
ty::RawPtr(ty, mutbl) => {
|
||||
assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
|
||||
(
|
||||
format!("a mutable pointer to `{}`", ty_mut.ty),
|
||||
format!("a mutable pointer to `{}`", ty),
|
||||
"mutable pointers are invariant over their type parameter".to_string(),
|
||||
)
|
||||
}
|
||||
|
@ -555,8 +555,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
|
||||
search_stack.push((*elem_ty, elem_hir_ty));
|
||||
}
|
||||
|
||||
(ty::RawPtr(mut_ty), hir::TyKind::Ptr(mut_hir_ty)) => {
|
||||
search_stack.push((mut_ty.ty, &mut_hir_ty.ty));
|
||||
(ty::RawPtr(mut_ty, _), hir::TyKind::Ptr(mut_hir_ty)) => {
|
||||
search_stack.push((*mut_ty, &mut_hir_ty.ty));
|
||||
}
|
||||
|
||||
_ => {
|
||||
|
@ -2284,8 +2284,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
||||
}
|
||||
}
|
||||
}
|
||||
ty::RawPtr(tnm) => {
|
||||
match tnm.mutbl {
|
||||
ty::RawPtr(_, mutbl) => {
|
||||
match mutbl {
|
||||
// `*const` raw pointers are not mutable
|
||||
hir::Mutability::Not => Err(place),
|
||||
// `*mut` raw pointers are always mutable, regardless of
|
||||
|
@ -2157,15 +2157,12 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
|
||||
}
|
||||
|
||||
CastKind::PointerCoercion(PointerCoercion::MutToConstPointer) => {
|
||||
let ty::RawPtr(ty::TypeAndMut { ty: ty_from, mutbl: hir::Mutability::Mut }) =
|
||||
op.ty(body, tcx).kind()
|
||||
let ty::RawPtr(ty_from, hir::Mutability::Mut) = op.ty(body, tcx).kind()
|
||||
else {
|
||||
span_mirbug!(self, rvalue, "unexpected base type for cast {:?}", ty,);
|
||||
return;
|
||||
};
|
||||
let ty::RawPtr(ty::TypeAndMut { ty: ty_to, mutbl: hir::Mutability::Not }) =
|
||||
ty.kind()
|
||||
else {
|
||||
let ty::RawPtr(ty_to, hir::Mutability::Not) = ty.kind() else {
|
||||
span_mirbug!(self, rvalue, "unexpected target type for cast {:?}", ty,);
|
||||
return;
|
||||
};
|
||||
|
@ -796,16 +796,16 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
|
||||
|
||||
// This counts how many pointers
|
||||
fn ptr_count(t: Ty<'_>) -> usize {
|
||||
match t.kind() {
|
||||
ty::RawPtr(p) => 1 + ptr_count(p.ty),
|
||||
match *t.kind() {
|
||||
ty::RawPtr(p_ty, _) => 1 + ptr_count(p_ty),
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
// Non-ptr type
|
||||
fn non_ptr(t: Ty<'_>) -> Ty<'_> {
|
||||
match t.kind() {
|
||||
ty::RawPtr(p) => non_ptr(p.ty),
|
||||
match *t.kind() {
|
||||
ty::RawPtr(p_ty, _) => non_ptr(p_ty),
|
||||
_ => t,
|
||||
}
|
||||
}
|
||||
@ -814,8 +814,8 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
|
||||
// to the element type of the first argument
|
||||
let (_, element_ty0) = arg_tys[0].simd_size_and_type(bx.tcx());
|
||||
let (_, element_ty1) = arg_tys[1].simd_size_and_type(bx.tcx());
|
||||
let (pointer_count, underlying_ty) = match element_ty1.kind() {
|
||||
ty::RawPtr(p) if p.ty == in_elem => (ptr_count(element_ty1), non_ptr(element_ty1)),
|
||||
let (pointer_count, underlying_ty) = match *element_ty1.kind() {
|
||||
ty::RawPtr(p_ty, _) if p_ty == in_elem => (ptr_count(element_ty1), non_ptr(element_ty1)),
|
||||
_ => {
|
||||
require!(
|
||||
false,
|
||||
@ -910,16 +910,16 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
|
||||
|
||||
// This counts how many pointers
|
||||
fn ptr_count(t: Ty<'_>) -> usize {
|
||||
match t.kind() {
|
||||
ty::RawPtr(p) => 1 + ptr_count(p.ty),
|
||||
match *t.kind() {
|
||||
ty::RawPtr(p_ty, _) => 1 + ptr_count(p_ty),
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
// Non-ptr type
|
||||
fn non_ptr(t: Ty<'_>) -> Ty<'_> {
|
||||
match t.kind() {
|
||||
ty::RawPtr(p) => non_ptr(p.ty),
|
||||
match *t.kind() {
|
||||
ty::RawPtr(p_ty, _) => non_ptr(p_ty),
|
||||
_ => t,
|
||||
}
|
||||
}
|
||||
@ -929,8 +929,8 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
|
||||
let (_, element_ty0) = arg_tys[0].simd_size_and_type(bx.tcx());
|
||||
let (_, element_ty1) = arg_tys[1].simd_size_and_type(bx.tcx());
|
||||
let (_, element_ty2) = arg_tys[2].simd_size_and_type(bx.tcx());
|
||||
let (pointer_count, underlying_ty) = match element_ty1.kind() {
|
||||
ty::RawPtr(p) if p.ty == in_elem && p.mutbl == hir::Mutability::Mut => {
|
||||
let (pointer_count, underlying_ty) = match *element_ty1.kind() {
|
||||
ty::RawPtr(p_ty, mutbl) if p_ty == in_elem && mutbl == hir::Mutability::Mut => {
|
||||
(ptr_count(element_ty1), non_ptr(element_ty1))
|
||||
}
|
||||
_ => {
|
||||
|
@ -1548,8 +1548,8 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
|
||||
|
||||
require!(
|
||||
matches!(
|
||||
element_ty1.kind(),
|
||||
ty::RawPtr(p) if p.ty == in_elem && p.ty.kind() == element_ty0.kind()
|
||||
*element_ty1.kind(),
|
||||
ty::RawPtr(p_ty, _) if p_ty == in_elem && p_ty.kind() == element_ty0.kind()
|
||||
),
|
||||
InvalidMonomorphization::ExpectedElementType {
|
||||
span,
|
||||
@ -1654,8 +1654,8 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
|
||||
|
||||
require!(
|
||||
matches!(
|
||||
pointer_ty.kind(),
|
||||
ty::RawPtr(p) if p.ty == values_elem && p.ty.kind() == values_elem.kind()
|
||||
*pointer_ty.kind(),
|
||||
ty::RawPtr(p_ty, _) if p_ty == values_elem && p_ty.kind() == values_elem.kind()
|
||||
),
|
||||
InvalidMonomorphization::ExpectedElementType {
|
||||
span,
|
||||
@ -1746,8 +1746,8 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
|
||||
// The second argument must be a mutable pointer type matching the element type
|
||||
require!(
|
||||
matches!(
|
||||
pointer_ty.kind(),
|
||||
ty::RawPtr(p) if p.ty == values_elem && p.ty.kind() == values_elem.kind() && p.mutbl.is_mut()
|
||||
*pointer_ty.kind(),
|
||||
ty::RawPtr(p_ty, p_mutbl) if p_ty == values_elem && p_ty.kind() == values_elem.kind() && p_mutbl.is_mut()
|
||||
),
|
||||
InvalidMonomorphization::ExpectedElementType {
|
||||
span,
|
||||
@ -1843,9 +1843,9 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
|
||||
|
||||
require!(
|
||||
matches!(
|
||||
element_ty1.kind(),
|
||||
ty::RawPtr(p)
|
||||
if p.ty == in_elem && p.mutbl.is_mut() && p.ty.kind() == element_ty0.kind()
|
||||
*element_ty1.kind(),
|
||||
ty::RawPtr(p_ty, p_mutbl)
|
||||
if p_ty == in_elem && p_mutbl.is_mut() && p_ty.kind() == element_ty0.kind()
|
||||
),
|
||||
InvalidMonomorphization::ExpectedElementType {
|
||||
span,
|
||||
@ -2074,8 +2074,8 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
|
||||
);
|
||||
|
||||
match in_elem.kind() {
|
||||
ty::RawPtr(p) => {
|
||||
let metadata = p.ty.ptr_metadata_ty(bx.tcx, |ty| {
|
||||
ty::RawPtr(p_ty, _) => {
|
||||
let metadata = p_ty.ptr_metadata_ty(bx.tcx, |ty| {
|
||||
bx.tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), ty)
|
||||
});
|
||||
require!(
|
||||
@ -2088,8 +2088,8 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
|
||||
}
|
||||
}
|
||||
match out_elem.kind() {
|
||||
ty::RawPtr(p) => {
|
||||
let metadata = p.ty.ptr_metadata_ty(bx.tcx, |ty| {
|
||||
ty::RawPtr(p_ty, _) => {
|
||||
let metadata = p_ty.ptr_metadata_ty(bx.tcx, |ty| {
|
||||
bx.tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), ty)
|
||||
});
|
||||
require!(
|
||||
|
@ -6,7 +6,7 @@ use rustc_middle::mir::interpret::{InterpResult, PointerArithmetic, Scalar};
|
||||
use rustc_middle::mir::CastKind;
|
||||
use rustc_middle::ty::adjustment::PointerCoercion;
|
||||
use rustc_middle::ty::layout::{IntegerExt, LayoutOf, TyAndLayout};
|
||||
use rustc_middle::ty::{self, FloatTy, Ty, TypeAndMut};
|
||||
use rustc_middle::ty::{self, FloatTy, Ty};
|
||||
use rustc_target::abi::Integer;
|
||||
use rustc_type_ir::TyKind::*;
|
||||
|
||||
|
@ -375,7 +375,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||
// We cannot use `builtin_deref` here since we need to reject `Box<T, MyAlloc>`.
|
||||
Ok(Some(match ty.kind() {
|
||||
ty::Ref(_, ty, _) => *ty,
|
||||
ty::RawPtr(mt) => mt.ty,
|
||||
ty::RawPtr(ty, _) => *ty,
|
||||
// We only accept `Box` with the default allocator.
|
||||
_ if ty.is_box_global(*self.tcx) => ty.boxed_ty(),
|
||||
_ => return Ok(None),
|
||||
|
@ -937,7 +937,10 @@ pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
|
||||
ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_, _) => (), // struct(u8, u8, u8, u8) is ok
|
||||
ty::Array(t, _) if matches!(t.kind(), ty::Param(_)) => (), // pass struct<T>([T; N]) through, let monomorphization catch errors
|
||||
ty::Array(t, _clen)
|
||||
if matches!(t.kind(), ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_, _)) =>
|
||||
if matches!(
|
||||
t.kind(),
|
||||
ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_, _)
|
||||
) =>
|
||||
{ /* struct([f32; 4]) is ok */ }
|
||||
_ => {
|
||||
struct_span_code_err!(
|
||||
|
@ -195,7 +195,7 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
|
||||
{
|
||||
Ok(())
|
||||
}
|
||||
(&RawPtr(tm_a), &RawPtr(tm_b)) if tm_a.mutbl == tm_b.mutbl => Ok(()),
|
||||
(&RawPtr(_, a_mutbl), &RawPtr(_, b_mutbl)) if a_mutbl == b_mutbl => Ok(()),
|
||||
(&Adt(def_a, args_a), &Adt(def_b, args_b)) if def_a.is_struct() && def_b.is_struct() => {
|
||||
if def_a != def_b {
|
||||
let source_path = tcx.def_path_str(def_a.did());
|
||||
@ -351,14 +351,17 @@ pub fn coerce_unsized_info<'tcx>(
|
||||
check_mutbl(mt_a, mt_b, &|ty| Ty::new_imm_ref(tcx, r_b, ty))
|
||||
}
|
||||
|
||||
(&ty::Ref(_, ty_a, mutbl_a), &ty::RawPtr(mt_b)) => {
|
||||
let mt_a = ty::TypeAndMut { ty: ty_a, mutbl: mutbl_a };
|
||||
check_mutbl(mt_a, mt_b, &|ty| Ty::new_imm_ptr(tcx, ty))
|
||||
}
|
||||
(&ty::Ref(_, ty_a, mutbl_a), &ty::RawPtr(ty_b, mutbl_b)) => check_mutbl(
|
||||
ty::TypeAndMut { ty: ty_a, mutbl: mutbl_a },
|
||||
ty::TypeAndMut { ty: ty_b, mutbl: mutbl_b },
|
||||
&|ty| Ty::new_imm_ptr(tcx, ty),
|
||||
),
|
||||
|
||||
(&ty::RawPtr(mt_a), &ty::RawPtr(mt_b)) => {
|
||||
check_mutbl(mt_a, mt_b, &|ty| Ty::new_imm_ptr(tcx, ty))
|
||||
}
|
||||
(&ty::RawPtr(ty_a, mutbl_a), &ty::RawPtr(ty_b, mutbl_b)) => check_mutbl(
|
||||
ty::TypeAndMut { ty: ty_a, mutbl: mutbl_a },
|
||||
ty::TypeAndMut { ty: ty_b, mutbl: mutbl_b },
|
||||
&|ty| Ty::new_imm_ptr(tcx, ty),
|
||||
),
|
||||
|
||||
(&ty::Adt(def_a, args_a), &ty::Adt(def_b, args_b))
|
||||
if def_a.is_struct() && def_b.is_struct() =>
|
||||
|
@ -323,7 +323,7 @@ fn emit_orphan_check_error<'tcx>(
|
||||
let is_foreign =
|
||||
!trait_ref.def_id.is_local() && matches!(is_target_ty, IsFirstInputType::No);
|
||||
|
||||
match &ty.kind() {
|
||||
match *ty.kind() {
|
||||
ty::Slice(_) => {
|
||||
push_to_foreign_or_name(
|
||||
is_foreign,
|
||||
@ -354,14 +354,14 @@ fn emit_orphan_check_error<'tcx>(
|
||||
ty::Alias(ty::Opaque, ..) => {
|
||||
opaque.push(errors::OnlyCurrentTraitsOpaque { span })
|
||||
}
|
||||
ty::RawPtr(ptr_ty) => {
|
||||
ty::RawPtr(ptr_ty, mutbl) => {
|
||||
if !self_ty.has_param() {
|
||||
let mut_key = ptr_ty.mutbl.prefix_str();
|
||||
let mut_key = mutbl.prefix_str();
|
||||
sugg = Some(errors::OnlyCurrentTraitsPointerSugg {
|
||||
wrapper_span: self_ty_span,
|
||||
struct_span: full_impl_span.shrink_to_lo(),
|
||||
mut_key,
|
||||
ptr_ty: ptr_ty.ty,
|
||||
ptr_ty,
|
||||
});
|
||||
}
|
||||
pointer.push(errors::OnlyCurrentTraitsPointer { span, pointer: ty });
|
||||
|
@ -253,8 +253,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
|
||||
self.add_constraints_from_ty(current, typ, variance);
|
||||
}
|
||||
|
||||
ty::RawPtr(ref mt) => {
|
||||
self.add_constraints_from_mt(current, mt, variance);
|
||||
ty::RawPtr(ty, mutbl) => {
|
||||
self.add_constraints_from_mt(current, &ty::TypeAndMut { ty, mutbl }, variance);
|
||||
}
|
||||
|
||||
ty::Tuple(subtys) => {
|
||||
|
@ -356,7 +356,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
|
||||
{
|
||||
sugg = Some((format!("&{}", mutbl.prefix_str()), false));
|
||||
}
|
||||
} else if let ty::RawPtr(mutbl, _) = *self.cast_ty.kind()
|
||||
} else if let ty::RawPtr(_, mutbl) = *self.cast_ty.kind()
|
||||
&& fcx.can_coerce(
|
||||
Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, self.expr_ty, mutbl),
|
||||
self.cast_ty,
|
||||
|
@ -222,8 +222,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
|
||||
|
||||
// Examine the supertype and consider auto-borrowing.
|
||||
match *b.kind() {
|
||||
ty::RawPtr(mt_b) => {
|
||||
return self.coerce_unsafe_ptr(a, b, mt_b.mutbl);
|
||||
ty::RawPtr(_, b_mutbl) => {
|
||||
return self.coerce_unsafe_ptr(a, b, b_mutbl);
|
||||
}
|
||||
ty::Ref(r_b, _, mutbl_b) => {
|
||||
return self.coerce_borrowed_pointer(a, b, r_b, mutbl_b);
|
||||
@ -978,7 +978,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
|
||||
|
||||
let (is_ref, mt_a) = match *a.kind() {
|
||||
ty::Ref(_, ty, mutbl) => (true, ty::TypeAndMut { ty, mutbl }),
|
||||
ty::RawPtr(mt) => (false, mt),
|
||||
ty::RawPtr(ty, mutbl) => (false, ty::TypeAndMut { ty, mutbl }),
|
||||
_ => return self.unify_and(a, b, identity),
|
||||
};
|
||||
coerce_mutbls(mt_a.mutbl, mutbl_b)?;
|
||||
|
@ -2685,8 +2685,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
expr,
|
||||
Some(span),
|
||||
);
|
||||
} else if let ty::RawPtr(ty_and_mut) = expr_t.kind()
|
||||
&& let ty::Adt(adt_def, _) = ty_and_mut.ty.kind()
|
||||
} else if let ty::RawPtr(ptr_ty, _) = expr_t.kind()
|
||||
&& let ty::Adt(adt_def, _) = ptr_ty.kind()
|
||||
&& let ExprKind::Field(base_expr, _) = expr.kind
|
||||
&& adt_def.variants().len() == 1
|
||||
&& adt_def
|
||||
|
@ -1394,9 +1394,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
arg: &hir::Expr<'tcx>,
|
||||
err: &mut Diag<'tcx>,
|
||||
) {
|
||||
if let ty::RawPtr(ty::TypeAndMut { mutbl: hir::Mutability::Mut, .. }) = expected_ty.kind()
|
||||
&& let ty::RawPtr(ty::TypeAndMut { mutbl: hir::Mutability::Not, .. }) =
|
||||
provided_ty.kind()
|
||||
if let ty::RawPtr(_, hir::Mutability::Mut) = expected_ty.kind()
|
||||
&& let ty::RawPtr(_, hir::Mutability::Not) = provided_ty.kind()
|
||||
&& let hir::ExprKind::Call(callee, _) = arg.kind
|
||||
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = callee.kind
|
||||
&& let Res::Def(_, def_id) = path.res
|
||||
|
@ -7,7 +7,6 @@ use crate::hir::is_range_literal;
|
||||
use crate::method::probe;
|
||||
use crate::method::probe::{IsSuggestion, Mode, ProbeScope};
|
||||
use crate::rustc_middle::ty::Article;
|
||||
use crate::ty::TypeAndMut;
|
||||
use core::cmp::min;
|
||||
use core::iter;
|
||||
use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX};
|
||||
@ -1479,7 +1478,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
expected_ty: Ty<'tcx>,
|
||||
) -> bool {
|
||||
// Expected type needs to be a raw pointer.
|
||||
let ty::RawPtr(mutbl, _) = expected_ty.kind() else {
|
||||
let ty::RawPtr(_, mutbl) = expected_ty.kind() else {
|
||||
return false;
|
||||
};
|
||||
|
||||
|
@ -1238,7 +1238,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
|
||||
return None;
|
||||
}
|
||||
|
||||
let &ty::RawPtr(ty::TypeAndMut { ty, mutbl: hir::Mutability::Mut }) = self_ty.kind() else {
|
||||
let &ty::RawPtr(ty, hir::Mutability::Mut) = self_ty.kind() else {
|
||||
return None;
|
||||
};
|
||||
|
||||
|
@ -2477,7 +2477,7 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
|
||||
Adt(..) if ty.is_box() => Some("`Box` must be non-null".into()),
|
||||
FnPtr(..) => Some("function pointers must be non-null".into()),
|
||||
Never => Some("the `!` type has no valid value".into()),
|
||||
RawPtr(tm) if matches!(tm.ty.kind(), Dynamic(..)) =>
|
||||
RawPtr(ty, _) if matches!(ty.kind(), Dynamic(..)) =>
|
||||
// raw ptr to dyn Trait
|
||||
{
|
||||
Some("the vtable of a wide raw pointer must be non-null".into())
|
||||
|
@ -322,10 +322,10 @@ fn structurally_same_type_impl<'tcx>(
|
||||
(Slice(a_ty), Slice(b_ty)) => {
|
||||
structurally_same_type_impl(seen_types, tcx, param_env, *a_ty, *b_ty, ckind)
|
||||
}
|
||||
(RawPtr(a_tymut), RawPtr(b_tymut)) => {
|
||||
a_tymut.mutbl == b_tymut.mutbl
|
||||
(RawPtr(a_ty, a_mutbl), RawPtr(b_ty, b_mutbl)) => {
|
||||
a_mutbl == b_mutbl
|
||||
&& structurally_same_type_impl(
|
||||
seen_types, tcx, param_env, a_tymut.ty, b_tymut.ty, ckind,
|
||||
seen_types, tcx, param_env, *a_ty, *b_ty, ckind,
|
||||
)
|
||||
}
|
||||
(Ref(_a_region, a_ty, a_mut), Ref(_b_region, b_ty, b_mut)) => {
|
||||
|
@ -1,7 +1,7 @@
|
||||
use rustc_ast::Mutability;
|
||||
use rustc_hir::{Expr, ExprKind, UnOp};
|
||||
use rustc_middle::ty::layout::LayoutOf as _;
|
||||
use rustc_middle::ty::{self, layout::TyAndLayout, TypeAndMut};
|
||||
use rustc_middle::ty::{self, layout::TyAndLayout};
|
||||
use rustc_span::sym;
|
||||
|
||||
use crate::{lints::InvalidReferenceCastingDiag, LateContext, LateLintPass, LintContext};
|
||||
@ -153,7 +153,7 @@ fn is_cast_from_ref_to_mut_ptr<'tcx>(
|
||||
let end_ty = cx.typeck_results().node_type(orig_expr.hir_id);
|
||||
|
||||
// Bail out early if the end type is **not** a mutable pointer.
|
||||
if !matches!(end_ty.kind(), ty::RawPtr(TypeAndMut { ty: _, mutbl: Mutability::Mut })) {
|
||||
if !matches!(end_ty.kind(), ty::RawPtr(_, Mutability::Mut)) {
|
||||
return None;
|
||||
}
|
||||
|
||||
|
@ -18,10 +18,10 @@ use rustc_errors::DiagMessage;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::{is_range_literal, Expr, ExprKind, Node};
|
||||
use rustc_middle::ty::layout::{IntegerExt, LayoutOf, SizeSkeleton};
|
||||
use rustc_middle::ty::GenericArgsRef;
|
||||
use rustc_middle::ty::{
|
||||
self, AdtKind, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
|
||||
};
|
||||
use rustc_middle::ty::{GenericArgsRef, TypeAndMut};
|
||||
use rustc_span::def_id::LocalDefId;
|
||||
use rustc_span::source_map;
|
||||
use rustc_span::symbol::sym;
|
||||
@ -1046,7 +1046,7 @@ fn get_nullable_type<'tcx>(
|
||||
}
|
||||
ty::Int(ty) => Ty::new_int(tcx, ty),
|
||||
ty::Uint(ty) => Ty::new_uint(tcx, ty),
|
||||
ty::RawPtr(ty_mut) => Ty::new_ptr(tcx, ty_mut.ty, ty_mut.mutbl),
|
||||
ty::RawPtr(ty, mutbl) => Ty::new_ptr(tcx, ty, mutbl),
|
||||
// As these types are always non-null, the nullable equivalent of
|
||||
// `Option<T>` of these types are their raw pointer counterparts.
|
||||
ty::Ref(_region, ty, mutbl) => Ty::new_ptr(tcx, ty, mutbl),
|
||||
|
@ -69,7 +69,7 @@ impl<'tcx> CastTy<'tcx> {
|
||||
ty::Uint(u) => Some(CastTy::Int(IntTy::U(u))),
|
||||
ty::Float(_) => Some(CastTy::Float),
|
||||
ty::Adt(d, _) if d.is_enum() && d.is_payloadfree() => Some(CastTy::Int(IntTy::CEnum)),
|
||||
ty::RawPtr(mt) => Some(CastTy::Ptr(mt)),
|
||||
ty::RawPtr(ty, mutbl) => Some(CastTy::Ptr(ty::TypeAndMut { ty, mutbl })),
|
||||
ty::FnPtr(..) => Some(CastTy::FnPtr),
|
||||
ty::Dynamic(_, _, ty::DynStar) => Some(CastTy::DynStar),
|
||||
_ => None,
|
||||
|
@ -120,7 +120,7 @@ pub fn simplify_type<'tcx>(
|
||||
ty::Str => Some(SimplifiedType::Str),
|
||||
ty::Array(..) => Some(SimplifiedType::Array),
|
||||
ty::Slice(..) => Some(SimplifiedType::Slice),
|
||||
ty::RawPtr(ptr) => Some(SimplifiedType::Ptr(ptr.mutbl)),
|
||||
ty::RawPtr(_, mutbl) => Some(SimplifiedType::Ptr(mutbl)),
|
||||
ty::Dynamic(trait_info, ..) => match trait_info.principal_def_id() {
|
||||
Some(principal_def_id) if !tcx.trait_is_auto(principal_def_id) => {
|
||||
Some(SimplifiedType::Trait(principal_def_id))
|
||||
@ -286,8 +286,10 @@ impl DeepRejectCtxt {
|
||||
}
|
||||
_ => false,
|
||||
},
|
||||
ty::RawPtr(obl) => match k {
|
||||
ty::RawPtr(imp) => obl.mutbl == imp.mutbl && self.types_may_unify(obl.ty, imp.ty),
|
||||
ty::RawPtr(obl_ty, obl_mutbl) => match *k {
|
||||
ty::RawPtr(imp_ty, imp_mutbl) => {
|
||||
obl_mutbl == imp_mutbl && self.types_may_unify(obl_ty, imp_ty)
|
||||
}
|
||||
_ => false,
|
||||
},
|
||||
ty::Dynamic(obl_preds, ..) => {
|
||||
|
@ -211,8 +211,8 @@ impl FlagComputation {
|
||||
|
||||
&ty::Slice(tt) => self.add_ty(tt),
|
||||
|
||||
ty::RawPtr(m) => {
|
||||
self.add_ty(m.ty);
|
||||
&ty::RawPtr(ty, _) => {
|
||||
self.add_ty(ty);
|
||||
}
|
||||
|
||||
&ty::Ref(r, ty, _) => {
|
||||
|
@ -920,8 +920,8 @@ where
|
||||
let param_env = cx.param_env();
|
||||
|
||||
let pointee_info = match *this.ty.kind() {
|
||||
ty::RawPtr(mt) if offset.bytes() == 0 => {
|
||||
tcx.layout_of(param_env.and(mt.ty)).ok().map(|layout| PointeeInfo {
|
||||
ty::RawPtr(p_ty, _) if offset.bytes() == 0 => {
|
||||
tcx.layout_of(param_env.and(p_ty)).ok().map(|layout| PointeeInfo {
|
||||
size: layout.size,
|
||||
align: layout.align.abi,
|
||||
safe: None,
|
||||
|
@ -263,7 +263,7 @@ fn characteristic_def_id_of_type_cached<'a>(
|
||||
characteristic_def_id_of_type_cached(subty, visited)
|
||||
}
|
||||
|
||||
ty::RawPtr(mt) => characteristic_def_id_of_type_cached(mt.ty, visited),
|
||||
ty::RawPtr(ty, _) => characteristic_def_id_of_type_cached(ty, visited),
|
||||
|
||||
ty::Ref(_, ty, _) => characteristic_def_id_of_type_cached(ty, visited),
|
||||
|
||||
|
@ -667,15 +667,15 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
|
||||
ty::Int(t) => p!(write("{}", t.name_str())),
|
||||
ty::Uint(t) => p!(write("{}", t.name_str())),
|
||||
ty::Float(t) => p!(write("{}", t.name_str())),
|
||||
ty::RawPtr(ref tm) => {
|
||||
ty::RawPtr(ty, mutbl) => {
|
||||
p!(write(
|
||||
"*{} ",
|
||||
match tm.mutbl {
|
||||
match mutbl {
|
||||
hir::Mutability::Mut => "mut",
|
||||
hir::Mutability::Not => "const",
|
||||
}
|
||||
));
|
||||
p!(print(tm.ty))
|
||||
p!(print(ty))
|
||||
}
|
||||
ty::Ref(r, ty, mutbl) => {
|
||||
p!("&");
|
||||
|
@ -560,7 +560,7 @@ impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for Ty<'tcx> {
|
||||
folder: &mut F,
|
||||
) -> Result<Self, F::Error> {
|
||||
let kind = match *self.kind() {
|
||||
ty::RawPtr(tm) => ty::RawPtr(tm.try_fold_with(folder)?),
|
||||
ty::RawPtr(ty, mutbl) => ty::RawPtr(ty.try_fold_with(folder)?, mutbl),
|
||||
ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
|
||||
ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
|
||||
ty::Adt(tid, args) => ty::Adt(tid, args.try_fold_with(folder)?),
|
||||
@ -607,7 +607,7 @@ impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for Ty<'tcx> {
|
||||
impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for Ty<'tcx> {
|
||||
fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
|
||||
match self.kind() {
|
||||
ty::RawPtr(ref tm) => tm.visit_with(visitor),
|
||||
ty::RawPtr(ty, _mutbl) => ty.visit_with(visitor),
|
||||
ty::Array(typ, sz) => {
|
||||
try_visit!(typ.visit_with(visitor));
|
||||
sz.visit_with(visitor)
|
||||
|
@ -1969,11 +1969,7 @@ impl<'tcx> Ty<'tcx> {
|
||||
|
||||
#[inline]
|
||||
pub fn is_mutable_ptr(self) -> bool {
|
||||
matches!(
|
||||
self.kind(),
|
||||
RawPtr(TypeAndMut { mutbl: hir::Mutability::Mut, .. })
|
||||
| Ref(_, _, hir::Mutability::Mut)
|
||||
)
|
||||
matches!(self.kind(), RawPtr(_, hir::Mutability::Mut) | Ref(_, _, hir::Mutability::Mut))
|
||||
}
|
||||
|
||||
/// Get the mutability of the reference or `None` when not a reference
|
||||
@ -2179,7 +2175,7 @@ impl<'tcx> Ty<'tcx> {
|
||||
Some(TypeAndMut { ty: self.boxed_ty(), mutbl: hir::Mutability::Not })
|
||||
}
|
||||
Ref(_, ty, mutbl) => Some(TypeAndMut { ty: *ty, mutbl: *mutbl }),
|
||||
RawPtr(mt) if explicit => Some(*mt),
|
||||
RawPtr(ty, mutbl) if explicit => Some(TypeAndMut { ty: *ty, mutbl: *mutbl }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
@ -158,8 +158,8 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
|
||||
ty::Slice(ty) => {
|
||||
stack.push(ty.into());
|
||||
}
|
||||
ty::RawPtr(mt) => {
|
||||
stack.push(mt.ty.into());
|
||||
ty::RawPtr(ty, _) => {
|
||||
stack.push(ty.into());
|
||||
}
|
||||
ty::Ref(lt, ty, _) => {
|
||||
stack.push(ty.into());
|
||||
|
@ -121,7 +121,7 @@ impl<'tcx> FunctionItemRefChecker<'_, 'tcx> {
|
||||
fn is_fn_ref(ty: Ty<'tcx>) -> Option<(DefId, GenericArgsRef<'tcx>)> {
|
||||
let referent_ty = match ty.kind() {
|
||||
ty::Ref(_, referent_ty, _) => Some(referent_ty),
|
||||
ty::RawPtr(ty_and_mut) => Some(&ty_and_mut.ty),
|
||||
ty::RawPtr(referent_ty, _) => Some(referent_ty),
|
||||
_ => None,
|
||||
};
|
||||
referent_ty
|
||||
|
@ -95,10 +95,9 @@ impl RustcInternal for RigidTy {
|
||||
}
|
||||
RigidTy::Str => rustc_ty::TyKind::Str,
|
||||
RigidTy::Slice(ty) => rustc_ty::TyKind::Slice(ty.internal(tables, tcx)),
|
||||
RigidTy::RawPtr(ty, mutability) => rustc_ty::TyKind::RawPtr(rustc_ty::TypeAndMut {
|
||||
ty: ty.internal(tables, tcx),
|
||||
mutbl: mutability.internal(tables, tcx),
|
||||
}),
|
||||
RigidTy::RawPtr(ty, mutability) => {
|
||||
rustc_ty::TyKind::RawPtr(ty.internal(tables, tcx), mutability.internal(tables, tcx))
|
||||
}
|
||||
RigidTy::Ref(region, ty, mutability) => rustc_ty::TyKind::Ref(
|
||||
region.internal(tables, tcx),
|
||||
ty.internal(tables, tcx),
|
||||
|
@ -683,13 +683,14 @@ fn encode_ty<'tcx>(
|
||||
typeid.push_str(&s);
|
||||
}
|
||||
|
||||
ty::RawPtr(tm) => {
|
||||
ty::RawPtr(ptr_ty, _mutbl) => {
|
||||
// FIXME: This can definitely not be so spaghettified.
|
||||
// P[K]<element-type>
|
||||
let mut s = String::new();
|
||||
s.push_str(&encode_ty(tcx, tm.ty, dict, options));
|
||||
s.push_str(&encode_ty(tcx, *ptr_ty, dict, options));
|
||||
if !ty.is_mutable_ptr() {
|
||||
s = format!("{}{}", "K", &s);
|
||||
compress(dict, DictKey::Ty(tm.ty, TyQ::Const), &mut s);
|
||||
compress(dict, DictKey::Ty(*ptr_ty, TyQ::Const), &mut s);
|
||||
};
|
||||
s = format!("{}{}", "P", &s);
|
||||
compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
|
||||
@ -930,7 +931,7 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio
|
||||
}
|
||||
}
|
||||
|
||||
ty::RawPtr(tm) => {
|
||||
ty::RawPtr(ptr_ty, _) => {
|
||||
if options.contains(TransformTyOptions::GENERALIZE_POINTERS) {
|
||||
if ty.is_mutable_ptr() {
|
||||
ty = Ty::new_mut_ptr(tcx, Ty::new_unit(tcx));
|
||||
@ -939,9 +940,9 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio
|
||||
}
|
||||
} else {
|
||||
if ty.is_mutable_ptr() {
|
||||
ty = Ty::new_mut_ptr(tcx, transform_ty(tcx, tm.ty, options));
|
||||
ty = Ty::new_mut_ptr(tcx, transform_ty(tcx, *ptr_ty, options));
|
||||
} else {
|
||||
ty = Ty::new_imm_ptr(tcx, transform_ty(tcx, tm.ty, options));
|
||||
ty = Ty::new_imm_ptr(tcx, transform_ty(tcx, *ptr_ty, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -361,12 +361,12 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
|
||||
ty.print(self)?;
|
||||
}
|
||||
|
||||
ty::RawPtr(mt) => {
|
||||
self.push(match mt.mutbl {
|
||||
ty::RawPtr(ty, mutbl) => {
|
||||
self.push(match mutbl {
|
||||
hir::Mutability::Not => "P",
|
||||
hir::Mutability::Mut => "O",
|
||||
});
|
||||
mt.ty.print(self)?;
|
||||
ty.print(self)?;
|
||||
}
|
||||
|
||||
ty::Array(ty, len) => {
|
||||
|
@ -2053,8 +2053,8 @@ pub(crate) fn clean_middle_ty<'tcx>(
|
||||
let n = print_const(cx, n);
|
||||
Array(Box::new(clean_middle_ty(bound_ty.rebind(ty), cx, None, None)), n.into())
|
||||
}
|
||||
ty::RawPtr(mt) => {
|
||||
RawPointer(mt.mutbl, Box::new(clean_middle_ty(bound_ty.rebind(mt.ty), cx, None, None)))
|
||||
ty::RawPtr(ty, mutbl) => {
|
||||
RawPointer(mutbl, Box::new(clean_middle_ty(bound_ty.rebind(ty), cx, None, None)))
|
||||
}
|
||||
ty::Ref(r, ty, mutbl) => BorrowedRef {
|
||||
lifetime: clean_middle_region(r),
|
||||
|
Loading…
Reference in New Issue
Block a user