Skip to content

Commit 03c4628

Browse files
committed
Avoid clone and update documentation
1 parent 04d33bb commit 03c4628

File tree

4 files changed

+97
-80
lines changed

4 files changed

+97
-80
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_data_structures::array_vec::ArrayVec;
1717
use hir::{self, GenericArg, GenericArgs};
1818
use hir::def::Def;
1919
use hir::def_id::DefId;
20+
use hir::HirVec;
2021
use middle::resolve_lifetime as rl;
2122
use namespace::Namespace;
2223
use rustc::ty::subst::{Kind, Subst, Substs};
@@ -34,6 +35,7 @@ use lint;
3435

3536
use std::iter;
3637
use syntax::ast;
38+
use syntax::ptr::P;
3739
use syntax::feature_gate::{GateIssue, emit_feature_err};
3840
use syntax_pos::{Span, MultiSpan};
3941

@@ -90,9 +92,9 @@ struct ConvertedBinding<'tcx> {
9092
span: Span,
9193
}
9294

93-
pub struct GenericArgMismatchErrorCode {
94-
pub lifetimes: (&'static str, &'static str),
95-
pub types: (&'static str, &'static str),
95+
struct GenericArgMismatchErrorCode {
96+
lifetimes: (&'static str, &'static str),
97+
types: (&'static str, &'static str),
9698
}
9799

98100
/// Dummy type used for the `Self` of a `TraitRef` created for converting
@@ -193,9 +195,72 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
193195
substs
194196
}
195197

