Rust: La inferencia de tipo sugiere una corrección a la variable inexistente `__next`

Creado en 28 may. 2018  ·  3Comentarios  ·  Fuente: rust-lang/rust

Este código: ( enlace al patio de recreo )

fn main() {
    let tiles = Default::default();
    for row in &mut tiles {
        for tile in row {
            *tile = 0;
        }
    }

    let tiles: [[usize; 3]; 3] = tiles;
}

Falla con este 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 `_`

La inferencia de tipos (legítimamente) falla en este código. Sin embargo, la variable que sugiere corregir se llama __next y en realidad no existe en mi código. Probablemente sea alguna variable interna generada por el compilador.

A-diagnostics C-bug

Todos 3 comentarios

Bueno, negarse a nombrar la variable en este caso sería bastante simple:

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")));

Pero más fundamentalmente, esta no es una buena etiqueta para establecer en bucles: no tenemos sintaxis para escribir y anotar variables de bucle.

El __next proviene de desugaring:

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

Pero más fundamentalmente, esta no es una buena etiqueta para establecer en bucles: no tenemos sintaxis para escribir y anotar variables de bucle.

De acuerdo, deberíamos verificar este caso.

Corregido en # 52418.

¿Fue útil esta página
0 / 5 - 0 calificaciones