@@ -215,12 +215,11 @@ pub(crate) enum GenericArgPosition {
215215
216216/// A marker denoting that the generic arguments that were 
217217/// provided did not match the respective generic parameters. 
218- #[ derive( Clone ,  Default ,   Debug ) ]  
218+ #[ derive( Clone ,  Debug ) ]  
219219pub  struct  GenericArgCountMismatch  { 
220-     /// Indicates whether a fatal error was reported (`Some`), or just a lint (`None`). 
221-      pub  reported :  Option < ErrorGuaranteed > , 
222-     /// A list of spans of arguments provided that were not valid. 
223-      pub  invalid_args :  Vec < Span > , 
220+     pub  reported :  ErrorGuaranteed , 
221+     /// A list of indices of arguments provided that were not valid. 
222+      pub  invalid_args :  Vec < usize > , 
224223} 
225224
226225/// Decorates the result of a generic argument count mismatch 
@@ -240,13 +239,14 @@ pub trait GenericArgsLowerer<'a, 'tcx> {
240239
241240    fn  provided_kind ( 
242241        & mut  self , 
242+         preceding_args :  & [ ty:: GenericArg < ' tcx > ] , 
243243        param :  & ty:: GenericParamDef , 
244244        arg :  & GenericArg < ' tcx > , 
245245    )  -> ty:: GenericArg < ' tcx > ; 
246246
247247    fn  inferred_kind ( 
248248        & mut  self , 
249-         args :   Option < & [ ty:: GenericArg < ' tcx > ] > , 
249+         preceding_args :   & [ ty:: GenericArg < ' tcx > ] , 
250250        param :  & ty:: GenericParamDef , 
251251        infer_args :  bool , 
252252    )  -> ty:: GenericArg < ' tcx > ; 
@@ -404,10 +404,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
404404            self_ty. is_some ( ) , 
405405        ) ; 
406406
407-         if  let  Err ( err)  = & arg_count. correct 
408-             && let  Some ( reported)  = err. reported 
409-         { 
410-             self . set_tainted_by_errors ( reported) ; 
407+         if  let  Err ( err)  = & arg_count. correct  { 
408+             self . set_tainted_by_errors ( err. reported ) ; 
411409        } 
412410
413411        // Skip processing if type has no generic parameters. 
@@ -425,6 +423,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
425423            span :  Span , 
426424            inferred_params :  Vec < Span > , 
427425            infer_args :  bool , 
426+             incorrect_args :  & ' a  Result < ( ) ,  GenericArgCountMismatch > , 
428427        } 
429428
430429        impl < ' a ,  ' tcx >  GenericArgsLowerer < ' a ,  ' tcx >  for  GenericArgsCtxt < ' a ,  ' tcx >  { 
@@ -439,11 +438,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
439438
440439            fn  provided_kind ( 
441440                & mut  self , 
441+                 preceding_args :  & [ ty:: GenericArg < ' tcx > ] , 
442442                param :  & ty:: GenericParamDef , 
443443                arg :  & GenericArg < ' tcx > , 
444444            )  -> ty:: GenericArg < ' tcx >  { 
445445                let  tcx = self . lowerer . tcx ( ) ; 
446446
447+                 if  let  Err ( incorrect)  = self . incorrect_args  { 
448+                     if  incorrect. invalid_args . contains ( & ( param. index  as  usize ) )  { 
449+                         return  param. to_error ( tcx,  preceding_args) ; 
450+                     } 
451+                 } 
452+ 
447453                let  mut  handle_ty_args = |has_default,  ty :  & hir:: Ty < ' tcx > | { 
448454                    if  has_default { 
449455                        tcx. check_optional_stability ( 
@@ -506,11 +512,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
506512
507513            fn  inferred_kind ( 
508514                & mut  self , 
509-                 args :   Option < & [ ty:: GenericArg < ' tcx > ] > , 
515+                 preceding_args :   & [ ty:: GenericArg < ' tcx > ] , 
510516                param :  & ty:: GenericParamDef , 
511517                infer_args :  bool , 
512518            )  -> ty:: GenericArg < ' tcx >  { 
513519                let  tcx = self . lowerer . tcx ( ) ; 
520+ 
521+                 if  let  Err ( incorrect)  = self . incorrect_args  { 
522+                     if  incorrect. invalid_args . contains ( & ( param. index  as  usize ) )  { 
523+                         return  param. to_error ( tcx,  preceding_args) ; 
524+                     } 
525+                 } 
514526                match  param. kind  { 
515527                    GenericParamDefKind :: Lifetime  => self 
516528                        . lowerer 
@@ -529,15 +541,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
529541                    GenericParamDefKind :: Type  {  has_default,  .. }  => { 
530542                        if  !infer_args && has_default { 
531543                            // No type parameter provided, but a default exists. 
532-                             let  args = args. unwrap ( ) ; 
533-                             if  args. iter ( ) . any ( |arg| match  arg. unpack ( )  { 
534-                                 GenericArgKind :: Type ( ty)  => ty. references_error ( ) , 
535-                                 _ => false , 
536-                             } )  { 
544+                             if  let  Some ( prev)  =
545+                                 preceding_args. iter ( ) . find_map ( |arg| match  arg. unpack ( )  { 
546+                                     GenericArgKind :: Type ( ty)  => ty. error_reported ( ) . err ( ) , 
547+                                     _ => None , 
548+                                 } ) 
549+                             { 
537550                                // Avoid ICE #86756 when type error recovery goes awry. 
538-                                 return  Ty :: new_misc_error ( tcx) . into ( ) ; 
551+                                 return  Ty :: new_error ( tcx,  prev ) . into ( ) ; 
539552                            } 
540-                             tcx. at ( self . span ) . type_of ( param. def_id ) . instantiate ( tcx,  args) . into ( ) 
553+                             tcx. at ( self . span ) 
554+                                 . type_of ( param. def_id ) 
555+                                 . instantiate ( tcx,  preceding_args) 
556+                                 . into ( ) 
541557                        }  else  if  infer_args { 
542558                            self . lowerer . ty_infer ( Some ( param) ,  self . span ) . into ( ) 
543559                        }  else  { 
@@ -557,7 +573,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
557573                        // FIXME(effects) see if we should special case effect params here 
558574                        if  !infer_args && has_default { 
559575                            tcx. const_param_default ( param. def_id ) 
560-                                 . instantiate ( tcx,  args . unwrap ( ) ) 
576+                                 . instantiate ( tcx,  preceding_args ) 
561577                                . into ( ) 
562578                        }  else  { 
563579                            if  infer_args { 
@@ -571,6 +587,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
571587                } 
572588            } 
573589        } 
590+         if  let  ty:: BoundConstness :: Const  | ty:: BoundConstness :: ConstIfConst  = constness
591+             && generics. has_self 
592+             && !tcx. has_attr ( def_id,  sym:: const_trait) 
593+         { 
594+             let  reported = tcx. dcx ( ) . emit_err ( crate :: errors:: ConstBoundForNonConstTrait  { 
595+                 span, 
596+                 modifier :  constness. as_str ( ) , 
597+             } ) ; 
598+             self . set_tainted_by_errors ( reported) ; 
599+             arg_count. correct  = Err ( GenericArgCountMismatch  {  reported,  invalid_args :  vec ! [ ]  } ) ; 
600+         } 
574601
575602        let  mut  args_ctx = GenericArgsCtxt  { 
576603            lowerer :  self , 
@@ -579,19 +606,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
579606            generic_args :  segment. args ( ) , 
580607            inferred_params :  vec ! [ ] , 
581608            infer_args :  segment. infer_args , 
609+             incorrect_args :  & arg_count. correct , 
582610        } ; 
583-         if  let  ty:: BoundConstness :: Const  | ty:: BoundConstness :: ConstIfConst  = constness
584-             && generics. has_self 
585-             && !tcx. has_attr ( def_id,  sym:: const_trait) 
586-         { 
587-             let  e = tcx. dcx ( ) . emit_err ( crate :: errors:: ConstBoundForNonConstTrait  { 
588-                 span, 
589-                 modifier :  constness. as_str ( ) , 
590-             } ) ; 
591-             self . set_tainted_by_errors ( e) ; 
592-             arg_count. correct  =
593-                 Err ( GenericArgCountMismatch  {  reported :  Some ( e) ,  invalid_args :  vec ! [ ]  } ) ; 
594-         } 
595611        let  args = lower_generic_args ( 
596612            tcx, 
597613            def_id, 
0 commit comments