@@ -990,6 +990,32 @@ fn check_impl_items_against_trait<'tcx>(
990990
991991 let trait_def = tcx. trait_def ( trait_ref. def_id ) ;
992992
993+ let infcx = tcx. infer_ctxt ( ) . ignoring_regions ( ) . build ( TypingMode :: non_body_analysis ( ) ) ;
994+
995+ let ocx = ObligationCtxt :: new_with_diagnostics ( & infcx) ;
996+ let cause = ObligationCause :: misc ( tcx. def_span ( impl_id) , impl_id) ;
997+ let param_env = tcx. param_env ( impl_id) ;
998+
999+ let self_is_guaranteed_unsized = match tcx
1000+ . struct_tail_raw (
1001+ trait_ref. self_ty ( ) ,
1002+ |ty| {
1003+ ocx. structurally_normalize_ty ( & cause, param_env, ty) . unwrap_or_else ( |_| {
1004+ Ty :: new_error_with_message (
1005+ tcx,
1006+ tcx. def_span ( impl_id) ,
1007+ "struct tail should be computable" ,
1008+ )
1009+ } )
1010+ } ,
1011+ || ( ) ,
1012+ )
1013+ . kind ( )
1014+ {
1015+ ty:: Dynamic ( _, _, ty:: DynKind :: Dyn ) | ty:: Slice ( _) | ty:: Str => true ,
1016+ _ => false ,
1017+ } ;
1018+
9931019 for & impl_item in impl_item_refs {
9941020 let ty_impl_item = tcx. associated_item ( impl_item) ;
9951021 let ty_trait_item = if let Some ( trait_item_id) = ty_impl_item. trait_item_def_id {
@@ -1019,6 +1045,15 @@ fn check_impl_items_against_trait<'tcx>(
10191045 }
10201046 }
10211047
1048+ if self_is_guaranteed_unsized && tcx. generics_require_sized_self ( ty_trait_item. def_id ) {
1049+ tcx. emit_node_span_lint (
1050+ rustc_lint_defs:: builtin:: DEAD_CODE ,
1051+ tcx. local_def_id_to_hir_id ( ty_impl_item. def_id . expect_local ( ) ) ,
1052+ tcx. def_span ( ty_impl_item. def_id ) ,
1053+ errors:: UselessImplItem ,
1054+ )
1055+ }
1056+
10221057 check_specialization_validity (
10231058 tcx,
10241059 trait_def,
@@ -1042,7 +1077,11 @@ fn check_impl_items_against_trait<'tcx>(
10421077 . as_ref ( )
10431078 . is_some_and ( |node_item| node_item. item . defaultness ( tcx) . has_value ( ) ) ;
10441079
1045- if !is_implemented && tcx. defaultness ( impl_id) . is_final ( ) {
1080+ if !is_implemented
1081+ && tcx. defaultness ( impl_id) . is_final ( )
1082+ // unsized types don't need to implement methods that have `Self: Sized` bounds.
1083+ && !( self_is_guaranteed_unsized && tcx. generics_require_sized_self ( trait_item_id) )
1084+ {
10461085 missing_items. push ( tcx. associated_item ( trait_item_id) ) ;
10471086 }
10481087
0 commit comments