@@ -990,6 +990,32 @@ fn check_impl_items_against_trait<'tcx>(
990
990
991
991
let trait_def = tcx. trait_def ( trait_ref. def_id ) ;
992
992
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
+
993
1019
for & impl_item in impl_item_refs {
994
1020
let ty_impl_item = tcx. associated_item ( impl_item) ;
995
1021
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>(
1019
1045
}
1020
1046
}
1021
1047
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
+
1022
1057
check_specialization_validity (
1023
1058
tcx,
1024
1059
trait_def,
@@ -1042,7 +1077,11 @@ fn check_impl_items_against_trait<'tcx>(
1042
1077
. as_ref ( )
1043
1078
. is_some_and ( |node_item| node_item. item . defaultness ( tcx) . has_value ( ) ) ;
1044
1079
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
+ {
1046
1085
missing_items. push ( tcx. associated_item ( trait_item_id) ) ;
1047
1086
}
1048
1087
0 commit comments