@@ -992,6 +992,32 @@ fn check_impl_items_against_trait<'tcx>(
992
992
993
993
let trait_def = tcx. trait_def ( trait_ref. def_id ) ;
994
994
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
+
995
1021
for & impl_item in impl_item_refs {
996
1022
let ty_impl_item = tcx. associated_item ( impl_item) ;
997
1023
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>(
1021
1047
}
1022
1048
}
1023
1049
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
+
1024
1059
check_specialization_validity (
1025
1060
tcx,
1026
1061
trait_def,
@@ -1044,7 +1079,11 @@ fn check_impl_items_against_trait<'tcx>(
1044
1079
. as_ref ( )
1045
1080
. is_some_and ( |node_item| node_item. item . defaultness ( tcx) . has_value ( ) ) ;
1046
1081
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
+ {
1048
1087
missing_items. push ( tcx. associated_item ( trait_item_id) ) ;
1049
1088
}
1050
1089
0 commit comments