@@ -992,6 +992,32 @@ fn check_impl_items_against_trait<'tcx>(
992992
993993 let trait_def = tcx. trait_def ( trait_ref. def_id ) ;
994994
995+ let infcx = tcx. infer_ctxt ( ) . ignoring_regions ( ) . build ( TypingMode :: non_body_analysis ( ) ) ;
996+
997+ let ocx = ObligationCtxt :: new_with_diagnostics ( & infcx) ;
998+ let cause = ObligationCause :: misc ( tcx. def_span ( impl_id) , impl_id) ;
999+ let param_env = tcx. param_env ( impl_id) ;
1000+
1001+ let self_is_guaranteed_unsized = match tcx
1002+ . struct_tail_raw (
1003+ trait_ref. self_ty ( ) ,
1004+ |ty| {
1005+ ocx. structurally_normalize_ty ( & cause, param_env, ty) . unwrap_or_else ( |_| {
1006+ Ty :: new_error_with_message (
1007+ tcx,
1008+ tcx. def_span ( impl_id) ,
1009+ "struct tail should be computable" ,
1010+ )
1011+ } )
1012+ } ,
1013+ || ( ) ,
1014+ )
1015+ . kind ( )
1016+ {
1017+ ty:: Dynamic ( _, _, ty:: DynKind :: Dyn ) | ty:: Slice ( _) | ty:: Str => true ,
1018+ _ => false ,
1019+ } ;
1020+
9951021 for & impl_item in impl_item_refs {
9961022 let ty_impl_item = tcx. associated_item ( impl_item) ;
9971023 let ty_trait_item = if let Some ( trait_item_id) = ty_impl_item. trait_item_def_id {
@@ -1021,6 +1047,15 @@ fn check_impl_items_against_trait<'tcx>(
10211047 }
10221048 }
10231049
1050+ if self_is_guaranteed_unsized && tcx. generics_require_sized_self ( ty_trait_item. def_id ) {
1051+ tcx. emit_node_span_lint (
1052+ rustc_lint_defs:: builtin:: DEAD_CODE ,
1053+ tcx. local_def_id_to_hir_id ( ty_impl_item. def_id . expect_local ( ) ) ,
1054+ tcx. def_span ( ty_impl_item. def_id ) ,
1055+ errors:: UselessImplItem ,
1056+ )
1057+ }
1058+
10241059 check_specialization_validity (
10251060 tcx,
10261061 trait_def,
@@ -1044,7 +1079,11 @@ fn check_impl_items_against_trait<'tcx>(
10441079 . as_ref ( )
10451080 . is_some_and ( |node_item| node_item. item . defaultness ( tcx) . has_value ( ) ) ;
10461081
1047- if !is_implemented && tcx. defaultness ( impl_id) . is_final ( ) {
1082+ if !is_implemented
1083+ && tcx. defaultness ( impl_id) . is_final ( )
1084+ // unsized types don't need to implement methods that have `Self: Sized` bounds.
1085+ && !( self_is_guaranteed_unsized && tcx. generics_require_sized_self ( trait_item_id) )
1086+ {
10481087 missing_items. push ( tcx. associated_item ( trait_item_id) ) ;
10491088 }
10501089
0 commit comments