Skip to content

Commit c9b7b1f

Browse files
committed
Refactor create_substs_for_generic_args a little
1 parent 039045c commit c9b7b1f

File tree

5 files changed

+44
-40
lines changed

5 files changed

+44
-40
lines changed

src/librustc/ty/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,16 @@ pub enum GenericParamDefKind {
931931
Const,
932932
}
933933

934+
impl GenericParamDefKind {
935+
pub fn descr(&self) -> &'static str {
936+
match self {
937+
GenericParamDefKind::Lifetime => "lifetime",
938+
GenericParamDefKind::Type { .. } => "type",
939+
GenericParamDefKind::Const => "constant",
940+
}
941+
}
942+
}
943+
934944
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
935945
pub struct GenericParamDef {
936946
pub name: Symbol,

src/librustc/ty/structural_impls.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ use std::sync::Arc;
1919

2020
impl fmt::Debug for ty::GenericParamDef {
2121
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22-
let type_name = match self.kind {
23-
ty::GenericParamDefKind::Lifetime => "Lifetime",
24-
ty::GenericParamDefKind::Type { .. } => "Type",
25-
ty::GenericParamDefKind::Const => "Const",
26-
};
27-
write!(f, "{}({}, {:?}, {})", type_name, self.name, self.def_id, self.index)
22+
write!(f, "{}({}, {:?}, {})", self.kind.descr(), self.name, self.def_id, self.index)
2823
}
2924
}
3025

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
Generic arguments must be provided in the same order as the corresponding generic
2-
parameters are declared.
1+
Generic arguments must be provided in the same order as the corresponding
2+
generic parameters are declared.
33

44
Erroneous code example:
55

66
```compile_fail,E0747
77
struct S<'a, T>(&'a T);
88
9-
type X = S<(), 'static>; // error: the type argument is provided before the lifetime argument
9+
type X = S<(), 'static>; // error: the type argument is provided before the
10+
// lifetime argument
1011
```

src/librustc_hir/hir.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,14 @@ impl GenericArg<'_> {
298298
_ => false,
299299
}
300300
}
301+
302+
pub fn descr(&self) -> &'static str {
303+
match self {
304+
GenericArg::Lifetime(_) => "lifetime",
305+
GenericArg::Type(_) => "type",
306+
GenericArg::Const(_) => "constant",
307+
}
308+
}
301309
}
302310

303311
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]

src/librustc_typeck/astconv.rs

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::middle::resolve_lifetime as rl;
1212
use crate::require_c_abi_if_c_variadic;
1313
use crate::util::common::ErrorReported;
1414
use rustc::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
15-
use rustc::session::parse::feature_err;
15+
use rustc::session::{parse::feature_err, Session};
1616
use rustc::ty::subst::{self, InternalSubsts, Subst, SubstsRef};
1717
use rustc::ty::{self, Const, DefIdTree, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
1818
use 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

Comments
 (0)