Do not use casting for suggestion to add type to numeric literal

This commit is contained in:
Esteban Küber 2018-01-04 12:23:14 -08:00
parent 87242f3cc7
commit f7aed3eb27
7 changed files with 59 additions and 22 deletions

View File

@ -241,7 +241,7 @@ pub struct LifetimeDef {
}
/// A "Path" is essentially Rust's notion of a name; for instance:
/// std::cmp::PartialEq . It's represented as a sequence of identifiers,
/// `std::cmp::PartialEq`. It's represented as a sequence of identifiers,
/// along with a bunch of supporting information.
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
pub struct Path {

View File

@ -215,20 +215,43 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
item_name,
ty_string
);
let snippet = tcx.sess.codemap().span_to_snippet(expr.span)
.unwrap_or("4".to_string());
let concrete_type = if actual.is_integral() {
"u32"
"i32"
} else {
"f32"
};
err.span_suggestion(expr.span,
&format!("you must specify a concrete type for \
this numeric value, like `{}`",
concrete_type),
format!("({} as {})",
snippet,
concrete_type));
match expr.node {
hir::ExprLit(_) => { // numeric literal
let snippet = tcx.sess.codemap().span_to_snippet(expr.span)
.unwrap_or("<numeric literal>".to_string());
// FIXME: use the literal for missing snippet
err.span_suggestion(expr.span,
&format!("you must specify a concrete type for \
this numeric value, like `{}`",
concrete_type),
format!("{}_{}",
snippet,
concrete_type));
}
hir::ExprPath(ref qpath) => { // local binding
if let &hir::QPath::Resolved(_, ref path) = &qpath {
if let hir::def::Def::Local(node_id) = path.def {
let span = tcx.hir.span(node_id);
let snippet = tcx.sess.codemap().span_to_snippet(span)
.unwrap();
err.span_suggestion(span,
&format!("you must specify a type for \
this binding, like `{}`",
concrete_type),
format!("{}: {}",
snippet,
concrete_type));
}
}
}
_ => {}
}
err.emit();
return;
} else {

View File

@ -4661,9 +4661,10 @@ x.powi(2); // same error as above
Because of this, you must give the numeric literal or binding a type:
```
let _ = (2.0 as f32).powi(2);
let _ = 2.0_f32.powi(2);
let x: f32 = 2.0;
let _ = x.powi(2);
let _ = (2.0 as f32).powi(2);
```
"##,
}

View File

@ -3,10 +3,10 @@ error[E0689]: can't call method `f` on ambiguous numeric type `{integer}`
|
19 | 3.f()
| ^
help: you must specify a concrete type for this numeric value, like `u32`
help: you must specify a concrete type for this numeric value, like `i32`
|
19 | (3 as u32).f()
| ^^^^^^^^^^
19 | 3_i32.f()
| ^^^^^
error: aborting due to previous error

View File

@ -35,8 +35,8 @@ error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
| -------------------- in this macro invocation
help: you must specify a concrete type for this numeric value, like `f32`
|
51 | (2.0 as f32).powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
| ^^^^^^^^^^^^
51 | 2.0_f32.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
| ^^^^^^^
error[E0599]: no method named `fake` found for type `{integer}` in the current scope
--> $DIR/macro-backtrace-invalid-internals.rs:33:13
@ -75,8 +75,8 @@ error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
| ------------------- in this macro invocation
help: you must specify a concrete type for this numeric value, like `f32`
|
57 | (2.0 as f32).powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
| ^^^^^^^^^^^^
57 | 2.0_f32.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
| ^^^^^^^
error: aborting due to 8 previous errors

View File

@ -11,5 +11,8 @@
fn main() {
let x = 2.0.powi(2);
//~^ ERROR can't call method `powi` on ambiguous numeric type `{float}`
let y = 2.0;
let x = y.powi(2);
//~^ ERROR can't call method `powi` on ambiguous numeric type `{float}`
println!("{:?}", x);
}

View File

@ -5,8 +5,18 @@ error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
| ^^^^
help: you must specify a concrete type for this numeric value, like `f32`
|
12 | let x = (2.0 as f32).powi(2);
| ^^^^^^^^^^^^
12 | let x = 2.0_f32.powi(2);
| ^^^^^^^
error: aborting due to previous error
error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
--> $DIR/method-on-ambiguous-numeric-type.rs:15:15
|
15 | let x = y.powi(2);
| ^^^^
help: you must specify a type for this binding, like `f32`
|
14 | let y: f32 = 2.0;
| ^^^^^^
error: aborting due to 2 previous errors