@@ -20,7 +20,6 @@ mod lint;
20
20
mod object_safety;
21
21
22
22
use crate :: bounds:: Bounds ;
23
- use crate :: collect:: HirPlaceholderCollector ;
24
23
use crate :: errors:: { AmbiguousLifetimeBound , WildPatTy } ;
25
24
use crate :: hir_ty_lowering:: errors:: { prohibit_assoc_item_binding, GenericsArgsErrExtend } ;
26
25
use crate :: hir_ty_lowering:: generics:: { check_generic_arg_count, lower_generic_args} ;
@@ -34,7 +33,6 @@ use rustc_errors::{
34
33
use rustc_hir as hir;
35
34
use rustc_hir:: def:: { CtorOf , DefKind , Namespace , Res } ;
36
35
use rustc_hir:: def_id:: { DefId , LocalDefId } ;
37
- use rustc_hir:: intravisit:: { walk_generics, Visitor as _} ;
38
36
use rustc_hir:: { GenericArg , GenericArgs , HirId } ;
39
37
use rustc_infer:: infer:: InferCtxt ;
40
38
use rustc_infer:: traits:: ObligationCause ;
@@ -157,6 +155,14 @@ pub trait HirTyLowerer<'tcx> {
157
155
poly_trait_ref : ty:: PolyTraitRef < ' tcx > ,
158
156
) -> Ty < ' tcx > ;
159
157
158
+ fn lower_fn_sig (
159
+ & self ,
160
+ decl : & hir:: FnDecl < ' tcx > ,
161
+ generics : Option < & hir:: Generics < ' _ > > ,
162
+ hir_id : HirId ,
163
+ hir_ty : Option < & hir:: Ty < ' _ > > ,
164
+ ) -> ( Vec < Ty < ' tcx > > , Ty < ' tcx > ) ;
165
+
160
166
/// Returns `AdtDef` if `ty` is an ADT.
161
167
///
162
168
/// Note that `ty` might be a alias type that needs normalization.
@@ -2270,92 +2276,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2270
2276
let bound_vars = tcx. late_bound_vars ( hir_id) ;
2271
2277
debug ! ( ?bound_vars) ;
2272
2278
2273
- // We proactively collect all the inferred type params to emit a single error per fn def.
2274
- let mut visitor = HirPlaceholderCollector :: default ( ) ;
2275
- let mut infer_replacements = vec ! [ ] ;
2276
-
2277
- if let Some ( generics) = generics {
2278
- walk_generics ( & mut visitor, generics) ;
2279
- }
2280
-
2281
- let input_tys: Vec < _ > = decl
2282
- . inputs
2283
- . iter ( )
2284
- . enumerate ( )
2285
- . map ( |( i, a) | {
2286
- if let hir:: TyKind :: Infer = a. kind
2287
- && !self . allow_infer ( )
2288
- {
2289
- if let Some ( suggested_ty) =
2290
- self . suggest_trait_fn_ty_for_impl_fn_infer ( hir_id, Some ( i) )
2291
- {
2292
- infer_replacements. push ( ( a. span , suggested_ty. to_string ( ) ) ) ;
2293
- return Ty :: new_error_with_message (
2294
- self . tcx ( ) ,
2295
- a. span ,
2296
- suggested_ty. to_string ( ) ,
2297
- ) ;
2298
- }
2299
- }
2300
-
2301
- // Only visit the type looking for `_` if we didn't fix the type above
2302
- visitor. visit_ty ( a) ;
2303
- self . lower_arg_ty ( a, None )
2304
- } )
2305
- . collect ( ) ;
2306
-
2307
- let output_ty = match decl. output {
2308
- hir:: FnRetTy :: Return ( output) => {
2309
- if let hir:: TyKind :: Infer = output. kind
2310
- && !self . allow_infer ( )
2311
- && let Some ( suggested_ty) =
2312
- self . suggest_trait_fn_ty_for_impl_fn_infer ( hir_id, None )
2313
- {
2314
- infer_replacements. push ( ( output. span , suggested_ty. to_string ( ) ) ) ;
2315
- Ty :: new_error_with_message ( self . tcx ( ) , output. span , suggested_ty. to_string ( ) )
2316
- } else {
2317
- visitor. visit_ty ( output) ;
2318
- self . lower_ty ( output)
2319
- }
2320
- }
2321
- hir:: FnRetTy :: DefaultReturn ( ..) => tcx. types . unit ,
2322
- } ;
2279
+ let ( input_tys, output_ty) = self . lower_fn_sig ( decl, generics, hir_id, hir_ty) ;
2323
2280
2324
2281
debug ! ( ?output_ty) ;
2325
2282
2326
2283
let fn_ty = tcx. mk_fn_sig ( input_tys, output_ty, decl. c_variadic , safety, abi) ;
2327
2284
let bare_fn_ty = ty:: Binder :: bind_with_vars ( fn_ty, bound_vars) ;
2328
2285
2329
- if !self . allow_infer ( ) && !( visitor. 0 . is_empty ( ) && infer_replacements. is_empty ( ) ) {
2330
- // We always collect the spans for placeholder types when evaluating `fn`s, but we
2331
- // only want to emit an error complaining about them if infer types (`_`) are not
2332
- // allowed. `allow_infer` gates this behavior. We check for the presence of
2333
- // `ident_span` to not emit an error twice when we have `fn foo(_: fn() -> _)`.
2334
-
2335
- let mut diag = crate :: collect:: placeholder_type_error_diag (
2336
- tcx,
2337
- generics,
2338
- visitor. 0 ,
2339
- infer_replacements. iter ( ) . map ( |( s, _) | * s) . collect ( ) ,
2340
- true ,
2341
- hir_ty,
2342
- "function" ,
2343
- ) ;
2344
-
2345
- if !infer_replacements. is_empty ( ) {
2346
- diag. multipart_suggestion (
2347
- format ! (
2348
- "try replacing `_` with the type{} in the corresponding trait method signature" ,
2349
- rustc_errors:: pluralize!( infer_replacements. len( ) ) ,
2350
- ) ,
2351
- infer_replacements,
2352
- Applicability :: MachineApplicable ,
2353
- ) ;
2354
- }
2355
-
2356
- self . set_tainted_by_errors ( diag. emit ( ) ) ;
2357
- }
2358
-
2359
2286
// Find any late-bound regions declared in return type that do
2360
2287
// not appear in the arguments. These are not well-formed.
2361
2288
//
@@ -2385,7 +2312,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2385
2312
/// corresponding function in the trait that the impl implements, if it exists.
2386
2313
/// If arg_idx is Some, then it corresponds to an input type index, otherwise it
2387
2314
/// corresponds to the return type.
2388
- fn suggest_trait_fn_ty_for_impl_fn_infer (
2315
+ pub ( super ) fn suggest_trait_fn_ty_for_impl_fn_infer (
2389
2316
& self ,
2390
2317
fn_hir_id : HirId ,
2391
2318
arg_idx : Option < usize > ,
0 commit comments