@@ -30,7 +30,7 @@ use rustc_trait_selection::traits;
3030use rustc_trait_selection:: traits:: outlives_bounds:: InferCtxtExt as _;
3131use rustc_type_ir:: fold:: TypeFoldable ;
3232use tracing:: { debug, instrument} ;
33- use ty:: TypingMode ;
33+ use ty:: { TypingEnv , TypingMode } ;
3434use { rustc_attr_parsing as attr, rustc_hir as hir} ;
3535
3636use super :: compare_impl_item:: check_type_bounds;
@@ -1035,6 +1035,9 @@ fn check_impl_items_against_trait<'tcx>(
10351035
10361036 let trait_def = tcx. trait_def ( trait_ref. def_id ) ;
10371037
1038+ let self_is_sized =
1039+ trait_ref. self_ty ( ) . is_sized ( tcx, TypingEnv :: non_body_analysis ( tcx, impl_id) ) ;
1040+
10381041 for & impl_item in impl_item_refs {
10391042 let ty_impl_item = tcx. associated_item ( impl_item) ;
10401043 let ty_trait_item = if let Some ( trait_item_id) = ty_impl_item. trait_item_def_id {
@@ -1064,6 +1067,15 @@ fn check_impl_items_against_trait<'tcx>(
10641067 }
10651068 }
10661069
1070+ if !self_is_sized && tcx. generics_require_sized_self ( ty_trait_item. def_id ) {
1071+ tcx. emit_node_span_lint (
1072+ rustc_lint_defs:: builtin:: DEAD_CODE ,
1073+ tcx. local_def_id_to_hir_id ( ty_impl_item. def_id . expect_local ( ) ) ,
1074+ tcx. def_span ( ty_impl_item. def_id ) ,
1075+ errors:: UselessImplItem ,
1076+ )
1077+ }
1078+
10671079 check_specialization_validity (
10681080 tcx,
10691081 trait_def,
@@ -1087,7 +1099,11 @@ fn check_impl_items_against_trait<'tcx>(
10871099 . as_ref ( )
10881100 . is_some_and ( |node_item| node_item. item . defaultness ( tcx) . has_value ( ) ) ;
10891101
1090- if !is_implemented && tcx. defaultness ( impl_id) . is_final ( ) {
1102+ if !is_implemented
1103+ && tcx. defaultness ( impl_id) . is_final ( )
1104+ // unsized types don't need to implement methods that have `Self: Sized` bounds.
1105+ && ( self_is_sized || !tcx. generics_require_sized_self ( trait_item_id) )
1106+ {
10911107 missing_items. push ( tcx. associated_item ( trait_item_id) ) ;
10921108 }
10931109
0 commit comments