@@ -25,6 +25,26 @@ pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
25
25
pub ( super ) lctx : & ' a mut LoweringContext < ' lowering , ' hir > ,
26
26
}
27
27
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
+
28
48
impl ItemLowerer < ' _ , ' _ , ' _ > {
29
49
fn with_trait_impl_ref < T > (
30
50
& mut self ,
@@ -298,8 +318,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
298
318
} ,
299
319
) ;
300
320
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 ) ;
303
322
let generics = self . lower_generics (
304
323
& generics,
305
324
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
@@ -311,8 +330,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
311
330
} ) => {
312
331
let ty = self . arena . alloc ( self . ty ( span, hir:: TyKind :: Err ) ) ;
313
332
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 ) ;
316
334
let generics = self . lower_generics (
317
335
& generics,
318
336
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
@@ -856,8 +874,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
856
874
self . lower_ty ( x, ImplTraitContext :: Disallowed ( ImplTraitPosition :: Type ) )
857
875
} ) ;
858
876
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 ) ;
861
878
let generics = self . lower_generics (
862
879
& generics,
863
880
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
@@ -941,8 +958,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
941
958
}
942
959
AssocItemKind :: TyAlias ( box TyAlias { generics, where_clauses, ty, .. } ) => {
943
960
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 ) ;
946
962
let generics = self . lower_generics (
947
963
& generics,
948
964
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
0 commit comments