Skip to content

Commit e55113e

Browse files
MahadMuhammadphilberty
authored andcommitted
gccrs: [E0054/E0604/E0620/E0606] TypeCasting ErrorCodes
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 <[email protected]>
1 parent 152eb85 commit e55113e

File tree

6 files changed

+47
-14
lines changed

6 files changed

+47
-14
lines changed

gcc/rust/typecheck/rust-casts.cc

+24-2
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,33 @@ TypeCastRules::check_ptr_ptr_cast ()
300300
void
301301
TypeCastRules::emit_cast_error () const
302302
{
303-
// error[E0604]
304303
rich_location r (line_table, locus);
305304
r.add_range (from.get_locus ());
306305
r.add_range (to.get_locus ());
307-
rust_error_at (r, ErrorCode::E0054, "invalid cast %<%s%> to %<%s%>",
306+
ErrorCode error_code;
307+
std::string error_msg;
308+
switch (to.get_ty ()->get_kind ())
309+
{
310+
case TyTy::TypeKind::BOOL:
311+
error_msg = "cannot cast %qs as %qs";
312+
error_code = ErrorCode::E0054;
313+
break;
314+
case TyTy::TypeKind::CHAR:
315+
error_msg
316+
+= "cannot cast %qs as %qs, only %<u8%> can be cast as %<char%>";
317+
error_code = ErrorCode::E0604;
318+
break;
319+
case TyTy::TypeKind::SLICE:
320+
error_msg = "cast to unsized type: %qs as %qs";
321+
error_code = ErrorCode::E0620;
322+
break;
323+
324+
default:
325+
error_msg = "casting %qs as %qs is invalid";
326+
error_code = ErrorCode::E0606;
327+
break;
328+
}
329+
rust_error_at (r, error_code, error_msg.c_str (),
308330
from.get_ty ()->get_name ().c_str (),
309331
to.get_ty ()->get_name ().c_str ());
310332
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
let x = 5;
3+
let x_is_nonzero = x as bool; // { dg-error "cannot cast .<integer>. as .bool." }
4+
5+
0u32 as char; // { dg-error "cannot cast .u32. as .char., only .u8. can be cast as .char." }
6+
7+
let x = &[1_usize, 2] as [usize]; // { dg-error "cast to unsized type: .& .usize:CAPACITY.. as ..usize.." }
8+
9+
let a = &0u8; // Here, `x` is a `&u8`.
10+
let y: u32 = a as u32; // { dg-error "casting .& u8. as .u32. is invalid" }
11+
}

gcc/testsuite/rust/compile/bad_as_bool_char.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ pub fn main ()
22
{
33
let t = true;
44
let f = false;
5-
let fone = t as f32; // { dg-error "invalid cast" }
6-
let fzero = f as f64; // { dg-error "invalid cast" }
5+
let fone = t as f32; // { dg-error "casting .bool. as .f32. is invalid" }
6+
let fzero = f as f64; // { dg-error "casting .bool. as .f64. is invalid" }
77

8-
let nb = 0u8 as bool; // { dg-error "invalid cast .u8. to .bool. \\\[E0054\\\]" }
9-
let nc = true as char; // { dg-error "invalid cast" }
8+
let nb = 0u8 as bool; // { dg-error "cannot cast .u8. as .bool." }
9+
let nc = true as char; // { dg-error "cannot cast .bool. as .char., only .u8. can be cast as .char." }
1010

1111
let a = 'a';
1212
let b = 'b';
13-
let fa = a as f32; // { dg-error "invalid cast" }
14-
let bb = b as bool; // { dg-error "invalid cast .char. to .bool. \\\[E0054\\\]" }
13+
let fa = a as f32; // { dg-error "casting .char. as .f32. is invalid" }
14+
let bb = b as bool; // { dg-error "cannot cast .char. as .bool." }
1515

1616
let t32: u32 = 33;
17-
let ab = t32 as char; // { dg-error "invalid cast" }
17+
let ab = t32 as char; // { dg-error "cannot cast .u32. as .char., only .u8. can be cast as .char." }
1818
}

gcc/testsuite/rust/compile/cast1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
22
let a: i32 = 123;
33
let b = a as char;
4-
// { dg-error "invalid cast .i32. to .char." "" { target *-*-* } .-1 }
4+
// { dg-error "cannot cast .i32. as .char., only .u8. can be cast as .char." "" { target *-*-* } .-1 }
55
}

gcc/testsuite/rust/compile/cast4.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
22
let a: i32 = 123;
33
let u = a as bool;
4-
// { dg-error "invalid cast .i32. to .bool." "" { target *-*-* } .-1 }
4+
// { dg-error "cannot cast .i32. as .bool." "" { target *-*-* } .-1 }
55
}

gcc/testsuite/rust/compile/cast5.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
fn main() {
22
const A: char = 0x1F888 as char;
3-
// { dg-error "invalid cast .<integer>. to .char." "" { target *-*-* } .-1 }
3+
// { dg-error "cannot cast .<integer>. as .char., only .u8. can be cast as .char." "" { target *-*-* } .-1 }
44
const B: char = 129160 as char;
5-
// { dg-error "invalid cast .<integer>. to .char." "" { target *-*-* } .-1 }
5+
// { dg-error "cannot cast .<integer>. as .char., only .u8. can be cast as .char." "" { target *-*-* } .-1 }
66
const C: i32 = 42;
77
const D: char = C as char;
8-
// { dg-error "invalid cast .i32. to .char." "" { target *-*-* } .-1 }
8+
// { dg-error "cannot cast .i32. as .char., only .u8. can be cast as .char." "" { target *-*-* } .-1 }
99
const E: char = '\u{01F888}';
1010
const F: u8 = 42;
1111
const G: char= F as char;

0 commit comments

Comments
 (0)