@@ -12,7 +12,7 @@ use crate::middle::resolve_lifetime as rl;
12
12
use crate :: require_c_abi_if_c_variadic;
13
13
use crate :: util:: common:: ErrorReported ;
14
14
use rustc:: lint:: builtin:: AMBIGUOUS_ASSOCIATED_ITEMS ;
15
- use rustc:: session:: parse:: feature_err;
15
+ use rustc:: session:: { parse:: feature_err, Session } ;
16
16
use rustc:: ty:: subst:: { self , InternalSubsts , Subst , SubstsRef } ;
17
17
use rustc:: ty:: { self , Const , DefIdTree , ToPredicate , Ty , TyCtxt , TypeFoldable , WithConstness } ;
18
18
use rustc:: ty:: { GenericParamDef , GenericParamDefKind } ;
@@ -446,6 +446,20 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
446
446
( arg_count_mismatch, unexpected_spans)
447
447
}
448
448
449
+ /// Report an error that a generic argument did not match the generic parameter that was
450
+ /// expected.
451
+ fn generic_arg_mismatch_err ( sess : & Session , arg : & GenericArg < ' _ > , kind : & ' static str ) {
452
+ struct_span_err ! (
453
+ sess,
454
+ arg. span( ) ,
455
+ E0747 ,
456
+ "{} provided when a {} was expected" ,
457
+ arg. descr( ) ,
458
+ kind,
459
+ )
460
+ . emit ( ) ;
461
+ }
462
+
449
463
/// Creates the relevant generic argument substitutions
450
464
/// corresponding to a set of generic parameters. This is a
451
465
/// rather complex function. Let us try to explain the role
@@ -541,12 +555,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
541
555
let mut args =
542
556
generic_args. iter ( ) . flat_map ( |generic_args| generic_args. args . iter ( ) ) . peekable ( ) ;
543
557
544
- let arg_kind = |arg| match arg {
545
- & GenericArg :: Lifetime ( _) => "lifetime" ,
546
- & GenericArg :: Type ( _) => "type" ,
547
- & GenericArg :: Const ( _) => "constant" ,
548
- } ;
549
-
550
558
// If we encounter a type or const when we expect a lifetime, we infer the lifetimes.
551
559
// If we later encounter a lifetime, we know that the arguments were provided in the
552
560
// wrong order. `force_infer_lt` records the type or const that forced lifetimes to be
@@ -582,20 +590,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
582
590
// the arguments don't match up with the parameters, we won't issue
583
591
// an additional error, as the user already knows what's wrong.
584
592
if !arg_count_mismatch {
585
- let param_kind = match kind {
586
- GenericParamDefKind :: Lifetime => "lifetime" ,
587
- GenericParamDefKind :: Type { .. } => "type" ,
588
- GenericParamDefKind :: Const => "constant" ,
589
- } ;
590
- struct_span_err ! (
591
- tcx. sess,
592
- arg. span( ) ,
593
- E0747 ,
594
- "{} provided when a {} was expected" ,
595
- arg_kind( arg) ,
596
- param_kind,
597
- )
598
- . emit ( ) ;
593
+ Self :: generic_arg_mismatch_err ( tcx. sess , arg, kind. descr ( ) ) ;
599
594
}
600
595
601
596
// We've reported the error, but we want to make sure that this
@@ -607,6 +602,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
607
602
}
608
603
}
609
604
}
605
+
610
606
( Some ( & arg) , None ) => {
611
607
// We should never be able to reach this point with well-formed input.
612
608
// There are two situations in which we can encounter this issue.
@@ -620,29 +616,23 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
620
616
// after a type or const). We want to throw an error in this case.
621
617
622
618
if !arg_count_mismatch {
623
- let kind = arg_kind ( arg) ;
619
+ let kind = arg. descr ( ) ;
624
620
assert_eq ! ( kind, "lifetime" ) ;
625
621
let provided =
626
622
force_infer_lt. expect ( "lifetimes ought to have been inferred" ) ;
627
- struct_span_err ! (
628
- tcx. sess,
629
- provided. span( ) ,
630
- E0747 ,
631
- "{} provided when a {} was expected" ,
632
- arg_kind( provided) ,
633
- kind,
634
- )
635
- . emit ( ) ;
623
+ Self :: generic_arg_mismatch_err ( tcx. sess , provided, kind) ;
636
624
}
637
625
638
626
break ;
639
627
}
628
+
640
629
( None , Some ( & param) ) => {
641
630
// If there are fewer arguments than parameters, it means
642
631
// we're inferring the remaining arguments.
643
632
substs. push ( inferred_kind ( Some ( & substs) , param, infer_args) ) ;
644
633
params. next ( ) ;
645
634
}
635
+
646
636
( None , None ) => break ,
647
637
}
648
638
}
0 commit comments