Skip to content
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

Suggest _ for missing generic arguments in turbofish #122651

Merged
merged 2 commits into from
Mar 23, 2024
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
Expand Up @@ -435,6 +435,22 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
&self,
num_params_to_take: usize,
) -> String {
let is_in_a_method_call = self
.tcx
.hir()
.parent_iter(self.path_segment.hir_id)
.skip(1)
.find_map(|(_, node)| match node {
hir::Node::Expr(expr) => Some(expr),
_ => None,
})
.is_some_and(|expr| {
matches!(
expr.kind,
hir::ExprKind::MethodCall(hir::PathSegment { args: Some(_), .. }, ..)
)
});
Comment on lines +438 to +452
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this going to be true when we're inside a nested item? You should probably limit this search somehow so it doesn't trigger for like

fn hello() {
  x.foo({
    fn bar() {
        let x: Vec;
    }
  });

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find_map picks only the first expression, so it stops at the block around fn. I don't think it's possible to have an item directly as an argument.

I've considered checking whether MethodCall's args contain self.path_segment, but that requires digging a few layers deep, and the code for it is pretty involved.

BTW, let x: Vec<T> is not a great suggestion, and let x: Vec<_> would have been better. I'm wondering whether it should always use _ on non-items inside function bodies, and limit suggestions of parameter names to items where _ is not allowed.


let fn_sig = self.tcx.hir().get_if_local(self.def_id).and_then(hir::Node::fn_sig);
let is_used_in_input = |def_id| {
fn_sig.is_some_and(|fn_sig| {
Expand All @@ -453,14 +469,17 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
.skip(self.params_offset + self.num_provided_type_or_const_args())
.take(num_params_to_take)
.map(|param| match param.kind {
// This is being inferred from the item's inputs, no need to set it.
ty::GenericParamDefKind::Type { .. } if is_used_in_input(param.def_id) => {
"_".to_string()
// If it's in method call (turbofish), it might be inferred from the expression (e.g. `.collect::<Vec<_>>()`)
// If it is being inferred from the item's inputs, no need to set it.
ty::GenericParamDefKind::Type { .. }
if is_in_a_method_call || is_used_in_input(param.def_id) =>
{
"_"
}
_ => param.name.to_string(),
_ => param.name.as_str(),
})
.collect::<Vec<_>>()
.join(", ")
.intersperse(", ")
.collect()
}

fn get_unbound_associated_types(&self) -> Vec<String> {
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/generics/generic-type-less-params-with-defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@ struct Heap;
struct Vec<T, A = Heap>(
marker::PhantomData<(T,A)>);

struct HashMap<K, V, S = ()>(marker::PhantomData<(K,V,S)>);

fn main() {
let _: Vec;
//~^ ERROR missing generics for struct `Vec`
//~| SUGGESTION <T>

let _x = (1..10).collect::<HashMap>();
//~^ ERROR missing generics for struct `HashMap`
//~| SUGGESTION <_, _>

().extend::<[(); 0]>({
fn not_the_extend() {
let _: Vec;
//~^ ERROR missing generics for struct `Vec`
//~| SUGGESTION <T>
}
[]
});
}
36 changes: 34 additions & 2 deletions tests/ui/generics/generic-type-less-params-with-defaults.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0107]: missing generics for struct `Vec`
--> $DIR/generic-type-less-params-with-defaults.rs:9:12
--> $DIR/generic-type-less-params-with-defaults.rs:11:12
|
LL | let _: Vec;
| ^^^ expected at least 1 generic argument
Expand All @@ -14,6 +14,38 @@ help: add missing generic argument
LL | let _: Vec<T>;
| +++

error: aborting due to 1 previous error
error[E0107]: missing generics for struct `HashMap`
--> $DIR/generic-type-less-params-with-defaults.rs:15:32
|
LL | let _x = (1..10).collect::<HashMap>();
| ^^^^^^^ expected at least 2 generic arguments
|
note: struct defined here, with at least 2 generic parameters: `K`, `V`
--> $DIR/generic-type-less-params-with-defaults.rs:8:8
|
LL | struct HashMap<K, V, S = ()>(marker::PhantomData<(K,V,S)>);
| ^^^^^^^ - -
help: add missing generic arguments
|
LL | let _x = (1..10).collect::<HashMap<_, _>>();
| ++++++

error[E0107]: missing generics for struct `Vec`
--> $DIR/generic-type-less-params-with-defaults.rs:21:20
|
LL | let _: Vec;
| ^^^ expected at least 1 generic argument
|
note: struct defined here, with at least 1 generic parameter: `T`
--> $DIR/generic-type-less-params-with-defaults.rs:5:8
|
LL | struct Vec<T, A = Heap>(
| ^^^ -
help: add missing generic argument
|
LL | let _: Vec<T>;
| +++

error: aborting due to 3 previous errors

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