Suggest _ for missing generic arguments in turbofish#122651
Suggest _ for missing generic arguments in turbofish#122651bors merged 2 commits intorust-lang:masterfrom
_ for missing generic arguments in turbofish#122651Conversation
|
r? @spastorino rustbot has assigned @spastorino. Use r? to explicitly pick a reviewer |
| 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(_), .. }, ..) | ||
| ) | ||
| }); |
There was a problem hiding this comment.
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;
}
});
There was a problem hiding this comment.
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.
|
Seems good to me but r? @compiler-errors as they've already made the initial review. |
|
@bors r=spastorino,compiler-errors |
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#114009 (compiler: allow transmute of ZST arrays with generics) - rust-lang#122195 (Note that the caller chooses a type for type param) - rust-lang#122651 (Suggest `_` for missing generic arguments in turbofish) - rust-lang#122784 (Add `tag_for_variant` query) - rust-lang#122839 (Split out `PredicatePolarity` from `ImplPolarity`) - rust-lang#122873 (Merge my contributor emails into one using mailmap) - rust-lang#122885 (Adjust better spastorino membership to triagebot's adhoc_groups) - rust-lang#122888 (add a couple more tests) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#114009 (compiler: allow transmute of ZST arrays with generics) - rust-lang#122195 (Note that the caller chooses a type for type param) - rust-lang#122651 (Suggest `_` for missing generic arguments in turbofish) - rust-lang#122784 (Add `tag_for_variant` query) - rust-lang#122839 (Split out `PredicatePolarity` from `ImplPolarity`) - rust-lang#122873 (Merge my contributor emails into one using mailmap) - rust-lang#122885 (Adjust better spastorino membership to triagebot's adhoc_groups) - rust-lang#122888 (add a couple more tests) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#122651 - kornelski:flat-turbofish, r=spastorino,compiler-errors Suggest `_` for missing generic arguments in turbofish The compiler may suggest unusable generic type names for missing generic arguments in an expression context: ```rust fn main() { (0..1).collect::<Vec>() } ``` > help: add missing generic argument > > (0..1).collect::<Vec<T>>() but `T` is not a valid name in this context, and this suggestion won't compile. I've changed it to use `_` inside method calls (turbofish), so it will suggest `(0..1).collect::<Vec<_>>()` which _may_ compile. It's possible that the suggested `_` will be ambiguous, but there is very extensive E0283 that will help resolve that, which is more helpful than a basic "cannot find type `T` in this scope" users would get otherwise. Out of caution to limit scope of the change I've limited it to just turbofish, but I suspect `_` could be the better choice in more cases. Perhaps in all expressions?
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#114009 (compiler: allow transmute of ZST arrays with generics) - rust-lang#122195 (Note that the caller chooses a type for type param) - rust-lang#122651 (Suggest `_` for missing generic arguments in turbofish) - rust-lang#122784 (Add `tag_for_variant` query) - rust-lang#122839 (Split out `PredicatePolarity` from `ImplPolarity`) - rust-lang#122873 (Merge my contributor emails into one using mailmap) - rust-lang#122885 (Adjust better spastorino membership to triagebot's adhoc_groups) - rust-lang#122888 (add a couple more tests) r? `@ghost` `@rustbot` modify labels: rollup
The compiler may suggest unusable generic type names for missing generic arguments in an expression context:
but
Tis not a valid name in this context, and this suggestion won't compile.I've changed it to use
_inside method calls (turbofish), so it will suggest(0..1).collect::<Vec<_>>()which may compile.It's possible that the suggested
_will be ambiguous, but there is very extensive E0283 that will help resolve that, which is more helpful than a basic "cannot find typeTin this scope" users would get otherwise.Out of caution to limit scope of the change I've limited it to just turbofish, but I suspect
_could be the better choice in more cases. Perhaps in all expressions?