]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #91245 - cameron1024:suggest-i32-u32-char-cast, r=nagisa
authorMatthias Krüger <matthias.krueger@famsik.de>
Wed, 8 Dec 2021 22:18:03 +0000 (23:18 +0100)
committerGitHub <noreply@github.com>
Wed, 8 Dec 2021 22:18:03 +0000 (23:18 +0100)
suggest casting between i/u32 and char

As discussed in https://github.com/rust-lang/rust/issues/91063 , this adds a suggestion for converting between i32/u32 <-> char with `as`, and a short explanation for why this is safe

compiler/rustc_typeck/src/check/demand.rs
src/test/ui/cast/cast-int-to-char.rs [new file with mode: 0644]
src/test/ui/cast/cast-int-to-char.stderr [new file with mode: 0644]
src/test/ui/match/match-type-err-first-arm.stderr
src/test/ui/proc-macro/macro-brackets.stderr

index b210c78cae007989d7ea08f895bebc42dab1da13..b7e276b69656f84d397f0c74e6d0f67f618868f8 100644 (file)
@@ -1264,6 +1264,18 @@ pub fn check_for_cast(
                 }
                 true
             }
+            (
+                &ty::Uint(ty::UintTy::U32 | ty::UintTy::U64 | ty::UintTy::U128)
+                | &ty::Int(ty::IntTy::I32 | ty::IntTy::I64 | ty::IntTy::I128),
+                &ty::Char,
+            ) => {
+                err.multipart_suggestion_verbose(
+                    &format!("{}, since a `char` always occupies 4 bytes", cast_msg,),
+                    cast_suggestion,
+                    Applicability::MachineApplicable,
+                );
+                true
+            }
             _ => false,
         }
     }
diff --git a/src/test/ui/cast/cast-int-to-char.rs b/src/test/ui/cast/cast-int-to-char.rs
new file mode 100644 (file)
index 0000000..3799569
--- /dev/null
@@ -0,0 +1,9 @@
+fn foo<T>(_t: T) {}
+
+fn main() {
+    foo::<u32>('0');  //~ ERROR
+    foo::<i32>('0');  //~ ERROR
+    foo::<u64>('0');  //~ ERROR
+    foo::<i64>('0');  //~ ERROR
+    foo::<char>(0u32);  //~ ERROR
+}
diff --git a/src/test/ui/cast/cast-int-to-char.stderr b/src/test/ui/cast/cast-int-to-char.stderr
new file mode 100644 (file)
index 0000000..55b9462
--- /dev/null
@@ -0,0 +1,53 @@
+error[E0308]: mismatched types
+  --> $DIR/cast-int-to-char.rs:4:16
+   |
+LL |     foo::<u32>('0');
+   |                ^^^ expected `u32`, found `char`
+   |
+help: you can cast a `char` to a `u32`, since a `char` always occupies 4 bytes
+   |
+LL |     foo::<u32>('0' as u32);
+   |                    ++++++
+
+error[E0308]: mismatched types
+  --> $DIR/cast-int-to-char.rs:5:16
+   |
+LL |     foo::<i32>('0');
+   |                ^^^ expected `i32`, found `char`
+   |
+help: you can cast a `char` to an `i32`, since a `char` always occupies 4 bytes
+   |
+LL |     foo::<i32>('0' as i32);
+   |                    ++++++
+
+error[E0308]: mismatched types
+  --> $DIR/cast-int-to-char.rs:6:16
+   |
+LL |     foo::<u64>('0');
+   |                ^^^ expected `u64`, found `char`
+   |
+help: you can cast a `char` to a `u64`, since a `char` always occupies 4 bytes
+   |
+LL |     foo::<u64>('0' as u64);
+   |                    ++++++
+
+error[E0308]: mismatched types
+  --> $DIR/cast-int-to-char.rs:7:16
+   |
+LL |     foo::<i64>('0');
+   |                ^^^ expected `i64`, found `char`
+   |
+help: you can cast a `char` to an `i64`, since a `char` always occupies 4 bytes
+   |
+LL |     foo::<i64>('0' as i64);
+   |                    ++++++
+
+error[E0308]: mismatched types
+  --> $DIR/cast-int-to-char.rs:8:17
+   |
+LL |     foo::<char>(0u32);
+   |                 ^^^^ expected `char`, found `u32`
+
+error: aborting due to 5 previous errors
+
+For more information about this error, try `rustc --explain E0308`.
index fd489afa84db4ed5f2dcb156003113673956015a..1cfe7ce1ed726f5416ad4ef3fa78e2f842b06907 100644 (file)
@@ -6,6 +6,11 @@ LL | fn test_func1(n: i32) -> i32 {
 LL |     match n {
 LL |         12 => 'b',
    |               ^^^ expected `i32`, found `char`
+   |
+help: you can cast a `char` to an `i32`, since a `char` always occupies 4 bytes
+   |
+LL |         12 => 'b' as i32,
+   |                   ++++++
 
 error[E0308]: `match` arms have incompatible types
   --> $DIR/match-type-err-first-arm.rs:18:14
index 9aaf612eb54a13854f3abdb3a6ef0d3d58bef35a..d3516375291b97546ee218f4f5a1dd58ab6ceb74 100644 (file)
@@ -3,6 +3,11 @@ error[E0308]: mismatched types
    |
 LL | id![static X: u32 = 'a';];
    |                     ^^^ expected `u32`, found `char`
+   |
+help: you can cast a `char` to a `u32`, since a `char` always occupies 4 bytes
+   |
+LL | id![static X: u32 = 'a' as u32;];
+   |                         ++++++
 
 error: aborting due to previous error