Skip to content

Commit 9d3d9d5

Browse files
Rollup merge of rust-lang#47702 - etaoins:fix-into-cast-paren-precedence, r=petrochenkov
Fix into() cast paren check precedence As discussed in rust-lang#47699 the logic for determining if an expression needs parenthesis when suggesting an `.into()` cast is incorrect. Two broken examples from nightly are: ``` error[E0308]: mismatched types --> main.rs:4:10 | 4 | test(foo as i8); | ^^^^^^^^^ expected i32, found i8 help: you can cast an `i8` to `i32`, which will sign-extend the source value | 4 | test(foo as i8.into()); | ``` ``` error[E0308]: mismatched types --> main.rs:4:10 | 4 | test(*foo); | ^^^^ expected i32, found i8 help: you can cast an `i8` to `i32`, which will sign-extend the source value | 4 | test(*foo.into()); | ``` As suggested by @petrochenkov switch the precedence check to `PREC_POSTFIX`. This catches both `as` and unary operators. Fixes rust-lang#47699. r? @petrochenkov
2 parents 47cb91a + 65b1e86 commit 9d3d9d5

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/librustc_typeck/check/demand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc::infer::InferOk;
1515
use rustc::traits::ObligationCause;
1616

1717
use syntax::ast;
18-
use syntax::util::parser::AssocOp;
18+
use syntax::util::parser::PREC_POSTFIX;
1919
use syntax_pos::{self, Span};
2020
use rustc::hir;
2121
use rustc::hir::print;
@@ -336,7 +336,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
336336
// For now, don't suggest casting with `as`.
337337
let can_cast = false;
338338

339-
let needs_paren = expr.precedence().order() < (AssocOp::As.precedence() as i8);
339+
let needs_paren = expr.precedence().order() < (PREC_POSTFIX as i8);
340340

341341
if let Ok(src) = self.tcx.sess.codemap().span_to_snippet(expr.span) {
342342
let msg = format!("you can cast an `{}` to `{}`", checked_ty, expected_ty);

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

+5
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,9 @@ fn main() {
312312
foo::<f32>(x_f64);
313313
//~^ ERROR mismatched types
314314
foo::<f32>(x_f32);
315+
316+
foo::<u32>(x_u8 as u16);
317+
//~^ ERROR mismatched types
318+
foo::<i32>(-x_i8);
319+
//~^ ERROR mismatched types
315320
}

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -882,5 +882,25 @@ error[E0308]: mismatched types
882882
312 | foo::<f32>(x_f64);
883883
| ^^^^^ expected f32, found f64
884884

885-
error: aborting due to 132 previous errors
885+
error[E0308]: mismatched types
886+
--> $DIR/numeric-cast.rs:316:16
887+
|
888+
316 | foo::<u32>(x_u8 as u16);
889+
| ^^^^^^^^^^^ expected u32, found u16
890+
help: you can cast an `u16` to `u32`, which will zero-extend the source value
891+
|
892+
316 | foo::<u32>((x_u8 as u16).into());
893+
| ^^^^^^^^^^^^^^^^^^^^
894+
895+
error[E0308]: mismatched types
896+
--> $DIR/numeric-cast.rs:318:16
897+
|
898+
318 | foo::<i32>(-x_i8);
899+
| ^^^^^ expected i32, found i8
900+
help: you can cast an `i8` to `i32`, which will sign-extend the source value
901+
|
902+
318 | foo::<i32>((-x_i8).into());
903+
| ^^^^^^^^^^^^^^
904+
905+
error: aborting due to 134 previous errors
886906

0 commit comments

Comments
 (0)