Skip to content

Commit 1c99841

Browse files
committed
Standardize lifetime and type parameter count mismatch errors
They now always say how many lifetime / type parameters were expected and are explicit about stating "lifetime" or "type" instead of just "parameter".
1 parent 79d32e9 commit 1c99841

File tree

6 files changed

+42
-30
lines changed

6 files changed

+42
-30
lines changed

src/librustc_typeck/check/mod.rs

+24-19
Original file line numberDiff line numberDiff line change
@@ -4510,28 +4510,32 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
45104510
}
45114511
};
45124512

4513-
let count = |n| {
4514-
format!("{} parameter{}", n, if n == 1 { "" } else { "s" })
4513+
let count_lifetime_params = |n| {
4514+
format!("{} lifetime parameter{}", n, if n == 1 { "" } else { "s" })
4515+
};
4516+
let count_type_params = |n| {
4517+
format!("{} type parameter{}", n, if n == 1 { "" } else { "s" })
45154518
};
45164519

45174520
// Check provided lifetime parameters.
45184521
let lifetime_defs = segment.map_or(&[][..], |(_, generics)| &generics.regions);
45194522
if lifetimes.len() > lifetime_defs.len() {
4523+
let expected_text = count_lifetime_params(lifetime_defs.len());
4524+
let actual_text = count_lifetime_params(lifetimes.len());
45204525
struct_span_err!(self.tcx.sess, span, E0088,
45214526
"too many lifetime parameters provided: \
4522-
expected {}, found {}",
4523-
count(lifetime_defs.len()),
4524-
count(lifetimes.len()))
4525-
.span_label(span, &format!("unexpected lifetime parameter{}",
4526-
match lifetimes.len() { 1 => "", _ => "s" }))
4527+
expected at most {}, found {}",
4528+
expected_text, actual_text)
4529+
.span_label(span, &format!("expected {}", expected_text))
45274530
.emit();
45284531
} else if lifetimes.len() > 0 && lifetimes.len() < lifetime_defs.len() {
4532+
let expected_text = count_lifetime_params(lifetime_defs.len());
4533+
let actual_text = count_lifetime_params(lifetimes.len());
45294534
struct_span_err!(self.tcx.sess, span, E0090,
45304535
"too few lifetime parameters provided: \
4531-
expected {}, found {}",
4532-
count(lifetime_defs.len()),
4533-
count(lifetimes.len()))
4534-
.span_label(span, &format!("too few lifetime parameters"))
4536+
expected {}, found {}",
4537+
expected_text, actual_text)
4538+
.span_label(span, &format!("expected {}", expected_text))
45354539
.emit();
45364540
}
45374541

@@ -4552,26 +4556,27 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
45524556
.count();
45534557
if types.len() > type_defs.len() {
45544558
let span = types[type_defs.len()].span;
4559+
let expected_text = count_type_params(type_defs.len());
4560+
let actual_text = count_type_params(types.len());
45554561
struct_span_err!(self.tcx.sess, span, E0087,
45564562
"too many type parameters provided: \
45574563
expected at most {}, found {}",
4558-
count(type_defs.len()),
4559-
count(types.len()))
4560-
.span_label(span, &format!("too many type parameters")).emit();
4564+
expected_text, actual_text)
4565+
.span_label(span, &format!("expected {}", expected_text))
4566+
.emit();
45614567

45624568
// To prevent derived errors to accumulate due to extra
45634569
// type parameters, we force instantiate_value_path to
45644570
// use inference variables instead of the provided types.
45654571
*segment = None;
45664572
} else if !infer_types && types.len() < required_len {
4567-
let adjust = |len| if len > 1 { "parameters" } else { "parameter" };
4568-
let required_param_str = adjust(required_len);
4573+
let expected_text = count_type_params(required_len);
4574+
let actual_text = count_type_params(types.len());
45694575
struct_span_err!(self.tcx.sess, span, E0089,
45704576
"too few type parameters provided: \
45714577
expected {}, found {}",
4572-
count(required_len),
4573-
count(types.len()))
4574-
.span_label(span, &format!("expected {} type {}", required_len, required_param_str))
4578+
expected_text, actual_text)
4579+
.span_label(span, &format!("expected {}", expected_text))
45754580
.emit();
45764581
}
45774582

src/test/compile-fail/E0087.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
fn foo<T>() {}
11+
fn foo() {}
12+
fn bar<T>() {}
1213

1314
fn main() {
14-
foo::<f64, bool>(); //~ ERROR E0087
15-
//~^ NOTE too many type parameters
15+
foo::<f64>(); //~ ERROR expected at most 0 type parameters, found 1 type parameter [E0087]
16+
//~^ NOTE expected 0 type parameters
17+
18+
bar::<f64, u64>(); //~ ERROR expected at most 1 type parameter, found 2 type parameters [E0087]
19+
//~^ NOTE expected 1 type parameter
1620
}

src/test/compile-fail/E0088.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ fn f() {}
1212
fn g<'a>() {}
1313

1414
fn main() {
15-
f::<'static>(); //~ ERROR E0088
16-
//~^ unexpected lifetime parameter
15+
f::<'static>();
16+
//~^ ERROR expected at most 0 lifetime parameters, found 1 lifetime parameter [E0088]
17+
//~| NOTE expected 0 lifetime parameters
1718

18-
g::<'static, 'static>(); //~ ERROR E0088
19-
//~^ unexpected lifetime parameters
19+
g::<'static, 'static>();
20+
//~^ ERROR expected at most 0 lifetime parameters, found 2 lifetime parameters [E0088]
21+
//~| NOTE expected 0 lifetime parameters
2022
}

src/test/compile-fail/E0089.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
fn foo<T, U>() {}
1212

1313
fn main() {
14-
foo::<f64>(); //~ ERROR expected 2 parameters, found 1 parameter [E0089]
14+
foo::<f64>(); //~ ERROR expected 2 type parameters, found 1 type parameter [E0089]
1515
//~| NOTE expected 2 type parameters
1616
}

src/test/compile-fail/E0090.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
// except according to those terms.
1010

1111
fn foo<'a: 'b, 'b: 'a>() {}
12+
1213
fn main() {
13-
foo::<'static>();//~ ERROR E0090
14-
//~^ too few lifetime parameters
14+
foo::<'static>(); //~ ERROR expected 2 lifetime parameters, found 1 lifetime parameter [E0090]
15+
//~^ NOTE expected 2 lifetime parameters
1516
}

src/test/compile-fail/ufcs-qpath-missing-params.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ impl<'a> IntoCow<'a, str> for String {
2222

2323
fn main() {
2424
<String as IntoCow>::into_cow("foo".to_string());
25-
//~^ ERROR too few type parameters provided: expected 1 parameter
25+
//~^ ERROR too few type parameters provided: expected 1 type parameter
2626
}

0 commit comments

Comments
 (0)