@@ -111,11 +111,6 @@ pub trait AstConv<'tcx> {
111
111
fn record_ty ( & self , hir_id : hir:: HirId , ty : Ty < ' tcx > , span : Span ) ;
112
112
}
113
113
114
- pub enum SizedByDefault {
115
- Yes ,
116
- No ,
117
- }
118
-
119
114
#[ derive( Debug ) ]
120
115
struct ConvertedBinding < ' a , ' tcx > {
121
116
hir_id : hir:: HirId ,
@@ -853,28 +848,31 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
853
848
. is_some ( )
854
849
}
855
850
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 > (
858
853
& 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 > ] ) > ,
862
857
span : Span ,
863
- ) -> bool {
858
+ ) {
864
859
let tcx = self . tcx ( ) ;
865
860
866
861
// Try to find an unbound in bounds.
867
862
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
+ }
874
871
}
875
872
}
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 {
878
876
let self_ty_def_id = tcx. hir ( ) . local_def_id ( self_ty) . to_def_id ( ) ;
879
877
for clause in where_clause {
880
878
match clause {
@@ -886,46 +884,40 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
886
884
} ,
887
885
_ => continue ,
888
886
}
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 ) ;
900
888
}
901
889
_ => { }
902
890
}
903
891
}
904
892
}
905
893
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 ;
920
901
}
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.
923
914
}
915
+ }
916
+ if sized_def_id. is_err ( ) {
924
917
// No lang item for `Sized`, so we can't add it as a bound.
925
- None => { }
918
+ return ;
926
919
}
927
-
928
- true
920
+ bounds. implicitly_sized = Some ( span) ;
929
921
}
930
922
931
923
/// This helper takes a *converted* parameter type (`param_ty`)
@@ -1006,19 +998,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1006
998
& self ,
1007
999
param_ty : Ty < ' tcx > ,
1008
1000
ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
1009
- self_ty : Option < hir:: HirId > ,
1010
- where_clause : Option < & [ hir:: WherePredicate < ' _ > ] > ,
1011
- sized_by_default : SizedByDefault ,
1012
- span : Span ,
1013
1001
) -> 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)
1022
1003
}
1023
1004
1024
1005
/// 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 {
1027
1008
& self ,
1028
1009
param_ty : Ty < ' tcx > ,
1029
1010
ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
1030
- self_ty : Option < hir:: HirId > ,
1031
- where_clause : Option < & [ hir:: WherePredicate < ' _ > ] > ,
1032
- sized_by_default : SizedByDefault ,
1033
- span : Span ,
1034
1011
assoc_name : Ident ,
1035
1012
) -> Bounds < ' tcx > {
1036
1013
let mut result = Vec :: new ( ) ;
@@ -1045,32 +1022,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1045
1022
}
1046
1023
}
1047
1024
1048
- self . compute_bounds_inner ( param_ty, & result, self_ty , where_clause , sized_by_default , span )
1025
+ self . compute_bounds_inner ( param_ty, & result)
1049
1026
}
1050
1027
1051
1028
fn compute_bounds_inner (
1052
1029
& self ,
1053
1030
param_ty : Ty < ' tcx > ,
1054
1031
ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
1055
- self_ty : Option < hir:: HirId > ,
1056
- where_clause : Option < & [ hir:: WherePredicate < ' _ > ] > ,
1057
- sized_by_default : SizedByDefault ,
1058
- span : Span ,
1059
1032
) -> Bounds < ' tcx > {
1060
1033
let mut bounds = Bounds :: default ( ) ;
1061
1034
1062
1035
self . add_bounds ( param_ty, ast_bounds, & mut bounds, ty:: List :: empty ( ) ) ;
1063
1036
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
-
1074
1037
bounds
1075
1038
}
1076
1039
0 commit comments