@@ -111,11 +111,6 @@ pub trait AstConv<'tcx> {
111111 fn record_ty ( & self , hir_id : hir:: HirId , ty : Ty < ' tcx > , span : Span ) ;
112112}
113113
114- pub enum SizedByDefault {
115- Yes ,
116- No ,
117- }
118-
119114#[ derive( Debug ) ]
120115struct ConvertedBinding < ' a , ' tcx > {
121116 hir_id : hir:: HirId ,
@@ -853,28 +848,31 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
853848 . is_some ( )
854849 }
855850
856- // Returns `true` if a bounds list includes `?Sized`.
857- fn is_unsized (
851+ // Sets `implicitly_sized` to true on `Bounds` if necessary
852+ pub ( crate ) fn add_implicitly_sized < ' hir > (
858853 & self ,
859- ast_bounds : & [ hir :: GenericBound < ' _ > ] ,
860- self_ty : Option < hir:: HirId > ,
861- where_clause : Option < & [ hir:: WherePredicate < ' _ > ] > ,
854+ bounds : & mut Bounds < ' hir > ,
855+ ast_bounds : & ' hir [ hir :: GenericBound < ' hir > ] ,
856+ self_ty_where_predicates : Option < ( hir :: HirId , & ' hir [ hir:: WherePredicate < ' hir > ] ) > ,
862857 span : Span ,
863- ) -> bool {
858+ ) {
864859 let tcx = self . tcx ( ) ;
865860
866861 // Try to find an unbound in bounds.
867862 let mut unbound = None ;
868- for ab in ast_bounds {
869- if let hir:: GenericBound :: Trait ( ptr, hir:: TraitBoundModifier :: Maybe ) = ab {
870- if unbound. is_none ( ) {
871- unbound = Some ( & ptr. trait_ref ) ;
872- } else {
873- tcx. sess . emit_err ( MultipleRelaxedDefaultBounds { span } ) ;
863+ let mut search_bounds = |ast_bounds : & ' hir [ hir:: GenericBound < ' hir > ] | {
864+ for ab in ast_bounds {
865+ if let hir:: GenericBound :: Trait ( ptr, hir:: TraitBoundModifier :: Maybe ) = ab {
866+ if unbound. is_none ( ) {
867+ unbound = Some ( & ptr. trait_ref ) ;
868+ } else {
869+ tcx. sess . emit_err ( MultipleRelaxedDefaultBounds { span } ) ;
870+ }
874871 }
875872 }
876- }
877- if let ( Some ( self_ty) , Some ( where_clause) ) = ( self_ty, where_clause) {
873+ } ;
874+ search_bounds ( ast_bounds) ;
875+ if let Some ( ( self_ty, where_clause) ) = self_ty_where_predicates {
878876 let self_ty_def_id = tcx. hir ( ) . local_def_id ( self_ty) . to_def_id ( ) ;
879877 for clause in where_clause {
880878 match clause {
@@ -886,46 +884,40 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
886884 } ,
887885 _ => continue ,
888886 }
889- for ab in pred. bounds {
890- if let hir:: GenericBound :: Trait ( ptr, hir:: TraitBoundModifier :: Maybe ) =
891- ab
892- {
893- if unbound. is_none ( ) {
894- unbound = Some ( & ptr. trait_ref ) ;
895- } else {
896- tcx. sess . emit_err ( MultipleRelaxedDefaultBounds { span } ) ;
897- }
898- }
899- }
887+ search_bounds ( pred. bounds ) ;
900888 }
901889 _ => { }
902890 }
903891 }
904892 }
905893
906- let kind_id = tcx. lang_items ( ) . require ( LangItem :: Sized ) ;
907- match unbound {
908- Some ( tpb) => {
909- if let Ok ( kind_id) = kind_id {
910- if tpb. path . res != Res :: Def ( DefKind :: Trait , kind_id) {
911- tcx. sess . span_warn (
912- span,
913- "default bound relaxed for a type parameter, but \
914- this does nothing because the given bound is not \
915- a default; only `?Sized` is supported",
916- ) ;
917- return false ;
918- }
919- }
894+ let sized_def_id = tcx. lang_items ( ) . require ( LangItem :: Sized ) ;
895+ match ( & sized_def_id, unbound) {
896+ ( Ok ( sized_def_id) , Some ( tpb) )
897+ if tpb. path . res == Res :: Def ( DefKind :: Trait , * sized_def_id) =>
898+ {
899+ // There was in fact a `?Sized` bound, return without doing anything
900+ return ;
920901 }
921- _ if kind_id. is_ok ( ) => {
922- return false ;
902+ ( _, Some ( _) ) => {
903+ // There was a `?Trait` bound, but it was not `?Sized`; warn.
904+ tcx. sess . span_warn (
905+ span,
906+ "default bound relaxed for a type parameter, but \
907+ this does nothing because the given bound is not \
908+ a default; only `?Sized` is supported",
909+ ) ;
910+ // Otherwise, add implicitly sized if `Sized` is available.
911+ }
912+ _ => {
913+ // There was no `?Sized` bound; add implicitly sized if `Sized` is available.
923914 }
915+ }
916+ if sized_def_id. is_err ( ) {
924917 // No lang item for `Sized`, so we can't add it as a bound.
925- None => { }
918+ return ;
926919 }
927-
928- true
920+ bounds. implicitly_sized = Some ( span) ;
929921 }
930922
931923 /// This helper takes a *converted* parameter type (`param_ty`)
@@ -1006,19 +998,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1006998 & self ,
1007999 param_ty : Ty < ' tcx > ,
10081000 ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
1009- self_ty : Option < hir:: HirId > ,
1010- where_clause : Option < & [ hir:: WherePredicate < ' _ > ] > ,
1011- sized_by_default : SizedByDefault ,
1012- span : Span ,
10131001 ) -> Bounds < ' tcx > {
1014- self . compute_bounds_inner (
1015- param_ty,
1016- & ast_bounds,
1017- self_ty,
1018- where_clause,
1019- sized_by_default,
1020- span,
1021- )
1002+ self . compute_bounds_inner ( param_ty, & ast_bounds)
10221003 }
10231004
10241005 /// Convert the bounds in `ast_bounds` that refer to traits which define an associated type
@@ -1027,10 +1008,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10271008 & self ,
10281009 param_ty : Ty < ' tcx > ,
10291010 ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
1030- self_ty : Option < hir:: HirId > ,
1031- where_clause : Option < & [ hir:: WherePredicate < ' _ > ] > ,
1032- sized_by_default : SizedByDefault ,
1033- span : Span ,
10341011 assoc_name : Ident ,
10351012 ) -> Bounds < ' tcx > {
10361013 let mut result = Vec :: new ( ) ;
@@ -1045,32 +1022,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10451022 }
10461023 }
10471024
1048- self . compute_bounds_inner ( param_ty, & result, self_ty , where_clause , sized_by_default , span )
1025+ self . compute_bounds_inner ( param_ty, & result)
10491026 }
10501027
10511028 fn compute_bounds_inner (
10521029 & self ,
10531030 param_ty : Ty < ' tcx > ,
10541031 ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
1055- self_ty : Option < hir:: HirId > ,
1056- where_clause : Option < & [ hir:: WherePredicate < ' _ > ] > ,
1057- sized_by_default : SizedByDefault ,
1058- span : Span ,
10591032 ) -> Bounds < ' tcx > {
10601033 let mut bounds = Bounds :: default ( ) ;
10611034
10621035 self . add_bounds ( param_ty, ast_bounds, & mut bounds, ty:: List :: empty ( ) ) ;
10631036
1064- bounds. implicitly_sized = if let SizedByDefault :: Yes = sized_by_default {
1065- if !self . is_unsized ( ast_bounds, self_ty, where_clause, span) {
1066- Some ( span)
1067- } else {
1068- None
1069- }
1070- } else {
1071- None
1072- } ;
1073-
10741037 bounds
10751038 }
10761039
0 commit comments