Skip to content

Commit 8333253

Browse files
Check fat pointer metadata compatibility modulo regions
1 parent 0940040 commit 8333253

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

compiler/rustc_hir_typeck/src/cast.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use super::FnCtxt;
3333
use crate::type_error_struct;
3434
use rustc_errors::{struct_span_err, Applicability, DelayDm, DiagnosticBuilder, ErrorGuaranteed};
3535
use rustc_hir as hir;
36+
use rustc_macros::{TypeFoldable, TypeVisitable};
3637
use rustc_middle::mir::Mutability;
3738
use rustc_middle::ty::adjustment::AllowTwoPhase;
3839
use rustc_middle::ty::cast::{CastKind, CastTy};
@@ -65,7 +66,7 @@ pub struct CastCheck<'tcx> {
6566
/// The kind of pointer and associated metadata (thin, length or vtable) - we
6667
/// only allow casts between fat pointers if their metadata have the same
6768
/// kind.
68-
#[derive(Copy, Clone, PartialEq, Eq)]
69+
#[derive(Debug, Copy, Clone, PartialEq, Eq, TypeVisitable, TypeFoldable)]
6970
enum PointerKind<'tcx> {
7071
/// No metadata attached, ie pointer to sized type or foreign type
7172
Thin,
@@ -74,11 +75,11 @@ enum PointerKind<'tcx> {
7475
/// Slice
7576
Length,
7677
/// The unsize info of this projection
77-
OfProjection(&'tcx ty::ProjectionTy<'tcx>),
78+
OfProjection(ty::ProjectionTy<'tcx>),
7879
/// The unsize info of this opaque ty
7980
OfOpaque(DefId, SubstsRef<'tcx>),
8081
/// The unsize info of this parameter
81-
OfParam(&'tcx ty::ParamTy),
82+
OfParam(ty::ParamTy),
8283
}
8384

8485
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -119,9 +120,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
119120
// Pointers to foreign types are thin, despite being unsized
120121
ty::Foreign(..) => Some(PointerKind::Thin),
121122
// We should really try to normalize here.
122-
ty::Projection(ref pi) => Some(PointerKind::OfProjection(pi)),
123+
ty::Projection(pi) => Some(PointerKind::OfProjection(pi)),
123124
ty::Opaque(def_id, substs) => Some(PointerKind::OfOpaque(def_id, substs)),
124-
ty::Param(ref p) => Some(PointerKind::OfParam(p)),
125+
ty::Param(p) => Some(PointerKind::OfParam(p)),
125126
// Insufficient type information.
126127
ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) => None,
127128

@@ -903,7 +904,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
903904
}
904905

905906
// vtable kinds must match
906-
if cast_kind == expr_kind {
907+
if fcx.tcx.erase_regions(cast_kind) == fcx.tcx.erase_regions(expr_kind) {
907908
Ok(CastKind::PtrPtrCast)
908909
} else {
909910
Err(CastError::DifferingKinds)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// check-pass
2+
3+
trait Tag<'a> {
4+
type Type: ?Sized;
5+
}
6+
7+
trait IntoRaw: for<'a> Tag<'a> {
8+
fn into_raw(this: *const <Self as Tag<'_>>::Type) -> *mut <Self as Tag<'_>>::Type;
9+
}
10+
11+
impl<T: for<'a> Tag<'a>> IntoRaw for T {
12+
fn into_raw(this: *const <Self as Tag<'_>>::Type) -> *mut <Self as Tag<'_>>::Type {
13+
this as *mut T::Type
14+
}
15+
}
16+
17+
fn main() {}

0 commit comments

Comments
 (0)