@@ -54,17 +54,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
54
54
}
55
55
( ty:: Param ( expected) , ty:: Param ( found) ) => {
56
56
let generics = tcx. generics_of ( body_owner_def_id) ;
57
- if let Some ( param) = generics. opt_type_param ( expected, tcx) {
58
- let e_span = tcx. def_span ( param. def_id ) ;
59
- if !sp. contains ( e_span) {
60
- diag. span_label ( e_span, "expected type parameter" ) ;
61
- }
57
+ let e_span = tcx. def_span ( generics. type_param ( expected, tcx) . def_id ) ;
58
+ if !sp. contains ( e_span) {
59
+ diag. span_label ( e_span, "expected type parameter" ) ;
62
60
}
63
- if let Some ( param) = generics. opt_type_param ( found, tcx) {
64
- let f_span = tcx. def_span ( param. def_id ) ;
65
- if !sp. contains ( f_span) {
66
- diag. span_label ( f_span, "found type parameter" ) ;
67
- }
61
+ let f_span = tcx. def_span ( generics. type_param ( found, tcx) . def_id ) ;
62
+ if !sp. contains ( f_span) {
63
+ diag. span_label ( f_span, "found type parameter" ) ;
68
64
}
69
65
diag. note (
70
66
"a type parameter was expected, but a different one was found; \
@@ -87,29 +83,22 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
87
83
| ( ty:: Alias ( ty:: Projection , proj) , ty:: Param ( p) )
88
84
if !tcx. is_impl_trait_in_trait ( proj. def_id ) =>
89
85
{
90
- let parent = tcx
91
- . generics_of ( body_owner_def_id)
92
- . opt_type_param ( p, tcx)
93
- . and_then ( |param| {
94
- let p_def_id = param. def_id ;
95
- let p_span = tcx. def_span ( p_def_id) ;
96
- let expected = match ( values. expected . kind ( ) , values. found . kind ( ) ) {
97
- ( ty:: Param ( _) , _) => "expected " ,
98
- ( _, ty:: Param ( _) ) => "found " ,
99
- _ => "" ,
100
- } ;
101
- if !sp. contains ( p_span) {
102
- diag. span_label (
103
- p_span,
104
- format ! ( "{expected}this type parameter" ) ,
105
- ) ;
106
- }
107
- p_def_id. as_local ( ) . and_then ( |id| {
108
- let local_id = tcx. local_def_id_to_hir_id ( id) ;
109
- let generics = tcx. parent_hir_node ( local_id) . generics ( ) ?;
110
- Some ( ( id, generics) )
111
- } )
112
- } ) ;
86
+ let param = tcx. generics_of ( body_owner_def_id) . type_param ( p, tcx) ;
87
+ let p_def_id = param. def_id ;
88
+ let p_span = tcx. def_span ( p_def_id) ;
89
+ let expected = match ( values. expected . kind ( ) , values. found . kind ( ) ) {
90
+ ( ty:: Param ( _) , _) => "expected " ,
91
+ ( _, ty:: Param ( _) ) => "found " ,
92
+ _ => "" ,
93
+ } ;
94
+ if !sp. contains ( p_span) {
95
+ diag. span_label ( p_span, format ! ( "{expected}this type parameter" ) ) ;
96
+ }
97
+ let parent = p_def_id. as_local ( ) . and_then ( |id| {
98
+ let local_id = tcx. local_def_id_to_hir_id ( id) ;
99
+ let generics = tcx. parent_hir_node ( local_id) . generics ( ) ?;
100
+ Some ( ( id, generics) )
101
+ } ) ;
113
102
let mut note = true ;
114
103
if let Some ( ( local_id, generics) ) = parent {
115
104
// Synthesize the associated type restriction `Add<Output = Expected>`.
@@ -183,16 +172,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
183
172
( ty:: Param ( p) , ty:: Dynamic ( ..) | ty:: Alias ( ty:: Opaque , ..) )
184
173
| ( ty:: Dynamic ( ..) | ty:: Alias ( ty:: Opaque , ..) , ty:: Param ( p) ) => {
185
174
let generics = tcx. generics_of ( body_owner_def_id) ;
186
- if let Some ( param) = generics. opt_type_param ( p, tcx) {
187
- let p_span = tcx. def_span ( param. def_id ) ;
188
- let expected = match ( values. expected . kind ( ) , values. found . kind ( ) ) {
189
- ( ty:: Param ( _) , _) => "expected " ,
190
- ( _, ty:: Param ( _) ) => "found " ,
191
- _ => "" ,
192
- } ;
193
- if !sp. contains ( p_span) {
194
- diag. span_label ( p_span, format ! ( "{expected}this type parameter" ) ) ;
195
- }
175
+ let p_span = tcx. def_span ( generics. type_param ( p, tcx) . def_id ) ;
176
+ let expected = match ( values. expected . kind ( ) , values. found . kind ( ) ) {
177
+ ( ty:: Param ( _) , _) => "expected " ,
178
+ ( _, ty:: Param ( _) ) => "found " ,
179
+ _ => "" ,
180
+ } ;
181
+ if !sp. contains ( p_span) {
182
+ diag. span_label ( p_span, format ! ( "{expected}this type parameter" ) ) ;
196
183
}
197
184
diag. help ( "type parameters must be constrained to match other types" ) ;
198
185
if tcx. sess . teach ( diag. code . unwrap ( ) ) {
@@ -233,11 +220,9 @@ impl<T> Trait<T> for X {
233
220
ty:: Closure ( ..) | ty:: CoroutineClosure ( ..) | ty:: Coroutine ( ..) ,
234
221
) => {
235
222
let generics = tcx. generics_of ( body_owner_def_id) ;
236
- if let Some ( param) = generics. opt_type_param ( p, tcx) {
237
- let p_span = tcx. def_span ( param. def_id ) ;
238
- if !sp. contains ( p_span) {
239
- diag. span_label ( p_span, "expected this type parameter" ) ;
240
- }
223
+ let p_span = tcx. def_span ( generics. type_param ( p, tcx) . def_id ) ;
224
+ if !sp. contains ( p_span) {
225
+ diag. span_label ( p_span, "expected this type parameter" ) ;
241
226
}
242
227
diag. help ( format ! (
243
228
"every closure has a distinct type and so could not always match the \
@@ -246,16 +231,14 @@ impl<T> Trait<T> for X {
246
231
}
247
232
( ty:: Param ( p) , _) | ( _, ty:: Param ( p) ) => {
248
233
let generics = tcx. generics_of ( body_owner_def_id) ;
249
- if let Some ( param) = generics. opt_type_param ( p, tcx) {
250
- let p_span = tcx. def_span ( param. def_id ) ;
251
- let expected = match ( values. expected . kind ( ) , values. found . kind ( ) ) {
252
- ( ty:: Param ( _) , _) => "expected " ,
253
- ( _, ty:: Param ( _) ) => "found " ,
254
- _ => "" ,
255
- } ;
256
- if !sp. contains ( p_span) {
257
- diag. span_label ( p_span, format ! ( "{expected}this type parameter" ) ) ;
258
- }
234
+ let p_span = tcx. def_span ( generics. type_param ( p, tcx) . def_id ) ;
235
+ let expected = match ( values. expected . kind ( ) , values. found . kind ( ) ) {
236
+ ( ty:: Param ( _) , _) => "expected " ,
237
+ ( _, ty:: Param ( _) ) => "found " ,
238
+ _ => "" ,
239
+ } ;
240
+ if !sp. contains ( p_span) {
241
+ diag. span_label ( p_span, format ! ( "{expected}this type parameter" ) ) ;
259
242
}
260
243
}
261
244
( ty:: Alias ( ty:: Projection | ty:: Inherent , proj_ty) , _)
@@ -545,10 +528,8 @@ impl<T> Trait<T> for X {
545
528
return false ;
546
529
} ;
547
530
let generics = tcx. generics_of ( body_owner_def_id) ;
548
- let Some ( param) = generics. opt_type_param ( param_ty, tcx) else {
549
- return false ;
550
- } ;
551
- let Some ( def_id) = param. def_id . as_local ( ) else {
531
+ let def_id = generics. type_param ( param_ty, tcx) . def_id ;
532
+ let Some ( def_id) = def_id. as_local ( ) else {
552
533
return false ;
553
534
} ;
554
535
0 commit comments