aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorMuhammad Mahad <mahadtxt@gmail.com>2023-08-05 17:28:45 +0500
committerPhilip Herron <philip.herron@embecosm.com>2023-08-10 09:37:44 +0000
commite55113ea2bf0cec2f8436a576ce6f2bcaacd1c27 (patch)
tree2c1bd92a8c1f14bda549938234b12e9438aefb81 /gcc/rust
parent152eb852643c4cfdf753a19f70ccc49d1b602502 (diff)
downloadgcc-e55113ea2bf0cec2f8436a576ce6f2bcaacd1c27.zip
gcc-e55113ea2bf0cec2f8436a576ce6f2bcaacd1c27.tar.gz
gcc-e55113ea2bf0cec2f8436a576ce6f2bcaacd1c27.tar.bz2
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 <mahadtxt@gmail.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/typecheck/rust-casts.cc26
1 files changed, 24 insertions, 2 deletions
diff --git a/gcc/rust/typecheck/rust-casts.cc b/gcc/rust/typecheck/rust-casts.cc
index 75b2872..7439deb 100644
--- a/gcc/rust/typecheck/rust-casts.cc
+++ b/gcc/rust/typecheck/rust-casts.cc
@@ -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 ());
}