@@ -215,12 +215,11 @@ pub(crate) enum GenericArgPosition {
215
215
216
216
/// A marker denoting that the generic arguments that were
217
217
/// provided did not match the respective generic parameters.
218
- #[ derive( Clone , Default , Debug ) ]
218
+ #[ derive( Clone , Debug ) ]
219
219
pub 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 > ,
224
223
}
225
224
226
225
/// Decorates the result of a generic argument count mismatch
@@ -240,13 +239,14 @@ pub trait GenericArgsLowerer<'a, 'tcx> {
240
239
241
240
fn provided_kind (
242
241
& mut self ,
242
+ preceding_args : & [ ty:: GenericArg < ' tcx > ] ,
243
243
param : & ty:: GenericParamDef ,
244
244
arg : & GenericArg < ' tcx > ,
245
245
) -> ty:: GenericArg < ' tcx > ;
246
246
247
247
fn inferred_kind (
248
248
& mut self ,
249
- args : Option < & [ ty:: GenericArg < ' tcx > ] > ,
249
+ preceding_args : & [ ty:: GenericArg < ' tcx > ] ,
250
250
param : & ty:: GenericParamDef ,
251
251
infer_args : bool ,
252
252
) -> ty:: GenericArg < ' tcx > ;
@@ -404,10 +404,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
404
404
self_ty. is_some ( ) ,
405
405
) ;
406
406
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 ) ;
411
409
}
412
410
413
411
// Skip processing if type has no generic parameters.
@@ -425,6 +423,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
425
423
span : Span ,
426
424
inferred_params : Vec < Span > ,
427
425
infer_args : bool ,
426
+ incorrect_args : & ' a Result < ( ) , GenericArgCountMismatch > ,
428
427
}
429
428
430
429
impl < ' a , ' tcx > GenericArgsLowerer < ' a , ' tcx > for GenericArgsCtxt < ' a , ' tcx > {
@@ -439,11 +438,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
439
438
440
439
fn provided_kind (
441
440
& mut self ,
441
+ preceding_args : & [ ty:: GenericArg < ' tcx > ] ,
442
442
param : & ty:: GenericParamDef ,
443
443
arg : & GenericArg < ' tcx > ,
444
444
) -> ty:: GenericArg < ' tcx > {
445
445
let tcx = self . lowerer . tcx ( ) ;
446
446
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
+
447
453
let mut handle_ty_args = |has_default, ty : & hir:: Ty < ' tcx > | {
448
454
if has_default {
449
455
tcx. check_optional_stability (
@@ -506,11 +512,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
506
512
507
513
fn inferred_kind (
508
514
& mut self ,
509
- args : Option < & [ ty:: GenericArg < ' tcx > ] > ,
515
+ preceding_args : & [ ty:: GenericArg < ' tcx > ] ,
510
516
param : & ty:: GenericParamDef ,
511
517
infer_args : bool ,
512
518
) -> ty:: GenericArg < ' tcx > {
513
519
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
+ }
514
526
match param. kind {
515
527
GenericParamDefKind :: Lifetime => self
516
528
. lowerer
@@ -529,15 +541,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
529
541
GenericParamDefKind :: Type { has_default, .. } => {
530
542
if !infer_args && has_default {
531
543
// 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
+ {
537
550
// 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 ( ) ;
539
552
}
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 ( )
541
557
} else if infer_args {
542
558
self . lowerer . ty_infer ( Some ( param) , self . span ) . into ( )
543
559
} else {
@@ -557,7 +573,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
557
573
// FIXME(effects) see if we should special case effect params here
558
574
if !infer_args && has_default {
559
575
tcx. const_param_default ( param. def_id )
560
- . instantiate ( tcx, args . unwrap ( ) )
576
+ . instantiate ( tcx, preceding_args )
561
577
. into ( )
562
578
} else {
563
579
if infer_args {
@@ -571,6 +587,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
571
587
}
572
588
}
573
589
}
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
+ }
574
601
575
602
let mut args_ctx = GenericArgsCtxt {
576
603
lowerer : self ,
@@ -579,19 +606,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
579
606
generic_args : segment. args ( ) ,
580
607
inferred_params : vec ! [ ] ,
581
608
infer_args : segment. infer_args ,
609
+ incorrect_args : & arg_count. correct ,
582
610
} ;
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
- }
595
611
let args = lower_generic_args (
596
612
tcx,
597
613
def_id,
0 commit comments