Skip to content

Commit 509ea8e

Browse files
committed
Only suggest casting numeric types using into()
1 parent af91d99 commit 509ea8e

File tree

6 files changed

+303
-798
lines changed

6 files changed

+303
-798
lines changed

src/librustc_typeck/check/demand.rs

+21-13
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
331331
// We want to minimize the amount of casting operations that are suggested, as it can be a
332332
// lossy operation with potentially bad side effects, so we only suggest when encountering
333333
// an expression that indicates that the original type couldn't be directly changed.
334-
let can_cast = match expr.node {
335-
hir::ExprPath(..) |
336-
hir::ExprCall(..) |
337-
hir::ExprMethodCall(..) |
338-
hir::ExprBinary(..) => true,
339-
_ => false,
340-
};
334+
//
335+
// For now, don't suggest casting with `as`.
336+
let can_cast = false;
341337

342338
let needs_paren = match expr.node {
343339
hir::ExprBinary(..) => true,
@@ -369,7 +365,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
369365
(None, _) | (_, None) => {
370366
if can_cast {
371367
err.span_suggestion(expr.span,
372-
&format!("{}, which {}", msg, depending_on_isize),
368+
&format!("{}, which {}",
369+
msg,
370+
depending_on_isize),
373371
cast_suggestion);
374372
}
375373
}
@@ -393,7 +391,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
393391
(None, _) | (_, None) => {
394392
if can_cast {
395393
err.span_suggestion(expr.span,
396-
&format!("{}, which {}", msg, depending_on_usize),
394+
&format!("{}, which {}",
395+
msg,
396+
depending_on_usize),
397397
cast_suggestion);
398398
}
399399
}
@@ -420,12 +420,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
420420
}
421421
(None, _) => {
422422
err.span_suggestion(expr.span,
423-
&format!("{}, which {}", msg, depending_on_isize),
423+
&format!("{}, which {}",
424+
msg,
425+
depending_on_isize),
424426
cast_suggestion);
425427
}
426428
(_, None) => {
427429
err.span_suggestion(expr.span,
428-
&format!("{}, which {}", msg, depending_on_usize),
430+
&format!("{}, which {}",
431+
msg,
432+
depending_on_usize),
429433
cast_suggestion);
430434
}
431435
_ => {
@@ -452,12 +456,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
452456
}
453457
(None, _) => {
454458
err.span_suggestion(expr.span,
455-
&format!("{}, which {}", msg, depending_on_usize),
459+
&format!("{}, which {}",
460+
msg,
461+
depending_on_usize),
456462
cast_suggestion);
457463
}
458464
(_, None) => {
459465
err.span_suggestion(expr.span,
460-
&format!("{}, which {}", msg, depending_on_isize),
466+
&format!("{}, which {}",
467+
msg,
468+
depending_on_isize),
461469
cast_suggestion);
462470
}
463471
_ => {

src/test/ui/mismatched_types/issue-26480.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ error[E0308]: mismatched types
66
...
77
37 | write!(hello);
88
| -------------- in this macro invocation
9-
help: you can cast an `usize` to `u64`, which will truncate or zero-extend depending on the bit width of `usize`
10-
|
11-
26 | ($arr.len() * size_of($arr[0])) as u64); //~ ERROR mismatched types
12-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139

1410
error[E0605]: non-primitive cast: `{integer}` as `()`
1511
--> $DIR/issue-26480.rs:32:19

