This code: (Playground link)
fn main() {
let tiles = Default::default();
for row in &mut tiles {
for tile in row {
*tile = 0;
}
}
let tiles: [[usize; 3]; 3] = tiles;
}
Fails with this error:
error[E0282]: type annotations needed
--> src/main.rs:5:13
|
4 | for tile in row {
| ---- consider giving `__next` a type
5 | *tile = 0;
| ^^^^^ cannot infer type for `_`
Type inference (rightfully) fails on this code. However, the variable that it suggests fixing is called __next
and does not actually exist in my code. It is probably some internal variable generated by the compiler.
Well, declining to name the variable in this case would be simple enough—
diff --git a/src/librustc/infer/error_reporting/need_type_info.rs b/src/librustc/infer/error_reporting/need_type_info.rs
index 7352c14..25bfb3e 100644
--- a/src/librustc/infer/error_reporting/need_type_info.rs
+++ b/src/librustc/infer/error_reporting/need_type_info.rs
@@ -131,7 +131,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
labels.clear();
labels.push((pattern.span, format!("consider giving this closure parameter a type")));
} else if let Some(pattern) = local_visitor.found_local_pattern {
- if let Some(simple_name) = pattern.simple_name() {
+ // don't put internal desugared-loop identifier in user-facing
+ // message (Issue #51116)
+ let simple_name = pattern.simple_name().filter(|n| n.as_str() != "__next");
+ if let Some(simple_name) = simple_name {
labels.push((pattern.span, format!("consider giving `{}` a type", simple_name)));
} else {
labels.push((pattern.span, format!("consider giving the pattern a type")));
But more fundamentally, this isn't a good label to set on loops: we don't have syntax to type-annotate loop variables.
The __next
is coming from desugaring:
But more fundamentally, this isn't a good label to set on loops: we don't have syntax to type-annotate loop variables.
Agree, we should check for this case.
Fixed in #52418.