@@ -498,6 +498,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
498498 None
499499 } ;
500500
501+ let mut delay_as_bug = false ;
501502 let mut infer_subdiags = Vec :: new ( ) ;
502503 let mut multi_suggestions = Vec :: new ( ) ;
503504 match kind {
@@ -513,7 +514,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
513514 type_name : ty_to_string ( self , ty, def_id) ,
514515 } ) ;
515516 }
516- InferSourceKind :: ClosureArg { insert_span, ty, .. } => {
517+ InferSourceKind :: ClosureArg { insert_span, ty, kind, .. } => {
518+ if let PatKind :: Err ( _) = kind {
519+ // We will have already emitted an error about this pattern.
520+ delay_as_bug = true ;
521+ }
517522 infer_subdiags. push ( SourceKindSubdiag :: LetLike {
518523 span : insert_span,
519524 name : String :: new ( ) ,
@@ -532,7 +537,17 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
532537 def_id : _,
533538 generic_args,
534539 have_turbofish,
540+ hir_id,
535541 } => {
542+ if let hir:: Node :: PathSegment ( segment) = self . tcx . hir_node ( hir_id)
543+ && let Some ( args) = segment. args
544+ && let Some ( hir:: GenericArg :: Type ( ty) ) = args. args . get ( argument_index)
545+ && let hir:: TyKind :: Path ( hir:: QPath :: Resolved ( _, path) ) = ty. kind
546+ && let Res :: Err = path. res
547+ {
548+ // We have already emitted a name resolution error.
549+ delay_as_bug = true ;
550+ }
536551 let generics = self . tcx . generics_of ( generics_def_id) ;
537552 let is_type = term. as_type ( ) . is_some ( ) ;
538553
@@ -653,8 +668,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
653668 } ) ,
654669 } ;
655670 * err. long_ty_path ( ) = long_ty_path;
656- if let InferSourceKind :: ClosureArg { kind : PatKind :: Err ( _ ) , .. } = kind {
657- // We will have already emitted an error about this pattern .
671+ if silence {
672+ // We have already emitted an earlier more relevant error .
658673 err. downgrade_to_delayed_bug ( ) ;
659674 }
660675 err
@@ -687,6 +702,7 @@ enum InferSourceKind<'tcx> {
687702 def_id : DefId ,
688703 generic_args : & ' tcx [ GenericArg < ' tcx > ] ,
689704 have_turbofish : bool ,
705+ hir_id : HirId ,
690706 } ,
691707 FullyQualifiedMethodCall {
692708 receiver : & ' tcx Expr < ' tcx > ,
@@ -751,6 +767,7 @@ struct InsertableGenericArgs<'tcx> {
751767 generics_def_id : DefId ,
752768 def_id : DefId ,
753769 have_turbofish : bool ,
770+ hir_id : HirId ,
754771}
755772
756773/// A visitor which searches for the "best" spot to use in the inference error.
@@ -1013,6 +1030,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
10131030 generics_def_id : def_id,
10141031 def_id,
10151032 have_turbofish : false ,
1033+ hir_id : expr. hir_id ,
10161034 }
10171035 } ;
10181036 return Box :: new ( insertable. into_iter ( ) ) ;
@@ -1052,6 +1070,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
10521070 generics_def_id,
10531071 def_id : path. res . def_id ( ) ,
10541072 have_turbofish,
1073+ hir_id : path. segments . last ( ) . unwrap ( ) . hir_id ,
10551074 }
10561075 } ;
10571076
@@ -1072,6 +1091,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
10721091 generics_def_id,
10731092 def_id : res. def_id ( ) ,
10741093 have_turbofish,
1094+ hir_id : segment. hir_id ,
10751095 } )
10761096 } )
10771097 . chain ( last_segment_using_path_data)
@@ -1106,6 +1126,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
11061126 generics_def_id : def_id,
11071127 def_id,
11081128 have_turbofish : false ,
1129+ hir_id : segment. hir_id ,
11091130 }
11101131 } ;
11111132
@@ -1233,6 +1254,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
12331254 generics_def_id,
12341255 def_id,
12351256 have_turbofish,
1257+ hir_id,
12361258 } = args;
12371259 let generics = tcx. generics_of ( generics_def_id) ;
12381260 if let Some ( mut argument_index) = generics
@@ -1260,6 +1282,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
12601282 def_id,
12611283 generic_args,
12621284 have_turbofish,
1285+ hir_id,
12631286 } ,
12641287 } ) ;
12651288 }
0 commit comments