Skip to content

Commit 7ff11d7

Browse files
committed
Unify lifetime/type arguments error messages for (non-)builtin bounds
1 parent a194519 commit 7ff11d7

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ fn create_region_substs<'tcx>(
305305
rscope.anon_regions(span, expected_num_region_params);
306306

307307
if supplied_num_region_params != 0 || anon_regions.is_err() {
308-
span_err!(tcx.sess, span, E0107,
309-
"wrong number of lifetime parameters: expected {}, found {}",
310-
expected_num_region_params, supplied_num_region_params);
308+
report_lifetime_number_error(tcx, span,
309+
supplied_num_region_params,
310+
expected_num_region_params);
311311
}
312312

313313
match anon_regions {
@@ -355,31 +355,14 @@ fn create_substs_for_ast_path<'tcx>(
355355
.count();
356356

357357
let mut type_substs = types_provided;
358+
check_type_argument_count(this.tcx(), span, supplied_ty_param_count,
359+
required_ty_param_count, formal_ty_param_count);
360+
358361
if supplied_ty_param_count < required_ty_param_count {
359-
let expected = if required_ty_param_count < formal_ty_param_count {
360-
"expected at least"
361-
} else {
362-
"expected"
363-
};
364-
span_err!(this.tcx().sess, span, E0243,
365-
"wrong number of type arguments: {} {}, found {}",
366-
expected,
367-
required_ty_param_count,
368-
supplied_ty_param_count);
369362
while type_substs.len() < required_ty_param_count {
370363
type_substs.push(tcx.types.err);
371364
}
372365
} else if supplied_ty_param_count > formal_ty_param_count {
373-
let expected = if required_ty_param_count < formal_ty_param_count {
374-
"expected at most"
375-
} else {
376-
"expected"
377-
};
378-
span_err!(this.tcx().sess, span, E0244,
379-
"wrong number of type arguments: {} {}, found {}",
380-
expected,
381-
formal_ty_param_count,
382-
supplied_ty_param_count);
383366
type_substs.truncate(formal_ty_param_count);
384367
}
385368
assert!(type_substs.len() >= required_ty_param_count &&
@@ -1849,10 +1832,13 @@ pub fn partition_bounds<'a>(tcx: &ty::ctxt,
18491832
&mut builtin_bounds) {
18501833
let segments = &b.trait_ref.path.segments;
18511834
let parameters = &segments[segments.len() - 1].parameters;
1852-
if !parameters.is_empty() {
1853-
span_err!(tcx.sess, b.trait_ref.path.span, E0316,
1854-
"builtin bounds do not require arguments, {} given",
1855-
parameters.lifetimes().len() + parameters.types().len());
1835+
if parameters.types().len() > 0 {
1836+
check_type_argument_count(tcx, b.trait_ref.path.span,
1837+
parameters.types().len(), 0, 0);
1838+
}
1839+
if parameters.lifetimes().len() > 0{
1840+
report_lifetime_number_error(tcx, b.trait_ref.path.span,
1841+
parameters.lifetimes().len(), 0);
18561842
}
18571843
continue; // success
18581844
}
@@ -1886,3 +1872,34 @@ fn prohibit_projections<'tcx>(tcx: &ty::ctxt<'tcx>,
18861872
"associated type bindings are not allowed here");
18871873
}
18881874
}
1875+
1876+
fn check_type_argument_count(tcx: &ty::ctxt, span: Span, supplied: usize,
1877+
required: usize, accepted: usize) {
1878+
if supplied < required {
1879+
let expected = if required < accepted {
1880+
"expected at least"
1881+
} else {
1882+
"expected"
1883+
};
1884+
span_err!(tcx.sess, span, E0243,
1885+
"wrong number of type arguments: {} {}, found {}",
1886+
expected, required, supplied);
1887+
} else if supplied > accepted {
1888+
let expected = if required < accepted {
1889+
"expected at most"
1890+
} else {
1891+
"expected"
1892+
};
1893+
span_err!(tcx.sess, span, E0244,
1894+
"wrong number of type arguments: {} {}, found {}",
1895+
expected,
1896+
accepted,
1897+
supplied);
1898+
}
1899+
}
1900+
1901+
fn report_lifetime_number_error(tcx: &ty::ctxt, span: Span, number: usize, expected: usize) {
1902+
span_err!(tcx.sess, span, E0107,
1903+
"wrong number of lifetime parameters: expected {}, found {}",
1904+
expected, number);
1905+
}

src/librustc_typeck/diagnostics.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ register_diagnostics! {
172172
E0248, // found value name used as a type
173173
E0249, // expected constant expr for array length
174174
E0250, // expected constant expr for array length
175-
E0316 // wrong number of type arguments to a built-in trait
176175
}
177176

178177
__build_diagnostic_array! { DIAGNOSTICS }

src/test/compile-fail/typeck-builtin-bound-type-parameters.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
// except according to those terms.
1010

1111
fn foo1<T:Copy<U>, U>(x: T) {}
12-
//~^ ERROR: builtin bounds do not require arguments, 1 given
12+
//~^ ERROR: wrong number of type arguments: expected 0, found 1
1313

1414
trait Trait: Copy<Send> {}
15-
//~^ ERROR: builtin bounds do not require arguments, 1 given
15+
//~^ ERROR: wrong number of type arguments: expected 0, found 1
1616

1717
struct MyStruct1<T: Copy<T>>;
18-
//~^ ERROR builtin bounds do not require arguments, 1 given
18+
//~^ ERROR wrong number of type arguments: expected 0, found 1
1919

2020
struct MyStruct2<'a, T: Copy<'a>>;
21-
//~^ ERROR: builtin bounds do not require arguments, 1 given
21+
//~^ ERROR: wrong number of lifetime parameters: expected 0, found 1
2222

2323
fn foo2<'a, T:Copy<'a, U>, U>(x: T) {}
24-
//~^ ERROR builtin bounds do not require arguments, 2 given
24+
//~^ ERROR: wrong number of type arguments: expected 0, found 1
25+
//~^^ ERROR: wrong number of lifetime parameters: expected 0, found 1
2526

2627
fn main() {
2728
}

0 commit comments

Comments
 (0)