Skip to content

Commit 1a6a3e4

Browse files
authored
Rollup merge of rust-lang#91245 - cameron1024:suggest-i32-u32-char-cast, r=nagisa
suggest casting between i/u32 and char As discussed in rust-lang#91063 , this adds a suggestion for converting between i32/u32 <-> char with `as`, and a short explanation for why this is safe
2 parents f9e77f2 + 37ca2eb commit 1a6a3e4

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

compiler/rustc_typeck/src/check/demand.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12641264
}
12651265
true
12661266
}
1267+
(
1268+
&ty::Uint(ty::UintTy::U32 | ty::UintTy::U64 | ty::UintTy::U128)
1269+
| &ty::Int(ty::IntTy::I32 | ty::IntTy::I64 | ty::IntTy::I128),
1270+
&ty::Char,
1271+
) => {
1272+
err.multipart_suggestion_verbose(
1273+
&format!("{}, since a `char` always occupies 4 bytes", cast_msg,),
1274+
cast_suggestion,
1275+
Applicability::MachineApplicable,
1276+
);
1277+
true
1278+
}
12671279
_ => false,
12681280
}
12691281
}

src/test/ui/cast/cast-int-to-char.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn foo<T>(_t: T) {}
2+
3+
fn main() {
4+
foo::<u32>('0'); //~ ERROR
5+
foo::<i32>('0'); //~ ERROR
6+
foo::<u64>('0'); //~ ERROR
7+
foo::<i64>('0'); //~ ERROR
8+
foo::<char>(0u32); //~ ERROR
9+
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/cast-int-to-char.rs:4:16
3+
|
4+
LL | foo::<u32>('0');
5+
| ^^^ expected `u32`, found `char`
6+
|
7+
help: you can cast a `char` to a `u32`, since a `char` always occupies 4 bytes
8+
|
9+
LL | foo::<u32>('0' as u32);
10+
| ++++++
11+
12+
error[E0308]: mismatched types
13+
--> $DIR/cast-int-to-char.rs:5:16
14+
|
15+
LL | foo::<i32>('0');
16+
| ^^^ expected `i32`, found `char`
17+
|
18+
help: you can cast a `char` to an `i32`, since a `char` always occupies 4 bytes
19+
|
20+
LL | foo::<i32>('0' as i32);
21+
| ++++++
22+
23+
error[E0308]: mismatched types
24+
--> $DIR/cast-int-to-char.rs:6:16
25+
|
26+
LL | foo::<u64>('0');
27+
| ^^^ expected `u64`, found `char`
28+
|
29+
help: you can cast a `char` to a `u64`, since a `char` always occupies 4 bytes
30+
|
31+
LL | foo::<u64>('0' as u64);
32+
| ++++++
33+
34+
error[E0308]: mismatched types
35+
--> $DIR/cast-int-to-char.rs:7:16
36+
|
37+
LL | foo::<i64>('0');
38+
| ^^^ expected `i64`, found `char`
39+
|
40+
help: you can cast a `char` to an `i64`, since a `char` always occupies 4 bytes
41+
|
42+
LL | foo::<i64>('0' as i64);
43+
| ++++++
44+
45+
error[E0308]: mismatched types
46+
--> $DIR/cast-int-to-char.rs:8:17
47+
|
48+
LL | foo::<char>(0u32);
49+
| ^^^^ expected `char`, found `u32`
50+
51+
error: aborting due to 5 previous errors
52+
53+
For more information about this error, try `rustc --explain E0308`.

src/test/ui/match/match-type-err-first-arm.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ LL | fn test_func1(n: i32) -> i32 {
66
LL | match n {
77
LL | 12 => 'b',
88
| ^^^ expected `i32`, found `char`
9+
|
10+
help: you can cast a `char` to an `i32`, since a `char` always occupies 4 bytes
11+
|
12+
LL | 12 => 'b' as i32,
13+
| ++++++
914

1015
error[E0308]: `match` arms have incompatible types
1116
--> $DIR/match-type-err-first-arm.rs:18:14

src/test/ui/proc-macro/macro-brackets.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0308]: mismatched types
33
|
44
LL | id![static X: u32 = 'a';];
55
| ^^^ expected `u32`, found `char`
6+
|
7+
help: you can cast a `char` to a `u32`, since a `char` always occupies 4 bytes
8+
|
9+
LL | id![static X: u32 = 'a' as u32;];
10+
| ++++++
611

712
error: aborting due to previous error
813

0 commit comments

Comments
 (0)