Skip to content

Commit 70c88e5

Browse files
committed
Don't suggest incorrect syntax
1 parent 6ec1b62 commit 70c88e5

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

src/librustc_typeck/check/method/suggest.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
187187
out_of_scope_traits,
188188
lev_candidate,
189189
mode,
190-
..
191190
}) => {
192191
let tcx = self.tcx;
193192

@@ -264,13 +263,29 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
264263
let span = tcx.hir.span(node_id);
265264
let snippet = tcx.sess.codemap().span_to_snippet(span)
266265
.unwrap();
267-
err.span_suggestion(span,
268-
&format!("you must specify a type for \
269-
this binding, like `{}`",
270-
concrete_type),
271-
format!("{}: {}",
272-
snippet,
273-
concrete_type));
266+
267+
let parent_node = self.tcx.hir.get(
268+
self.tcx.hir.get_parent_node(node_id),
269+
);
270+
let msg = format!(
271+
"you must specify a type for this binding, like `{}`",
272+
concrete_type,
273+
);
274+
match parent_node {
275+
hir_map::NodeLocal(hir::Local {
276+
source: hir::LocalSource::Normal,
277+
..
278+
}) => {
279+
err.span_suggestion(
280+
span,
281+
&msg,
282+
format!("{}: {}", snippet, concrete_type),
283+
);
284+
}
285+
_ => {
286+
err.span_label(span, msg);
287+
}
288+
}
274289
}
275290
}
276291
}

src/test/ui/suggestions/method-on-ambiguous-numeric-type.rs

+4
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ fn main() {
1515
let x = y.neg();
1616
//~^ ERROR can't call method `neg` on ambiguous numeric type `{float}`
1717
println!("{:?}", x);
18+
for i in 0..100 {
19+
println!("{}", i.pow(2));
20+
//~^ ERROR can't call method `pow` on ambiguous numeric type `{integer}`
21+
}
1822
}

src/test/ui/suggestions/method-on-ambiguous-numeric-type.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ help: you must specify a type for this binding, like `f32`
1818
LL | let y: f32 = 2.0;
1919
| ^^^^^^
2020

21-
error: aborting due to 2 previous errors
21+
error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
22+
--> $DIR/method-on-ambiguous-numeric-type.rs:19:26
23+
|
24+
LL | for i in 0..100 {
25+
| - you must specify a type for this binding, like `i32`
26+
LL | println!("{}", i.pow(2));
27+
| ^^^
28+
29+
error: aborting due to 3 previous errors
2230

2331
For more information about this error, try `rustc --explain E0689`.

0 commit comments

Comments
 (0)