@@ -1106,15 +1106,23 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11061106 self . prove_predicate (
11071107 ty:: ClauseKind :: WellFormed ( src_ty. into ( ) ) ,
11081108 location. to_locations ( ) ,
1109- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1109+ ConstraintCategory :: Cast {
1110+ is_raw_ptr_dyn_type_cast : false ,
1111+ is_implicit_coercion,
1112+ unsize_to : None ,
1113+ } ,
11101114 ) ;
11111115
11121116 let src_ty = self . normalize ( src_ty, location) ;
11131117 if let Err ( terr) = self . sub_types (
11141118 src_ty,
11151119 * ty,
11161120 location. to_locations ( ) ,
1117- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1121+ ConstraintCategory :: Cast {
1122+ is_raw_ptr_dyn_type_cast : false ,
1123+ is_implicit_coercion,
1124+ unsize_to : None ,
1125+ } ,
11181126 ) {
11191127 span_mirbug ! (
11201128 self ,
@@ -1135,7 +1143,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11351143 self . prove_predicate (
11361144 ty:: ClauseKind :: WellFormed ( src_ty. into ( ) ) ,
11371145 location. to_locations ( ) ,
1138- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1146+ ConstraintCategory :: Cast {
1147+ is_raw_ptr_dyn_type_cast : false ,
1148+ is_implicit_coercion,
1149+ unsize_to : None ,
1150+ } ,
11391151 ) ;
11401152
11411153 // The type that we see in the fcx is like
@@ -1148,7 +1160,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11481160 src_ty,
11491161 * ty,
11501162 location. to_locations ( ) ,
1151- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1163+ ConstraintCategory :: Cast {
1164+ is_raw_ptr_dyn_type_cast : false ,
1165+ is_implicit_coercion,
1166+ unsize_to : None ,
1167+ } ,
11521168 ) {
11531169 span_mirbug ! (
11541170 self ,
@@ -1177,7 +1193,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11771193 ty_fn_ptr_from,
11781194 * ty,
11791195 location. to_locations ( ) ,
1180- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1196+ ConstraintCategory :: Cast {
1197+ is_raw_ptr_dyn_type_cast : false ,
1198+ is_implicit_coercion,
1199+ unsize_to : None ,
1200+ } ,
11811201 ) {
11821202 span_mirbug ! (
11831203 self ,
@@ -1210,7 +1230,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12101230 ty_fn_ptr_from,
12111231 * ty,
12121232 location. to_locations ( ) ,
1213- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1233+ ConstraintCategory :: Cast {
1234+ is_raw_ptr_dyn_type_cast : false ,
1235+ is_implicit_coercion,
1236+ unsize_to : None ,
1237+ } ,
12141238 ) {
12151239 span_mirbug ! (
12161240 self ,
@@ -1239,6 +1263,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12391263 trait_ref,
12401264 location. to_locations ( ) ,
12411265 ConstraintCategory :: Cast {
1266+ is_raw_ptr_dyn_type_cast : false ,
12421267 is_implicit_coercion,
12431268 unsize_to : Some ( unsize_to) ,
12441269 } ,
@@ -1264,7 +1289,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12641289 * ty_from,
12651290 * ty_to,
12661291 location. to_locations ( ) ,
1267- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1292+ ConstraintCategory :: Cast {
1293+ is_raw_ptr_dyn_type_cast : false ,
1294+ is_implicit_coercion,
1295+ unsize_to : None ,
1296+ } ,
12681297 ) {
12691298 span_mirbug ! (
12701299 self ,
@@ -1327,7 +1356,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
13271356 * ty_elem,
13281357 * ty_to,
13291358 location. to_locations ( ) ,
1330- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1359+ ConstraintCategory :: Cast {
1360+ is_raw_ptr_dyn_type_cast : false ,
1361+ is_implicit_coercion,
1362+ unsize_to : None ,
1363+ } ,
13311364 ) {
13321365 span_mirbug ! (
13331366 self ,
@@ -1484,11 +1517,12 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
14841517 trait_ref,
14851518 location. to_locations ( ) ,
14861519 ConstraintCategory :: Cast {
1520+ is_raw_ptr_dyn_type_cast : false ,
14871521 is_implicit_coercion : true ,
14881522 unsize_to : None ,
14891523 } ,
14901524 ) ;
1491- } else if let ty:: Dynamic ( src_tty, _src_lt ) =
1525+ } else if let ty:: Dynamic ( src_tty, src_lt ) =
14921526 * self . struct_tail ( src. ty , location) . kind ( )
14931527 && let ty:: Dynamic ( dst_tty, dst_lt) =
14941528 * self . struct_tail ( dst. ty , location) . kind ( )
@@ -1503,15 +1537,13 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
15031537 // Debug`) are in `rustc_hir_typeck`.
15041538
15051539 // Remove auto traits.
1506- // Auto trait checks are handled in `rustc_hir_typeck` as FCW .
1540+ // Auto trait checks are handled in `rustc_hir_typeck`.
15071541 let src_obj = Ty :: new_dynamic (
15081542 tcx,
15091543 tcx. mk_poly_existential_predicates (
15101544 & src_tty. without_auto_traits ( ) . collect :: < Vec < _ > > ( ) ,
15111545 ) ,
1512- // FIXME: Once we disallow casting `*const dyn Trait + 'short`
1513- // to `*const dyn Trait + 'long`, then this can just be `src_lt`.
1514- dst_lt,
1546+ src_lt,
15151547 ) ;
15161548 let dst_obj = Ty :: new_dynamic (
15171549 tcx,
@@ -1523,16 +1555,47 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
15231555
15241556 debug ! ( ?src_tty, ?dst_tty, ?src_obj, ?dst_obj) ;
15251557
1558+ // Trait parameters are Invariant, the only part that actually has
1559+ // subtyping here is the lifetime bound of the dyn-type.
1560+ //
1561+ // For example in `dyn Trait<'a> + 'b <: dyn Trait<'c> + 'd` we would
1562+ // require that `'a == 'c` but only that `'b: 'd`.
1563+ //
1564+ // We must not allow freely casting lifetime bounds of dyn-types as it
1565+ // may allow for inaccessible VTable methods being callable: #136702
15261566 self . sub_types (
15271567 src_obj,
15281568 dst_obj,
15291569 location. to_locations ( ) ,
15301570 ConstraintCategory :: Cast {
1571+ is_raw_ptr_dyn_type_cast : true ,
15311572 is_implicit_coercion : false ,
15321573 unsize_to : None ,
15331574 } ,
15341575 )
15351576 . unwrap ( ) ;
1577+ } else if let ty:: Dynamic ( src_tty, src_lt) =
1578+ * self . struct_tail ( src. ty , location) . kind ( )
1579+ && let ty:: Dynamic ( dst_tty, dst_lt) =
1580+ * self . struct_tail ( dst. ty , location) . kind ( )
1581+ && src_tty. principal ( ) . is_none ( )
1582+ && dst_tty. principal ( ) . is_none ( )
1583+ {
1584+ // The principalless (no non-auto traits) case:
1585+ // You can only cast `dyn Send + 'long` to `dyn Send + 'short`.
1586+ self . constraints . outlives_constraints . push ( OutlivesConstraint {
1587+ sup : src_lt. as_var ( ) ,
1588+ sub : dst_lt. as_var ( ) ,
1589+ locations : location. to_locations ( ) ,
1590+ span : location. to_locations ( ) . span ( self . body ) ,
1591+ category : ConstraintCategory :: Cast {
1592+ is_raw_ptr_dyn_type_cast : true ,
1593+ is_implicit_coercion : false ,
1594+ unsize_to : None ,
1595+ } ,
1596+ variance_info : ty:: VarianceDiagInfo :: default ( ) ,
1597+ from_closure : false ,
1598+ } ) ;
15361599 }
15371600 }
15381601 CastKind :: Transmute => {
0 commit comments