mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 16:51:05 +01:00
Fixes integer builtins to produce the correct values (and not CHECK-fail) when used on integer literals. Also adds impls to the prelude to use the new builtins to perform operations on integer literals. Perhaps most importantly, this allows directly initializing `i32` values with negative numbers, as the negation operation on integer literals now works. For testing I've added tests for use of literals with one operator in each class (addition, multiplication, ordering, bitwise, etc) for which there are distinct rules or overflow behavior, rather than exhaustively testing all the combinations. This is aimed at finding a good tradeoff between maintainability of the tests and thorough test coverage. Also fixes lowering of heterogeneous shifts and comparisons. These are currently disabled when one of the operands is an integer literal, but we may want to allow that when the integer literal operand has a known constant value.
64 lines
2.1 KiB
Plaintext
64 lines
2.1 KiB
Plaintext
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
// EXTRA-ARGS: --no-dump-sem-ir
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/builtins/int/smod.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/smod.carbon
|
|
|
|
// --- int_div.carbon
|
|
|
|
fn Mod(a: i32, b: i32) -> i32 = "int.smod";
|
|
|
|
var arr: [i32; Mod(5, 3)];
|
|
let arr_p: [i32; 2]* = &arr;
|
|
|
|
fn RuntimeCallIsValid(a: i32, b: i32) -> i32 {
|
|
return Mod(a, b);
|
|
}
|
|
|
|
// --- fail_overflow.carbon
|
|
|
|
package FailOverflow;
|
|
|
|
fn Mod(a: i32, b: i32) -> i32 = "int.smod";
|
|
fn Sub(a: i32, b: i32) -> i32 = "int.ssub";
|
|
fn Negate(a: i32) -> i32 = "int.snegate";
|
|
|
|
// -0x7FFF_FFFF % -1 is OK.
|
|
let a: i32 = Mod(Negate(0x7FFF_FFFF), Negate(1));
|
|
|
|
// -0x8000_0000 % 1 is OK.
|
|
let b: i32 = Mod(Sub(Negate(0x7FFF_FFFF), 1), 1);
|
|
|
|
// -0x8000_0000 / -1 overflows, so -0x8000_0000 % -1 is disallowed, even though
|
|
// its result is representable.
|
|
// CHECK:STDERR: fail_overflow.carbon:[[@LINE+4]]:14: error: integer overflow in calculation `-2147483648 % -1` [CompileTimeIntegerOverflow]
|
|
// CHECK:STDERR: let c: i32 = Mod(Sub(Negate(0x7FFF_FFFF), 1), Negate(1));
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
let c: i32 = Mod(Sub(Negate(0x7FFF_FFFF), 1), Negate(1));
|
|
|
|
// --- fail_div_by_zero.carbon
|
|
|
|
package FailDivByZero;
|
|
|
|
fn Mod(a: i32, b: i32) -> i32 = "int.smod";
|
|
|
|
// Remainder of division by zero is not defined.
|
|
|
|
// CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:14: error: division by zero [CompileTimeDivisionByZero]
|
|
// CHECK:STDERR: let a: i32 = Mod(1, 0);
|
|
// CHECK:STDERR: ^~~~~~~~~
|
|
// CHECK:STDERR:
|
|
let a: i32 = Mod(1, 0);
|
|
|
|
// CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+3]]:14: error: division by zero [CompileTimeDivisionByZero]
|
|
// CHECK:STDERR: let b: i32 = Mod(0, 0);
|
|
// CHECK:STDERR: ^~~~~~~~~
|
|
let b: i32 = Mod(0, 0);
|