Skip to content

Suggest _ in turbofish if param will be inferred from fn argument #89148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::structured_errors::StructuredDiagnostic;
use rustc_errors::{pluralize, Applicability, DiagnosticBuilder, DiagnosticId};
use rustc_hir as hir;
use rustc_middle::hir::map::fn_sig;
use rustc_middle::middle::resolve_lifetime::LifetimeScopeForPath;
use rustc_middle::ty::{self as ty, TyCtxt};
use rustc_session::Session;
Expand Down Expand Up @@ -292,12 +293,30 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
&self,
num_params_to_take: usize,
) -> String {
let fn_sig = self.tcx.hir().get_if_local(self.def_id).and_then(|node| fn_sig(node));
let is_used_in_input = |def_id| {
fn_sig.map_or(false, |fn_sig| {
fn_sig.decl.inputs.iter().any(|ty| match ty.kind {
hir::TyKind::Path(hir::QPath::Resolved(
None,
hir::Path { res: hir::def::Res::Def(_, id), .. },
)) if *id == def_id => true,
_ => false,
})
})
};
self.gen_params
.params
.iter()
.skip(self.params_offset + self.num_provided_type_or_const_args())
.take(num_params_to_take)
.map(|param| param.name.to_string())
.map(|param| match param.kind {
// This is being infered from the item's inputs, no need to set it.
ty::GenericParamDefKind::Type { .. } if is_used_in_input(param.def_id) => {
"_".to_string()
}
_ => param.name.to_string(),
})
.collect::<Vec<_>>()
.join(", ")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run-rustfix

fn two_type_params<A, B>(_: B) {}

fn main() {
two_type_params::<String, _>(100); //~ ERROR this function takes 2 generic arguments
two_type_params::<String, _>(100);
}
8 changes: 8 additions & 0 deletions src/test/ui/suggestions/missing-type-param-used-in-param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run-rustfix

fn two_type_params<A, B>(_: B) {}

fn main() {
two_type_params::<String>(100); //~ ERROR this function takes 2 generic arguments
two_type_params::<String, _>(100);
}
21 changes: 21 additions & 0 deletions src/test/ui/suggestions/missing-type-param-used-in-param.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0107]: this function takes 2 generic arguments but 1 generic argument was supplied
--> $DIR/missing-type-param-used-in-param.rs:6:5
|
LL | two_type_params::<String>(100);
| ^^^^^^^^^^^^^^^ ------ supplied 1 generic argument
| |
| expected 2 generic arguments
|
note: function defined here, with 2 generic parameters: `A`, `B`
--> $DIR/missing-type-param-used-in-param.rs:3:4
|
LL | fn two_type_params<A, B>(_: B) {}
| ^^^^^^^^^^^^^^^ - -
help: add missing generic argument
|
LL | two_type_params::<String, _>(100);
| +++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0107`.