Skip to content

Commit 3959dca

Browse files
Rollup merge of #53283 - zackmdavis:and_the_case_of_the_flotation_device, r=estebank
wherein we suggest float for integer literals where a float was expected @sunjay pointed out that this is a nice thing that we could do. Resolves #53280. r? @estebank
2 parents f618071 + 58f660f commit 3959dca

4 files changed

+106
-2
lines changed

src/librustc/ty/error.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ty::{self, BoundRegion, Region, Ty, TyCtxt};
1313
use std::fmt;
1414
use rustc_target::spec::abi;
1515
use syntax::ast;
16-
use errors::DiagnosticBuilder;
16+
use errors::{Applicability, DiagnosticBuilder};
1717
use syntax_pos::Span;
1818

1919
use hir;
@@ -250,6 +250,21 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
250250
db.note("no two closures, even if identical, have the same type");
251251
db.help("consider boxing your closure and/or using it as a trait object");
252252
}
253+
match (&values.found.sty, &values.expected.sty) { // Issue #53280
254+
(ty::TyInfer(ty::IntVar(_)), ty::TyFloat(_)) => {
255+
if let Ok(snippet) = self.sess.codemap().span_to_snippet(sp) {
256+
if snippet.chars().all(|c| c.is_digit(10) || c == '-' || c == '_') {
257+
db.span_suggestion_with_applicability(
258+
sp,
259+
"use a float literal",
260+
format!("{}.0", snippet),
261+
Applicability::MachineApplicable
262+
);
263+
}
264+
}
265+
},
266+
_ => {}
267+
}
253268
},
254269
OldStyleLUB(err) => {
255270
db.note("this was previously accepted by the compiler but has been phased out");

src/test/ui/catch-block-type-error.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0271]: type mismatch resolving `<std::option::Option<f32> as std::ops::Tr
22
--> $DIR/catch-block-type-error.rs:18:9
33
|
44
LL | 42
5-
| ^^ expected f32, found integral variable
5+
| ^^
6+
| |
7+
| expected f32, found integral variable
8+
| help: use a float literal: `42.0`
69
|
710
= note: expected type `f32`
811
found type `{integer}`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 sixteen: f32 = 16;
13+
//~^ ERROR mismatched types
14+
//~| HELP use a float literal
15+
let a_million_and_seventy: f64 = 1_000_070;
16+
//~^ ERROR mismatched types
17+
//~| HELP use a float literal
18+
let negative_nine: f32 = -9;
19+
//~^ ERROR mismatched types
20+
//~| HELP use a float literal
21+
22+
23+
// only base-10 literals get the suggestion
24+
25+
let sixteen_again: f64 = 0x10;
26+
//~^ ERROR mismatched types
27+
let and_once_more: f32 = 0o20;
28+
//~^ ERROR mismatched types
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:12:24
3+
|
4+
LL | let sixteen: f32 = 16;
5+
| ^^
6+
| |
7+
| expected f32, found integral variable
8+
| help: use a float literal: `16.0`
9+
|
10+
= note: expected type `f32`
11+
found type `{integer}`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:15:38
15+
|
16+
LL | let a_million_and_seventy: f64 = 1_000_070;
17+
| ^^^^^^^^^
18+
| |
19+
| expected f64, found integral variable
20+
| help: use a float literal: `1_000_070.0`
21+
|
22+
= note: expected type `f64`
23+
found type `{integer}`
24+
25+
error[E0308]: mismatched types
26+
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:18:30
27+
|
28+
LL | let negative_nine: f32 = -9;
29+
| ^^
30+
| |
31+
| expected f32, found integral variable
32+
| help: use a float literal: `-9.0`
33+
|
34+
= note: expected type `f32`
35+
found type `{integer}`
36+
37+
error[E0308]: mismatched types
38+
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:25:30
39+
|
40+
LL | let sixteen_again: f64 = 0x10;
41+
| ^^^^ expected f64, found integral variable
42+
|
43+
= note: expected type `f64`
44+
found type `{integer}`
45+
46+
error[E0308]: mismatched types
47+
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:27:30
48+
|
49+
LL | let and_once_more: f32 = 0o20;
50+
| ^^^^ expected f32, found integral variable
51+
|
52+
= note: expected type `f32`
53+
found type `{integer}`
54+
55+
error: aborting due to 5 previous errors
56+
57+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)