]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: [E0054/E0604/E0620/E0606] TypeCasting ErrorCodes
authorMuhammad Mahad <mahadtxt@gmail.com>
Sat, 5 Aug 2023 12:28:45 +0000 (17:28 +0500)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 18:00:29 +0000 (19:00 +0100)
Added errorcodes according to different conditions
and updated error message according to type casting
type.

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::emit_cast_error):
Refactored ErrorCodes & error messages.

gcc/testsuite/ChangeLog:

* rust/compile/bad_as_bool_char.rs:
Updated comment to pass test case.
* rust/compile/cast1.rs: likewise.
* rust/compile/cast4.rs: likewise.
* rust/compile/cast5.rs: likewise.
* rust/compile/all-cast.rs: New test for all error codes.

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
gcc/rust/typecheck/rust-casts.cc
gcc/testsuite/rust/compile/all-cast.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/bad_as_bool_char.rs
gcc/testsuite/rust/compile/cast1.rs
gcc/testsuite/rust/compile/cast4.rs
gcc/testsuite/rust/compile/cast5.rs

index 9300e7a7f71b082b4acc4f37ca97c821afa768d1..87b31b5820f6be44d5be170e380ce80d068046c6 100644 (file)
@@ -300,11 +300,33 @@ TypeCastRules::check_ptr_ptr_cast ()
 void
 TypeCastRules::emit_cast_error () const
 {
-  // error[E0604]
   rich_location r (line_table, locus);
   r.add_range (from.get_locus ());
   r.add_range (to.get_locus ());
-  rust_error_at (r, ErrorCode::E0054, "invalid cast %<%s%> to %<%s%>",
+  ErrorCode error_code;
+  std::string error_msg;
+  switch (to.get_ty ()->get_kind ())
+    {
+    case TyTy::TypeKind::BOOL:
+      error_msg = "cannot cast %qs as %qs";
+      error_code = ErrorCode::E0054;
+      break;
+    case TyTy::TypeKind::CHAR:
+      error_msg
+       += "cannot cast %qs as %qs, only %<u8%> can be cast as %<char%>";
+      error_code = ErrorCode::E0604;
+      break;
+    case TyTy::TypeKind::SLICE:
+      error_msg = "cast to unsized type: %qs as %qs";
+      error_code = ErrorCode::E0620;
+      break;
+
+    default:
+      error_msg = "casting %qs as %qs is invalid";
+      error_code = ErrorCode::E0606;
+      break;
+    }
+  rust_error_at (r, error_code, error_msg.c_str (),
                 from.get_ty ()->get_name ().c_str (),
                 to.get_ty ()->get_name ().c_str ());
 }
diff --git a/gcc/testsuite/rust/compile/all-cast.rs b/gcc/testsuite/rust/compile/all-cast.rs
new file mode 100644 (file)
index 0000000..fa24373
--- /dev/null
@@ -0,0 +1,11 @@
+fn main() {
+    let x = 5;
+    let x_is_nonzero = x as bool; // { dg-error "cannot cast .<integer>. as .bool." }
+
+    0u32 as char; // { dg-error "cannot cast .u32. as .char., only .u8. can be cast as .char." }
+
+    let x = &[1_usize, 2] as [usize]; // { dg-error "cast to unsized type: .& .usize:CAPACITY.. as ..usize.." }
+
+    let a = &0u8; // Here, `x` is a `&u8`.
+    let y: u32 = a as u32; // { dg-error "casting .& u8. as .u32. is invalid" }
+}
index 9652915fe1170e10add8b4b12234afd15efa256a..1828d2256f29b0e049b4e2daa55ed8f91e934723 100644 (file)
@@ -2,17 +2,17 @@ pub fn main ()
 {
   let t = true;
   let f = false;
-  let fone = t as f32;   // { dg-error "invalid cast" }
-  let fzero = f as f64;  // { dg-error "invalid cast" }
+  let fone = t as f32;   // { dg-error "casting .bool. as .f32. is invalid" }
+  let fzero = f as f64;  // { dg-error "casting .bool. as .f64. is invalid" }
 
-  let nb = 0u8 as bool;  // { dg-error "invalid cast .u8. to .bool. \\\[E0054\\\]" }
-  let nc = true as char; // { dg-error "invalid cast" }
+  let nb = 0u8 as bool;  // { dg-error "cannot cast .u8. as .bool." }
+  let nc = true as char; // { dg-error "cannot cast .bool. as .char., only .u8. can be cast as .char." }
 
   let a = 'a';
   let b = 'b';
-  let fa = a as f32;     // { dg-error "invalid cast" }
-  let bb = b as bool;    // { dg-error "invalid cast .char. to .bool. \\\[E0054\\\]" }
+  let fa = a as f32;     // { dg-error "casting .char. as .f32. is invalid" }
+  let bb = b as bool;    // { dg-error "cannot cast .char. as .bool." }
 
   let t32: u32 = 33;
-  let ab = t32 as char;  // { dg-error "invalid cast" }
+  let ab = t32 as char;  // { dg-error "cannot cast .u32. as .char., only .u8. can be cast as .char." }
 }
index 74c4b1eaac4c773165abb95d7a63aa256814d686..0472d582db84a5990b22232e687c14c6a20e24ba 100644 (file)
@@ -1,5 +1,5 @@
 fn main() {
     let a: i32 = 123;
     let b = a as char;
-    // { dg-error "invalid cast .i32. to .char." "" { target *-*-* } .-1 }
+    // { dg-error "cannot cast .i32. as .char., only .u8. can be cast as .char." "" { target *-*-* } .-1 }
 }
index ab00010e35b1ce6a7d0537dc0a8d52c96627dfbf..22ac0c5acf7edfb4e291a53239ad24b1e75cb891 100644 (file)
@@ -1,5 +1,5 @@
 fn main() {
     let a: i32 = 123;
     let u = a as bool;
-    // { dg-error "invalid cast .i32. to .bool." "" { target *-*-* } .-1 }
+    // { dg-error "cannot cast .i32. as .bool." "" { target *-*-* } .-1 }
 }
index ecc10c1490a68cc21b62607561dd90ee04b9aae7..2e340dd4fe4b7b78c506a7635ee9cb9958451f00 100644 (file)
@@ -1,11 +1,11 @@
 fn main() {
     const A: char = 0x1F888 as char;
-    // { dg-error "invalid cast .<integer>. to .char." "" { target *-*-* } .-1 }
+    // { dg-error "cannot cast .<integer>. as .char., only .u8. can be cast as .char." "" { target *-*-* } .-1 }
     const B: char = 129160 as char;
-    // { dg-error "invalid cast .<integer>. to .char." "" { target *-*-* } .-1 }
+    // { dg-error "cannot cast .<integer>. as .char., only .u8. can be cast as .char." "" { target *-*-* } .-1 }
     const C: i32 = 42;
     const D: char = C as char;
-    // { dg-error "invalid cast .i32. to .char." "" { target *-*-* } .-1 }
+    // { dg-error "cannot cast .i32. as .char., only .u8. can be cast as .char." "" { target *-*-* } .-1 }
     const E: char = '\u{01F888}';
     const F: u8 = 42; 
     const G: char= F as char;