Skip to content

Commit 8f02447

Browse files
committed
Auto merge of #51670 - estebank:issue-51634, r=oli-obk
Don't suggest incorrect syntax Fix #51634.
2 parents 4dc2d74 + cc0ab82 commit 8f02447

File tree

4 files changed

+100
-12
lines changed

4 files changed

+100
-12
lines changed

src/librustc_typeck/check/method/suggest.rs

+31-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use util::nodemap::FxHashSet;
2525
use syntax::ast;
2626
use syntax::util::lev_distance::find_best_match_for_name;
2727
use errors::DiagnosticBuilder;
28-
use syntax_pos::Span;
28+
use syntax_pos::{Span, FileName};
29+
2930

3031
use rustc::hir::def_id::LOCAL_CRATE;
3132
use rustc::hir;
@@ -187,7 +188,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
187188
out_of_scope_traits,
188189
lev_candidate,
189190
mode,
190-
..
191191
}) => {
192192
let tcx = self.tcx;
193193

@@ -264,13 +264,35 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
264264
let span = tcx.hir.span(node_id);
265265
let snippet = tcx.sess.codemap().span_to_snippet(span)
266266
.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));
267+
let filename = tcx.sess.codemap().span_to_filename(span);
268+
269+
let parent_node = self.tcx.hir.get(
270+
self.tcx.hir.get_parent_node(node_id),
271+
);
272+
let msg = format!(
273+
"you must specify a type for this binding, like `{}`",
274+
concrete_type,
275+
);
276+
277+
match (filename, parent_node) {
278+
(FileName::Real(_), hir_map::NodeLocal(hir::Local {
279+
source: hir::LocalSource::Normal,
280+
ty,
281+
..
282+
})) => {
283+
err.span_suggestion(
284+
// account for `let x: _ = 42;`
285+
// ^^^^
286+
span.to(ty.as_ref().map(|ty| ty.span)
287+
.unwrap_or(span)),
288+
&msg,
289+
format!("{}: {}", snippet, concrete_type),
290+
);
291+
}
292+
_ => {
293+
err.span_label(span, msg);
294+
}
295+
}
274296
}
275297
}
276298
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
#[macro_export]
12+
macro_rules! mac {
13+
($ident:ident) => { let $ident = 42; }
14+
}

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

+24
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,35 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// aux-build:macro-in-other-crate.rs
12+
13+
#[macro_use] extern crate macro_in_other_crate;
14+
15+
macro_rules! local_mac {
16+
($ident:ident) => { let $ident = 42; }
17+
}
18+
1119
fn main() {
1220
let x = 2.0.neg();
1321
//~^ ERROR can't call method `neg` on ambiguous numeric type `{float}`
22+
1423
let y = 2.0;
1524
let x = y.neg();
1625
//~^ ERROR can't call method `neg` on ambiguous numeric type `{float}`
1726
println!("{:?}", x);
27+
28+
for i in 0..100 {
29+
println!("{}", i.pow(2));
30+
//~^ ERROR can't call method `pow` on ambiguous numeric type `{integer}`
31+
}
32+
33+
local_mac!(local_bar);
34+
local_bar.pow(2);
35+
//~^ ERROR can't call method `pow` on ambiguous numeric type `{integer}`
36+
}
37+
38+
fn qux() {
39+
mac!(bar);
40+
bar.pow(2);
41+
//~^ ERROR can't call method `pow` on ambiguous numeric type `{integer}`
1842
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0689]: can't call method `neg` on ambiguous numeric type `{float}`
2-
--> $DIR/method-on-ambiguous-numeric-type.rs:12:17
2+
--> $DIR/method-on-ambiguous-numeric-type.rs:20:17
33
|
44
LL | let x = 2.0.neg();
55
| ^^^
@@ -9,7 +9,7 @@ LL | let x = 2.0_f32.neg();
99
| ^^^^^^^
1010

1111
error[E0689]: can't call method `neg` on ambiguous numeric type `{float}`
12-
--> $DIR/method-on-ambiguous-numeric-type.rs:15:15
12+
--> $DIR/method-on-ambiguous-numeric-type.rs:24:15
1313
|
1414
LL | let x = y.neg();
1515
| ^^^
@@ -18,6 +18,34 @@ 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:29: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[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
30+
--> $DIR/method-on-ambiguous-numeric-type.rs:34:15
31+
|
32+
LL | local_bar.pow(2);
33+
| ^^^
34+
help: you must specify a type for this binding, like `i32`
35+
|
36+
LL | ($ident:ident) => { let $ident: i32 = 42; }
37+
| ^^^^^^^^^^^
38+
39+
error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
40+
--> $DIR/method-on-ambiguous-numeric-type.rs:40:9
41+
|
42+
LL | mac!(bar);
43+
| ---------- you must specify a type for this binding, like `i32`
44+
LL | bar.pow(2);
45+
| ^^^
46+
|
47+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
48+
49+
error: aborting due to 5 previous errors
2250

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

0 commit comments

Comments
 (0)