diff --git a/compiler/rustc_const_eval/src/interpret/eval_context.rs b/compiler/rustc_const_eval/src/interpret/eval_context.rs index aeac556aad82b..fd0a02c5a0492 100644 --- a/compiler/rustc_const_eval/src/interpret/eval_context.rs +++ b/compiler/rustc_const_eval/src/interpret/eval_context.rs @@ -501,6 +501,11 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { ty::Foreign(_) => interp_ok(None), + ty::UnsafeBinder(inner) => self.size_and_align_from_meta( + metadata, + &TyAndLayout { layout: layout.layout, ty: inner.skip_binder() }, + ), + _ => span_bug!(self.cur_span(), "size_and_align_of::<{}> not supported", layout.ty), } } diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index da40d54f262e1..018400239f01a 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -524,9 +524,9 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> { fn check_wide_ptr_meta( &mut self, meta: MemPlaceMeta, - pointee: TyAndLayout<'tcx>, + ty: Ty<'tcx>, ) -> InterpResult<'tcx> { - let tail = self.ecx.tcx.struct_tail_for_codegen(pointee.ty, self.ecx.typing_env); + let tail = self.ecx.tcx.struct_tail_for_codegen(ty, self.ecx.typing_env); match tail.kind() { ty::Dynamic(data, _) => { let vtable = meta.unwrap_meta().to_pointer(self.ecx); @@ -549,6 +549,9 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> { ty::Foreign(..) => { // Unsized, but not wide. } + &ty::UnsafeBinder(inner) => { + self.check_wide_ptr_meta(meta, inner.skip_binder())?; + } _ => bug!("Unexpected unsized type tail: {:?}", tail), } @@ -577,7 +580,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> { // Handle wide pointers. // Check metadata early, for better diagnostics if place.layout.is_unsized() { - self.check_wide_ptr_meta(place.meta(), place.layout)?; + self.check_wide_ptr_meta(place.meta(), place.layout.ty)?; } // Determine size and alignment of pointee. @@ -928,7 +931,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> { // might actually be invalid (i.e., too big)! let place = self.ecx.imm_ptr_to_mplace(&ptr)?; assert!(place.layout.is_unsized()); - self.check_wide_ptr_meta(place.meta(), place.layout)?; + self.check_wide_ptr_meta(place.meta(), place.layout.ty)?; } interp_ok(true) } diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index 201cb061cf7f4..f8b34cd818826 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -33,7 +33,7 @@ use rustc_errors::codes::*; use rustc_errors::{Applicability, Diag, ErrorGuaranteed}; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::{self as hir, ExprKind}; -use rustc_infer::infer::DefineOpaqueTypes; +use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes}; use rustc_infer::traits::ObligationCauseCode; use rustc_lint_defs::builtin::{TRIVIAL_CASTS, TRIVIAL_NUMERIC_CASTS}; use rustc_macros::{TypeFoldable, TypeVisitable}; @@ -135,7 +135,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Some(&f) => self.pointer_kind(f, span)?, }, - ty::UnsafeBinder(_) => unimplemented!("FIXME(unsafe_binder)"), + ty::UnsafeBinder(inner) => self.infcx.probe(|_| { + let inner_ty = self.infcx.instantiate_binder_with_fresh_vars( + span, + BoundRegionConversionTime::HigherRankedType, + *inner, + ); + self.pointer_kind(inner_ty, span) + })?, // Pointers to foreign types are thin, despite being unsized ty::Foreign(..) => Some(PointerKind::Thin), diff --git a/compiler/rustc_middle/src/ty/context/impl_interner.rs b/compiler/rustc_middle/src/ty/context/impl_interner.rs index 00e36c34cd8ce..3670486cde39a 100644 --- a/compiler/rustc_middle/src/ty/context/impl_interner.rs +++ b/compiler/rustc_middle/src/ty/context/impl_interner.rs @@ -817,6 +817,7 @@ bidirectional_lang_item_map! { // tidy-alphabetical-start DynMetadata, + ManuallyDrop, Option, OwnedBox, Poll, diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 1b97d7bb2c399..3582a98697d9c 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1784,9 +1784,9 @@ impl<'tcx> Ty<'tcx> { pub fn ptr_metadata_ty_or_tail( self, tcx: TyCtxt<'tcx>, - normalize: impl FnMut(Unnormalized<'tcx, Ty<'tcx>>) -> Ty<'tcx>, + mut normalize: impl FnMut(Unnormalized<'tcx, Ty<'tcx>>) -> Ty<'tcx>, ) -> Result, Ty<'tcx>> { - let tail = tcx.struct_tail_raw(self, &ObligationCause::dummy(), normalize, || {}); + let tail = tcx.struct_tail_raw(self, &ObligationCause::dummy(), &mut normalize, || {}); match tail.kind() { // Sized types ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) @@ -1828,7 +1828,7 @@ impl<'tcx> Ty<'tcx> { // metadata of `tail`. ty::Param(_) | ty::Alias(..) => Err(tail), - ty::UnsafeBinder(_) => unimplemented!("FIXME(unsafe_binder)"), + ty::UnsafeBinder(inner) => inner.skip_binder().ptr_metadata_ty_or_tail(tcx, normalize), ty::Infer(ty::TyVar(_)) | ty::Pat(..) @@ -1988,7 +1988,6 @@ impl<'tcx> Ty<'tcx> { | ty::Float(_) | ty::FnDef(..) | ty::FnPtr(..) - | ty::UnsafeBinder(_) | ty::RawPtr(..) | ty::Char | ty::Ref(..) @@ -2022,6 +2021,8 @@ impl<'tcx> Ty<'tcx> { ty::Infer(ty::TyVar(_)) => false, + ty::UnsafeBinder(inner) => inner.skip_binder().has_trivial_sizedness(tcx, sizedness), + ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => { bug!("`has_trivial_sizedness` applied to unexpected type: {:?}", self) } diff --git a/compiler/rustc_next_trait_solver/src/solve/normalizes_to.rs b/compiler/rustc_next_trait_solver/src/solve/normalizes_to.rs index aaec60cffec9d..88b990923e2cf 100644 --- a/compiler/rustc_next_trait_solver/src/solve/normalizes_to.rs +++ b/compiler/rustc_next_trait_solver/src/solve/normalizes_to.rs @@ -1,5 +1,6 @@ use std::debug_assert_matches; +use derive_where::derive_where; use rustc_type_ir::fast_reject::DeepRejectCtxt; use rustc_type_ir::inherent::*; use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverProjectionLangItem, SolverTraitLangItem}; @@ -8,8 +9,8 @@ use rustc_type_ir::solve::{ RerunNonErased, RerunReason, RerunResultExt, }; use rustc_type_ir::{ - self as ty, CallerBoundsIterator, Const, FieldInfo, Interner, NormalizesTo, PredicateKind, - Region, Unnormalized, Upcast as _, + self as ty, BoundVarIndexKind, CallerBoundsIterator, Const, FieldInfo, Interner, NormalizesTo, + PredicateKind, Region, Unnormalized, Upcast as _, fold_regions, }; use tracing::instrument; @@ -159,6 +160,164 @@ where } } +/// Represents one step in the processing of computing `::Metadata`. +/// Depending on what `T` is, we may know different things. +#[derive_where(Debug; I: Interner)] +enum PointeeStep { + /// We know the metadata ty for this type, so we can return it directly + Metadata(I::Ty), + /// The metadata ty for this type is `DynMetadata` for some dyn layout type. + /// + /// For all types without unsafe binders, this should *just* be a normal + /// `DynMetadata`. + /// + /// For types with unsafe binders, this will look like + /// `DynMetadata ManuallyDrop>`. + Dyn(I::Ty), + /// The metadata ty for this type is the metadata ty for some struct tail. + /// For `Tail(U)`, we get `::Metadata`. If this is known-sized, + /// then we can return `()`; otherwise, we return this as a rigid alias. + Tail(I::Ty), + /// If we encounter a rigid alias, a param, or a placeholder, we just + /// fallback to checking if the type is `Sized` and if so, return `()`. + Opaque, +} + +/// Take a single step in computing `::Metadata`. +/// See `PointeeStep` for the different cases we may encounter. +#[tracing::instrument(skip(cx), ret)] +fn take_pointee_step(cx: I, self_ty: I::Ty) -> PointeeStep { + match self_ty.kind() { + ty::Bool + | ty::Char + | ty::Int(..) + | ty::Uint(..) + | ty::Float(..) + | ty::Array(..) + | ty::Pat(..) + | ty::RawPtr(..) + | ty::Ref(..) + | ty::FnDef(..) + | ty::FnPtr(..) + | ty::Closure(..) + | ty::CoroutineClosure(..) + | ty::Infer(ty::IntVar(..) | ty::FloatVar(..)) + | ty::Coroutine(..) + | ty::CoroutineWitness(..) + | ty::Never + | ty::Foreign(..) => PointeeStep::Metadata(Ty::new_unit(cx)), + + ty::Error(e) => PointeeStep::Metadata(Ty::new_error(cx, e)), + + ty::Str | ty::Slice(_) => PointeeStep::Metadata(Ty::new_usize(cx)), + + ty::Dynamic(..) => PointeeStep::Dyn(self_ty), + + ty::Alias(ty::IsRigid::Yes, _) | ty::Param(_) | ty::Placeholder(..) => PointeeStep::Opaque, + + ty::Adt(def, args) + if def.is_struct() + && let Some(tail_ty) = def.struct_tail_ty(cx) => + { + PointeeStep::Tail(tail_ty.instantiate(cx, args).skip_norm_wip()) + } + ty::Adt(..) => PointeeStep::Metadata(Ty::new_unit(cx)), + + ty::Tuple(elements) if let Some(tail_ty) = elements.last() => PointeeStep::Tail(tail_ty), + ty::Tuple(..) => PointeeStep::Metadata(Ty::new_unit(cx)), + + ty::UnsafeBinder(binder) => take_unsafe_binder_step(cx, binder.into()), + + ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) + | ty::Alias(ty::IsRigid::No, _) + | ty::Bound(..) => { + panic!("unexpected self ty `{:?}` when normalizing `::Metadata`", self_ty) + } + + // FIXME(scrabsha) + ty::View(..) => unimplemented!(), + } +} + +/// If we encounter an `unsafe<..> X` type, then we need to be a bit clever in +/// the metadata that we return. There are a couple things to consider: +/// - If we see nested unsafe binders, we combine both into a single under binder, +/// which may bind some inner metadata ty. +/// - If we see nested `ManuallyDrop` types, then we just collapse them into as single one. +/// - If the eventual metadata ty is a `dyn Trait + 'a`, then the metadata is +/// `DynMetadata ManuallyDrop>`, not `DynMetadata`. +/// This is because the `unsafe<'a> DynMetadata` doesn't +/// implement the necessary `Pointee::Metadata` bounds. Furthermore, we can't +/// return `DynMetadata dyn Trait + 'a>` because that type is not well-formed. +/// We also don't return `DynMetadata`, because we don't +/// want to lie about lifetimes lie that. +fn take_unsafe_binder_step(cx: I, binder: ty::Binder) -> PointeeStep { + let manually_drop = cx.require_adt_lang_item(SolverAdtLangItem::ManuallyDrop); + let as_manually_drop = |ty: I::Ty| { + if let ty::Adt(def, args) = ty.kind() + && def.def_id() == manually_drop + { + Some(args.as_slice()[0].expect_ty()) + } else { + None + } + }; + let mk_unsafe_binder = |inner: I::Ty, bound_vars: I::BoundVarKinds| { + let manually_drop_args = cx.mk_args_from_iter([I::GenericArg::from(inner)].into_iter()); + let manually_drop_self = Ty::new_adt(cx, cx.adt_def(manually_drop), manually_drop_args); + Ty::new_unsafe_binder(cx, ty::Binder::bind_with_vars(manually_drop_self, bound_vars)) + }; + + // `unsafe<..> X` means `X` is `ManuallyDrop` or `Copy` (and thus metadata is `()`, because `trait Copy: Sized`). + let Some(bound_ty) = as_manually_drop(binder.skip_binder()) else { + return PointeeStep::Metadata(Ty::new_unit(cx)); + }; + + match bound_ty.kind() { + // `unsafe<'a> ManuallyDrop X>` => `unsafe<'a, 'b> ManuallyDrop`. + ty::UnsafeBinder(inner) => { + let outer_bound_vars = binder.bound_vars(); + let inner_bound_vars = inner.bound_vars(); + + let inner = fold_regions(cx, inner.skip_binder(), |r, curr_db| match r.kind() { + ty::ReBound(BoundVarIndexKind::Bound(db), br) if db == curr_db => { + let bound_region = ty::BoundRegion { + kind: br.kind, + var: ty::BoundVar::from_u32( + br.var.as_u32() + outer_bound_vars.len() as u32, + ), + }; + ty::Region::new_bound(cx, db, bound_region) + } + ty::ReBound(BoundVarIndexKind::Bound(db), br) if db == (curr_db + 1) => { + ty::Region::new_bound(cx, curr_db, br) + } + _ => r, + }); + + let bound_vars = BoundVarKinds::from_vars( + cx, + outer_bound_vars.iter().chain(inner_bound_vars.iter()), + ); + PointeeStep::Tail(mk_unsafe_binder(inner, bound_vars)) + } + // `unsafe<..> MD>` => `unsafe<..> MD`. + _ if let Some(inner) = as_manually_drop(bound_ty) => { + PointeeStep::Tail(mk_unsafe_binder(inner, binder.bound_vars())) + } + _ => match take_pointee_step(cx, bound_ty) { + PointeeStep::Metadata(metadata) => PointeeStep::Metadata(metadata), + PointeeStep::Tail(tail) => { + PointeeStep::Tail(mk_unsafe_binder(tail, binder.bound_vars())) + } + PointeeStep::Dyn(dyn_ty) => { + PointeeStep::Dyn(mk_unsafe_binder(dyn_ty, binder.bound_vars())) + } + PointeeStep::Opaque => PointeeStep::Opaque, + }, + } +} + impl assembly::GoalKind for NormalizesTo where D: SolverDelegate, @@ -745,38 +904,19 @@ where ty::AliasTermKind::ProjectionTy { def_id: metadata_def_id }, goal.predicate.alias.kind ); - let metadata_ty = match goal.predicate.self_ty().kind() { - ty::Bool - | ty::Char - | ty::Int(..) - | ty::Uint(..) - | ty::Float(..) - | ty::Array(..) - | ty::Pat(..) - | ty::RawPtr(..) - | ty::Ref(..) - | ty::FnDef(..) - | ty::FnPtr(..) - | ty::Closure(..) - | ty::CoroutineClosure(..) - | ty::Infer(ty::IntVar(..) | ty::FloatVar(..)) - | ty::Coroutine(..) - | ty::CoroutineWitness(..) - | ty::Never - | ty::Foreign(..) => Ty::new_unit(cx), - - ty::Error(e) => Ty::new_error(cx, e), - ty::Str | ty::Slice(_) => Ty::new_usize(cx), - - ty::Dynamic(_, _) => { + let metadata_ty = match take_pointee_step(cx, goal.predicate.self_ty()) { + PointeeStep::Metadata(metadata_ty) => metadata_ty, + PointeeStep::Dyn(dyn_ty) => { let dyn_metadata = cx.require_adt_lang_item(SolverAdtLangItem::DynMetadata); cx.type_of(dyn_metadata.into()) - .instantiate(cx, &[I::GenericArg::from(goal.predicate.self_ty())]) + .instantiate(cx, &[I::GenericArg::from(dyn_ty)]) .skip_norm_wip() } - - ty::Alias(ty::IsRigid::Yes, _) | ty::Param(_) | ty::Placeholder(..) => { + PointeeStep::Tail(tail_ty) => { + Ty::new_projection(cx, ty::IsRigid::No, metadata_def_id, [tail_ty]) + } + PointeeStep::Opaque => { // This is the "fallback impl" for type parameters, unnormalizable projections // and opaque types: If the `self_ty` is `Sized`, then the metadata is `()`. // FIXME(ptr_metadata): This impl overlaps with the other impls and shouldn't @@ -804,41 +944,7 @@ where }) }); } - - ty::Adt(def, args) if def.is_struct() => match def.struct_tail_ty(cx) { - None => Ty::new_unit(cx), - Some(tail_ty) => Ty::new_projection( - cx, - ty::IsRigid::No, - metadata_def_id, - [tail_ty.instantiate(cx, args).skip_norm_wip()], - ), - }, - ty::Adt(_, _) => Ty::new_unit(cx), - - ty::Tuple(elements) => match elements.last() { - None => Ty::new_unit(cx), - Some(tail_ty) => { - Ty::new_projection(cx, ty::IsRigid::No, metadata_def_id, [tail_ty]) - } - }, - - ty::UnsafeBinder(_) => { - // FIXME(unsafe_binder): Figure out how to handle pointee for unsafe binders. - unimplemented!() - } - - ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) - | ty::Alias(ty::IsRigid::No, _) - | ty::Bound(..) => panic!( - "unexpected self ty `{:?}` when normalizing `::Metadata`", - goal.predicate.self_ty() - ), - - // FIXME(scrabsha) - ty::View(..) => unimplemented!(), }; - ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| { ecx.instantiate_normalizes_to_term(goal, metadata_ty.into())?; ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 1a37f4920c0c3..1a1a6483c8adb 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -1149,7 +1149,10 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( // Integers and floats always have `u8` as their discriminant. | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true, - ty::UnsafeBinder(_) => unimplemented!("FIXME(unsafe_binder)"), + ty::UnsafeBinder(_) => { + selcx.tcx().dcx().err("feature(unsafe_binders) requires -Znext-solver"); + false + } // type parameters, opaques, and unnormalized projections don't have // a known discriminant and may need to be normalized further or rely @@ -1239,7 +1242,10 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( true } - ty::UnsafeBinder(_) => unimplemented!("FIXME(unsafe_binder)"), + ty::UnsafeBinder(_) => { + selcx.tcx().dcx().err("feature(unsafe_binders) requires -Znext-solver"); + false + } // FIXME(compiler-errors): are Bound and Placeholder types ever known sized? ty::Param(_) diff --git a/compiler/rustc_type_ir/src/lang_items.rs b/compiler/rustc_type_ir/src/lang_items.rs index 5cf5df714fd68..8162dd4176904 100644 --- a/compiler/rustc_type_ir/src/lang_items.rs +++ b/compiler/rustc_type_ir/src/lang_items.rs @@ -20,6 +20,7 @@ pub enum SolverProjectionLangItem { pub enum SolverAdtLangItem { // tidy-alphabetical-start DynMetadata, + ManuallyDrop, Option, OwnedBox, Poll, diff --git a/tests/crashes/154871.rs b/tests/crashes/154871.rs deleted file mode 100644 index c106028623d75..0000000000000 --- a/tests/crashes/154871.rs +++ /dev/null @@ -1,7 +0,0 @@ -//@ known-bug: #154871 -struct Struct { - b: unsafe<> (), -} -fn main() { - std::ptr::null::; -} diff --git a/tests/ui/unsafe-binders/layout.rs b/tests/ui/unsafe-binders/layout.rs new file mode 100644 index 0000000000000..1c07ca95f39e7 --- /dev/null +++ b/tests/ui/unsafe-binders/layout.rs @@ -0,0 +1,27 @@ +//@ compile-flags: -Znext-solver +//@ check-pass + +// An unsafe binder has the layout of its inner type, niches included. + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features)] + +use std::fmt::Debug; +use std::mem::{ManuallyDrop as MD, align_of, size_of}; +use std::num::NonZero; +use std::ptr::DynMetadata; + +const _: () = assert!(size_of:: &'a u8>() == size_of::<&u8>()); +const _: () = assert!(size_of:: &'a u8>>() == size_of::<&u8>()); +const _: () = assert!(size_of:: NonZero>>() == 4); +const _: () = assert!(align_of:: (&'a u8, u16)>() == align_of::<(&u8, u16)>()); + +// The vtable niche survives a binder in the metadata type and in the pointee. +type BoundMeta = DynMetadata MD>; +const _: () = assert!(size_of::>() == size_of::()); +const _: () = assert!( + size_of:: MD>>() + == size_of::<*const unsafe<'a> MD>() +); + +fn main() {} diff --git a/tests/ui/unsafe-binders/pointee/alias-tail-bound-lifetime-no-leak.rs b/tests/ui/unsafe-binders/pointee/alias-tail-bound-lifetime-no-leak.rs new file mode 100644 index 0000000000000..c6e88e58fdc84 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/alias-tail-bound-lifetime-no-leak.rs @@ -0,0 +1,32 @@ +//@ compile-flags: -Znext-solver + +// We should only be able to normalize an alias within an unsafe binder if that +// normalization would hold when universally qualified over the bound regions. + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features, dead_code)] + +use std::mem::ManuallyDrop as MD; +use std::ptr::Pointee; + +trait Tr { + type Assoc<'a>: ?Sized; +} + +fn metadata_is, M>() {} + +fn only_static() +where + T::Assoc<'static>: Pointee, +{ + metadata_is:: MD>, usize>(); //~ ERROR type mismatch resolving +} + +fn only_one<'x, T: Tr>() +where + T::Assoc<'x>: Pointee, +{ + metadata_is:: MD>, usize>(); //~ ERROR type mismatch resolving +} + +fn main() {} diff --git a/tests/ui/unsafe-binders/pointee/alias-tail-bound-lifetime-no-leak.stderr b/tests/ui/unsafe-binders/pointee/alias-tail-bound-lifetime-no-leak.stderr new file mode 100644 index 0000000000000..278a478170d3e --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/alias-tail-bound-lifetime-no-leak.stderr @@ -0,0 +1,35 @@ +error[E0271]: type mismatch resolving ` ManuallyDrop<::Assoc<'a>> as Pointee>::Metadata == usize` + --> $DIR/alias-tail-bound-lifetime-no-leak.rs:22:19 + | +LL | metadata_is:: MD>, usize>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found associated type + | + = note: expected type `usize` + found associated type ` ManuallyDrop<::Assoc<'a>> as Pointee>::Metadata` + = help: consider constraining the associated type ` ManuallyDrop<::Assoc<'a>> as Pointee>::Metadata` to `usize` + = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html +note: required by a bound in `metadata_is` + --> $DIR/alias-tail-bound-lifetime-no-leak.rs:16:36 + | +LL | fn metadata_is, M>() {} + | ^^^^^^^^^^^^ required by this bound in `metadata_is` + +error[E0271]: type mismatch resolving ` ManuallyDrop<::Assoc<'a>> as Pointee>::Metadata == usize` + --> $DIR/alias-tail-bound-lifetime-no-leak.rs:29:19 + | +LL | metadata_is:: MD>, usize>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found associated type + | + = note: expected type `usize` + found associated type ` ManuallyDrop<::Assoc<'a>> as Pointee>::Metadata` + = help: consider constraining the associated type ` ManuallyDrop<::Assoc<'a>> as Pointee>::Metadata` to `usize` + = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html +note: required by a bound in `metadata_is` + --> $DIR/alias-tail-bound-lifetime-no-leak.rs:16:36 + | +LL | fn metadata_is, M>() {} + | ^^^^^^^^^^^^ required by this bound in `metadata_is` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0271`. diff --git a/tests/ui/unsafe-binders/pointee/alias-tail-bound-lifetime.rs b/tests/ui/unsafe-binders/pointee/alias-tail-bound-lifetime.rs new file mode 100644 index 0000000000000..aa536126b931c --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/alias-tail-bound-lifetime.rs @@ -0,0 +1,74 @@ +//@ compile-flags: -Znext-solver +//@ check-fail +//@ known-bug: #130516 + +// Tests where an alias ends up as the metadata ty and includes lifetimes +// from an unsafe binder. + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features, dead_code)] + +use std::mem::ManuallyDrop as MD; +use std::ptr::{self, Pointee}; + +trait Tr { + type Assoc<'a>: ?Sized; +} + +fn metadata_is, M>() {} +fn same_metadata() +where + A: Pointee::Metadata>, +{ +} + +struct W<'a, T: Tr>(u8, T::Assoc<'a>); + +// This is rigid and we can't normalize this. +fn round_trip(p: *const unsafe<'a> MD>) -> *const unsafe<'a> MD> { + let m = ptr::metadata(p); + let _copy = m; + ptr::from_raw_parts(p as *const (), m) +} + +// We should be able to use the where-clause to know that the metadata is `usize`. +fn higher_ranked_where_clause() +where + for<'a> T::Assoc<'a>: Pointee, +{ + metadata_is:: MD>, usize>(); +} + +// Same, for a `Sized` alias. +fn higher_ranked_sized() +where + for<'a> T::Assoc<'a>: Sized, +{ + metadata_is:: MD>, ()>(); +} + +fn main() {} + +// These are rigid and we can't normalize them. +fn nested_tail_rigid() { + same_metadata:: MD<(u8, T::Assoc<'a>)>, unsafe<'a> MD>>(); + same_metadata:: MD>, unsafe<'a> MD>>(); +} + +// Again, should be able to normalize from the where-clause. +fn nested_tail_where_clause() +where + for<'a> T::Assoc<'a>: Pointee, +{ + metadata_is:: MD<(u8, T::Assoc<'a>)>, usize>(); + metadata_is:: MD>, usize>(); +} + +// And, similarly. +fn nested_tail_sized() +where + for<'a> T::Assoc<'a>: Sized, +{ + metadata_is:: MD<(u8, T::Assoc<'a>)>, ()>(); + metadata_is:: MD>, ()>(); +} diff --git a/tests/ui/unsafe-binders/pointee/alias-tail-bound-lifetime.stderr b/tests/ui/unsafe-binders/pointee/alias-tail-bound-lifetime.stderr new file mode 100644 index 0000000000000..60c9cec4e1d50 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/alias-tail-bound-lifetime.stderr @@ -0,0 +1,51 @@ +error[E0271]: type mismatch resolving ` ManuallyDrop<::Assoc<'a>> as Pointee>::Metadata == usize` + --> $DIR/alias-tail-bound-lifetime.rs:39:19 + | +LL | metadata_is:: MD>, usize>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found associated type + | + = note: expected type `usize` + found associated type ` ManuallyDrop<::Assoc<'a>> as Pointee>::Metadata` + = help: consider constraining the associated type ` ManuallyDrop<::Assoc<'a>> as Pointee>::Metadata` to `usize` + = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html +note: required by a bound in `metadata_is` + --> $DIR/alias-tail-bound-lifetime.rs:18:36 + | +LL | fn metadata_is, M>() {} + | ^^^^^^^^^^^^ required by this bound in `metadata_is` + +error[E0271]: type mismatch resolving ` ManuallyDrop<(u8, ::Assoc<'a>)> as Pointee>::Metadata == usize` + --> $DIR/alias-tail-bound-lifetime.rs:63:19 + | +LL | metadata_is:: MD<(u8, T::Assoc<'a>)>, usize>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found associated type + | + = note: expected type `usize` + found associated type ` ManuallyDrop<::Assoc<'a>> as Pointee>::Metadata` + = help: consider constraining the associated type ` ManuallyDrop<::Assoc<'a>> as Pointee>::Metadata` to `usize` + = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html +note: required by a bound in `metadata_is` + --> $DIR/alias-tail-bound-lifetime.rs:18:36 + | +LL | fn metadata_is, M>() {} + | ^^^^^^^^^^^^ required by this bound in `metadata_is` + +error[E0271]: type mismatch resolving ` ManuallyDrop> as Pointee>::Metadata == usize` + --> $DIR/alias-tail-bound-lifetime.rs:64:19 + | +LL | metadata_is:: MD>, usize>(); + | ^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found associated type + | + = note: expected type `usize` + found associated type ` ManuallyDrop<::Assoc<'a>> as Pointee>::Metadata` + = help: consider constraining the associated type ` ManuallyDrop<::Assoc<'a>> as Pointee>::Metadata` to `usize` + = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html +note: required by a bound in `metadata_is` + --> $DIR/alias-tail-bound-lifetime.rs:18:36 + | +LL | fn metadata_is, M>() {} + | ^^^^^^^^^^^^ required by this bound in `metadata_is` + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0271`. diff --git a/tests/ui/unsafe-binders/pointee/cast-dyn-lifetime-extension.rs b/tests/ui/unsafe-binders/pointee/cast-dyn-lifetime-extension.rs new file mode 100644 index 0000000000000..be7d9cb031fbf --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/cast-dyn-lifetime-extension.rs @@ -0,0 +1,78 @@ +//@ compile-flags: -Znext-solver +//@ check-fail +//@ known-bug: #130516 + +// Casting out of an unsafe binder should require an `unwrap_binder!` call.. + +#![feature(unsafe_binders)] +#![allow(incomplete_features, dead_code)] + +use std::fmt::Debug; +use std::mem::ManuallyDrop as MD; +use std::unsafe_binder::unwrap_binder; + +trait Tr<'a> {} + +fn direct<'x>(p: *const MD) -> *const MD { + p as _ +} + +fn through_binder<'x>(p: *const MD) -> *const MD { + let q = p as *const unsafe<'a> MD; + // FIXME(unsafe_binders): this should be an error, like in `direct`. + q as _ +} + +// Casting out can't pick any lifetime for the bound region, not just `'static`. +fn out_to_param<'x>(q: *const unsafe<'a> MD) -> *const MD { + q as _ +} + +// The same holds for bound regions in the principal trait's arguments, for +// trait objects without a principal, and for `dyn` tails reached through a tuple. +fn out_principal_arg( + q: *const unsafe<'a> MD + 'static>, +) -> *const MD + 'static> { + q as _ +} +fn out_no_principal(q: *const unsafe<'a> MD) -> *const MD { + q as _ +} +fn out_through_tuple( + q: *const unsafe<'a> MD<(u8, dyn Debug + 'a)>, +) -> *const MD<(u8, dyn Debug + 'static)> { + q as _ +} + +// The same holds for `*mut` pointers. +fn out_mut(q: *mut unsafe<'a> MD) -> *mut MD { + q as _ +} +fn out_const_from_mut(q: *mut unsafe<'a> MD) -> *const MD { + q as _ +} + +// Casting into a binder, and between binders, is fine: the target's bound +// regions can be anything. +fn into_binder<'x>(p: *const MD + 'x>) -> *const unsafe<'a> MD + 'a> { + p as _ +} +fn binder_to_binder( + q: *const unsafe<'a> MD + 'a>, +) -> *const unsafe<'b> MD + 'b> { + q as _ +} + +// Unwrapping through the pointer is the way out. +fn out_via_unwrap(q: *const unsafe<'a> MD) -> *const MD { + unsafe { &raw const unwrap_binder!(*q) } +} + +fn into_binder_mut<'x>(p: *mut MD) -> *mut unsafe<'a> MD { + p as _ +} +fn out_via_unwrap_mut(q: *mut unsafe<'a> MD) -> *mut MD { + unsafe { &raw mut unwrap_binder!(*q) } +} + +fn main() {} diff --git a/tests/ui/unsafe-binders/pointee/cast-dyn-lifetime-extension.stderr b/tests/ui/unsafe-binders/pointee/cast-dyn-lifetime-extension.stderr new file mode 100644 index 0000000000000..9efe64d915694 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/cast-dyn-lifetime-extension.stderr @@ -0,0 +1,28 @@ +error: lifetime may not live long enough + --> $DIR/cast-dyn-lifetime-extension.rs:17:5 + | +LL | fn direct<'x>(p: *const MD) -> *const MD { + | -- lifetime `'x` defined here +LL | p as _ + | ^^^^^^ returning this value requires that `'x` must outlive `'static` + | +note: raw pointer casts of trait objects cannot extend lifetimes + --> $DIR/cast-dyn-lifetime-extension.rs:17:5 + | +LL | p as _ + | ^^^^^^ + = note: this was previously accepted by the compiler but was changed recently + = help: see for more information +help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `p` + | +LL - fn direct<'x>(p: *const MD) -> *const MD { +LL + fn direct<'x>(p: *const MD) -> *const MD { + | +help: alternatively, add an explicit `'static` bound to this reference + | +LL - fn direct<'x>(p: *const MD) -> *const MD { +LL + fn direct<'x>(p: *const ManuallyDrop<(dyn Debug + 'static)>) -> *const MD { + | + +error: aborting due to 1 previous error + diff --git a/tests/ui/unsafe-binders/pointee/casts-reject.rs b/tests/ui/unsafe-binders/pointee/casts-reject.rs new file mode 100644 index 0000000000000..819ee2316ec81 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/casts-reject.rs @@ -0,0 +1,25 @@ +//@ compile-flags: -Znext-solver + +// Casts with unsafe binders should not change the metadata kind or the +// principal trait, or add auto traits. + +#![feature(unsafe_binders)] +#![allow(incomplete_features, dead_code)] + +use std::fmt::{Debug, Display}; +use std::mem::ManuallyDrop as MD; + +fn thin_to_wide(p: *const u8) -> *const unsafe<> MD<[u8]> { + p as _ //~ ERROR cannot cast thin pointer +} +fn slice_to_dyn(p: *const MD<[u8]>) -> *const unsafe<'a> MD { + p as _ //~ ERROR is invalid +} +fn change_principal(p: *const MD) -> *const unsafe<'a> MD { + p as _ //~ ERROR is invalid +} +fn add_auto_trait(p: *const MD) -> *const unsafe<'a> MD { + p as _ //~ ERROR cannot add auto trait `Send` +} + +fn main() {} diff --git a/tests/ui/unsafe-binders/pointee/casts-reject.stderr b/tests/ui/unsafe-binders/pointee/casts-reject.stderr new file mode 100644 index 0000000000000..b931dd13078ec --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/casts-reject.stderr @@ -0,0 +1,35 @@ +error[E0607]: cannot cast thin pointer `*const u8` to wide pointer `*const unsafe<> ManuallyDrop<[u8]>` + --> $DIR/casts-reject.rs:13:5 + | +LL | p as _ + | ^^^^^^ + +error[E0606]: casting `*const ManuallyDrop<[u8]>` as `*const unsafe<'a> ManuallyDrop<(dyn Debug + 'a)>` is invalid + --> $DIR/casts-reject.rs:16:5 + | +LL | p as _ + | ^^^^^^ + | + = note: the pointers have different metadata + +error[E0606]: casting `*const ManuallyDrop<(dyn Debug + 'static)>` as `*const unsafe<'a> ManuallyDrop<(dyn std::fmt::Display + 'a)>` is invalid + --> $DIR/casts-reject.rs:19:5 + | +LL | p as _ + | ^^^^^^ + | + = note: the trait objects may have different vtables + +error[E0804]: cannot add auto trait `Send` to dyn bound via pointer cast + --> $DIR/casts-reject.rs:22:5 + | +LL | p as _ + | ^^^^^^ unsupported cast + | + = note: this could allow UB elsewhere + = help: use `transmute` if you're sure this is sound + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0606, E0607, E0804. +For more information about an error, try `rustc --explain E0606`. diff --git a/tests/ui/unsafe-binders/pointee/casts.rs b/tests/ui/unsafe-binders/pointee/casts.rs new file mode 100644 index 0000000000000..325b887d830b3 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/casts.rs @@ -0,0 +1,40 @@ +//@ compile-flags: -Znext-solver +//@ check-pass + +// Pointer casts to and from binders where the metadata kind matches. + +#![feature(unsafe_binders)] +#![allow(incomplete_features, dead_code)] + +use std::fmt::Debug; +use std::mem::ManuallyDrop as MD; + +trait Tr { + type Assoc<'a>: ?Sized; +} + +fn slice_in(p: *const MD<[u8]>) -> *const unsafe<> MD<[u8]> { + p as _ +} +fn slice_out(p: *const unsafe<> MD<[u8]>) -> *const MD<[u8]> { + p as _ +} +fn dyn_in<'x>(p: *const MD) -> *const unsafe<'a> MD { + p as _ +} +fn to_thin(p: *const unsafe<'a> MD) -> *const u8 { + p as _ +} +fn param(p: *const MD) -> *const unsafe<'a> MD<(&'a u8, U)> { + p as _ +} +fn alias_identity( + p: *const unsafe<'a> MD>, +) -> *const unsafe<'b> MD> { + p as _ +} +fn alias_in<'x, T: Tr>(p: *const MD>) -> *const unsafe<'a> MD> { + p as _ +} + +fn main() {} diff --git a/tests/ui/unsafe-binders/pointee/const-eval.rs b/tests/ui/unsafe-binders/pointee/const-eval.rs new file mode 100644 index 0000000000000..4fd22ff8b090e --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/const-eval.rs @@ -0,0 +1,20 @@ +//@ compile-flags: -Znext-solver +//@ check-pass + +// Const-eval computes the metadata and dynamic size of binders on its own path. + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features, dead_code)] + +use std::mem::{ManuallyDrop as MD, size_of_val}; +use std::ptr; + +const S: &[u8] = &[1, 2, 3]; +const P: *const unsafe<> MD<[u8]> = S as *const [u8] as *const MD<[u8]> as *const _; + +const LEN: usize = ptr::metadata(P); +const _: () = assert!(LEN == 3); +const SIZE: usize = unsafe { size_of_val(&*P) }; +const _: () = assert!(SIZE == 3); + +fn main() {} diff --git a/tests/ui/unsafe-binders/pointee/dyn-tail-bound-lifetime.rs b/tests/ui/unsafe-binders/pointee/dyn-tail-bound-lifetime.rs new file mode 100644 index 0000000000000..e9540f11740c2 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/dyn-tail-bound-lifetime.rs @@ -0,0 +1,80 @@ +//@ compile-flags: -Znext-solver +//@ check-pass + +// The metadata of a type with a `dyn Trait` under an unsafe binder is a bit weird: +// It ends up being a `DynMetadata ManuallyDrop>>`, +// because this is the only way to express the metadata in such a way that is +// well-formed, meets the `Pointee::Metadata` bounds, and doesn't transmute +// unsafe binder lifetimes to `'static`. + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features, dead_code)] + +use std::fmt::Debug; +use std::mem::ManuallyDrop as MD; +use std::ptr::{self, DynMetadata, Pointee}; + +trait Tr<'a> {} + +type B1 = unsafe<'a> MD; +type B2 = unsafe<'a> MD + 'a>; +type B3 = unsafe<'a> MD + 'a>; +type B4 = unsafe<'a> MD<(&'a u8, dyn Debug + 'a)>; + +// The `dyn` tail is reached through a struct that isn't `ManuallyDrop`. Only the +// type directly inside the binder has to be `Copy` or `ManuallyDrop`, so `W` can +// be unsized. +struct W<'a>(u8, MD); +type B5 = unsafe<'a> MD<(u8, W<'a>)>; + +// Only the bound regions that occur in the `dyn` tail are re-bound. +type B6 = unsafe<'a, 'b> MD<(&'b u8, dyn Tr<'a> + 'a)>; + +// Nested `ManuallyDrop`s collapse. +type B7 = unsafe<'a> MD>; + +fn metadata_is, M>() {} + +fn exact_metadata() { + metadata_is:: MD>>(); + metadata_is:: MD + 'a>>>(); + metadata_is:: MD + 'a>>>(); + metadata_is:: MD>>(); + metadata_is:: MD>>(); + metadata_is:: MD + 'a>>>(); + metadata_is:: MD>>(); +} + +// Free regions in the `dyn` stay free; only the bound ones are re-bound. +fn mixed_free_and_bound<'x>() { + metadata_is:: MD + 'a>, DynMetadata MD + 'a>>>(); +} + +fn round_trip_b1(p: *const B1) -> *const B1 { + ptr::from_raw_parts(p as *const (), ptr::metadata(p)) +} +fn round_trip_b2(p: *const B2) -> *const B2 { + ptr::from_raw_parts(p as *const (), ptr::metadata(p)) +} +fn round_trip_b3(p: *const B3) -> *const B3 { + ptr::from_raw_parts(p as *const (), ptr::metadata(p)) +} +fn round_trip_b4(p: *const B4) -> *const B4 { + ptr::from_raw_parts(p as *const (), ptr::metadata(p)) +} +fn round_trip_b5(p: *const B5) -> *const B5 { + ptr::from_raw_parts(p as *const (), ptr::metadata(p)) +} + +// `Pointee::Metadata`'s item bounds must hold for whatever the metadata is. +fn item_bounds(p: *const B1) { + fn bounds(_: M) {} + bounds(ptr::metadata(p)); +} + +// The metadata is a `DynMetadata`, so its methods are available. +fn dyn_metadata_methods(p: *const B1) -> usize { + ptr::metadata(p).size_of() +} + +fn main() {} diff --git a/tests/ui/unsafe-binders/pointee/dyn-tail-free-lifetime.rs b/tests/ui/unsafe-binders/pointee/dyn-tail-free-lifetime.rs new file mode 100644 index 0000000000000..ed9235fb73d99 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/dyn-tail-free-lifetime.rs @@ -0,0 +1,24 @@ +//@ compile-flags: -Znext-solver +//@ check-fail +//@ known-bug: #130516 + +// If a `dyn Trait` does not mention any lifetimes from an unsafe binder, then +// the `DynMetadata` should be "normal" and not mention the binder or `ManuallyDrop`. + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features, dead_code)] + +use std::fmt::Debug; +use std::mem::ManuallyDrop as MD; +use std::ptr::{DynMetadata, Pointee}; + +fn metadata_is, M>() {} + +trait Tr<'x> {} + +fn f<'x>() { + metadata_is:: MD<(&'a u8, dyn Debug)>, DynMetadata>(); + metadata_is:: MD + 'x>, DynMetadata + 'x>>(); +} + +fn main() {} diff --git a/tests/ui/unsafe-binders/pointee/dyn-tail-free-lifetime.stderr b/tests/ui/unsafe-binders/pointee/dyn-tail-free-lifetime.stderr new file mode 100644 index 0000000000000..814ba9bf1b2a7 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/dyn-tail-free-lifetime.stderr @@ -0,0 +1,32 @@ +error[E0271]: type mismatch resolving ` ManuallyDrop<(&u8, dyn Debug)> as Pointee>::Metadata == DynMetadata` + --> $DIR/dyn-tail-free-lifetime.rs:20:19 + | +LL | metadata_is:: MD<(&'a u8, dyn Debug)>, DynMetadata>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `DynMetadata`, found `DynMetadata ManuallyDrop<_>>` + | + = note: expected struct `DynMetadata` + found struct `DynMetadata ManuallyDrop>` + = help: `unsafe<> ManuallyDrop` implements `Debug` so you could box the found value and coerce it to the trait object `Box`, you will have to change the expected type as well +note: required by a bound in `metadata_is` + --> $DIR/dyn-tail-free-lifetime.rs:15:36 + | +LL | fn metadata_is, M>() {} + | ^^^^^^^^^^^^ required by this bound in `metadata_is` + +error[E0271]: type mismatch resolving ` ManuallyDrop> as Pointee>::Metadata == DynMetadata>` + --> $DIR/dyn-tail-free-lifetime.rs:21:19 + | +LL | metadata_is:: MD + 'x>, DynMetadata + 'x>>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `DynMetadata>`, found `DynMetadata ManuallyDrop<_>>` + | + = note: expected struct `DynMetadata<(dyn Tr<'x> + 'x)>` + found struct `DynMetadata ManuallyDrop<(dyn Tr<'x> + 'x)>>` +note: required by a bound in `metadata_is` + --> $DIR/dyn-tail-free-lifetime.rs:15:36 + | +LL | fn metadata_is, M>() {} + | ^^^^^^^^^^^^ required by this bound in `metadata_is` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0271`. diff --git a/tests/ui/unsafe-binders/pointee/metadata-after-monomorphization.rs b/tests/ui/unsafe-binders/pointee/metadata-after-monomorphization.rs new file mode 100644 index 0000000000000..2c58093ee8f4b --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/metadata-after-monomorphization.rs @@ -0,0 +1,77 @@ +//@ compile-flags: -Znext-solver +//@ build-fail +//@ failure-status: 101 +//@ known-bug: #130516 +//@ rustc-env:RUST_BACKTRACE=0 +//@ normalize-stderr: "note: .*\n\n" -> "" +//@ normalize-stderr: "(compiler/[a-z_/]+\.rs):\d+:\d+" -> "$1:LL:CC" +//@ normalize-stderr: "query stack during panic:\n(.*\n)*?(end of query stack|\.\.\. and \d+ other queries.*)\n" -> "" +//@ normalize-stderr: "Normalizing .* without wrapping in a `Binder`" -> "Normalizing .. without wrapping in a `Binder`" +//@ normalize-stderr: "`ProjectionClause\(.*\)` has escaping bound vars" -> "`ProjectionClause(..)` has escaping bound vars" +//@ compile-flags: -Zunstable-options -Csymbol-mangling-version=legacy -Cdebuginfo=2 + +// After monomorphization, the metadata of every binder must be concrete: the +// rigid projection from `alias-tail-bound-lifetime.rs` now has a concrete tail. +// This exercises pointer layout, `PtrMetadata` codegen, `size_of_val` / +// `align_of_val` through the binder, debuginfo for wide pointers to binders, and +// unwrapping through a wide pointer. It uses legacy mangling because v0 +// mangling ICEs on unsafe binders (#154367). +// +// Expected: run-pass. See the `.stderr` files for the current behavior. + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features, dead_code)] + +use std::fmt::Debug; +use std::mem::{ManuallyDrop as MD, align_of_val, size_of, size_of_val}; +use std::marker::PhantomData; +use std::ptr; +use std::unsafe_binder::unwrap_binder; + +trait Tr { + type Assoc<'a>: ?Sized; +} + +struct Slice; +impl Tr for Slice { + type Assoc<'a> = [&'a u8]; +} + +struct Dyn; +impl Tr for Dyn { + type Assoc<'a> = dyn Debug + 'a; +} + +fn generic(p: *const unsafe<'a> MD>) -> (usize, usize) { + assert_eq!(size_of::<*const unsafe<'a> MD>>(), 2 * size_of::()); + let q: *const unsafe<'a> MD> + = ptr::from_raw_parts(p as *const (), ptr::metadata(p)); + let r = unsafe { &*q }; + let inner: &MD> = unsafe { &unwrap_binder!(*r) }; + assert_eq!(size_of_val(inner), size_of_val(r)); + (size_of_val(r), align_of_val(r)) +} + +// The `dyn` tail is reached through a struct that isn't `ManuallyDrop`. +struct Wd<'a, T: ?Sized>(u8, PhantomData<&'a ()>, MD); + +fn main() { + let (a, b, c) = (1u8, 2u8, 3u8); + let s: &[&u8] = &[&a, &b, &c]; + let p = s as *const [&u8] as *const MD<[&u8]> as *const unsafe<'a> MD<[&'a u8]>; + assert_eq!(generic::(p), (3 * size_of::(), size_of::())); + + let x = MD::new(0u64); + let p = &x as &MD as *const MD as *const unsafe<'a> MD; + assert_eq!(generic::(p), (8, 8)); + let r = unsafe { &unwrap_binder!(*p) }; + assert_eq!(format!("{:?}", &**r), "0"); + + let w = Wd(1u8, PhantomData, MD::new(0u64)); + let p = &w as &Wd<'_, dyn Debug> as *const Wd<'_, dyn Debug> + as *const unsafe<'a> MD>; + let r = unsafe { &*p }; + assert_eq!(size_of_val(r), size_of_val(&w)); + let inner: &MD> = unsafe { &unwrap_binder!(*r) }; + assert_eq!(format!("{:?}", &*inner.2), "0"); +} diff --git a/tests/ui/unsafe-binders/pointee/metadata-after-monomorphization.stderr b/tests/ui/unsafe-binders/pointee/metadata-after-monomorphization.stderr new file mode 100644 index 0000000000000..76b628bd94795 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/metadata-after-monomorphization.stderr @@ -0,0 +1,5 @@ + +thread 'rustc' ($TID) panicked at compiler/rustc_codegen_llvm/src/debuginfo/utils.rs:LL:CC: +wide_pointer_kind() - Encountered unexpected `pointee_tail_ty`: Binder { value: std::mem::ManuallyDrop<[&'^0 u8]>, bound_vars: [Region(BrAnon)] } +error: the compiler unexpectedly panicked. This is a bug + diff --git a/tests/ui/unsafe-binders/pointee/metadata-inlined.rs b/tests/ui/unsafe-binders/pointee/metadata-inlined.rs new file mode 100644 index 0000000000000..944aa23629ff0 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/metadata-inlined.rs @@ -0,0 +1,40 @@ +//@ compile-flags: -Znext-solver -O +//@ build-fail +//@ failure-status: 101 +//@ known-bug: #130516 +//@ rustc-env:RUST_BACKTRACE=0 +//@ normalize-stderr: "note: .*\n\n" -> "" +//@ normalize-stderr: "(compiler/[a-z_/]+\.rs):\d+:\d+" -> "$1:LL:CC" +//@ normalize-stderr: "query stack during panic:\n(.*\n)*?(end of query stack|\.\.\. and \d+ other queries.*)\n" -> "" + +// Pointer metadata of a binder whose tail is an alias mentioning the bound +// lifetime works after the MIR inliner runs (`-O`). + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features)] + +use std::mem::ManuallyDrop as MD; +use std::ptr::{self, Pointee}; + +trait Assoc { + type Assoc<'a>: ?Sized; +} + +struct Slice; +impl Assoc for Slice { + type Assoc<'a> = [&'a u8]; +} + +#[inline] +fn meta( + p: *const unsafe<'a> MD>, +) -> MD> as Pointee>::Metadata { + ptr::metadata(p) +} + +fn main() { + let arr: [&u8; 2] = [&1, &2]; + let s: &[&u8] = &arr; + let p = s as *const [&u8] as *const unsafe<'a> MD<[&'a u8]>; + assert_eq!(meta::(p), 2); +} diff --git a/tests/ui/unsafe-binders/pointee/metadata-inlined.stderr b/tests/ui/unsafe-binders/pointee/metadata-inlined.stderr new file mode 100644 index 0000000000000..d37205e585dd9 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/metadata-inlined.stderr @@ -0,0 +1,5 @@ + +thread 'rustc' ($TID) panicked at compiler/rustc_next_trait_solver/src/placeholder.rs:LL:CC: +Bound vars '^0 outside of `self.universe_indices`: [] +error: the compiler unexpectedly panicked. This is a bug + diff --git a/tests/ui/unsafe-binders/pointee/nested-binders.rs b/tests/ui/unsafe-binders/pointee/nested-binders.rs new file mode 100644 index 0000000000000..f17f0d867996d --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/nested-binders.rs @@ -0,0 +1,84 @@ +//@ compile-flags: -Znext-solver +//@ check-fail +//@ known-bug: #130516 + +// Nested unsafe binders should collapse into a single binder for metadata. + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features, dead_code)] + +use std::fmt::Debug; +use std::mem::ManuallyDrop as MD; +use std::ptr::{self, DynMetadata, Pointee}; + +trait Tr<'a> {} + +trait Assoc { + type Assoc<'a, 'b>: ?Sized; +} + +fn metadata_is, M>() {} +fn same_metadata() +where + A: Pointee::Metadata>, +{ +} + +// The bound regions of both binders only occur in sized types. +fn sized() { + metadata_is:: MD &'a &'b u8>, ()>(); + metadata_is:: MD<(&'a u8, unsafe<'b> &'b u8)>, ()>(); +} + +// The bound regions occur in the tail, but not in the metadata. +fn slice() { + metadata_is:: MD MD<[&'a &'b u8]>>, usize>(); +} + +// Only the inner binder's region occurs in the `dyn` tail. +type InnerOnly = unsafe<'a> MD<(&'a u8, unsafe<'b> MD)>; + +// Only the outer binder's region occurs in the `dyn` tail. +type OuterOnly = unsafe<'a> MD MD<(&'b u8, dyn Debug + 'a)>>; + +// Both binders' regions occur in the `dyn` tail. +type Both = unsafe<'a> MD MD + 'b>>; + +// As `Both`, with the outer region reaching the tail through a struct. +struct S<'x>(u8, unsafe<'b> MD + 'b>); +type BothThroughStruct = unsafe<'a> MD>; + +fn dyn_tail() { + metadata_is:: MD>>(); + metadata_is:: MD>>(); + metadata_is:: MD + 'b>>>(); + metadata_is:: MD + 'b>>>(); + + same_metadata:: MD>(); + same_metadata:: MD>(); + same_metadata:: MD + 'b>>(); + same_metadata::(); +} + +// Metadata round-trips at the nested binder type. +fn round_trip(p: *const Both) -> *const Both { + ptr::from_raw_parts(p as *const (), ptr::metadata(p)) +} + +// Generic alias tail mentioning both binders' regions. Without a where-clause +// the metadata is rigid, and a where-clause that holds for all regions of both +// binders fixes it. +fn alias_rigid( + p: *const unsafe<'a> MD MD>>, +) -> *const unsafe<'a> MD MD>> { + ptr::from_raw_parts(p as *const (), ptr::metadata(p)) +} + +fn alias_where_clause() +where + for<'a, 'b> T::Assoc<'a, 'b>: Pointee, +{ + metadata_is:: MD MD>>, usize>(); +} + +fn main() {} diff --git a/tests/ui/unsafe-binders/pointee/nested-binders.stderr b/tests/ui/unsafe-binders/pointee/nested-binders.stderr new file mode 100644 index 0000000000000..1c2f800cbcd5a --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/nested-binders.stderr @@ -0,0 +1,19 @@ +error[E0271]: type mismatch resolving ` ManuallyDrop ManuallyDrop<::Assoc<'a, 'b>>> as Pointee>::Metadata == usize` + --> $DIR/nested-binders.rs:81:19 + | +LL | metadata_is:: MD MD>>, usize>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found associated type + | + = note: expected type `usize` + found associated type ` ManuallyDrop<::Assoc<'a, 'b>> as Pointee>::Metadata` + = help: consider constraining the associated type ` ManuallyDrop<::Assoc<'a, 'b>> as Pointee>::Metadata` to `usize` + = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html +note: required by a bound in `metadata_is` + --> $DIR/nested-binders.rs:20:36 + | +LL | fn metadata_is, M>() {} + | ^^^^^^^^^^^^ required by this bound in `metadata_is` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0271`. diff --git a/tests/ui/unsafe-binders/pointee/param-tail.rs b/tests/ui/unsafe-binders/pointee/param-tail.rs new file mode 100644 index 0000000000000..e47551d9531d4 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/param-tail.rs @@ -0,0 +1,31 @@ +//@ compile-flags: -Znext-solver +//@ check-fail +//@ known-bug: #130516 + +// If the metadata ty under an unsafe binder doesn't mention any bound lifetimes, +// then it should be "normal" and not mention the binder or `ManuallyDrop`. + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features, dead_code)] + +use std::mem::ManuallyDrop as MD; +use std::ptr::Pointee; + +fn metadata_is, M>() {} +fn same_metadata() +where + A: Pointee::Metadata>, +{ +} + +fn unsized_param() { + same_metadata:: MD, T>(); + same_metadata:: MD<(&'a u8, T)>, T>(); +} + +fn sized_param() { + metadata_is:: T, ()>(); + metadata_is:: (&'a u8, T), ()>(); +} + +fn main() {} diff --git a/tests/ui/unsafe-binders/pointee/param-tail.stderr b/tests/ui/unsafe-binders/pointee/param-tail.stderr new file mode 100644 index 0000000000000..0a5f2425c77ff --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/param-tail.stderr @@ -0,0 +1,42 @@ +error[E0271]: type mismatch resolving ` ManuallyDrop as Pointee>::Metadata == ::Metadata` + --> $DIR/param-tail.rs:22:21 + | +LL | fn unsized_param() { + | - expected this type parameter +LL | same_metadata:: MD, T>(); + | ^^^^^^^^^^^^^^^^ expected type parameter `T`, found `unsafe<> ManuallyDrop` + | + = note: expected associated type `::Metadata` + found associated type ` ManuallyDrop as Pointee>::Metadata` + = note: an associated type was expected, but a different one was found +note: required by a bound in `same_metadata` + --> $DIR/param-tail.rs:17:16 + | +LL | fn same_metadata() +LL | where +LL | A: Pointee::Metadata>, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `same_metadata` + +error[E0271]: type mismatch resolving ` ManuallyDrop<(&u8, T)> as Pointee>::Metadata == ::Metadata` + --> $DIR/param-tail.rs:23:21 + | +LL | fn unsized_param() { + | - expected this type parameter +LL | same_metadata:: MD, T>(); +LL | same_metadata:: MD<(&'a u8, T)>, T>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `unsafe<> ManuallyDrop` + | + = note: expected associated type `::Metadata` + found associated type ` ManuallyDrop as Pointee>::Metadata` + = note: an associated type was expected, but a different one was found +note: required by a bound in `same_metadata` + --> $DIR/param-tail.rs:17:16 + | +LL | fn same_metadata() +LL | where +LL | A: Pointee::Metadata>, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `same_metadata` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0271`. diff --git a/tests/ui/unsafe-binders/pointee/simd-cast-ptr.rs b/tests/ui/unsafe-binders/pointee/simd-cast-ptr.rs new file mode 100644 index 0000000000000..13b24b7154324 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/simd-cast-ptr.rs @@ -0,0 +1,26 @@ +//@ compile-flags: -Znext-solver +//@ build-fail +//@ failure-status: 101 +//@ known-bug: #130516 +//@ rustc-env:RUST_BACKTRACE=0 +//@ normalize-stderr: "note: .*\n\n" -> "" +//@ normalize-stderr: "(compiler/[a-z_/]+\.rs):\d+:\d+" -> "$1:LL:CC" +//@ normalize-stderr: "query stack during panic:\n(.*\n)*?(end of query stack|\.\.\. and \d+ other queries.*)\n" -> "" + +// `simd_cast_ptr` should accept vectors of thin pointers to unsafe binders. + +#![feature(unsafe_binders, repr_simd, core_intrinsics)] +#![allow(incomplete_features)] + +#[path = "../../../auxiliary/minisimd.rs"] +mod minisimd; +use minisimd::*; + +use std::intrinsics::simd::simd_cast_ptr; + +fn main() { + let x = 1u8; + let ptrs: Simd<*const u8, 2> = Simd([&x as *const u8, std::ptr::null()]); + let cast: Simd<*const unsafe<> u8, 2> = unsafe { simd_cast_ptr(ptrs) }; + assert!(cast.into_array()[0] as *const u8 == &x as *const u8); +} diff --git a/tests/ui/unsafe-binders/pointee/simd-cast-ptr.stderr b/tests/ui/unsafe-binders/pointee/simd-cast-ptr.stderr new file mode 100644 index 0000000000000..590503ec784e3 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/simd-cast-ptr.stderr @@ -0,0 +1,5 @@ + +thread 'rustc' ($TID) panicked at compiler/rustc_symbol_mangling/src/v0.rs:679:37: +not implemented +error: the compiler unexpectedly panicked. This is a bug + diff --git a/tests/ui/unsafe-binders/pointee/sized-by-where-clause.rs b/tests/ui/unsafe-binders/pointee/sized-by-where-clause.rs new file mode 100644 index 0000000000000..dcc7bbf76fac3 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/sized-by-where-clause.rs @@ -0,0 +1,27 @@ +//@ compile-flags: -Znext-solver +//@ check-pass + +// If a pointee ty is known to be `Sized`, then we know that metadata is `()`. + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features, dead_code)] + +use std::ptr::{Pointee, Thin}; + +trait Tr { + type Assoc<'a>; +} + +fn metadata_is, M>() {} +fn is_thin() {} + +fn f() +where + for<'a> T::Assoc<'a>: Copy, +{ + metadata_is:: T::Assoc<'a>, ()>(); + is_thin:: T::Assoc<'a>>(); + is_thin:: (u8, T::Assoc<'a>)>(); +} + +fn main() {} diff --git a/tests/ui/unsafe-binders/pointee/sized-inner.rs b/tests/ui/unsafe-binders/pointee/sized-inner.rs new file mode 100644 index 0000000000000..b66b2587b052d --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/sized-inner.rs @@ -0,0 +1,23 @@ +//@ compile-flags: -Znext-solver +//@ check-pass + +// Metadata of binders over sized types is `()`, and cannot mention the bound regions. + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features, dead_code)] + +use std::ptr::{self, Pointee}; + +fn metadata_is, M>() {} + +struct BinderTail { + b: unsafe<> (), +} + +fn main() { + metadata_is:: &'a u8, ()>(); + metadata_is:: (&'a u8, u32), ()>(); + metadata_is:: u8, ()>(); + metadata_is::(); + let _ = ptr::null::(); +} diff --git a/tests/ui/unsafe-binders/pointee/slice-tail.rs b/tests/ui/unsafe-binders/pointee/slice-tail.rs new file mode 100644 index 0000000000000..5077ac9421748 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/slice-tail.rs @@ -0,0 +1,25 @@ +//@ compile-flags: -Znext-solver +//@ check-pass + +// Binders over unsized types whose metadata is `usize`. + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features, dead_code)] + +use std::mem::ManuallyDrop as MD; +use std::ptr::Pointee; + +fn metadata_is, M>() {} + +struct S(u8, unsafe<'a> MD<[&'a u8]>); + +// The bound region only appears in a sized field. +struct W<'a, T: ?Sized>(&'a u8, T); + +fn main() { + metadata_is:: MD<[u8]>, usize>(); + metadata_is:: MD<[&'a u8]>, usize>(); + metadata_is:: MD<(&'a u8, str)>, usize>(); + metadata_is:: MD>, usize>(); + metadata_is::(); +} diff --git a/tests/ui/unsafe-binders/pointee/unsize.rs b/tests/ui/unsafe-binders/pointee/unsize.rs new file mode 100644 index 0000000000000..928a74ac71f06 --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/unsize.rs @@ -0,0 +1,19 @@ +//@ compile-flags: -Znext-solver + +// Unsafe binders do not have an unsize impl, regardless of whether the inner type does. + +#![feature(unsafe_binders)] +#![allow(incomplete_features, dead_code)] + +use std::fmt::Debug; +use std::mem::ManuallyDrop as MD; + +fn slice(b: Box MD<[&'a u8; 2]>>) -> Box MD<[&'a u8]>> { + b //~ ERROR mismatched types +} + +fn dyn_(b: Box MD<&'a u8>>) -> Box MD> { + b //~ ERROR mismatched types +} + +fn main() {} diff --git a/tests/ui/unsafe-binders/pointee/unsize.stderr b/tests/ui/unsafe-binders/pointee/unsize.stderr new file mode 100644 index 0000000000000..1b2dfffd50dda --- /dev/null +++ b/tests/ui/unsafe-binders/pointee/unsize.stderr @@ -0,0 +1,26 @@ +error[E0308]: mismatched types + --> $DIR/unsize.rs:12:5 + | +LL | fn slice(b: Box MD<[&'a u8; 2]>>) -> Box MD<[&'a u8]>> { + | ---------------------------- expected `Box ManuallyDrop<[&'a u8]>>` because of return type +LL | b + | ^ expected `Box ManuallyDrop<[&u8]>>`, found `Box ManuallyDrop<[&u8; 2]>>` + | + = note: expected struct `Box ManuallyDrop<[&'a u8]>>` + found struct `Box ManuallyDrop<[&'a u8; 2]>>` + +error[E0308]: mismatched types + --> $DIR/unsize.rs:16:5 + | +LL | fn dyn_(b: Box MD<&'a u8>>) -> Box MD> { + | ---------------------------------- expected `Box ManuallyDrop<(dyn Debug + 'a)>>` because of return type +LL | b + | ^ expected `Box ManuallyDrop>`, found `Box ManuallyDrop<&u8>>` + | + = note: expected struct `Box ManuallyDrop<(dyn Debug + 'a)>>` + found struct `Box ManuallyDrop<&'a u8>>` + = help: `&u8` implements `Debug` so you could box the found value and coerce it to the trait object `Box`, you will have to change the expected type as well + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/unsafe-binders/unsized-binder-sizedness.rs b/tests/ui/unsafe-binders/unsized-binder-sizedness.rs new file mode 100644 index 0000000000000..fe4502c07f3f2 --- /dev/null +++ b/tests/ui/unsafe-binders/unsized-binder-sizedness.rs @@ -0,0 +1,53 @@ +//@ compile-flags: -Znext-solver +// An unsafe binder is `Sized` if and only if its inner type is `Sized`. + +#![feature(unsafe_binders)] +#![allow(incomplete_features)] + +use std::fmt::Debug; +use std::mem::ManuallyDrop; + +trait Tr { + type Assoc<'a>: ?Sized; +} + +fn requires_sized() {} + +fn sized_bound() { + requires_sized:: ManuallyDrop<[u8]>>(); //~ ERROR the size for values of type + requires_sized:: ManuallyDrop>(); //~ ERROR the size for values of type +} + +// The inner type is only known to be `Sized` from the where-clauses, so these +// go through the solvers' structural `Sized` impls instead of the fast path. +fn sized_by_where_clause() +where + for<'a> T::Assoc<'a>: Sized, + U: Sized, +{ + requires_sized:: ManuallyDrop>>(); + requires_sized:: ManuallyDrop<(&'a u8, U)>>(); +} + +fn not_sized_generic() { + requires_sized:: ManuallyDrop>>(); //~ ERROR the size for values of type + requires_sized:: ManuallyDrop<(&'a u8, U)>>(); //~ ERROR the size for values of type +} + +fn by_value(x: unsafe<> ManuallyDrop<[u8]>) -> unsafe<> ManuallyDrop<[u8]> { +//~^ ERROR the size for values of type +//~| ERROR the size for values of type + x +} + +fn move_out_of_box(x: Box ManuallyDrop<[u8]>>) { + let _y = *x; //~ ERROR the size for values of type +} + +fn size() -> usize { + std::mem::size_of::() +} + +fn main() { + size:: ManuallyDrop<[u8]>>(); //~ ERROR the size for values of type +} diff --git a/tests/ui/unsafe-binders/unsized-binder-sizedness.stderr b/tests/ui/unsafe-binders/unsized-binder-sizedness.stderr new file mode 100644 index 0000000000000..10b616ec6ad14 --- /dev/null +++ b/tests/ui/unsafe-binders/unsized-binder-sizedness.stderr @@ -0,0 +1,140 @@ +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> $DIR/unsized-binder-sizedness.rs:37:48 + | +LL | fn by_value(x: unsafe<> ManuallyDrop<[u8]>) -> unsafe<> ManuallyDrop<[u8]> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: within `unsafe<> ManuallyDrop<[u8]>`, the trait `Sized` is not implemented for `[u8]` +note: required because it appears within the type `ManuallyDrop<[u8]>` + --> $SRC_DIR/core/src/mem/manually_drop.rs:LL:COL + = note: required because it appears within the type `unsafe<> ManuallyDrop<[u8]>` + = note: the return type of a function must have a statically known size + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> $DIR/unsized-binder-sizedness.rs:17:22 + | +LL | requires_sized:: ManuallyDrop<[u8]>>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: within `unsafe<> ManuallyDrop<[u8]>`, the trait `Sized` is not implemented for `[u8]` +note: required because it appears within the type `ManuallyDrop<[u8]>` + --> $SRC_DIR/core/src/mem/manually_drop.rs:LL:COL + = note: required because it appears within the type `unsafe<> ManuallyDrop<[u8]>` +note: required by a bound in `requires_sized` + --> $DIR/unsized-binder-sizedness.rs:14:22 + | +LL | fn requires_sized() {} + | ^^^^^ required by this bound in `requires_sized` + +error[E0277]: the size for values of type `(dyn Debug + 'a)` cannot be known at compilation time + --> $DIR/unsized-binder-sizedness.rs:18:22 + | +LL | requires_sized:: ManuallyDrop>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: within `unsafe<'a> ManuallyDrop<(dyn Debug + 'a)>`, the trait `for<'a> Sized` is not implemented for `(dyn Debug + 'a)` +note: required because it appears within the type `ManuallyDrop<(dyn Debug + 'a)>` + --> $SRC_DIR/core/src/mem/manually_drop.rs:LL:COL + = note: required because it appears within the type `unsafe<'a> ManuallyDrop<(dyn Debug + 'a)>` +note: required by a bound in `requires_sized` + --> $DIR/unsized-binder-sizedness.rs:14:22 + | +LL | fn requires_sized() {} + | ^^^^^ required by this bound in `requires_sized` + +error[E0277]: the size for values of type `::Assoc<'a>` cannot be known at compilation time + --> $DIR/unsized-binder-sizedness.rs:33:22 + | +LL | requires_sized:: ManuallyDrop>>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: within `unsafe<'a> ManuallyDrop<::Assoc<'a>>`, the trait `for<'a> Sized` is not implemented for `::Assoc<'a>` +note: required because it appears within the type `ManuallyDrop<::Assoc<'a>>` + --> $SRC_DIR/core/src/mem/manually_drop.rs:LL:COL + = note: required because it appears within the type `unsafe<'a> ManuallyDrop<::Assoc<'a>>` +note: required by a bound in `requires_sized` + --> $DIR/unsized-binder-sizedness.rs:14:22 + | +LL | fn requires_sized() {} + | ^^^^^ required by this bound in `requires_sized` +help: consider further restricting the associated type + | +LL | fn not_sized_generic() where for<'a> ::Assoc<'a>: Sized { + | +++++++++++++++++++++++++++++++++++++++++ + +error[E0277]: the size for values of type `U` cannot be known at compilation time + --> $DIR/unsized-binder-sizedness.rs:34:22 + | +LL | fn not_sized_generic() { + | - this type parameter needs to be `Sized` +LL | requires_sized:: ManuallyDrop>>(); +LL | requires_sized:: ManuallyDrop<(&'a u8, U)>>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = note: required because it appears within the type `(&'a u8, U)` +note: required because it appears within the type `ManuallyDrop<(&'a u8, U)>` + --> $SRC_DIR/core/src/mem/manually_drop.rs:LL:COL + = note: required because it appears within the type `unsafe<'a> ManuallyDrop<(&'a u8, U)>` +note: required by a bound in `requires_sized` + --> $DIR/unsized-binder-sizedness.rs:14:22 + | +LL | fn requires_sized() {} + | ^^^^^ required by this bound in `requires_sized` +help: consider removing the `?Sized` bound to make the type parameter `Sized` + | +LL - fn not_sized_generic() { +LL + fn not_sized_generic() { + | + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> $DIR/unsized-binder-sizedness.rs:37:16 + | +LL | fn by_value(x: unsafe<> ManuallyDrop<[u8]>) -> unsafe<> ManuallyDrop<[u8]> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: within `unsafe<> ManuallyDrop<[u8]>`, the trait `Sized` is not implemented for `[u8]` +note: required because it appears within the type `ManuallyDrop<[u8]>` + --> $SRC_DIR/core/src/mem/manually_drop.rs:LL:COL + = note: required because it appears within the type `unsafe<> ManuallyDrop<[u8]>` + = help: unsized fn params are gated as an unstable feature + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> $DIR/unsized-binder-sizedness.rs:44:9 + | +LL | let _y = *x; + | ^^ doesn't have a size known at compile-time + | + = help: within `unsafe<> ManuallyDrop<[u8]>`, the trait `Sized` is not implemented for `[u8]` +note: required because it appears within the type `ManuallyDrop<[u8]>` + --> $SRC_DIR/core/src/mem/manually_drop.rs:LL:COL + = note: required because it appears within the type `unsafe<> ManuallyDrop<[u8]>` + = note: all local variables must have a statically known size +help: references are always `Sized`, even if they point to unsized data; consider not dereferencing the expression + | +LL - let _y = *x; +LL + let _y = x; + | + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> $DIR/unsized-binder-sizedness.rs:52:12 + | +LL | size:: ManuallyDrop<[u8]>>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: within `unsafe<> ManuallyDrop<[u8]>`, the trait `Sized` is not implemented for `[u8]` +note: required because it appears within the type `ManuallyDrop<[u8]>` + --> $SRC_DIR/core/src/mem/manually_drop.rs:LL:COL + = note: required because it appears within the type `unsafe<> ManuallyDrop<[u8]>` +note: required by an implicit `Sized` bound in `size` + --> $DIR/unsized-binder-sizedness.rs:47:9 + | +LL | fn size() -> usize { + | ^ required by the implicit `Sized` requirement on this type parameter in `size` +help: consider relaxing the implicit `Sized` restriction + | +LL | fn size() -> usize { + | ++++++++ + +error: aborting due to 8 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/unsafe-binders/unsized-binder-thin-pointer.rs b/tests/ui/unsafe-binders/unsized-binder-thin-pointer.rs new file mode 100644 index 0000000000000..a8a5730a84f5c --- /dev/null +++ b/tests/ui/unsafe-binders/unsized-binder-thin-pointer.rs @@ -0,0 +1,36 @@ +//@ compile-flags: -Znext-solver +//@ check-fail + +// A pointer to an unsafe binder should be thin if a pointer to the inner type +// would be thin; and wide if a pointer to the inner type would be wide. + +#![feature(unsafe_binders, ptr_metadata)] +#![allow(incomplete_features)] + +use std::fmt::Debug; +use std::mem::{ManuallyDrop, size_of}; +use std::ptr::Thin; + +const THIN: usize = size_of::(); +const WIDE: usize = 2 * size_of::(); + +const _: () = assert!(size_of::<&unsafe<'a> &'a [u8]>() == THIN); +const _: () = assert!(size_of::<&unsafe<'a> (&'a u8, u32)>() == THIN); +const _: () = assert!(size_of::<&ManuallyDrop<[u8]>>() == WIDE); +const _: () = assert!(size_of::<&unsafe<> ManuallyDrop<[u8]>>() == WIDE); +const _: () = assert!(size_of::<&ManuallyDrop>() == WIDE); +const _: () = assert!(size_of::<&unsafe<'a> ManuallyDrop>() == WIDE); + +fn is_thin() {} + +fn thin() { + is_thin:: &'a [u8]>(); + is_thin:: (&'a u8, u32)>(); +} + +fn wide() { + is_thin:: ManuallyDrop<[u8]>>(); //~ ERROR the trait bound + is_thin:: ManuallyDrop>(); //~ ERROR the trait bound +} + +fn main() {} diff --git a/tests/ui/unsafe-binders/unsized-binder-thin-pointer.stderr b/tests/ui/unsafe-binders/unsized-binder-thin-pointer.stderr new file mode 100644 index 0000000000000..9f87c6628ff99 --- /dev/null +++ b/tests/ui/unsafe-binders/unsized-binder-thin-pointer.stderr @@ -0,0 +1,27 @@ +error[E0277]: the trait bound `unsafe<> ManuallyDrop<[u8]>: Thin` is not satisfied + --> $DIR/unsized-binder-thin-pointer.rs:32:15 + | +LL | is_thin:: ManuallyDrop<[u8]>>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the nightly-only, unstable trait `Thin` is not implemented for `unsafe<> ManuallyDrop<[u8]>` + | +note: required by a bound in `is_thin` + --> $DIR/unsized-binder-thin-pointer.rs:24:24 + | +LL | fn is_thin() {} + | ^^^^ required by this bound in `is_thin` + +error[E0277]: the trait bound `unsafe<'a> ManuallyDrop<(dyn Debug + 'a)>: Thin` is not satisfied + --> $DIR/unsized-binder-thin-pointer.rs:33:15 + | +LL | is_thin:: ManuallyDrop>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the nightly-only, unstable trait `Thin` is not implemented for `unsafe<'a> ManuallyDrop<(dyn Debug + 'a)>` + | +note: required by a bound in `is_thin` + --> $DIR/unsized-binder-thin-pointer.rs:24:24 + | +LL | fn is_thin() {} + | ^^^^ required by this bound in `is_thin` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0277`.