@@ -12,7 +12,7 @@ use crate::middle::resolve_lifetime as rl;
1212use crate :: require_c_abi_if_c_variadic;
1313use crate :: util:: common:: ErrorReported ;
1414use rustc:: lint:: builtin:: AMBIGUOUS_ASSOCIATED_ITEMS ;
15- use rustc:: session:: parse:: feature_err;
15+ use rustc:: session:: { parse:: feature_err, Session } ;
1616use rustc:: ty:: subst:: { self , InternalSubsts , Subst , SubstsRef } ;
1717use rustc:: ty:: { self , Const , DefIdTree , ToPredicate , Ty , TyCtxt , TypeFoldable , WithConstness } ;
1818use rustc:: ty:: { GenericParamDef , GenericParamDefKind } ;
@@ -446,6 +446,20 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
446446 ( arg_count_mismatch, unexpected_spans)
447447 }
448448
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+
449463 /// Creates the relevant generic argument substitutions
450464 /// corresponding to a set of generic parameters. This is a
451465 /// rather complex function. Let us try to explain the role
@@ -541,12 +555,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
541555 let mut args =
542556 generic_args. iter ( ) . flat_map ( |generic_args| generic_args. args . iter ( ) ) . peekable ( ) ;
543557
544- let arg_kind = |arg| match arg {
545- & GenericArg :: Lifetime ( _) => "lifetime" ,
546- & GenericArg :: Type ( _) => "type" ,
547- & GenericArg :: Const ( _) => "constant" ,
548- } ;
549-
550558 // If we encounter a type or const when we expect a lifetime, we infer the lifetimes.
551559 // If we later encounter a lifetime, we know that the arguments were provided in the
552560 // 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 {
582590 // the arguments don't match up with the parameters, we won't issue
583591 // an additional error, as the user already knows what's wrong.
584592 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 ( ) ) ;
599594 }
600595
601596 // We've reported the error, but we want to make sure that this
@@ -607,6 +602,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
607602 }
608603 }
609604 }
605+
610606 ( Some ( & arg) , None ) => {
611607 // We should never be able to reach this point with well-formed input.
612608 // There are two situations in which we can encounter this issue.
@@ -620,29 +616,23 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
620616 // after a type or const). We want to throw an error in this case.
621617
622618 if !arg_count_mismatch {
623- let kind = arg_kind ( arg) ;
619+ let kind = arg. descr ( ) ;
624620 assert_eq ! ( kind, "lifetime" ) ;
625621 let provided =
626622 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) ;
636624 }
637625
638626 break ;
639627 }
628+
640629 ( None , Some ( & param) ) => {
641630 // If there are fewer arguments than parameters, it means
642631 // we're inferring the remaining arguments.
643632 substs. push ( inferred_kind ( Some ( & substs) , param, infer_args) ) ;
644633 params. next ( ) ;
645634 }
635+
646636 ( None , None ) => break ,
647637 }
648638 }
0 commit comments