@@ -20,7 +20,6 @@ mod lint;
2020mod object_safety;
2121
2222use crate :: bounds:: Bounds ;
23- use crate :: collect:: HirPlaceholderCollector ;
2423use crate :: errors:: { AmbiguousLifetimeBound , WildPatTy } ;
2524use crate :: hir_ty_lowering:: errors:: { prohibit_assoc_item_binding, GenericsArgsErrExtend } ;
2625use crate :: hir_ty_lowering:: generics:: { check_generic_arg_count, lower_generic_args} ;
@@ -34,7 +33,6 @@ use rustc_errors::{
3433use rustc_hir as hir;
3534use rustc_hir:: def:: { CtorOf , DefKind , Namespace , Res } ;
3635use rustc_hir:: def_id:: { DefId , LocalDefId } ;
37- use rustc_hir:: intravisit:: { walk_generics, Visitor as _} ;
3836use rustc_hir:: { GenericArg , GenericArgs , HirId } ;
3937use rustc_infer:: infer:: InferCtxt ;
4038use rustc_infer:: traits:: ObligationCause ;
@@ -157,6 +155,14 @@ pub trait HirTyLowerer<'tcx> {
157155 poly_trait_ref : ty:: PolyTraitRef < ' tcx > ,
158156 ) -> Ty < ' tcx > ;
159157
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+
160166 /// Returns `AdtDef` if `ty` is an ADT.
161167 ///
162168 /// Note that `ty` might be a alias type that needs normalization.
@@ -2270,92 +2276,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
22702276 let bound_vars = tcx. late_bound_vars ( hir_id) ;
22712277 debug ! ( ?bound_vars) ;
22722278
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) ;
23232280
23242281 debug ! ( ?output_ty) ;
23252282
23262283 let fn_ty = tcx. mk_fn_sig ( input_tys, output_ty, decl. c_variadic , safety, abi) ;
23272284 let bare_fn_ty = ty:: Binder :: bind_with_vars ( fn_ty, bound_vars) ;
23282285
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-
23592286 // Find any late-bound regions declared in return type that do
23602287 // not appear in the arguments. These are not well-formed.
23612288 //
@@ -2385,7 +2312,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23852312 /// corresponding function in the trait that the impl implements, if it exists.
23862313 /// If arg_idx is Some, then it corresponds to an input type index, otherwise it
23872314 /// 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 (
23892316 & self ,
23902317 fn_hir_id : HirId ,
23912318 arg_idx : Option < usize > ,
0 commit comments