Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[E0054-E0604-E0620-E0606] TypeCasting ErrorCodes #2528

Merged
merged 1 commit into from
Aug 10, 2023

Conversation

MahadMuhammad
Copy link
Contributor

Added All Casting ErrorCodes & Updated error message accordingly:

  • Added errorcodes according to different conditions and updated error message according to type casting type.

These are all the error codes related to casting in rustc.


Added ErrorCodes:


Code Tested:

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" }
}

Output:

/home/mahad/Desktop/mahad/gccrs/gcc/testsuite/rust/compile/all-cast.rs:3:24: error: cannot cast ‘<integer>’ as ‘bool’ [E0054]
    3 |     let x_is_nonzero = x as bool; // { dg-error "cannot cast .<integer>. as .bool." }
      |                        ^    ~~~~
/home/mahad/Desktop/mahad/gccrs/gcc/testsuite/rust/compile/all-cast.rs:5:5: error: cannot cast ‘u32’ as ‘char’, only ‘u8’ can be cast as ‘char’ [E0604]
    5 |     0u32 as char; // { dg-error "cannot cast .u32. as .char., only .u8. can be cast as .char." }
      |     ^~~~    ~~~~
/home/mahad/Desktop/mahad/gccrs/gcc/testsuite/rust/compile/all-cast.rs:7:13: error: cast to unsized type: ‘& [usize:CAPACITY]’ as ‘[usize]’ [E0620]
    7 |     let x = &[1_usize, 2] as [usize]; // { dg-error "cast to unsized type: .& .usize:CAPACITY.. as ..usize.." }
      |             ^                ~
/home/mahad/Desktop/mahad/gccrs/gcc/testsuite/rust/compile/all-cast.rs:10:18: error: casting ‘& u8’ as ‘u32’ is invalid [E0606]
   10 |     let y: u32 = a as u32; // { dg-error "casting .& u8. as .u32. is invalid" }
      |                  ^    ~~~

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.

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>
@P-E-P P-E-P added enhancement diagnostic diagnostic static analysis labels Aug 7, 2023
@philberty philberty added this to the GCC 14 Stage 3 milestone Aug 10, 2023
Copy link
Member

@philberty philberty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM really good job on this one!!! Really nice to see specific errors for specific cases being possible with the new type checking code

@philberty philberty added this pull request to the merge queue Aug 10, 2023
Merged via the queue into Rust-GCC:master with commit e55113e Aug 10, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
diagnostic diagnostic static analysis enhancement
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants