X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;ds=sidebyside;f=compiler%2Frustc_hir_typeck%2Fsrc%2Fcoercion.rs;h=c1e4ab600f34f204242165cba1ed560b1fd580cb;hb=9db23f8d30e8d00e2e5e18b51f7bb8e582520600;hp=174b43313825e1b770760d191e3249529beacc89;hpb=68a8109ddbb44fdfdd455036d2e53a8bf3545d4e;p=rust.git diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 174b4331382..c1e4ab600f3 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -775,7 +775,7 @@ fn coerce_dyn_star( // 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,16 +785,33 @@ fn coerce_dyn_star( let predicate = predicate.with_self_ty(self.tcx, a); Obligation::new(self.tcx, 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.tcx, + self.cause.clone(), + self.param_env, + ty::Binder::dummy(ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate( + a, b_region, + ))), + ), + ]) + .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.tcx, 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(), + )); + } Ok(InferOk { value: (vec![Adjustment { kind: Adjust::DynStar, target: b }], b),