diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 722934ac39a53..93fb282546bb0 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -4011,8 +4011,12 @@ impl<'a> LoweringContext<'a> { let iter = self.str_to_ident("iter"); let next_ident = self.str_to_ident("__next"); + let next_sp = self.allow_internal_unstable( + CompilerDesugaringKind::ForLoop, + head_sp, + ); let next_pat = self.pat_ident_binding_mode( - pat.span, + next_sp, next_ident, hir::BindingAnnotation::Mutable, ); diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index fa9c5cefdf3d3..c9ac6cdedbbc6 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -412,6 +412,7 @@ impl_stable_hash_for!(enum ::syntax_pos::hygiene::CompilerDesugaringKind { DotFill, QuestionMark, ExistentialReturnType, + ForLoop, Catch }); diff --git a/src/librustc/infer/error_reporting/need_type_info.rs b/src/librustc/infer/error_reporting/need_type_info.rs index 773de8912ce9b..04d14f40b850b 100644 --- a/src/librustc/infer/error_reporting/need_type_info.rs +++ b/src/librustc/infer/error_reporting/need_type_info.rs @@ -13,6 +13,7 @@ use hir::intravisit::{self, Visitor, NestedVisitorMap}; use infer::InferCtxt; use infer::type_variable::TypeVariableOrigin; use ty::{self, Ty, TyInfer, TyVar}; +use syntax::codemap::CompilerDesugaringKind; use syntax_pos::Span; use errors::DiagnosticBuilder; @@ -132,7 +133,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { 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_ident) = pattern.simple_ident() { - labels.push((pattern.span, format!("consider giving `{}` a type", simple_ident))); + match pattern.span.compiler_desugaring_kind() { + None => labels.push((pattern.span, + format!("consider giving `{}` a type", simple_ident))), + Some(CompilerDesugaringKind::ForLoop) => labels.push(( + pattern.span, + "the element type for this iterator is not specified".to_string(), + )), + _ => {} + } } else { labels.push((pattern.span, format!("consider giving the pattern a type"))); } diff --git a/src/libsyntax_pos/hygiene.rs b/src/libsyntax_pos/hygiene.rs index c7076478332f4..1531f030127e3 100644 --- a/src/libsyntax_pos/hygiene.rs +++ b/src/libsyntax_pos/hygiene.rs @@ -602,6 +602,7 @@ pub enum CompilerDesugaringKind { /// `impl Trait` with `Foo`. ExistentialReturnType, Async, + ForLoop, } impl CompilerDesugaringKind { @@ -612,6 +613,7 @@ impl CompilerDesugaringKind { CompilerDesugaringKind::QuestionMark => "?", CompilerDesugaringKind::Catch => "do catch", CompilerDesugaringKind::ExistentialReturnType => "existential type", + CompilerDesugaringKind::ForLoop => "for loop", }) } } diff --git a/src/test/ui/issue-20261.stderr b/src/test/ui/issue-20261.stderr index a4a2aec8969ae..a7a7ea7c69b69 100644 --- a/src/test/ui/issue-20261.stderr +++ b/src/test/ui/issue-20261.stderr @@ -2,7 +2,7 @@ error[E0282]: type annotations needed --> $DIR/issue-20261.rs:14:11 | LL | for (ref i,) in [].iter() { - | -------- consider giving `__next` a type + | --------- the element type for this iterator is not specified LL | i.clone(); | ^^^^^ cannot infer type for `_` | diff --git a/src/test/ui/issue-51116.rs b/src/test/ui/issue-51116.rs new file mode 100644 index 0000000000000..34217c6236c43 --- /dev/null +++ b/src/test/ui/issue-51116.rs @@ -0,0 +1,24 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let tiles = Default::default(); + for row in &mut tiles { + for tile in row { + //~^ NOTE the element type for this iterator is not specified + *tile = 0; + //~^ ERROR type annotations needed + //~| NOTE cannot infer type for `_` + //~| NOTE type must be known at this point + } + } + + let tiles: [[usize; 3]; 3] = tiles; +} diff --git a/src/test/ui/issue-51116.stderr b/src/test/ui/issue-51116.stderr new file mode 100644 index 0000000000000..0c38688340bf3 --- /dev/null +++ b/src/test/ui/issue-51116.stderr @@ -0,0 +1,14 @@ +error[E0282]: type annotations needed + --> $DIR/issue-51116.rs:16:13 + | +LL | for tile in row { + | --- the element type for this iterator is not specified +LL | //~^ NOTE the element type for this iterator is not specified +LL | *tile = 0; + | ^^^^^ cannot infer type for `_` + | + = note: type must be known at this point + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0282`.