@@ -1467,21 +1467,19 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
14671467 //
14681468 // Note that other checks (such as denying `dyn Send` -> `dyn
14691469 // 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 ( )
14711471 && let ty:: Dynamic ( dst_tty, dst_lt, ty:: Dyn ) = * dst_tail. kind ( )
14721472 && src_tty. principal ( ) . is_some ( )
14731473 && dst_tty. principal ( ) . is_some ( )
14741474 {
14751475 // 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`.
14771477 let src_obj = Ty :: new_dynamic (
14781478 tcx,
14791479 tcx. mk_poly_existential_predicates (
14801480 & src_tty. without_auto_traits ( ) . collect :: < Vec < _ > > ( ) ,
14811481 ) ,
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,
14851483 ty:: Dyn ,
14861484 ) ;
14871485 let dst_obj = Ty :: new_dynamic (
@@ -1495,6 +1493,22 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
14951493
14961494 debug ! ( ?src_tty, ?dst_tty, ?src_obj, ?dst_obj) ;
14971495
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.
14981512 self . sub_types (
14991513 src_obj,
15001514 dst_obj,
0 commit comments