Skip to content

Commit 4737790

Browse files
committed
Rollup merge of #22680 - FlaPer87:type_builtin, r=nikomatsakis
Fixes #20302
2 parents 4337ddb + 7ff11d7 commit 4737790

File tree

2 files changed

+75
-24
lines changed

2 files changed

+75
-24
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 47 additions & 24 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 &&
@@ -1847,7 +1830,16 @@ pub fn partition_bounds<'a>(tcx: &ty::ctxt,
18471830
if ty::try_add_builtin_trait(tcx,
18481831
trait_did,
18491832
&mut builtin_bounds) {
1850-
// FIXME(#20302) -- we should check for things like Copy<T>
1833+
let segments = &b.trait_ref.path.segments;
1834+
let parameters = &segments[segments.len() - 1].parameters;
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);
1842+
}
18511843
continue; // success
18521844
}
18531845
}
@@ -1880,3 +1872,34 @@ fn prohibit_projections<'tcx>(tcx: &ty::ctxt<'tcx>,
18801872
"associated type bindings are not allowed here");
18811873
}
18821874
}
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn foo1<T:Copy<U>, U>(x: T) {}
12+
//~^ ERROR: wrong number of type arguments: expected 0, found 1
13+
14+
trait Trait: Copy<Send> {}
15+
//~^ ERROR: wrong number of type arguments: expected 0, found 1
16+
17+
struct MyStruct1<T: Copy<T>>;
18+
//~^ ERROR wrong number of type arguments: expected 0, found 1
19+
20+
struct MyStruct2<'a, T: Copy<'a>>;
21+
//~^ ERROR: wrong number of lifetime parameters: expected 0, found 1
22+
23+
fn foo2<'a, T:Copy<'a, U>, U>(x: T) {}
24+
//~^ ERROR: wrong number of type arguments: expected 0, found 1
25+
//~^^ ERROR: wrong number of lifetime parameters: expected 0, found 1
26+
27+
fn main() {
28+
}

0 commit comments

Comments
 (0)