Skip to content

Commit 8336f1b

Browse files
committed
Rollup merge of #52418 - estebank:desugaring-type, r=nikomatsakis
Do not use desugared ident when suggesting adding a type Re #51116.
2 parents b1c6b76 + 2c5b60d commit 8336f1b

File tree

7 files changed

+57
-3
lines changed

7 files changed

+57
-3
lines changed

src/librustc/hir/lowering.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4011,8 +4011,12 @@ impl<'a> LoweringContext<'a> {
40114011
let iter = self.str_to_ident("iter");
40124012

40134013
let next_ident = self.str_to_ident("__next");
4014+
let next_sp = self.allow_internal_unstable(
4015+
CompilerDesugaringKind::ForLoop,
4016+
head_sp,
4017+
);
40144018
let next_pat = self.pat_ident_binding_mode(
4015-
pat.span,
4019+
next_sp,
40164020
next_ident,
40174021
hir::BindingAnnotation::Mutable,
40184022
);

src/librustc/ich/impls_syntax.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ impl_stable_hash_for!(enum ::syntax_pos::hygiene::CompilerDesugaringKind {
412412
DotFill,
413413
QuestionMark,
414414
ExistentialReturnType,
415+
ForLoop,
415416
Catch
416417
});
417418

src/librustc/infer/error_reporting/need_type_info.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use hir::intravisit::{self, Visitor, NestedVisitorMap};
1313
use infer::InferCtxt;
1414
use infer::type_variable::TypeVariableOrigin;
1515
use ty::{self, Ty, TyInfer, TyVar};
16+
use syntax::codemap::CompilerDesugaringKind;
1617
use syntax_pos::Span;
1718
use errors::DiagnosticBuilder;
1819

@@ -132,7 +133,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
132133
labels.push((pattern.span, format!("consider giving this closure parameter a type")));
133134
} else if let Some(pattern) = local_visitor.found_local_pattern {
134135
if let Some(simple_ident) = pattern.simple_ident() {
135-
labels.push((pattern.span, format!("consider giving `{}` a type", simple_ident)));
136+
match pattern.span.compiler_desugaring_kind() {
137+
None => labels.push((pattern.span,
138+
format!("consider giving `{}` a type", simple_ident))),
139+
Some(CompilerDesugaringKind::ForLoop) => labels.push((
140+
pattern.span,
141+
"the element type for this iterator is not specified".to_string(),
142+
)),
143+
_ => {}
144+
}
136145
} else {
137146
labels.push((pattern.span, format!("consider giving the pattern a type")));
138147
}

src/libsyntax_pos/hygiene.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ pub enum CompilerDesugaringKind {
602602
/// `impl Trait` with `Foo`.
603603
ExistentialReturnType,
604604
Async,
605+
ForLoop,
605606
}
606607

607608
impl CompilerDesugaringKind {
@@ -612,6 +613,7 @@ impl CompilerDesugaringKind {
612613
CompilerDesugaringKind::QuestionMark => "?",
613614
CompilerDesugaringKind::Catch => "do catch",
614615
CompilerDesugaringKind::ExistentialReturnType => "existential type",
616+
CompilerDesugaringKind::ForLoop => "for loop",
615617
})
616618
}
617619
}

src/test/ui/issue-20261.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0282]: type annotations needed
22
--> $DIR/issue-20261.rs:14:11
33
|
44
LL | for (ref i,) in [].iter() {
5-
| -------- consider giving `__next` a type
5+
| --------- the element type for this iterator is not specified
66
LL | i.clone();
77
| ^^^^^ cannot infer type for `_`
88
|

src/test/ui/issue-51116.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let tiles = Default::default();
13+
for row in &mut tiles {
14+
for tile in row {
15+
//~^ NOTE the element type for this iterator is not specified
16+
*tile = 0;
17+
//~^ ERROR type annotations needed
18+
//~| NOTE cannot infer type for `_`
19+
//~| NOTE type must be known at this point
20+
}
21+
}
22+
23+
let tiles: [[usize; 3]; 3] = tiles;
24+
}

src/test/ui/issue-51116.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/issue-51116.rs:16:13
3+
|
4+
LL | for tile in row {
5+
| --- the element type for this iterator is not specified
6+
LL | //~^ NOTE the element type for this iterator is not specified
7+
LL | *tile = 0;
8+
| ^^^^^ cannot infer type for `_`
9+
|
10+
= note: type must be known at this point
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)