From 84b06d572d9eb6b81f301fcc93be9463582b0304 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 12 Nov 2022 22:27:18 +0000 Subject: [PATCH] Enforce that dyn* casts are actually pointer-sized --- compiler/rustc_hir/src/lang_items.rs | 2 ++ compiler/rustc_hir_typeck/src/coercion.rs | 34 ++++++++++++++----- compiler/rustc_span/src/symbol.rs | 1 + .../src/traits/select/candidate_assembly.rs | 27 +++++++++++++++ library/core/src/marker.rs | 9 +++++ .../check-size-at-cast-polymorphic-bad.rs | 15 ++++++++ .../check-size-at-cast-polymorphic-bad.stderr | 15 ++++++++ .../check-size-at-cast-polymorphic.rs | 16 +++++++++ src/test/ui/dyn-star/check-size-at-cast.rs | 10 ++++++ .../ui/dyn-star/check-size-at-cast.stderr | 11 ++++++ 10 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 src/test/ui/dyn-star/check-size-at-cast-polymorphic-bad.rs create mode 100644 src/test/ui/dyn-star/check-size-at-cast-polymorphic-bad.stderr create mode 100644 src/test/ui/dyn-star/check-size-at-cast-polymorphic.rs create mode 100644 src/test/ui/dyn-star/check-size-at-cast.rs create mode 100644 src/test/ui/dyn-star/check-size-at-cast.stderr diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs index a55224d10972a..a748af6577448 100644 --- a/compiler/rustc_hir/src/lang_items.rs +++ b/compiler/rustc_hir/src/lang_items.rs @@ -270,6 +270,8 @@ language_item_table! { TryTraitBranch, sym::branch, branch_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None; TryTraitFromYeet, sym::from_yeet, from_yeet_fn, Target::Fn, GenericRequirement::None; + PointerSized, sym::pointer_sized, pointer_sized, Target::Trait, GenericRequirement::Exact(0); + PollReady, sym::Ready, poll_ready_variant, Target::Variant, GenericRequirement::None; PollPending, sym::Pending, poll_pending_variant, Target::Variant, GenericRequirement::None; diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 71949b4211819..8e9f8fec4e424 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -775,7 +775,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { // Check the obligations of the cast -- for example, when casting // `usize` to `dyn* Clone + 'static`: - let obligations = predicates + let mut obligations: Vec<_> = predicates .iter() .map(|predicate| { // For each existential predicate (e.g., `?Self: Clone`) substitute @@ -785,15 +785,33 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { let predicate = predicate.with_self_ty(self.tcx, a); Obligation::new(self.cause.clone(), self.param_env, predicate) }) - // Enforce the region bound (e.g., `usize: 'static`, in our example). - .chain([Obligation::new( + .chain([ + // Enforce the region bound (e.g., `usize: 'static`, in our example). + Obligation::new( + self.cause.clone(), + self.param_env, + ty::Binder::dummy(ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate( + a, b_region, + ))) + .to_predicate(self.tcx), + ), + ]) + .collect(); + + // Enforce that the type is `usize`/pointer-sized. For now, only those + // can be coerced to `dyn*`, except for `dyn* -> dyn*` upcasts. + if !a.is_dyn_star() { + obligations.push(Obligation::new( self.cause.clone(), self.param_env, - self.tcx.mk_predicate(ty::Binder::dummy(ty::PredicateKind::TypeOutlives( - ty::OutlivesPredicate(a, b_region), - ))), - )]) - .collect(); + ty::Binder::dummy(ty::TraitRef::new( + self.tcx.require_lang_item(hir::LangItem::PointerSized, Some(self.cause.span)), + self.tcx.mk_substs_trait(a, &[]), + )) + .to_poly_trait_predicate() + .to_predicate(self.tcx), + )); + } Ok(InferOk { value: (vec![Adjustment { kind: Adjust::DynStar, target: b }], b), diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index a56450a3573d1..b4cc51a820085 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1066,6 +1066,7 @@ symbols! { plugins, pointee_trait, pointer, + pointer_sized, poll, position, post_dash_lto: "post-lto", diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index 3671a0d87df57..f672493759f5b 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -304,6 +304,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { self.assemble_candidates_for_transmutability(obligation, &mut candidates); } else if lang_items.tuple_trait() == Some(def_id) { self.assemble_candidate_for_tuple(obligation, &mut candidates); + } else if lang_items.pointer_sized() == Some(def_id) { + self.assemble_candidate_for_ptr_sized(obligation, &mut candidates); } else { if lang_items.clone_trait() == Some(def_id) { // Same builtin conditions as `Copy`, i.e., every type which has builtin support @@ -1046,4 +1048,29 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { | ty::Placeholder(_) => {} } } + + fn assemble_candidate_for_ptr_sized( + &mut self, + obligation: &TraitObligation<'tcx>, + candidates: &mut SelectionCandidateSet<'tcx>, + ) { + // The regions of a type don't affect the size of the type + let self_ty = self + .tcx() + .erase_regions(self.tcx().erase_late_bound_regions(obligation.predicate.self_ty())); + + // But if there are inference variables, we have to wait until it's resolved. + if self_ty.has_non_region_infer() { + candidates.ambiguous = true; + return; + } + + let usize_layout = + self.tcx().layout_of(ty::ParamEnv::empty().and(self.tcx().types.usize)).unwrap().layout; + if let Ok(layout) = self.tcx().layout_of(obligation.param_env.and(self_ty)) + && layout.layout.size().bytes() == usize_layout.size().bytes() + { + candidates.vec.push(BuiltinCandidate { has_nested: false }); + } + } } diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 3eff6033f8da9..42c3428019769 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -809,6 +809,15 @@ pub trait Destruct {} #[cfg_attr(not(bootstrap), rustc_deny_explicit_impl)] pub trait Tuple {} +/// A marker for things +#[unstable(feature = "pointer_sized_trait", issue = "none")] +#[cfg_attr(not(bootstrap), lang = "pointer_sized")] +#[rustc_on_unimplemented( + message = "`{Self}` needs to be a pointer-sized type", + label = "`{Self}` needs to be a pointer-sized type" +)] +pub trait PointerSized {} + /// Implementations of `Copy` for primitive types. /// /// Implementations that cannot be described in Rust diff --git a/src/test/ui/dyn-star/check-size-at-cast-polymorphic-bad.rs b/src/test/ui/dyn-star/check-size-at-cast-polymorphic-bad.rs new file mode 100644 index 0000000000000..e19e36cc7d7b5 --- /dev/null +++ b/src/test/ui/dyn-star/check-size-at-cast-polymorphic-bad.rs @@ -0,0 +1,15 @@ +#![feature(dyn_star)] +#![allow(incomplete_features)] + +use std::fmt::Debug; + +fn dyn_debug(_: (dyn* Debug + '_)) { + +} + +fn polymorphic(t: &T) { + dyn_debug(t); + //~^ ERROR `&T` needs to be a pointer-sized type +} + +fn main() {} diff --git a/src/test/ui/dyn-star/check-size-at-cast-polymorphic-bad.stderr b/src/test/ui/dyn-star/check-size-at-cast-polymorphic-bad.stderr new file mode 100644 index 0000000000000..53ccbe43dcc9e --- /dev/null +++ b/src/test/ui/dyn-star/check-size-at-cast-polymorphic-bad.stderr @@ -0,0 +1,15 @@ +error[E0277]: `&T` needs to be a pointer-sized type + --> $DIR/check-size-at-cast-polymorphic-bad.rs:11:15 + | +LL | dyn_debug(t); + | ^ `&T` needs to be a pointer-sized type + | + = help: the trait `PointerSized` is not implemented for `&T` +help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement + | +LL | fn polymorphic(t: &T) where &T: PointerSized { + | ++++++++++++++++++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/dyn-star/check-size-at-cast-polymorphic.rs b/src/test/ui/dyn-star/check-size-at-cast-polymorphic.rs new file mode 100644 index 0000000000000..5c0a3d256f607 --- /dev/null +++ b/src/test/ui/dyn-star/check-size-at-cast-polymorphic.rs @@ -0,0 +1,16 @@ +// check-pass + +#![feature(dyn_star)] +#![allow(incomplete_features)] + +use std::fmt::Debug; + +fn dyn_debug(_: (dyn* Debug + '_)) { + +} + +fn polymorphic(t: &T) { + dyn_debug(t); +} + +fn main() {} diff --git a/src/test/ui/dyn-star/check-size-at-cast.rs b/src/test/ui/dyn-star/check-size-at-cast.rs new file mode 100644 index 0000000000000..1f22f79836154 --- /dev/null +++ b/src/test/ui/dyn-star/check-size-at-cast.rs @@ -0,0 +1,10 @@ +#![feature(dyn_star)] +#![allow(incomplete_features)] + +use std::fmt::Debug; + +fn main() { + let i = [1, 2, 3, 4] as dyn* Debug; + //~^ ERROR `[i32; 4]` needs to be a pointer-sized type + dbg!(i); +} diff --git a/src/test/ui/dyn-star/check-size-at-cast.stderr b/src/test/ui/dyn-star/check-size-at-cast.stderr new file mode 100644 index 0000000000000..af2a1ccf71c6d --- /dev/null +++ b/src/test/ui/dyn-star/check-size-at-cast.stderr @@ -0,0 +1,11 @@ +error[E0277]: `[i32; 4]` needs to be a pointer-sized type + --> $DIR/check-size-at-cast.rs:7:13 + | +LL | let i = [1, 2, 3, 4] as dyn* Debug; + | ^^^^^^^^^^^^ `[i32; 4]` needs to be a pointer-sized type + | + = help: the trait `PointerSized` is not implemented for `[i32; 4]` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`.