right_shift.carbon 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // EXTRA-ARGS: --no-dump-sem-ir
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/builtins/int/right_shift.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/right_shift.carbon
  12. // --- int_right_shift.carbon
  13. fn RightShift(a: i32, b: i32) -> i32 = "int.right_shift";
  14. var arr: [i32; RightShift(22, 2)];
  15. let arr_p: [i32; 5]* = &arr;
  16. fn RuntimeCallIsValid(a: i32, b: i32) -> i32 {
  17. return RightShift(a, b);
  18. }
  19. // TODO: Test mixed types for LHS and RHS.
  20. // --- arith_shift.carbon
  21. // TODO: Also test unsigned / logical right shift.
  22. package ArithShift;
  23. fn RightShift(a: i32, b: i32) -> i32 = "int.right_shift";
  24. fn Negate(a: i32) -> i32 = "int.snegate";
  25. // -1 >> 1 is -1.
  26. var arr1: [i32; Negate(RightShift(Negate(1), 1))];
  27. let arr1_p: [i32; 1]* = &arr1;
  28. // -10 >> 2 is -3.
  29. var arr2: [i32; Negate(RightShift(Negate(10), 2))];
  30. let arr2_p: [i32; 3]* = &arr2;
  31. // --- fail_bad_shift.carbon
  32. package BadShift;
  33. fn RightShift(a: i32, b: i32) -> i32 = "int.right_shift";
  34. fn Negate(a: i32) -> i32 = "int.snegate";
  35. // Shift greater than size is disallowed.
  36. let size_1: i32 = RightShift(1, 31);
  37. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:19: error: shift distance not in range [0, 32) in 1 >> 32 [CompileTimeShiftOutOfRange]
  38. // CHECK:STDERR: let size_2: i32 = RightShift(1, 32);
  39. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  40. // CHECK:STDERR:
  41. let size_2: i32 = RightShift(1, 32);
  42. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:19: error: shift distance not in range [0, 32) in 1 >> 33 [CompileTimeShiftOutOfRange]
  43. // CHECK:STDERR: let size_3: i32 = RightShift(1, 33);
  44. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  45. // CHECK:STDERR:
  46. let size_3: i32 = RightShift(1, 33);
  47. // Negative shifts aren't allowed either.
  48. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+3]]:21: error: shift distance not in range [0, 32) in 1 >> -1 [CompileTimeShiftOutOfRange]
  49. // CHECK:STDERR: let negative: i32 = RightShift(1, Negate(1));
  50. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  51. let negative: i32 = RightShift(1, Negate(1));