@@ -12,7 +12,7 @@ use rustc_middle::ty::{
1212 self as ty, IsSuggestable , Ty , TyCtxt , TypeSuperVisitable , TypeVisitable , TypeVisitableExt ,
1313 TypeVisitor , Upcast ,
1414} ;
15- use rustc_span:: { DesugaringKind , ErrorGuaranteed , Ident , Span , kw} ;
15+ use rustc_span:: { ErrorGuaranteed , Ident , Span , kw} ;
1616use rustc_trait_selection:: traits;
1717use tracing:: { debug, instrument} ;
1818
@@ -25,17 +25,17 @@ use crate::hir_ty_lowering::{
2525#[ derive( Debug , Default ) ]
2626struct CollectedBound {
2727 /// `Trait`
28- positive : Option < Span > ,
28+ positive : bool ,
2929 /// `?Trait`
30- maybe : Option < Span > ,
30+ maybe : bool ,
3131 /// `!Trait`
32- negative : Option < Span > ,
32+ negative : bool ,
3333}
3434
3535impl CollectedBound {
3636 /// Returns `true` if any of `Trait`, `?Trait` or `!Trait` were encountered.
3737 fn any ( & self ) -> bool {
38- self . positive . is_some ( ) || self . maybe . is_some ( ) || self . negative . is_some ( )
38+ self . positive || self . maybe || self . negative
3939 }
4040}
4141
@@ -59,8 +59,7 @@ impl CollectedSizednessBounds {
5959
6060fn search_bounds_for < ' tcx > (
6161 hir_bounds : & ' tcx [ hir:: GenericBound < ' tcx > ] ,
62- where_bounds : & ' tcx [ hir:: WherePredicate < ' tcx > ] ,
63- context : ImpliedBoundsContext ,
62+ context : ImpliedBoundsContext < ' tcx > ,
6463 mut f : impl FnMut ( & ' tcx PolyTraitRef < ' tcx > ) ,
6564) {
6665 let mut search_bounds = |hir_bounds : & ' tcx [ hir:: GenericBound < ' tcx > ] | {
@@ -74,8 +73,8 @@ fn search_bounds_for<'tcx>(
7473 } ;
7574
7675 search_bounds ( hir_bounds) ;
77- if let ImpliedBoundsContext :: TyParam ( self_ty) = context {
78- for clause in where_bounds {
76+ if let ImpliedBoundsContext :: TyParam ( self_ty, where_clause ) = context {
77+ for clause in where_clause {
7978 if let hir:: WherePredicateKind :: BoundPredicate ( pred) = clause. kind
8079 && pred. is_param_bound ( self_ty. to_def_id ( ) )
8180 {
@@ -87,20 +86,19 @@ fn search_bounds_for<'tcx>(
8786
8887fn collect_bounds < ' a , ' tcx > (
8988 hir_bounds : & ' a [ hir:: GenericBound < ' tcx > ] ,
90- where_bounds : & ' tcx [ hir:: WherePredicate < ' tcx > ] ,
91- context : ImpliedBoundsContext ,
89+ context : ImpliedBoundsContext < ' tcx > ,
9290 target_did : DefId ,
9391) -> CollectedBound {
9492 let mut collect_into = CollectedBound :: default ( ) ;
95- search_bounds_for ( hir_bounds, where_bounds , context, |ptr| {
93+ search_bounds_for ( hir_bounds, context, |ptr| {
9694 if !matches ! ( ptr. trait_ref. path. res, Res :: Def ( DefKind :: Trait , did) if did == target_did) {
9795 return ;
9896 }
9997
10098 match ptr. modifiers . polarity {
101- hir:: BoundPolarity :: Maybe ( _) => collect_into. maybe = Some ( ptr . span ) ,
102- hir:: BoundPolarity :: Negative ( _) => collect_into. negative = Some ( ptr . span ) ,
103- hir:: BoundPolarity :: Positive => collect_into. positive = Some ( ptr . span ) ,
99+ hir:: BoundPolarity :: Maybe ( _) => collect_into. maybe = true ,
100+ hir:: BoundPolarity :: Negative ( _) => collect_into. negative = true ,
101+ hir:: BoundPolarity :: Positive => collect_into. positive = true ,
104102 }
105103 } ) ;
106104 collect_into
@@ -109,18 +107,17 @@ fn collect_bounds<'a, 'tcx>(
109107fn collect_sizedness_bounds < ' tcx > (
110108 tcx : TyCtxt < ' tcx > ,
111109 hir_bounds : & ' tcx [ hir:: GenericBound < ' tcx > ] ,
112- where_bounds : & ' tcx [ hir:: WherePredicate < ' tcx > ] ,
113- context : ImpliedBoundsContext ,
110+ context : ImpliedBoundsContext < ' tcx > ,
114111 span : Span ,
115112) -> CollectedSizednessBounds {
116113 let sized_did = tcx. require_lang_item ( hir:: LangItem :: Sized , span) ;
117- let sized = collect_bounds ( hir_bounds, where_bounds , context, sized_did) ;
114+ let sized = collect_bounds ( hir_bounds, context, sized_did) ;
118115
119116 let meta_sized_did = tcx. require_lang_item ( hir:: LangItem :: MetaSized , span) ;
120- let meta_sized = collect_bounds ( hir_bounds, where_bounds , context, meta_sized_did) ;
117+ let meta_sized = collect_bounds ( hir_bounds, context, meta_sized_did) ;
121118
122119 let pointee_sized_did = tcx. require_lang_item ( hir:: LangItem :: PointeeSized , span) ;
123- let pointee_sized = collect_bounds ( hir_bounds, where_bounds , context, pointee_sized_did) ;
120+ let pointee_sized = collect_bounds ( hir_bounds, context, pointee_sized_did) ;
124121
125122 CollectedSizednessBounds { sized, meta_sized, pointee_sized }
126123}
@@ -153,8 +150,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
153150 bounds : & mut Vec < ( ty:: Clause < ' tcx > , Span ) > ,
154151 self_ty : Ty < ' tcx > ,
155152 hir_bounds : & ' tcx [ hir:: GenericBound < ' tcx > ] ,
156- where_bounds : & ' tcx [ hir:: WherePredicate < ' tcx > ] ,
157- context : ImpliedBoundsContext ,
153+ context : ImpliedBoundsContext < ' tcx > ,
158154 span : Span ,
159155 ) {
160156 let tcx = self . tcx ( ) ;
@@ -181,48 +177,28 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
181177 return ;
182178 }
183179 }
184- ImpliedBoundsContext :: TyParam ( ..)
185- | ImpliedBoundsContext :: AssociatedType ( ..)
186- | ImpliedBoundsContext :: TraitObject
187- | ImpliedBoundsContext :: TraitAscription
188- | ImpliedBoundsContext :: ImplTrait => { }
180+ ImpliedBoundsContext :: TyParam ( ..) | ImpliedBoundsContext :: AssociatedTypeOrImplTrait => {
181+ }
189182 }
190- let collected = collect_sizedness_bounds ( tcx, hir_bounds, where_bounds, context, span) ;
191- if let Some ( span) = collected. sized . maybe . or ( collected. sized . negative )
192- && collected. sized . positive . is_none ( )
183+
184+ let collected = collect_sizedness_bounds ( tcx, hir_bounds, context, span) ;
185+ if ( collected. sized . maybe || collected. sized . negative )
186+ && !collected. sized . positive
193187 && !collected. meta_sized . any ( )
194188 && !collected. pointee_sized . any ( )
195189 {
196190 // `?Sized` is equivalent to `MetaSized` (but only add the bound if there aren't any
197191 // other explicit ones) - this can happen for trait aliases as well as bounds.
198192 add_trait_bound ( tcx, bounds, self_ty, meta_sized_did, span) ;
199193 } else if !collected. any ( ) {
200- let span = match context {
201- ImpliedBoundsContext :: TraitDef ( def)
202- | ImpliedBoundsContext :: TyParam ( def)
203- | ImpliedBoundsContext :: AssociatedType ( def) => {
204- self . tcx ( ) . with_stable_hashing_context ( |hcx| {
205- span. mark_with_reason (
206- None ,
207- DesugaringKind :: DefaultBound { def : def. into ( ) } ,
208- span. edition ( ) ,
209- hcx,
210- )
211- } )
212- }
213- _ => span,
214- } ;
215194 match context {
216195 ImpliedBoundsContext :: TraitDef ( ..) => {
217196 // If there are no explicit sizedness bounds on a trait then add a default
218197 // `MetaSized` supertrait.
219198 add_trait_bound ( tcx, bounds, self_ty, meta_sized_did, span) ;
220199 }
221200 ImpliedBoundsContext :: TyParam ( ..)
222- | ImpliedBoundsContext :: AssociatedType ( ..)
223- | ImpliedBoundsContext :: TraitObject
224- | ImpliedBoundsContext :: TraitAscription
225- | ImpliedBoundsContext :: ImplTrait => {
201+ | ImpliedBoundsContext :: AssociatedTypeOrImplTrait => {
226202 // If there are no explicit sizedness bounds on a parameter then add a default
227203 // `Sized` bound.
228204 let sized_did = tcx. require_lang_item ( hir:: LangItem :: Sized , span) ;
@@ -237,20 +213,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
237213 bounds : & mut Vec < ( ty:: Clause < ' tcx > , Span ) > ,
238214 self_ty : Ty < ' tcx > ,
239215 hir_bounds : & [ hir:: GenericBound < ' tcx > ] ,
240- where_bounds : & ' tcx [ hir:: WherePredicate < ' tcx > ] ,
241- context : ImpliedBoundsContext ,
216+ context : ImpliedBoundsContext < ' tcx > ,
242217 span : Span ,
243218 ) {
244219 self . tcx ( ) . default_traits ( ) . iter ( ) . for_each ( |default_trait| {
245- self . add_default_trait (
246- * default_trait,
247- bounds,
248- self_ty,
249- hir_bounds,
250- where_bounds,
251- context,
252- span,
253- ) ;
220+ self . add_default_trait ( * default_trait, bounds, self_ty, hir_bounds, context, span) ;
254221 } ) ;
255222 }
256223
@@ -263,8 +230,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
263230 bounds : & mut Vec < ( ty:: Clause < ' tcx > , Span ) > ,
264231 self_ty : Ty < ' tcx > ,
265232 hir_bounds : & [ hir:: GenericBound < ' tcx > ] ,
266- where_bounds : & ' tcx [ hir:: WherePredicate < ' tcx > ] ,
267- context : ImpliedBoundsContext ,
233+ context : ImpliedBoundsContext < ' tcx > ,
268234 span : Span ,
269235 ) {
270236 let tcx = self . tcx ( ) ;
@@ -278,7 +244,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
278244 }
279245
280246 if let Some ( trait_did) = tcx. lang_items ( ) . get ( trait_)
281- && self . should_add_default_traits ( trait_did, hir_bounds, where_bounds , context)
247+ && self . should_add_default_traits ( trait_did, hir_bounds, context)
282248 {
283249 add_trait_bound ( tcx, bounds, self_ty, trait_did, span) ;
284250 }
@@ -289,10 +255,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
289255 & self ,
290256 trait_def_id : DefId ,
291257 hir_bounds : & ' a [ hir:: GenericBound < ' tcx > ] ,
292- where_bounds : & ' tcx [ hir:: WherePredicate < ' tcx > ] ,
293- context : ImpliedBoundsContext ,
258+ context : ImpliedBoundsContext < ' tcx > ,
294259 ) -> bool {
295- let collected = collect_bounds ( hir_bounds, where_bounds , context, trait_def_id) ;
260+ let collected = collect_bounds ( hir_bounds, context, trait_def_id) ;
296261 !find_attr ! ( self . tcx( ) , crate , RustcNoImplicitBounds ) && !collected. any ( )
297262 }
298263
0 commit comments