@@ -113,11 +113,6 @@ pub trait AstConv<'tcx> {
113
113
fn record_ty ( & self , hir_id : hir:: HirId , ty : Ty < ' tcx > , span : Span ) ;
114
114
}
115
115
116
- pub enum SizedByDefault {
117
- Yes ,
118
- No ,
119
- }
120
-
121
116
#[ derive( Debug ) ]
122
117
struct ConvertedBinding < ' a , ' tcx > {
123
118
hir_id : hir:: HirId ,
@@ -856,27 +851,30 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
856
851
}
857
852
858
853
// Returns `true` if a bounds list includes `?Sized`.
859
- fn is_unsized (
854
+ pub ( crate ) fn add_implicitly_sized < ' hir > (
860
855
& self ,
861
- ast_bounds : & [ hir :: GenericBound < ' _ > ] ,
862
- self_ty : Option < hir:: HirId > ,
863
- where_clause : Option < & [ hir:: WherePredicate < ' _ > ] > ,
856
+ bounds : & mut Bounds < ' hir > ,
857
+ ast_bounds : & ' hir [ hir :: GenericBound < ' hir > ] ,
858
+ self_ty_where_predicates : Option < ( hir :: HirId , & ' hir [ hir:: WherePredicate < ' hir > ] ) > ,
864
859
span : Span ,
865
- ) -> bool {
860
+ ) {
866
861
let tcx = self . tcx ( ) ;
867
862
868
863
// Try to find an unbound in bounds.
869
864
let mut unbound = None ;
870
- for ab in ast_bounds {
871
- if let hir:: GenericBound :: Trait ( ptr, hir:: TraitBoundModifier :: Maybe ) = ab {
872
- if unbound. is_none ( ) {
873
- unbound = Some ( & ptr. trait_ref ) ;
874
- } else {
875
- tcx. sess . emit_err ( MultipleRelaxedDefaultBounds { span } ) ;
865
+ let mut search_bounds = |ast_bounds : & ' hir [ hir:: GenericBound < ' hir > ] | {
866
+ for ab in ast_bounds {
867
+ if let hir:: GenericBound :: Trait ( ptr, hir:: TraitBoundModifier :: Maybe ) = ab {
868
+ if unbound. is_none ( ) {
869
+ unbound = Some ( & ptr. trait_ref ) ;
870
+ } else {
871
+ tcx. sess . emit_err ( MultipleRelaxedDefaultBounds { span } ) ;
872
+ }
876
873
}
877
874
}
878
- }
879
- if let ( Some ( self_ty) , Some ( where_clause) ) = ( self_ty, where_clause) {
875
+ } ;
876
+ search_bounds ( ast_bounds) ;
877
+ if let Some ( ( self_ty, where_clause) ) = self_ty_where_predicates {
880
878
let self_ty_def_id = tcx. hir ( ) . local_def_id ( self_ty) . to_def_id ( ) ;
881
879
for clause in where_clause {
882
880
match clause {
@@ -888,46 +886,40 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
888
886
} ,
889
887
_ => continue ,
890
888
}
891
- for ab in pred. bounds {
892
- if let hir:: GenericBound :: Trait ( ptr, hir:: TraitBoundModifier :: Maybe ) =
893
- ab
894
- {
895
- if unbound. is_none ( ) {
896
- unbound = Some ( & ptr. trait_ref ) ;
897
- } else {
898
- tcx. sess . emit_err ( MultipleRelaxedDefaultBounds { span } ) ;
899
- }
900
- }
901
- }
889
+ search_bounds ( pred. bounds ) ;
902
890
}
903
891
_ => { }
904
892
}
905
893
}
906
894
}
907
895
908
- let kind_id = tcx. lang_items ( ) . require ( LangItem :: Sized ) ;
909
- match unbound {
910
- Some ( tpb) => {
911
- if let Ok ( kind_id) = kind_id {
912
- if tpb. path . res != Res :: Def ( DefKind :: Trait , kind_id) {
913
- tcx. sess . span_warn (
914
- span,
915
- "default bound relaxed for a type parameter, but \
916
- this does nothing because the given bound is not \
917
- a default; only `?Sized` is supported",
918
- ) ;
919
- return false ;
920
- }
921
- }
896
+ let sized_def_id = tcx. lang_items ( ) . require ( LangItem :: Sized ) ;
897
+ match ( & sized_def_id, unbound) {
898
+ ( Ok ( sized_def_id) , Some ( tpb) )
899
+ if tpb. path . res == Res :: Def ( DefKind :: Trait , * sized_def_id) =>
900
+ {
901
+ // There was in fact a `?Sized` bound, return without doing anything
902
+ return ;
922
903
}
923
- _ if kind_id. is_ok ( ) => {
924
- return false ;
904
+ ( _, Some ( _) ) => {
905
+ // There was a `?Trait` bound, but it was not `?Sized`; warn.
906
+ tcx. sess . span_warn (
907
+ span,
908
+ "default bound relaxed for a type parameter, but \
909
+ this does nothing because the given bound is not \
910
+ a default; only `?Sized` is supported",
911
+ ) ;
912
+ // Otherwise, add implicitly sized if `Sized` is available.
913
+ }
914
+ _ => {
915
+ // There was no `?Sized` bound; add implicitly sized if `Sized` is available.
925
916
}
917
+ }
918
+ if sized_def_id. is_err ( ) {
926
919
// No lang item for `Sized`, so we can't add it as a bound.
927
- None => { }
920
+ return ;
928
921
}
929
-
930
- true
922
+ bounds. implicitly_sized = Some ( span) ;
931
923
}
932
924
933
925
/// This helper takes a *converted* parameter type (`param_ty`)
@@ -1009,19 +1001,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1009
1001
& self ,
1010
1002
param_ty : Ty < ' tcx > ,
1011
1003
ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
1012
- self_ty : Option < hir:: HirId > ,
1013
- where_clause : Option < & [ hir:: WherePredicate < ' _ > ] > ,
1014
- sized_by_default : SizedByDefault ,
1015
- span : Span ,
1016
1004
) -> Bounds < ' tcx > {
1017
- self . compute_bounds_inner (
1018
- param_ty,
1019
- & ast_bounds,
1020
- self_ty,
1021
- where_clause,
1022
- sized_by_default,
1023
- span,
1024
- )
1005
+ self . compute_bounds_inner ( param_ty, & ast_bounds)
1025
1006
}
1026
1007
1027
1008
/// Convert the bounds in `ast_bounds` that refer to traits which define an associated type
@@ -1030,10 +1011,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1030
1011
& self ,
1031
1012
param_ty : Ty < ' tcx > ,
1032
1013
ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
1033
- self_ty : Option < hir:: HirId > ,
1034
- where_clause : Option < & [ hir:: WherePredicate < ' _ > ] > ,
1035
- sized_by_default : SizedByDefault ,
1036
- span : Span ,
1037
1014
assoc_name : Ident ,
1038
1015
) -> Bounds < ' tcx > {
1039
1016
let mut result = Vec :: new ( ) ;
@@ -1048,32 +1025,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1048
1025
}
1049
1026
}
1050
1027
1051
- self . compute_bounds_inner ( param_ty, & result, self_ty , where_clause , sized_by_default , span )
1028
+ self . compute_bounds_inner ( param_ty, & result)
1052
1029
}
1053
1030
1054
1031
fn compute_bounds_inner (
1055
1032
& self ,
1056
1033
param_ty : Ty < ' tcx > ,
1057
1034
ast_bounds : & [ hir:: GenericBound < ' _ > ] ,
1058
- self_ty : Option < hir:: HirId > ,
1059
- where_clause : Option < & [ hir:: WherePredicate < ' _ > ] > ,
1060
- sized_by_default : SizedByDefault ,
1061
- span : Span ,
1062
1035
) -> Bounds < ' tcx > {
1063
1036
let mut bounds = Bounds :: default ( ) ;
1064
1037
1065
1038
self . add_bounds ( param_ty, ast_bounds, & mut bounds, ty:: List :: empty ( ) ) ;
1066
1039
1067
- bounds. implicitly_sized = if let SizedByDefault :: Yes = sized_by_default {
1068
- if !self . is_unsized ( ast_bounds, self_ty, where_clause, span) {
1069
- Some ( span)
1070
- } else {
1071
- None
1072
- }
1073
- } else {
1074
- None
1075
- } ;
1076
-
1077
1040
bounds
1078
1041
}
1079
1042
0 commit comments