@@ -158,11 +158,6 @@ impl UnderspecifiedArgKind {
158
158
159
159
struct ClosureEraser < ' a , ' tcx > {
160
160
infcx : & ' a InferCtxt < ' tcx > ,
161
- // When recursing into types, if an ADT has type parameters with a default type we do *not*
162
- // want to replace that type parameter with `_`, as it will cause the normally hidden type
163
- // parameter to be rendered. The best example of this is `Vec<T, Alloc>`, which we want to
164
- // render as `Vec<T>` and not `Vec<T, _>` when `T` is unknown.
165
- do_not_hide_nested_type : bool ,
166
161
}
167
162
168
163
impl < ' a , ' tcx > ClosureEraser < ' a , ' tcx > {
@@ -177,8 +172,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ClosureEraser<'a, 'tcx> {
177
172
}
178
173
179
174
fn fold_ty ( & mut self , ty : Ty < ' tcx > ) -> Ty < ' tcx > {
180
- let prev = self . do_not_hide_nested_type ;
181
- let ty = match ty. kind ( ) {
175
+ match ty. kind ( ) {
182
176
ty:: Closure ( _, args) => {
183
177
// For a closure type, we turn it into a function pointer so that it gets rendered
184
178
// as `fn(args) -> Ret`.
@@ -188,52 +182,64 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ClosureEraser<'a, 'tcx> {
188
182
self . cx ( ) . signature_unclosure ( closure_sig, hir:: Safety :: Safe ) ,
189
183
)
190
184
}
191
- ty:: Adt ( def, _ ) => {
185
+ ty:: Adt ( def, args ) => {
192
186
let generics = self . cx ( ) . generics_of ( def. did ( ) ) ;
193
- if generics. own_params . iter ( ) . any ( |param| param. default_value ( self . cx ( ) ) . is_some ( ) )
194
- {
195
- // We have a type that has default types, like the allocator in Vec. We decided
196
- // to show `Vec` itself, because it hasn't yet been replaced by an `_` `Infer`,
197
- // but we want to ensure that the type parameter with default types does *not*
198
- // get replaced with `_` because then we'd end up with `Vec<_, _>`, instead of
199
- // `Vec<_>`.
200
- self . do_not_hide_nested_type = true ;
201
- ty. super_fold_with ( self )
202
- } else if ty. has_infer ( ) || self . do_not_hide_nested_type {
203
- // This type has an unsubstituted type variable, meaning that this type has a
204
- // (potentially deeply nested) type parameter from the corresponding type's
205
- // definition. We have explicitly asked this type to not be hidden. In either
206
- // case, we keep the type and don't substitute with `_` just yet.
207
- ty. super_fold_with ( self )
208
- } else {
209
- // When we have a type that doesn't have any inference variables, so we replace
187
+ let generics: Vec < bool > = generics
188
+ . own_params
189
+ . iter ( )
190
+ . map ( |param| param. default_value ( self . cx ( ) ) . is_some ( ) )
191
+ . collect ( ) ;
192
+ let mut all_infer = true ;
193
+ let ty = Ty :: new_adt (
194
+ self . cx ( ) ,
195
+ * def,
196
+ self . cx ( ) . mk_args_from_iter ( generics. into_iter ( ) . zip ( args. iter ( ) ) . map (
197
+ |( has_default, arg) | {
198
+ if arg. has_infer ( ) {
199
+ // This param has an unsubstituted type variable, meaning that this
200
+ // type has a (potentially deeply nested) type parameter from the
201
+ // corresponding type's definition. We have explicitly asked this
202
+ // type to not be hidden. In either case, we keep the type and don't
203
+ // substitute with `_` just yet.
204
+ all_infer = false ;
205
+ arg. fold_with ( self )
206
+ } else if has_default {
207
+ // We have a type param that has a default type, like the allocator
208
+ // in Vec. We decided to show `Vec` itself, because it hasn't yet
209
+ // been replaced by an `_` `Infer`, but we want to ensure that the
210
+ // type parameter with default types does *not* get replaced with
211
+ // `_` because then we'd end up with `Vec<_, _>`, instead of
212
+ // `Vec<_>`.
213
+ arg
214
+ } else {
215
+ self . new_infer ( ) . into ( )
216
+ }
217
+ } ,
218
+ ) ) ,
219
+ ) ;
220
+ if all_infer {
221
+ // We have a type that doesn't have any inference variables, so we replace
210
222
// the whole thing with `_`. The type system already knows about this type in
211
223
// its entirety and it is redundant to specify it for the user. The user only
212
224
// needs to specify the type parameters that we *couldn't* figure out.
213
225
self . new_infer ( )
226
+ } else {
227
+ ty
214
228
}
215
229
}
216
- _ if ty. has_infer ( ) || self . do_not_hide_nested_type => {
230
+ _ if ty. has_infer ( ) => {
217
231
// This type has a (potentially nested) type parameter that we couldn't figure out.
218
232
// We will print this depth of type, so at least the type name and at least one of
219
- // its type parameters. We unset `do_not_hide_nested_type` because this type can't
220
- // have type parameter defaults until next type we hit an ADT.
221
- self . do_not_hide_nested_type = false ;
233
+ // its type parameters.
222
234
ty. super_fold_with ( self )
223
235
}
224
236
// We don't have an unknown type parameter anywhere, replace with `_`.
225
237
_ => self . new_infer ( ) ,
226
- } ;
227
- self . do_not_hide_nested_type = prev;
228
- ty
238
+ }
229
239
}
230
240
231
241
fn fold_const ( & mut self , c : ty:: Const < ' tcx > ) -> ty:: Const < ' tcx > {
232
- let prev = self . do_not_hide_nested_type ;
233
242
// Avoid accidentally erasing the type of the const.
234
- self . do_not_hide_nested_type = true ;
235
- let c = c. super_fold_with ( self ) ;
236
- self . do_not_hide_nested_type = prev;
237
243
c
238
244
}
239
245
}
@@ -281,7 +287,7 @@ fn ty_to_string<'tcx>(
281
287
let ty = infcx. resolve_vars_if_possible ( ty) ;
282
288
// We use `fn` ptr syntax for closures, but this only works when the closure does not capture
283
289
// anything. We also remove all type parameters that are fully known to the type system.
284
- let ty = ty. fold_with ( & mut ClosureEraser { infcx, do_not_hide_nested_type : false } ) ;
290
+ let ty = ty. fold_with ( & mut ClosureEraser { infcx } ) ;
285
291
286
292
match ( ty. kind ( ) , called_method_def_id) {
287
293
// We don't want the regular output for `fn`s because it includes its path in
0 commit comments