Skip to content

Commit 84b06d5

Browse files
Enforce that dyn* casts are actually pointer-sized
1 parent 6d651a2 commit 84b06d5

File tree

10 files changed

+132
-8
lines changed

10 files changed

+132
-8
lines changed

compiler/rustc_hir/src/lang_items.rs

+2
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ language_item_table! {
270270
TryTraitBranch, sym::branch, branch_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
271271
TryTraitFromYeet, sym::from_yeet, from_yeet_fn, Target::Fn, GenericRequirement::None;
272272

273+
PointerSized, sym::pointer_sized, pointer_sized, Target::Trait, GenericRequirement::Exact(0);
274+
273275
PollReady, sym::Ready, poll_ready_variant, Target::Variant, GenericRequirement::None;
274276
PollPending, sym::Pending, poll_pending_variant, Target::Variant, GenericRequirement::None;
275277

compiler/rustc_hir_typeck/src/coercion.rs

+26-8
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
775775

776776
// Check the obligations of the cast -- for example, when casting
777777
// `usize` to `dyn* Clone + 'static`:
778-
let obligations = predicates
778+
let mut obligations: Vec<_> = predicates
779779
.iter()
780780
.map(|predicate| {
781781
// For each existential predicate (e.g., `?Self: Clone`) substitute
@@ -785,15 +785,33 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
785785
let predicate = predicate.with_self_ty(self.tcx, a);
786786
Obligation::new(self.cause.clone(), self.param_env, predicate)
787787
})
788-
// Enforce the region bound (e.g., `usize: 'static`, in our example).
789-
.chain([Obligation::new(
788+
.chain([
789+
// Enforce the region bound (e.g., `usize: 'static`, in our example).
790+
Obligation::new(
791+
self.cause.clone(),
792+
self.param_env,
793+
ty::Binder::dummy(ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(
794+
a, b_region,
795+
)))
796+
.to_predicate(self.tcx),
797+
),
798+
])
799+
.collect();
800+
801+
// Enforce that the type is `usize`/pointer-sized. For now, only those
802+
// can be coerced to `dyn*`, except for `dyn* -> dyn*` upcasts.
803+
if !a.is_dyn_star() {
804+
obligations.push(Obligation::new(
790805
self.cause.clone(),
791806
self.param_env,
792-
self.tcx.mk_predicate(ty::Binder::dummy(ty::PredicateKind::TypeOutlives(
793-
ty::OutlivesPredicate(a, b_region),
794-
))),
795-
)])
796-
.collect();
807+
ty::Binder::dummy(ty::TraitRef::new(
808+
self.tcx.require_lang_item(hir::LangItem::PointerSized, Some(self.cause.span)),
809+
self.tcx.mk_substs_trait(a, &[]),
810+
))
811+
.to_poly_trait_predicate()
812+
.to_predicate(self.tcx),
813+
));
814+
}
797815

