diff options
author | Philip Herron <herron.philip@googlemail.com> | 2024-12-02 13:54:29 +0000 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2024-12-02 17:31:30 +0000 |
commit | 0d00f79134c9210e4a0df21a1fc18e52f48bff0d (patch) | |
tree | eb30e08cd472aeb1a83bfd4f3fe8daa1f2cef03b /gcc/rust/rust-gcc.cc | |
parent | db3d8fb22c8d10a3687742a85de85242a96ac1ba (diff) | |
download | gcc-0d00f79134c9210e4a0df21a1fc18e52f48bff0d.zip gcc-0d00f79134c9210e4a0df21a1fc18e52f48bff0d.tar.gz gcc-0d00f79134c9210e4a0df21a1fc18e52f48bff0d.tar.bz2 |
gccrs: add checks for division by zero and left shift overflow
These are ported from the c-family code c-warn.cc and c/c-typchk.cc
Fixes Rust-GCC#2394
gcc/rust/ChangeLog:
* backend/rust-constexpr.cc (eval_store_expression): check for null
(eval_call_expression): remove bad warning
* rust-gcc.cc (arithmetic_or_logical_expression): add warnings
gcc/testsuite/ChangeLog:
* rust/compile/issue-2394.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Diffstat (limited to 'gcc/rust/rust-gcc.cc')
-rw-r--r-- | gcc/rust/rust-gcc.cc | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/gcc/rust/rust-gcc.cc b/gcc/rust/rust-gcc.cc index 0313102..0590479 100644 --- a/gcc/rust/rust-gcc.cc +++ b/gcc/rust/rust-gcc.cc @@ -1106,6 +1106,17 @@ arithmetic_or_logical_expression (ArithmeticOrLogicalOperator op, tree left, if (floating_point && extended_type != NULL_TREE) ret = convert (original_type, ret); + if (op == ArithmeticOrLogicalOperator::DIVIDE + && (integer_zerop (right) || fixed_zerop (right))) + { + rust_error_at (location, "division by zero"); + } + else if (op == ArithmeticOrLogicalOperator::LEFT_SHIFT + && (compare_tree_int (right, TYPE_PRECISION (TREE_TYPE (ret))) >= 0)) + { + rust_error_at (location, "left shift count >= width of type"); + } + return ret; } |