Skip to content

Do not use desugared ident when suggesting adding a type #52418

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

Merged
merged 3 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
1 change: 1 addition & 0 deletions src/librustc/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ impl_stable_hash_for!(enum ::syntax_pos::hygiene::CompilerDesugaringKind {
DotFill,
QuestionMark,
ExistentialReturnType,
ForLoop,
Catch
});

Expand Down
11 changes: 10 additions & 1 deletion src/librustc/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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")));
}
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax_pos/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ pub enum CompilerDesugaringKind {
/// `impl Trait` with `Foo`.
ExistentialReturnType,
Async,
ForLoop,
}

impl CompilerDesugaringKind {
Expand All @@ -612,6 +613,7 @@ impl CompilerDesugaringKind {
CompilerDesugaringKind::QuestionMark => "?",
CompilerDesugaringKind::Catch => "do catch",
CompilerDesugaringKind::ExistentialReturnType => "existential type",
CompilerDesugaringKind::ForLoop => "for loop",
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issue-20261.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 `_`
|
Expand Down
24 changes: 24 additions & 0 deletions src/test/ui/issue-51116.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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;
}
14 changes: 14 additions & 0 deletions src/test/ui/issue-51116.stderr
Original file line number Diff line number Diff line change
@@ -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`.