Normalize struct tail type when checking Pointee trait
This commit is contained in:
parent
77497c74f9
commit
5a1c460898
4 changed files with 61 additions and 10 deletions
|
@ -2143,9 +2143,12 @@ impl<'tcx> TyS<'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the type of metadata for (potentially fat) pointers to this type.
|
/// Returns the type of metadata for (potentially fat) pointers to this type.
|
||||||
pub fn ptr_metadata_ty(&'tcx self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
|
pub fn ptr_metadata_ty(
|
||||||
// FIXME: should this normalize?
|
&'tcx self,
|
||||||
let tail = tcx.struct_tail_without_normalization(self);
|
tcx: TyCtxt<'tcx>,
|
||||||
|
normalize: impl FnMut(Ty<'tcx>) -> Ty<'tcx>,
|
||||||
|
) -> Ty<'tcx> {
|
||||||
|
let tail = tcx.struct_tail_with_normalize(self, normalize);
|
||||||
match tail.kind() {
|
match tail.kind() {
|
||||||
// Sized types
|
// Sized types
|
||||||
ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
|
ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
|
||||||
|
|
|
@ -192,7 +192,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||||
pub fn struct_tail_with_normalize(
|
pub fn struct_tail_with_normalize(
|
||||||
self,
|
self,
|
||||||
mut ty: Ty<'tcx>,
|
mut ty: Ty<'tcx>,
|
||||||
normalize: impl Fn(Ty<'tcx>) -> Ty<'tcx>,
|
mut normalize: impl FnMut(Ty<'tcx>) -> Ty<'tcx>,
|
||||||
) -> Ty<'tcx> {
|
) -> Ty<'tcx> {
|
||||||
let recursion_limit = self.recursion_limit();
|
let recursion_limit = self.recursion_limit();
|
||||||
for iteration in 0.. {
|
for iteration in 0.. {
|
||||||
|
|
|
@ -1400,8 +1400,17 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
|
||||||
// Any type with multiple potential metadata types is therefore not eligible.
|
// Any type with multiple potential metadata types is therefore not eligible.
|
||||||
let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
|
let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
|
||||||
|
|
||||||
// FIXME: should this normalize?
|
let tail = selcx.tcx().struct_tail_with_normalize(self_ty, |ty| {
|
||||||
let tail = selcx.tcx().struct_tail_without_normalization(self_ty);
|
normalize_with_depth(
|
||||||
|
selcx,
|
||||||
|
obligation.param_env,
|
||||||
|
obligation.cause.clone(),
|
||||||
|
obligation.recursion_depth + 1,
|
||||||
|
ty,
|
||||||
|
)
|
||||||
|
.value
|
||||||
|
});
|
||||||
|
|
||||||
match tail.kind() {
|
match tail.kind() {
|
||||||
ty::Bool
|
ty::Bool
|
||||||
| ty::Char
|
| ty::Char
|
||||||
|
@ -1435,7 +1444,12 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
|
||||||
| ty::Bound(..)
|
| ty::Bound(..)
|
||||||
| ty::Placeholder(..)
|
| ty::Placeholder(..)
|
||||||
| ty::Infer(..)
|
| ty::Infer(..)
|
||||||
| ty::Error(_) => false,
|
| ty::Error(_) => {
|
||||||
|
if tail.has_infer_types() {
|
||||||
|
candidate_set.mark_ambiguous();
|
||||||
|
}
|
||||||
|
false
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super::ImplSource::Param(..) => {
|
super::ImplSource::Param(..) => {
|
||||||
|
@ -1640,18 +1654,30 @@ fn confirm_pointee_candidate<'cx, 'tcx>(
|
||||||
_: ImplSourcePointeeData,
|
_: ImplSourcePointeeData,
|
||||||
) -> Progress<'tcx> {
|
) -> Progress<'tcx> {
|
||||||
let tcx = selcx.tcx();
|
let tcx = selcx.tcx();
|
||||||
|
|
||||||
let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
|
let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
|
||||||
let substs = tcx.mk_substs([self_ty.into()].iter());
|
|
||||||
|
|
||||||
|
let mut obligations = vec![];
|
||||||
|
let metadata_ty = self_ty.ptr_metadata_ty(tcx, |ty| {
|
||||||
|
normalize_with_depth_to(
|
||||||
|
selcx,
|
||||||
|
obligation.param_env,
|
||||||
|
obligation.cause.clone(),
|
||||||
|
obligation.recursion_depth + 1,
|
||||||
|
ty,
|
||||||
|
&mut obligations,
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
let substs = tcx.mk_substs([self_ty.into()].iter());
|
||||||
let metadata_def_id = tcx.require_lang_item(LangItem::Metadata, None);
|
let metadata_def_id = tcx.require_lang_item(LangItem::Metadata, None);
|
||||||
|
|
||||||
let predicate = ty::ProjectionPredicate {
|
let predicate = ty::ProjectionPredicate {
|
||||||
projection_ty: ty::ProjectionTy { substs, item_def_id: metadata_def_id },
|
projection_ty: ty::ProjectionTy { substs, item_def_id: metadata_def_id },
|
||||||
ty: self_ty.ptr_metadata_ty(tcx),
|
ty: metadata_ty,
|
||||||
};
|
};
|
||||||
|
|
||||||
confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
|
confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
|
||||||
|
.with_addl_obligations(obligations)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn confirm_fn_pointer_candidate<'cx, 'tcx>(
|
fn confirm_fn_pointer_candidate<'cx, 'tcx>(
|
||||||
|
|
22
src/test/ui/traits/pointee-deduction.rs
Normal file
22
src/test/ui/traits/pointee-deduction.rs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
// run-pass
|
||||||
|
|
||||||
|
#![feature(ptr_metadata)]
|
||||||
|
|
||||||
|
use std::alloc::Layout;
|
||||||
|
use std::ptr::Pointee;
|
||||||
|
|
||||||
|
trait Foo {
|
||||||
|
type Bar;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Foo for () {
|
||||||
|
type Bar = ();
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Wrapper1<T: Foo>(<T as Foo>::Bar);
|
||||||
|
struct Wrapper2<T: Foo>(<Wrapper1<T> as Pointee>::Metadata);
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let _: Wrapper2<()> = Wrapper2(());
|
||||||
|
let _ = Layout::new::<Wrapper2<()>>();
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue