complement.carbon 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/complement.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/complement.carbon
  12. // --- int_complement.carbon
  13. library "[[@TEST_NAME]]";
  14. fn Complement(a: i32) -> i32 = "int.complement";
  15. fn And(a: i32, b: i32) -> i32 = "int.and";
  16. var arr: [i32; And(Complement(0x123456), 0xFFFFFF)];
  17. let arr_p: [i32; 0xEDCBA9]* = &arr;
  18. fn RuntimeCallIsValid(a: i32) -> i32 {
  19. return Complement(a);
  20. }
  21. // --- literal.carbon
  22. library "[[@TEST_NAME]]";
  23. fn Complement(a: Core.IntLiteral()) -> Core.IntLiteral() = "int.complement";
  24. class Expect(N:! Core.IntLiteral()) {}
  25. fn Test(N:! Core.IntLiteral()) -> Expect(N) { return {}; }
  26. fn F() {
  27. Test(Complement(0)) as Expect(-1);
  28. Test(Complement(1)) as Expect(-2);
  29. Test(Complement(-1)) as Expect(0);
  30. Test(Complement(-0x7FFF_FFFF_FFFF_FFFF)) as Expect(0x7FFF_FFFF_FFFF_FFFE);
  31. Test(Complement(-0x8000_0000_0000_0000)) as Expect(0x7FFF_FFFF_FFFF_FFFF);
  32. Test(Complement(0x7FFF_FFFF_FFFF_FFFF)) as Expect(-0x8000_0000_0000_0000);
  33. Test(Complement(0x8000_0000_0000_0000)) as Expect(-0x8000_0000_0000_0001);
  34. }
  35. // --- fail_literal_runtime.carbon
  36. library "[[@TEST_NAME]]";
  37. fn Complement(a: Core.IntLiteral()) -> Core.IntLiteral() = "int.complement";
  38. fn F(a: Core.IntLiteral()) -> Core.IntLiteral() {
  39. // CHECK:STDERR: fail_literal_runtime.carbon:[[@LINE+7]]:10: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  40. // CHECK:STDERR: return Complement(a);
  41. // CHECK:STDERR: ^~~~~~~~~~~~~
  42. // CHECK:STDERR: fail_literal_runtime.carbon:[[@LINE-6]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  43. // CHECK:STDERR: fn Complement(a: Core.IntLiteral()) -> Core.IntLiteral() = "int.complement";
  44. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  45. // CHECK:STDERR:
  46. return Complement(a);
  47. }