@@ -1467,21 +1467,19 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
1467
1467
//
1468
1468
// Note that other checks (such as denying `dyn Send` -> `dyn
1469
1469
// Debug`) are in `rustc_hir_typeck`.
1470
- if let ty:: Dynamic ( src_tty, _src_lt , ty:: Dyn ) = * src_tail. kind ( )
1470
+ if let ty:: Dynamic ( src_tty, src_lt , ty:: Dyn ) = * src_tail. kind ( )
1471
1471
&& let ty:: Dynamic ( dst_tty, dst_lt, ty:: Dyn ) = * dst_tail. kind ( )
1472
1472
&& src_tty. principal ( ) . is_some ( )
1473
1473
&& dst_tty. principal ( ) . is_some ( )
1474
1474
{
1475
1475
// Remove auto traits.
1476
- // Auto trait checks are handled in `rustc_hir_typeck` as FCW .
1476
+ // Auto trait checks are handled in `rustc_hir_typeck`.
1477
1477
let src_obj = Ty :: new_dynamic (
1478
1478
tcx,
1479
1479
tcx. mk_poly_existential_predicates (
1480
1480
& src_tty. without_auto_traits ( ) . collect :: < Vec < _ > > ( ) ,
1481
1481
) ,
1482
- // FIXME: Once we disallow casting `*const dyn Trait + 'short`
1483
- // to `*const dyn Trait + 'long`, then this can just be `src_lt`.
1484
- dst_lt,
1482
+ src_lt,
1485
1483
ty:: Dyn ,
1486
1484
) ;
1487
1485
let dst_obj = Ty :: new_dynamic (
@@ -1495,6 +1493,22 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
1495
1493
1496
1494
debug ! ( ?src_tty, ?dst_tty, ?src_obj, ?dst_obj) ;
1497
1495
1496
+ // Trait parameters are Invariant, the only part that actually has subtyping
1497
+ // here is the lifetime bound of the dyn-type.
1498
+ //
1499
+ // For example in `dyn Trait<'a> + 'b <: dyn Trait<'c> + 'd` we would require
1500
+ // that `'a == 'c` but only that `'b: 'd`.
1501
+ //
1502
+ // We must not allow freely casting lifetime bounds of dyn-types as it may allow
1503
+ // for inaccessible VTable methods being callable: #136702
1504
+ //
1505
+ // We don't enforce this for casts of principal-less dyn types as their VTables do
1506
+ // not contain any functions with `Self: 'a` bounds that could start holding after
1507
+ // a pointer cast.
1508
+ //
1509
+ // We also don't enforce this for casts of pointers to pointers to dyn types. E.g.
1510
+ // `*mut *mut dyn Trait + 'a -> *mut *mut dyn Trait + 'static` is allowed. This is
1511
+ // fine because there is no actual VTable in play.
1498
1512
self . sub_types (
1499
1513
src_obj,
1500
1514
dst_obj,
0 commit comments