Skip to content

Commit ed362c0

Browse files
estebankEsteban Küber
authored and
Esteban Küber
committed
Do not use desugared ident when suggesting adding a type
1 parent 29ee654 commit ed362c0

File tree

6 files changed

+50
-2
lines changed

6 files changed

+50
-2
lines changed

src/librustc/hir/lowering.rs

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

40134013
let next_ident = self.str_to_ident("__next");
4014+
let sp = self.allow_internal_unstable(CompilerDesugaringKind::ForLoop,
4015+
pat.span);
40144016
let next_pat = self.pat_ident_binding_mode(
4015-
pat.span,
4017+
sp,
40164018
next_ident,
40174019
hir::BindingAnnotation::Mutable,
40184020
);

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
132132
labels.push((pattern.span, format!("consider giving this closure parameter a type")));
133133
} else if let Some(pattern) = local_visitor.found_local_pattern {
134134
if let Some(simple_ident) = pattern.simple_ident() {
135-
labels.push((pattern.span, format!("consider giving `{}` a type", simple_ident)));
135+
labels.push((
136+
pattern.span,
137+
match pattern.span.compiler_desugaring_kind() {
138+
None => format!("consider giving `{}` a type", simple_ident),
139+
_ => "consider giving this a type".to_string(),
140+
}));
136141
} else {
137142
labels.push((pattern.span, format!("consider giving the pattern a type")));
138143
}

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-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 consider giving this a type
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+
| ---- consider giving this a type
6+
LL | //~^ NOTE consider giving this a type
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)