Rust: 型推論は、存在しない変数 `__next`への修正を提案します

作成日 2018年05月28日  ·  3コメント  ·  ソース: rust-lang/rust

このコード:(遊び場リンク

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と呼ばれ、実際には私のコードには存在しません。 これはおそらく、コンパイラによって生成された内部変数です。

A-diagnostics C-bug

全てのコメント3件

この場合、変数に名前を付けることを拒否するのは簡単です。

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は脱糖から来ています:

https://github.com/rust-lang/rust/blob/5ae5361cdd7b4e518d6733fb726521563d5b4cfa/src/librustc/hir/lowering.rs#L3355 -L3501

しかし、より基本的には、これはループに設定するのに適したラベルではありません。ループ変数に型注釈を付けるための構文がありません。

同意します。このケースを確認する必要があります。

#52418で修正されました。

このページは役に立ちましたか?
0 / 5 - 0 評価