Skip to content

Commit affea73

Browse files
committed
Suggest _ in turbofish if param will be inferred from fn argument
1 parent 15d9ba0 commit affea73

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::structured_errors::StructuredDiagnostic;
22
use rustc_errors::{pluralize, Applicability, DiagnosticBuilder, DiagnosticId};
33
use rustc_hir as hir;
4+
use rustc_middle::hir::map::fn_sig;
45
use rustc_middle::middle::resolve_lifetime::LifetimeScopeForPath;
56
use rustc_middle::ty::{self as ty, TyCtxt};
67
use rustc_session::Session;
@@ -292,12 +293,30 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
292293
&self,
293294
num_params_to_take: usize,
294295
) -> String {
296+
let fn_sig = self.tcx.hir().get_if_local(self.def_id).and_then(|node| fn_sig(node));
297+
let is_used_in_input = |def_id| {
298+
fn_sig.map_or(false, |fn_sig| {
299+
fn_sig.decl.inputs.iter().any(|ty| match ty.kind {
300+
hir::TyKind::Path(hir::QPath::Resolved(
301+
None,
302+
hir::Path { res: hir::def::Res::Def(_, id), .. },
303+
)) if *id == def_id => true,
304+
_ => false,
305+
})
306+
})
307+
};
295308
self.gen_params
296309
.params
297310
.iter()
298311
.skip(self.params_offset + self.num_provided_type_or_const_args())
299312
.take(num_params_to_take)
300-
.map(|param| param.name.to_string())
313+
.map(|param| match param.kind {
314+
// This is being infered from the item's inputs, no need to set it.
315+
ty::GenericParamDefKind::Type { .. } if is_used_in_input(param.def_id) => {
316+
"_".to_string()
317+
}
318+
_ => param.name.to_string(),
319+
})
301320
.collect::<Vec<_>>()
302321
.join(", ")
303322
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-rustfix
2+
3+
fn two_type_params<A, B>(_: B) {}
4+
5+
fn main() {
6+
two_type_params::<String, _>(100); //~ ERROR this function takes 2 generic arguments
7+
two_type_params::<String, _>(100);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-rustfix
2+
3+
fn two_type_params<A, B>(_: B) {}
4+
5+
fn main() {
6+
two_type_params::<String>(100); //~ ERROR this function takes 2 generic arguments
7+
two_type_params::<String, _>(100);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0107]: this function takes 2 generic arguments but 1 generic argument was supplied
2+
--> $DIR/missing-type-param-used-in-param.rs:6:5
3+
|
4+
LL | two_type_params::<String>(100);
5+
| ^^^^^^^^^^^^^^^ ------ supplied 1 generic argument
6+
| |
7+
| expected 2 generic arguments
8+
|
9+
note: function defined here, with 2 generic parameters: `A`, `B`
10+
--> $DIR/missing-type-param-used-in-param.rs:3:4
11+
|
12+
LL | fn two_type_params<A, B>(_: B) {}
13+
| ^^^^^^^^^^^^^^^ - -
14+
help: add missing generic argument
15+
|
16+
LL | two_type_params::<String, _>(100);
17+
| +++
18+
19+
error: aborting due to previous error
20+
21+
For more information about this error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)