798816
Ok(InferOk {
799817
value: (vec![Adjustment { kind: Adjust::DynStar, target: b }], b),

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,7 @@ symbols! {
10661066
plugins,
10671067
pointee_trait,
10681068
pointer,
1069+
pointer_sized,
10691070
poll,
10701071
position,
10711072
post_dash_lto: "post-lto",

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+27
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
304304
self.assemble_candidates_for_transmutability(obligation, &mut candidates);
305305
} else if lang_items.tuple_trait() == Some(def_id) {
306306
self.assemble_candidate_for_tuple(obligation, &mut candidates);
307+
} else if lang_items.pointer_sized() == Some(def_id) {
308+
self.assemble_candidate_for_ptr_sized(obligation, &mut candidates);
307309
} else {
308310
if lang_items.clone_trait() == Some(def_id) {
309311
// Same builtin conditions as `Copy`, i.e., every type which has builtin support
@@ -1046,4 +1048,29 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10461048
| ty::Placeholder(_) => {}
10471049
}
10481050
}
1051+
1052+
fn assemble_candidate_for_ptr_sized(
1053+
&mut self,
1054+
obligation: &TraitObligation<'tcx>,
1055+
candidates: &mut SelectionCandidateSet<'tcx>,
1056+
) {
1057+
// The regions of a type don't affect the size of the type
1058+
let self_ty = self
1059+
.tcx()
1060+
.erase_regions(self.tcx().erase_late_bound_regions(obligation.predicate.self_ty()));
1061+
1062+
// But if there are inference variables, we have to wait until it's resolved.
1063+
if self_ty.has_non_region_infer() {
1064+
candidates.ambiguous = true;
1065+
return;
1066+
}
1067+
1068+
let usize_layout =
1069+
self.tcx().layout_of(ty::ParamEnv::empty().and(self.tcx().types.usize)).unwrap().layout;
1070+
if let Ok(layout) = self.tcx().layout_of(obligation.param_env.and(self_ty))
1071+
&& layout.layout.size().bytes() == usize_layout.size().bytes()
1072+
{
1073+
candidates.vec.push(BuiltinCandidate { has_nested: false });
1074+
}
1075+
}
10491076
}

library/core/src/marker.rs

+9
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,15 @@ pub trait Destruct {}
809809
#[cfg_attr(not(bootstrap), rustc_deny_explicit_impl)]
810810
pub trait Tuple {}
811811

812+
/// A marker for things
813+
#[unstable(feature = "pointer_sized_trait", issue = "none")]
814+
#[cfg_attr(not(bootstrap), lang = "pointer_sized")]
815+
#[rustc_on_unimplemented(
816+
message = "`{Self}` needs to be a pointer-sized type",
817+
label = "`{Self}` needs to be a pointer-sized type"
818+
)]
819+
pub trait PointerSized {}
820+
812821
/// Implementations of `Copy` for primitive types.
813822
///
814823
/// Implementations that cannot be described in Rust
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(dyn_star)]
2+
#![allow(incomplete_features)]
3+
4+
use std::fmt::Debug;
5+
6+
fn dyn_debug(_: (dyn* Debug + '_)) {
7+
8+
}
9+
10+
fn polymorphic<T: Debug + ?Sized>(t: &T) {
11+
dyn_debug(t);
12+
//~^ ERROR `&T` needs to be a pointer-sized type
13+
}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0277]: `&T` needs to be a pointer-sized type
2+
--> $DIR/check-size-at-cast-polymorphic-bad.rs:11:15
3+
|
4+
LL | dyn_debug(t);
5+
| ^ `&T` needs to be a pointer-sized type
6+
|
7+
= help: the trait `PointerSized` is not implemented for `&T`
8+
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
9+
|
10+
LL | fn polymorphic<T: Debug + ?Sized>(t: &T) where &T: PointerSized {
11+
| ++++++++++++++++++++++
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
3+
#![feature(dyn_star)]
4+
#![allow(incomplete_features)]
5+
6+
use std::fmt::Debug;
7+
8+
fn dyn_debug(_: (dyn* Debug + '_)) {
9+
10+
}
11+
12+
fn polymorphic<T: Debug>(t: &T) {
13+
dyn_debug(t);
14+
}
15+
16+
fn main() {}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(dyn_star)]
2+
#![allow(incomplete_features)]
3+
4+
use std::fmt::Debug;
5+
6+
fn main() {
7+
let i = [1, 2, 3, 4] as dyn* Debug;
8+
//~^ ERROR `[i32; 4]` needs to be a pointer-sized type
9+
dbg!(i);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0277]: `[i32; 4]` needs to be a pointer-sized type
2+
--> $DIR/check-size-at-cast.rs:7:13
3+
|
4+
LL | let i = [1, 2, 3, 4] as dyn* Debug;
5+
| ^^^^^^^^^^^^ `[i32; 4]` needs to be a pointer-sized type
6+
|
7+
= help: the trait `PointerSized` is not implemented for `[i32; 4]`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)