@@ -132,18 +132,18 @@ impl<'cx, 'gcx, 'tcx> Elaborator<'cx, 'gcx, 'tcx> {
132132 // Get predicates declared on the trait.
133133 let predicates = tcx. super_predicates_of ( data. def_id ( ) ) ;
134134
135- let mut predicates: Vec < _ > = predicates. predicates
135+ let predicates = predicates. predicates
136136 . iter ( )
137- . map ( |( pred, _) | pred. subst_supertrait ( tcx, & data. to_poly_trait_ref ( ) ) )
138- . collect ( ) ;
137+ . map ( |( pred, _) | pred. subst_supertrait ( tcx, & data. to_poly_trait_ref ( ) ) ) ;
139138 debug ! ( "super_predicates: data={:?} predicates={:?}" ,
140- data, predicates) ;
139+ data, predicates. clone ( ) ) ;
141140
142141 // Only keep those bounds that we haven't already seen.
143142 // This is necessary to prevent infinite recursion in some
144143 // cases. One common case is when people define
145144 // `trait Sized: Sized { }` rather than `trait Sized { }`.
146- predicates. retain ( |pred| self . visited . insert ( pred) ) ;
145+ let visited = & mut self . visited ;
146+ let predicates = predicates. filter ( |pred| visited. insert ( pred) ) ;
147147
148148 self . stack . extend ( predicates) ;
149149 }
@@ -298,13 +298,21 @@ impl<'tcx> TraitAliasExpansionInfo<'tcx> {
298298 }
299299 }
300300
301- fn clone_and_push ( & self , trait_ref : ty:: PolyTraitRef < ' tcx > , span : Span ) -> Self {
302- let mut path = self . path . clone ( ) ;
303- path. push ( ( trait_ref, span) ) ;
304-
305- Self {
306- path
301+ /// Adds diagnostic labels to `diag` for the expansion path of a trait through all intermediate
302+ /// trait aliases.
303+ pub fn label_with_exp_info ( & self ,
304+ diag : & mut DiagnosticBuilder < ' _ > ,
305+ top_label : & str ,
306+ use_desc : & str
307+ ) {
308+ diag. span_label ( self . top ( ) . 1 , top_label) ;
309+ if self . path . len ( ) > 1 {
310+ for ( _, sp) in self . path . iter ( ) . rev ( ) . skip ( 1 ) . take ( self . path . len ( ) - 2 ) {
311+ diag. span_label ( * sp, format ! ( "referenced here ({})" , use_desc) ) ;
312+ }
307313 }
314+ diag. span_label ( self . bottom ( ) . 1 ,
315+ format ! ( "trait alias used in trait object type ({})" , use_desc) ) ;
308316 }
309317
310318 pub fn trait_ref ( & self ) -> & ty:: PolyTraitRef < ' tcx > {
@@ -318,33 +326,14 @@ impl<'tcx> TraitAliasExpansionInfo<'tcx> {
318326 pub fn bottom ( & self ) -> & ( ty:: PolyTraitRef < ' tcx > , Span ) {
319327 self . path . first ( ) . unwrap ( )
320328 }
321- }
322329
323- /// Emits diagnostic information relating to the expansion of a trait via trait aliases
324- /// (see [`TraitAliasExpansionInfo`]).
325- pub trait TraitAliasExpansionInfoDignosticBuilder {
326- fn label_with_exp_info < ' tcx > ( & mut self ,
327- info : & TraitAliasExpansionInfo < ' tcx > ,
328- top_label : & str ,
329- use_desc : & str
330- ) -> & mut Self ;
331- }
330+ fn clone_and_push ( & self , trait_ref : ty:: PolyTraitRef < ' tcx > , span : Span ) -> Self {
331+ let mut path = self . path . clone ( ) ;
332+ path. push ( ( trait_ref, span) ) ;
332333
333- impl < ' a > TraitAliasExpansionInfoDignosticBuilder for DiagnosticBuilder < ' a > {
334- fn label_with_exp_info < ' tcx > ( & mut self ,
335- info : & TraitAliasExpansionInfo < ' tcx > ,
336- top_label : & str ,
337- use_desc : & str
338- ) -> & mut Self {
339- self . span_label ( info. top ( ) . 1 , top_label) ;
340- if info. path . len ( ) > 1 {
341- for ( _, sp) in info. path . iter ( ) . rev ( ) . skip ( 1 ) . take ( info. path . len ( ) - 2 ) {
342- self . span_label ( * sp, format ! ( "referenced here ({})" , use_desc) ) ;
343- }
334+ Self {
335+ path
344336 }
345- self . span_label ( info. bottom ( ) . 1 ,
346- format ! ( "trait alias used in trait object type ({})" , use_desc) ) ;
347- self
348337 }
349338}
350339
@@ -388,16 +377,15 @@ impl<'cx, 'gcx, 'tcx> TraitAliasExpander<'cx, 'gcx, 'tcx> {
388377 // Get components of trait alias.
389378 let predicates = tcx. super_predicates_of ( trait_ref. def_id ( ) ) ;
390379
391- let items: Vec < _ > = predicates. predicates
380+ let items = predicates. predicates
392381 . iter ( )
393382 . rev ( )
394383 . filter_map ( |( pred, span) | {
395384 pred. subst_supertrait ( tcx, & trait_ref)
396385 . to_opt_poly_trait_ref ( )
397386 . map ( |trait_ref| item. clone_and_push ( trait_ref, * span) )
398- } )
399- . collect ( ) ;
400- debug ! ( "expand_trait_aliases: items={:?}" , items) ;
387+ } ) ;
388+ debug ! ( "expand_trait_aliases: items={:?}" , items. clone( ) ) ;
401389
402390 self . stack . extend ( items) ;
403391
0 commit comments