Skip to content

Commit 9cdfe03

Browse files
committed
Auto merge of #103390 - compiler-errors:metadata-mod-regions, r=eholk
Check fat pointer metadata compatibility modulo regions Regions don't really mean anything anyways during hir typeck. If this `erase_regions` makes anyone nervous, it's probably equally valid to just equate the types using a type relation, but regardless we should _not_ be using strict type equality while region variables are present. Fixes #103384
2 parents e07425d + 8333253 commit 9cdfe03

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};
@@ -67,7 +68,7 @@ pub struct CastCheck<'tcx> {
6768
/// The kind of pointer and associated metadata (thin, length or vtable) - we
6869
/// only allow casts between fat pointers if their metadata have the same
6970
/// kind.
70-
#[derive(Copy, Clone, PartialEq, Eq)]
71+
#[derive(Debug, Copy, Clone, PartialEq, Eq, TypeVisitable, TypeFoldable)]
7172
enum PointerKind<'tcx> {
7273
/// No metadata attached, ie pointer to sized type or foreign type
7374
Thin,
@@ -76,11 +77,11 @@ enum PointerKind<'tcx> {
7677
/// Slice
7778
Length,
7879
/// The unsize info of this projection
79-
OfProjection(&'tcx ty::ProjectionTy<'tcx>),
80+
OfProjection(ty::ProjectionTy<'tcx>),
8081
/// The unsize info of this opaque ty
8182
OfOpaque(DefId, SubstsRef<'tcx>),
8283
/// The unsize info of this parameter
83-
OfParam(&'tcx ty::ParamTy),
84+
OfParam(ty::ParamTy),
8485
}
8586

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

@@ -909,7 +910,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
909910
}
910911

911912
// vtable kinds must match
912-
if cast_kind == expr_kind {
913+
if fcx.tcx.erase_regions(cast_kind) == fcx.tcx.erase_regions(expr_kind) {
913914
Ok(CastKind::PtrPtrCast)
914915
} else {
915916
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)