left_shift.carbon 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/left_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/left_shift.carbon
  12. // --- int_left_shift.carbon
  13. fn LeftShift(a: i32, b: i32) -> i32 = "int.left_shift";
  14. var arr: [i32; LeftShift(5, 2)];
  15. let arr_p: [i32; 20]* = &arr;
  16. fn RuntimeCallIsValid(a: i32, b: i32) -> i32 {
  17. return LeftShift(a, b);
  18. }
  19. // TODO: Test mixed types for LHS and RHS.
  20. // --- fail_bad_shift.carbon
  21. package BadShift;
  22. fn LeftShift(a: i32, b: i32) -> i32 = "int.left_shift";
  23. fn Negate(a: i32) -> i32 = "int.snegate";
  24. // Shift greater than size is disallowed.
  25. let size_1: i32 = LeftShift(1, 31);
  26. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:19: error: shift distance not in range [0, 32) in 1 << 32 [CompileTimeShiftOutOfRange]
  27. // CHECK:STDERR: let size_2: i32 = LeftShift(1, 32);
  28. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  29. // CHECK:STDERR:
  30. let size_2: i32 = LeftShift(1, 32);
  31. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:19: error: shift distance not in range [0, 32) in 1 << 33 [CompileTimeShiftOutOfRange]
  32. // CHECK:STDERR: let size_3: i32 = LeftShift(1, 33);
  33. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  34. // CHECK:STDERR:
  35. let size_3: i32 = LeftShift(1, 33);
  36. // Overflow is allowed if the shift distance is in bounds.
  37. let overflow_1: i32 = LeftShift(1000, 31);
  38. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:23: error: shift distance not in range [0, 32) in 1000 << 32 [CompileTimeShiftOutOfRange]
  39. // CHECK:STDERR: let overflow_2: i32 = LeftShift(1000, 32);
  40. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
  41. // CHECK:STDERR:
  42. let overflow_2: i32 = LeftShift(1000, 32);
  43. // Oversize shifts aren't allowed even if there's no overflow.
  44. let no_overflow_1: i32 = LeftShift(0, 31);
  45. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:26: error: shift distance not in range [0, 32) in 0 << 32 [CompileTimeShiftOutOfRange]
  46. // CHECK:STDERR: let no_overflow_2: i32 = LeftShift(0, 32);
  47. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  48. // CHECK:STDERR:
  49. let no_overflow_2: i32 = LeftShift(0, 32);
  50. // Negative shifts aren't allowed either.
  51. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+3]]:21: error: shift distance not in range [0, 32) in 1 << -1 [CompileTimeShiftOutOfRange]
  52. // CHECK:STDERR: let negative: i32 = LeftShift(1, Negate(1));
  53. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  54. let negative: i32 = LeftShift(1, Negate(1));