@@ -25,6 +25,26 @@ pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
2525 pub ( super ) lctx : & ' a mut LoweringContext < ' lowering , ' hir > ,
2626}
2727
28+ /// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
29+ /// to the where clause that is prefered, if it exists. Otherwise, it sets the span to the other where
30+ /// clause if it exists.
31+ fn add_ty_alias_where_clause (
32+ generics : & mut ast:: Generics ,
33+ mut where_clauses : ( TyAliasWhereClause , TyAliasWhereClause ) ,
34+ prefer_first : bool ,
35+ ) {
36+ if !prefer_first {
37+ where_clauses = ( where_clauses. 1 , where_clauses. 0 ) ;
38+ }
39+ if where_clauses. 0 . 0 || !where_clauses. 1 . 0 {
40+ generics. where_clause . has_where_token = where_clauses. 0 . 0 ;
41+ generics. where_clause . span = where_clauses. 0 . 1 ;
42+ } else {
43+ generics. where_clause . has_where_token = where_clauses. 1 . 0 ;
44+ generics. where_clause . span = where_clauses. 1 . 1 ;
45+ }
46+ }
47+
2848impl ItemLowerer < ' _ , ' _ , ' _ > {
2949 fn with_trait_impl_ref < T > (
3050 & mut self ,
@@ -298,8 +318,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
298318 } ,
299319 ) ;
300320 let mut generics = generics. clone ( ) ;
301- generics. where_clause . has_where_token = where_clauses. 0 . 0 ;
302- generics. where_clause . span = where_clauses. 0 . 1 ;
321+ add_ty_alias_where_clause ( & mut generics, where_clauses, true ) ;
303322 let generics = self . lower_generics (
304323 & generics,
305324 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
@@ -311,8 +330,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
311330 } ) => {
312331 let ty = self . arena . alloc ( self . ty ( span, hir:: TyKind :: Err ) ) ;
313332 let mut generics = generics. clone ( ) ;
314- generics. where_clause . has_where_token = where_clauses. 0 . 0 ;
315- generics. where_clause . span = where_clauses. 0 . 1 ;
333+ add_ty_alias_where_clause ( & mut generics, * where_clauses, true ) ;
316334 let generics = self . lower_generics (
317335 & generics,
318336 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
@@ -856,8 +874,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
856874 self . lower_ty ( x, ImplTraitContext :: Disallowed ( ImplTraitPosition :: Type ) )
857875 } ) ;
858876 let mut generics = generics. clone ( ) ;
859- generics. where_clause . has_where_token = where_clauses. 1 . 0 ;
860- generics. where_clause . span = where_clauses. 1 . 1 ;
877+ add_ty_alias_where_clause ( & mut generics, where_clauses, false ) ;
861878 let generics = self . lower_generics (
862879 & generics,
863880 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
@@ -941,8 +958,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
941958 }
942959 AssocItemKind :: TyAlias ( box TyAlias { generics, where_clauses, ty, .. } ) => {
943960 let mut generics = generics. clone ( ) ;
944- generics. where_clause . has_where_token = where_clauses. 1 . 0 ;
945- generics. where_clause . span = where_clauses. 1 . 1 ;
961+ add_ty_alias_where_clause ( & mut generics, * where_clauses, false ) ;
946962 let generics = self . lower_generics (
947963 & generics,
948964 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
0 commit comments