src/test/ui/suggestions/numeric-cast-2.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ fn foo() -> i32 {
1212
4
1313
}
1414
fn main() {
15-
let x: u32 = foo();
15+
let x: u16 = foo();
16+
//~^ ERROR mismatched types
17+
let y: i64 = x + x;
1618
//~^ ERROR mismatched types
1719
let z: i32 = x + x;
1820
//~^ ERROR mismatched types
+10-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
error[E0308]: mismatched types
22
--> $DIR/numeric-cast-2.rs:15:18
33
|
4-
15 | let x: u32 = foo();
5-
| ^^^^^ expected u32, found i32
6-
help: you can cast an `i32` to `u32`, which will sign-extend the source value
7-
|
8-
15 | let x: u32 = foo() as u32;
9-
| ^^^^^^^^^^^^
4+
15 | let x: u16 = foo();
5+
| ^^^^^ expected u16, found i32
106

117
error[E0308]: mismatched types
128
--> $DIR/numeric-cast-2.rs:17:18
139
|
14-
17 | let z: i32 = x + x;
15-
| ^^^^^ expected i32, found u32
16-
help: you can cast an `u32` to `i32`, which will truncate the source value
10+
17 | let y: i64 = x + x;
11+
| ^^^^^ expected i64, found u16
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/numeric-cast-2.rs:19:18
1715
|
18-
17 | let z: i32 = (x + x) as i32;
19-
| ^^^^^^^^^^^^^^
16+
19 | let z: i32 = x + x;
17+
| ^^^^^ expected i32, found u16
2018

21-
error: aborting due to 2 previous errors
19+
error: aborting due to 3 previous errors
2220

src/test/ui/suggestions/numeric-cast.rs

-21
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ fn main() {
4646
//~^ ERROR mismatched types
4747
foo::<usize>(x_f64);
4848
//~^ ERROR mismatched types
49-
//~| WARN casting here will cause Undefined Behavior
5049
foo::<usize>(x_f32);
5150
//~^ ERROR mismatched types
52-
//~| WARN casting here will cause Undefined Behavior
5351

5452
foo::<isize>(x_usize);
5553
//~^ ERROR mismatched types
@@ -72,10 +70,8 @@ fn main() {
7270
//~^ ERROR mismatched types
7371
foo::<isize>(x_f64);
7472
//~^ ERROR mismatched types
75-
//~| WARN casting here will cause Undefined Behavior
7673
foo::<isize>(x_f32);
7774
//~^ ERROR mismatched types
78-
//~| WARN casting here will cause Undefined Behavior
7975

8076
foo::<u64>(x_usize);
8177
//~^ ERROR mismatched types
@@ -98,10 +94,8 @@ fn main() {
9894
//~^ ERROR mismatched types
9995
foo::<u64>(x_f64);
10096
//~^ ERROR mismatched types
101-
//~| WARN casting here will cause Undefined Behavior
10297
foo::<u64>(x_f32);
10398
//~^ ERROR mismatched types
104-
//~| WARN casting here will cause Undefined Behavior
10599

106100
foo::<i64>(x_usize);
107101
//~^ ERROR mismatched types
@@ -124,10 +118,8 @@ fn main() {
124118
//~^ ERROR mismatched types
125119
foo::<i64>(x_f64);
126120
//~^ ERROR mismatched types
127-
//~| WARN casting here will cause Undefined Behavior
128121
foo::<i64>(x_f32);
129122
//~^ ERROR mismatched types
130-
//~| WARN casting here will cause Undefined Behavior
131123

132124
foo::<u32>(x_usize);
133125
//~^ ERROR mismatched types
@@ -150,10 +142,8 @@ fn main() {
150142
//~^ ERROR mismatched types
151143
foo::<u32>(x_f64);
152144
//~^ ERROR mismatched types
153-
//~| WARN casting here will cause Undefined Behavior
154145
foo::<u32>(x_f32);
155146
//~^ ERROR mismatched types
156-
//~| WARN casting here will cause Undefined Behavior
157147

158148
foo::<i32>(x_usize);
159149
//~^ ERROR mismatched types
@@ -176,10 +166,8 @@ fn main() {
176166
//~^ ERROR mismatched types
177167
foo::<i32>(x_f64);
178168
//~^ ERROR mismatched types
179-
//~| WARN casting here will cause Undefined Behavior
180169
foo::<i32>(x_f32);
181170
//~^ ERROR mismatched types
182-
//~| WARN casting here will cause Undefined Behavior
183171

184172
foo::<u16>(x_usize);
185173
//~^ ERROR mismatched types
@@ -202,10 +190,8 @@ fn main() {
202190
//~^ ERROR mismatched types
203191
foo::<u16>(x_f64);
204192
//~^ ERROR mismatched types
205-
//~| WARN casting here will cause Undefined Behavior
206193
foo::<u16>(x_f32);
207194
//~^ ERROR mismatched types
208-
//~| WARN casting here will cause Undefined Behavior
209195

210196
foo::<i16>(x_usize);
211197
//~^ ERROR mismatched types
@@ -228,10 +214,8 @@ fn main() {
228214
//~^ ERROR mismatched types
229215
foo::<i16>(x_f64);
230216
//~^ ERROR mismatched types
231-
//~| WARN casting here will cause Undefined Behavior
232217
foo::<i16>(x_f32);
233218
//~^ ERROR mismatched types
234-
//~| WARN casting here will cause Undefined Behavior
235219

236220
foo::<u8>(x_usize);
237221
//~^ ERROR mismatched types
@@ -254,10 +238,8 @@ fn main() {
254238
//~^ ERROR mismatched types
255239
foo::<u8>(x_f64);
256240
//~^ ERROR mismatched types
257-
//~| WARN casting here will cause Undefined Behavior
258241
foo::<u8>(x_f32);
259242
//~^ ERROR mismatched types
260-
//~| WARN casting here will cause Undefined Behavior
261243

262244
foo::<i8>(x_usize);
263245
//~^ ERROR mismatched types
@@ -280,10 +262,8 @@ fn main() {
280262
foo::<i8>(x_i8);
281263
foo::<i8>(x_f64);
282264
//~^ ERROR mismatched types
283-
//~| WARN casting here will cause Undefined Behavior
284265
foo::<i8>(x_f32);
285266
//~^ ERROR mismatched types
286-
//~| WARN casting here will cause Undefined Behavior
287267

288268
foo::<f64>(x_usize);
289269
//~^ ERROR mismatched types
@@ -331,6 +311,5 @@ fn main() {
331311
//~^ ERROR mismatched types
332312
foo::<f32>(x_f64);
333313
//~^ ERROR mismatched types
334-
//~| WARN casting here will cause undefined behavior
335314
foo::<f32>(x_f32);
336315
}

0 commit comments

Comments
 (0)