198+
/// Report error if there is an explicit type parameter when using `impl Trait`.
199+
fn check_impl_trait(
200+
tcx: TyCtxt,
201+
span: Span,
202+
seg: &hir::PathSegment,
203+
generics: &ty::Generics,
204+
) -> bool {
205+
let explicit = !seg.infer_types;
206+
let impl_trait = generics.params.iter().any(|param| match param.kind {
207+
ty::GenericParamDefKind::Type {
208+
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait), ..
209+
} => true,
210+
_ => false,
211+
});
212+
213+
if explicit && impl_trait {
214+
let mut err = struct_span_err! {
215+
tcx.sess,
216+
span,
217+
E0632,
218+
"cannot provide explicit type parameters when `impl Trait` is \
219+
used in argument position."
220+
};
221+
222+
err.emit();
223+
}
224+
225+
impl_trait
226+
}
227+
228+
/// Check that the correct number of generic arguments have been provided.
229+
/// Used specifically for function calls.
230+
pub fn check_generic_arg_count_for_call(
231+
tcx: TyCtxt,
232+
span: Span,
233+
def: &ty::Generics,
234+
seg: &hir::PathSegment,
235+
is_method_call: bool,
236+
) -> bool {
237+
let empty_args = P(hir::GenericArgs {
238+
args: HirVec::new(), bindings: HirVec::new(), parenthesized: false,
239+
});
240+
let suppress_mismatch = Self::check_impl_trait(tcx, span, seg, &def);
241+
Self::check_generic_arg_count(
242+
tcx,
243+
span,
244+
def,
245+
if let Some(ref args) = seg.args {
246+
args
247+
} else {
248+
&empty_args
249+
},
250+
false, // `is_declaration`
251+
is_method_call,
252+
def.parent.is_none() && def.has_self, // `has_self`
253+
seg.infer_types || suppress_mismatch, // `infer_types`
254+
GenericArgMismatchErrorCode {
255+
lifetimes: ("E0090", "E0088"),
256+
types: ("E0089", "E0087"),
257+
},
258+
)
259+
}
260+
196261
/// Check that the correct number of generic arguments have been provided.
197262
/// This is used both for type declarations and function calls.
198-
pub fn check_generic_arg_count(
263+
fn check_generic_arg_count(
199264
tcx: TyCtxt,
200265
span: Span,
201266
def: &ty::Generics,

src/librustc_typeck/check/method/confirm.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010

1111
use super::{probe, MethodCallee};
1212

13-
use astconv::{AstConv, GenericArgMismatchErrorCode};
13+
use astconv::AstConv;
1414
use check::{FnCtxt, PlaceOp, callee, Needs};
1515
use hir::GenericArg;
1616
use hir::def_id::DefId;
17-
use hir::HirVec;
1817
use rustc::ty::subst::Substs;
1918
use rustc::traits;
2019
use rustc::ty::{self, Ty, GenericParamDefKind};
@@ -25,7 +24,6 @@ use rustc::ty::fold::TypeFoldable;
2524
use rustc::infer::{self, InferOk};
2625
use rustc::hir;
2726
use syntax_pos::Span;
28-
use syntax::ptr::P;
2927

3028
use std::ops::Deref;
3129

@@ -310,34 +308,24 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
310308
fn instantiate_method_substs(
311309
&mut self,
312310
pick: &probe::Pick<'tcx>,
313-
segment: &hir::PathSegment,
311+
seg: &hir::PathSegment,
314312
parent_substs: &Substs<'tcx>,
315313
) -> &'tcx Substs<'tcx> {
316314
// Determine the values for the generic parameters of the method.
317315
// If they were not explicitly supplied, just construct fresh
318316
// variables.
319-
let method_generics = self.tcx.generics_of(pick.item.def_id);
320-
let suppress_mismatch = self.fcx.check_impl_trait(self.span, segment, &method_generics);
321-
AstConv::check_generic_arg_count(
317+
let generics = self.tcx.generics_of(pick.item.def_id);
318+
AstConv::check_generic_arg_count_for_call(
322319
self.tcx,
323320
self.span,
324-
&method_generics,
325-
&segment.args.clone().unwrap_or_else(|| P(hir::GenericArgs {
326-
args: HirVec::new(), bindings: HirVec::new(), parenthesized: false,
327-
})),
328-
false, // `is_declaration`
321+
&generics,
322+
&seg,
329323
true, // `is_method_call`
330-
method_generics.parent.is_none() && method_generics.has_self,
331-
segment.infer_types || suppress_mismatch,
332-
GenericArgMismatchErrorCode {
333-
lifetimes: ("E0090", "E0088"),
334-
types: ("E0089", "E0087"),
335-
},
336324
);
337325

338326
// Create subst for early-bound lifetime parameters, combining
339327
// parameters from the type and those from the method.
340-
assert_eq!(method_generics.parent_count, parent_substs.len());
328+
assert_eq!(generics.parent_count, parent_substs.len());
341329

342330
AstConv::create_substs_for_generic_args(
343331
self.tcx,
@@ -348,7 +336,7 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
348336
// Provide the generic args, and whether types should be inferred.
349337
|_| {
350338
// The last argument of the returned tuple here is unimportant.
351-
if let Some(ref data) = segment.args {
339+
if let Some(ref data) = seg.args {
352340
(Some(data), false)
353341
} else {
354342
(None, false)

src/librustc_typeck/check/mod.rs

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,9 @@ pub use self::compare_method::{compare_impl_method, compare_const_impl};
8484
use self::method::MethodCallee;
8585
use self::TupleArgumentsFlag::*;
8686

87-
use astconv::{AstConv, GenericArgMismatchErrorCode};
87+
use astconv::AstConv;
8888
use hir::GenericArg;
8989
use hir::def::Def;
90-
use hir::HirVec;
9190
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
9291
use std::slice;
9392
use namespace::Namespace;
@@ -4945,22 +4944,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
49454944
// `impl Trait` is treated as a normal generic parameter internally,
49464945
// but we don't allow users to specify the parameter's value
49474946
// explicitly, so we have to do some error-checking here.
4948-
let suppress_mismatch = self.check_impl_trait(span, seg, &generics);
4949-
suppress_errors.insert(index, AstConv::check_generic_arg_count(
4947+
suppress_errors.insert(index, AstConv::check_generic_arg_count_for_call(
49504948
self.tcx,
49514949
span,
49524950
&generics,
4953-
&seg.args.clone().unwrap_or_else(|| P(hir::GenericArgs {
4954-
args: HirVec::new(), bindings: HirVec::new(), parenthesized: false,
4955-
})),
4956-
false, // `is_declaration`
4951+
&seg,
49574952
false, // `is_method_call`
4958-
generics.parent.is_none() && generics.has_self,
4959-
seg.infer_types || suppress_mismatch,
4960-
GenericArgMismatchErrorCode {
4961-
lifetimes: ("E0090", "E0088"), // FIXME: E0090 and E0088 should be unified.
4962-
types: ("E0089", "E0087"), // FIXME: E0089 and E0087 should be unified.
4963-
},
49644953
));
49654954
}
49664955

@@ -5112,35 +5101,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
51125101
directly, not through a function pointer");
51135102
}
51145103

5115-
/// Report error if there is an explicit type parameter when using `impl Trait`.
5116-
fn check_impl_trait(&self,
5117-
span: Span,
5118-
seg: &hir::PathSegment,
5119-
generics: &ty::Generics)
5120-
-> bool {
5121-
let explicit = !seg.infer_types;
5122-
let impl_trait = generics.params.iter().any(|param| match param.kind {
5123-
ty::GenericParamDefKind::Type {
5124-
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait), ..
5125-
} => true,
5126-
_ => false,
5127-
});
5128-
5129-
if explicit && impl_trait {
5130-
let mut err = struct_span_err! {
5131-
self.tcx.sess,
5132-
span,
5133-
E0632,
5134-
"cannot provide explicit type parameters when `impl Trait` is \
5135-
used in argument position."
5136-
};
5137-
5138-
err.emit();
5139-
}
5140-
5141-
impl_trait
5142-
}
5143-
51445104
// Resolves `typ` by a single level if `typ` is a type variable.
51455105
// If no resolution is possible, then an error is reported.
51465106
// Numeric inference variables may be left unresolved.

src/librustc_typeck/diagnostics.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,32 +1041,34 @@ enum NightsWatch {}
10411041
"##,
10421042

10431043
E0087: r##"
1044-
Too many type parameters were supplied for a function. For example:
1044+
Too many type arguments were supplied for a function. For example:
10451045
10461046
```compile_fail,E0087
10471047
fn foo<T>() {}
10481048
10491049
fn main() {
1050-
foo::<f64, bool>(); // error, expected 1 parameter, found 2 parameters
1050+
foo::<f64, bool>(); // error: wrong number of type arguments:
1051+
// expected 1, found 2
10511052
}
10521053
```
10531054
1054-
The number of supplied parameters must exactly match the number of defined type
1055+
The number of supplied arguments must exactly match the number of defined type
10551056
parameters.
10561057
"##,
10571058

10581059
E0088: r##"
1059-
You gave too many lifetime parameters. Erroneous code example:
1060+
You gave too many lifetime arguments. Erroneous code example:
10601061
10611062
```compile_fail,E0088
10621063
fn f() {}
10631064
10641065
fn main() {
1065-
f::<'static>() // error: too many lifetime parameters provided
1066+
f::<'static>() // error: wrong number of lifetime arguments:
1067+
// expected 0, found 1
10661068
}
10671069
```
10681070
1069-
Please check you give the right number of lifetime parameters. Example:
1071+
Please check you give the right number of lifetime arguments. Example:
10701072
10711073
```
10721074
fn f() {}
@@ -1101,42 +1103,44 @@ fn main() {
11011103
"##,
11021104

11031105
E0089: r##"
1104-
Not enough type parameters were supplied for a function. For example:
1106+
Too few type arguments were supplied for a function. For example:
11051107
11061108
```compile_fail,E0089
11071109
fn foo<T, U>() {}
11081110
11091111
fn main() {
1110-
foo::<f64>(); // error, expected 2 parameters, found 1 parameter
1112+
foo::<f64>(); // error: wrong number of type arguments: expected 2, found 1
11111113
}
11121114
```
11131115
1114-
Note that if a function takes multiple type parameters but you want the compiler
1116+
Note that if a function takes multiple type arguments but you want the compiler
11151117
to infer some of them, you can use type placeholders:
11161118
11171119
```compile_fail,E0089
11181120
fn foo<T, U>(x: T) {}
11191121
11201122
fn main() {
11211123
let x: bool = true;
1122-
foo::<f64>(x); // error, expected 2 parameters, found 1 parameter
1124+
foo::<f64>(x); // error: wrong number of type arguments:
1125+
// expected 2, found 1
11231126
foo::<_, f64>(x); // same as `foo::<bool, f64>(x)`
11241127
}
11251128
```
11261129
"##,
11271130

11281131
E0090: r##"
1129-
You gave too few lifetime parameters. Example:
1132+
You gave too few lifetime arguments. Example:
11301133
11311134
```compile_fail,E0090
11321135
fn foo<'a: 'b, 'b: 'a>() {}
11331136
11341137
fn main() {
1135-
foo::<'static>(); // error, expected 2 lifetime parameters
1138+
foo::<'static>(); // error: wrong number of lifetime arguments:
1139+
// expected 2, found 1
11361140
}
11371141
```
11381142
1139-
Please check you give the right number of lifetime parameters. Example:
1143+
Please check you give the right number of lifetime arguments. Example:
11401144
11411145
```
11421146
fn foo<'a: 'b, 'b: 'a>() {}

0 commit comments

Comments
 (0)