Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type inference suggests correction to non-existent variable __next #51116

Closed
sunjay opened this issue May 27, 2018 · 3 comments
Closed

Type inference suggests correction to non-existent variable __next #51116

sunjay opened this issue May 27, 2018 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug.

Comments

@sunjay
Copy link
Member

sunjay commented May 27, 2018

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.

@zackmdavis
Copy link
Member

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.

@Mark-Simulacrum Mark-Simulacrum added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. labels May 28, 2018
@estebank
Copy link
Contributor

The __next is coming from desugaring:

// Desugar ExprForLoop
// From: `[opt_ident]: for <pat> in <head> <body>`
ExprKind::ForLoop(ref pat, ref head, ref body, opt_label) => {
// to:
//
// {
// let result = match ::std::iter::IntoIterator::into_iter(<head>) {
// mut iter => {
// [opt_ident]: loop {
// let mut __next;
// match ::std::iter::Iterator::next(&mut iter) {
// ::std::option::Option::Some(val) => __next = val,
// ::std::option::Option::None => break
// };
// let <pat> = __next;
// StmtExpr(<body>);
// }
// }
// };
// result
// }
// expand <head>
let head = self.lower_expr(head);
let head_sp = head.span;
let iter = self.str_to_ident("iter");
let next_ident = self.str_to_ident("__next");
let next_pat = self.pat_ident_binding_mode(
pat.span,
next_ident,
hir::BindingAnnotation::Mutable,
);
// `::std::option::Option::Some(val) => next = val`
let pat_arm = {
let val_ident = self.str_to_ident("val");
let val_pat = self.pat_ident(pat.span, val_ident);
let val_expr = P(self.expr_ident(pat.span, val_ident, val_pat.id));
let next_expr = P(self.expr_ident(pat.span, next_ident, next_pat.id));
let assign = P(self.expr(
pat.span,
hir::ExprAssign(next_expr, val_expr),
ThinVec::new(),
));
let some_pat = self.pat_some(pat.span, val_pat);
self.arm(hir_vec![some_pat], assign)
};
// `::std::option::Option::None => break`
let break_arm = {
let break_expr =
self.with_loop_scope(e.id, |this| this.expr_break(e.span, ThinVec::new()));
let pat = self.pat_none(e.span);
self.arm(hir_vec![pat], break_expr)
};
// `mut iter`
let iter_pat =
self.pat_ident_binding_mode(head_sp, iter, hir::BindingAnnotation::Mutable);
// `match ::std::iter::Iterator::next(&mut iter) { ... }`
let match_expr = {
let iter = P(self.expr_ident(head_sp, iter, iter_pat.id));
let ref_mut_iter = self.expr_mut_addr_of(head_sp, iter);
let next_path = &["iter", "Iterator", "next"];
let next_path = P(self.expr_std_path(head_sp, next_path, ThinVec::new()));
let next_expr = P(self.expr_call(head_sp, next_path, hir_vec![ref_mut_iter]));
let arms = hir_vec![pat_arm, break_arm];
P(self.expr(
head_sp,
hir::ExprMatch(next_expr, arms, hir::MatchSource::ForLoopDesugar),
ThinVec::new(),
))
};
let match_stmt = respan(head_sp, hir::StmtExpr(match_expr, self.next_id().node_id));
let next_expr = P(self.expr_ident(head_sp, next_ident, next_pat.id));
// `let mut __next`
let next_let =
self.stmt_let_pat(head_sp, None, next_pat, hir::LocalSource::ForLoopDesugar);
// `let <pat> = __next`
let pat = self.lower_pat(pat);
let pat_let = self.stmt_let_pat(
head_sp,
Some(next_expr),
pat,
hir::LocalSource::ForLoopDesugar,
);
let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
let body_expr = P(self.expr_block(body_block, ThinVec::new()));
let body_stmt = respan(body.span, hir::StmtExpr(body_expr, self.next_id().node_id));
let loop_block = P(self.block_all(
e.span,
hir_vec![next_let, match_stmt, pat_let, body_stmt],
None,
));
// `[opt_ident]: loop { ... }`
let loop_expr = hir::ExprLoop(
loop_block,
self.lower_label(opt_label),
hir::LoopSource::ForLoop,
);
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(e.id);
let loop_expr = P(hir::Expr {
id: node_id,
hir_id,
node: loop_expr,
span: e.span,
attrs: ThinVec::new(),
});
// `mut iter => { ... }`
let iter_arm = self.arm(hir_vec![iter_pat], loop_expr);
// `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
let into_iter_expr = {
let into_iter_path = &["iter", "IntoIterator", "into_iter"];
let into_iter = P(self.expr_std_path(head_sp, into_iter_path, ThinVec::new()));
P(self.expr_call(head_sp, into_iter, hir_vec![head]))
};
let match_expr = P(self.expr_match(
head_sp,
into_iter_expr,
hir_vec![iter_arm],
hir::MatchSource::ForLoopDesugar,
));
// `{ let _result = ...; _result }`
// underscore prevents an unused_variables lint if the head diverges
let result_ident = self.str_to_ident("_result");
let (let_stmt, let_stmt_binding) =
self.stmt_let(e.span, false, result_ident, match_expr);
let result = P(self.expr_ident(e.span, result_ident, let_stmt_binding));
let block = P(self.block_all(e.span, hir_vec![let_stmt], Some(result)));
// add the attributes to the outer returned expr node
return self.expr_block(block, e.attrs.clone());
}

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.

kennytm added a commit to kennytm/rust that referenced this issue Jul 18, 2018
…sakis

Do not use desugared ident when suggesting adding a type

Re rust-lang#51116.
@estebank
Copy link
Contributor

estebank commented Aug 8, 2018

Fixed in #52418.

@estebank estebank closed this as completed Aug 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

4 participants