このコード:(遊び場リンク)
fn main() {
let tiles = Default::default();
for row in &mut tiles {
for tile in row {
*tile = 0;
}
}
let tiles: [[usize; 3]; 3] = tiles;
}
このエラーで失敗します:
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 `_`
このコードでは型推論は(当然のことながら)失敗します。 ただし、修正を提案する変数は__next
と呼ばれ、実際には私のコードには存在しません。 これはおそらく、コンパイラによって生成された内部変数です。
この場合、変数に名前を付けることを拒否するのは簡単です。
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")));
しかし、より基本的には、これはループに設定するのに適したラベルではありません。ループ変数に型注釈を付けるための構文がありません。
__next
は脱糖から来ています:
しかし、より基本的には、これはループに設定するのに適したラベルではありません。ループ変数に型注釈を付けるための構文がありません。
同意します。このケースを確認する必要があります。
#52418で修正されました。