@@ -1113,15 +1113,23 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11131113 self . prove_predicate (
11141114 ty:: ClauseKind :: WellFormed ( src_ty. into ( ) ) ,
11151115 location. to_locations ( ) ,
1116- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1116+ ConstraintCategory :: Cast {
1117+ is_raw_ptr_dyn_type_cast : false ,
1118+ is_implicit_coercion,
1119+ unsize_to : None ,
1120+ } ,
11171121 ) ;
11181122
11191123 let src_ty = self . normalize ( src_ty, location) ;
11201124 if let Err ( terr) = self . sub_types (
11211125 src_ty,
11221126 * ty,
11231127 location. to_locations ( ) ,
1124- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1128+ ConstraintCategory :: Cast {
1129+ is_raw_ptr_dyn_type_cast : false ,
1130+ is_implicit_coercion,
1131+ unsize_to : None ,
1132+ } ,
11251133 ) {
11261134 span_mirbug ! (
11271135 self ,
@@ -1142,7 +1150,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11421150 self . prove_predicate (
11431151 ty:: ClauseKind :: WellFormed ( src_ty. into ( ) ) ,
11441152 location. to_locations ( ) ,
1145- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1153+ ConstraintCategory :: Cast {
1154+ is_raw_ptr_dyn_type_cast : false ,
1155+ is_implicit_coercion,
1156+ unsize_to : None ,
1157+ } ,
11461158 ) ;
11471159
11481160 // The type that we see in the fcx is like
@@ -1155,7 +1167,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11551167 src_ty,
11561168 * ty,
11571169 location. to_locations ( ) ,
1158- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1170+ ConstraintCategory :: Cast {
1171+ is_raw_ptr_dyn_type_cast : false ,
1172+ is_implicit_coercion,
1173+ unsize_to : None ,
1174+ } ,
11591175 ) {
11601176 span_mirbug ! (
11611177 self ,
@@ -1184,7 +1200,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11841200 ty_fn_ptr_from,
11851201 * ty,
11861202 location. to_locations ( ) ,
1187- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1203+ ConstraintCategory :: Cast {
1204+ is_raw_ptr_dyn_type_cast : false ,
1205+ is_implicit_coercion,
1206+ unsize_to : None ,
1207+ } ,
11881208 ) {
11891209 span_mirbug ! (
11901210 self ,
@@ -1217,7 +1237,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12171237 ty_fn_ptr_from,
12181238 * ty,
12191239 location. to_locations ( ) ,
1220- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1240+ ConstraintCategory :: Cast {
1241+ is_raw_ptr_dyn_type_cast : false ,
1242+ is_implicit_coercion,
1243+ unsize_to : None ,
1244+ } ,
12211245 ) {
12221246 span_mirbug ! (
12231247 self ,
@@ -1246,6 +1270,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12461270 trait_ref,
12471271 location. to_locations ( ) ,
12481272 ConstraintCategory :: Cast {
1273+ is_raw_ptr_dyn_type_cast : false ,
12491274 is_implicit_coercion,
12501275 unsize_to : Some ( unsize_to) ,
12511276 } ,
@@ -1271,7 +1296,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12711296 * ty_from,
12721297 * ty_to,
12731298 location. to_locations ( ) ,
1274- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1299+ ConstraintCategory :: Cast {
1300+ is_raw_ptr_dyn_type_cast : false ,
1301+ is_implicit_coercion,
1302+ unsize_to : None ,
1303+ } ,
12751304 ) {
12761305 span_mirbug ! (
12771306 self ,
@@ -1334,7 +1363,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
13341363 * ty_elem,
13351364 * ty_to,
13361365 location. to_locations ( ) ,
1337- ConstraintCategory :: Cast { is_implicit_coercion, unsize_to : None } ,
1366+ ConstraintCategory :: Cast {
1367+ is_raw_ptr_dyn_type_cast : false ,
1368+ is_implicit_coercion,
1369+ unsize_to : None ,
1370+ } ,
13381371 ) {
13391372 span_mirbug ! (
13401373 self ,
@@ -1491,11 +1524,12 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
14911524 trait_ref,
14921525 location. to_locations ( ) ,
14931526 ConstraintCategory :: Cast {
1527+ is_raw_ptr_dyn_type_cast : false ,
14941528 is_implicit_coercion : true ,
14951529 unsize_to : None ,
14961530 } ,
14971531 ) ;
1498- } else if let ty:: Dynamic ( src_tty, _src_lt ) =
1532+ } else if let ty:: Dynamic ( src_tty, src_lt ) =
14991533 * self . struct_tail ( src. ty , location) . kind ( )
15001534 && let ty:: Dynamic ( dst_tty, dst_lt) =
15011535 * self . struct_tail ( dst. ty , location) . kind ( )
@@ -1510,15 +1544,13 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
15101544 // Debug`) are in `rustc_hir_typeck`.
15111545
15121546 // Remove auto traits.
1513- // Auto trait checks are handled in `rustc_hir_typeck` as FCW .
1547+ // Auto trait checks are handled in `rustc_hir_typeck`.
15141548 let src_obj = Ty :: new_dynamic (
15151549 tcx,
15161550 tcx. mk_poly_existential_predicates (
15171551 & src_tty. without_auto_traits ( ) . collect :: < Vec < _ > > ( ) ,
15181552 ) ,
1519- // FIXME: Once we disallow casting `*const dyn Trait + 'short`
1520- // to `*const dyn Trait + 'long`, then this can just be `src_lt`.
1521- dst_lt,
1553+ src_lt,
15221554 ) ;
15231555 let dst_obj = Ty :: new_dynamic (
15241556 tcx,
@@ -1530,11 +1562,29 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
15301562
15311563 debug ! ( ?src_tty, ?dst_tty, ?src_obj, ?dst_obj) ;
15321564
1565+ // Trait parameters are Invariant, the only part that actually has
1566+ // subtyping here is the lifetime bound of the dyn-type.
1567+ //
1568+ // For example in `dyn Trait<'a> + 'b <: dyn Trait<'c> + 'd` we would
1569+ // require that `'a == 'c` but only that `'b: 'd`.
1570+ //
1571+ // We must not allow freely casting lifetime bounds of dyn-types as it
1572+ // may allow for inaccessible VTable methods being callable: #136702
1573+ //
1574+ // We don't enforce this for casts of principal-less dyn types as their
1575+ // VTables do not contain any functions with `Self: 'a` bounds that
1576+ // could start holding after a pointer cast.
1577+ //
1578+ // We also don't enforce this for casts of pointers to pointers to dyn
1579+ // types. E.g. `*mut *mut dyn Trait + 'a -> *mut *mut dyn Trait +
1580+ // 'static` is allowed. This is fine because there is no actual VTable
1581+ // in play.
15331582 self . sub_types (
15341583 src_obj,
15351584 dst_obj,
15361585 location. to_locations ( ) ,
15371586 ConstraintCategory :: Cast {
1587+ is_raw_ptr_dyn_type_cast : true ,
15381588 is_implicit_coercion : false ,
15391589 unsize_to : None ,
15401590 } ,
0 commit comments