less_eq.carbon 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/less_eq.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/less_eq.carbon
  12. // --- int_less_eq.carbon
  13. library "[[@TEST_NAME]]";
  14. fn LessEq(a: i32, b: i32) -> bool = "int.less_eq";
  15. fn Negate(a: i32) -> i32 = "int.snegate";
  16. class True {}
  17. class False {}
  18. fn F(true_: True, false_: False) {
  19. true_ as (if LessEq(1, 2) then True else False);
  20. true_ as (if LessEq(1, 1) then True else False);
  21. false_ as (if LessEq(1, 0) then True else False);
  22. true_ as (if LessEq(Negate(1), 0) then True else False);
  23. false_ as (if LessEq(0, Negate(1)) then True else False);
  24. }
  25. fn RuntimeCallIsValid(a: i32, b: i32) -> bool {
  26. return LessEq(a, b);
  27. }
  28. // --- literal.carbon
  29. library "[[@TEST_NAME]]";
  30. fn LessEq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.less_eq";
  31. class Expect(B:! bool) {}
  32. fn Test(B:! bool) -> Expect(B) { return {}; }
  33. fn F() {
  34. Test(LessEq(5, 5)) as Expect(true);
  35. Test(LessEq(5, 6)) as Expect(true);
  36. Test(LessEq(6, 5)) as Expect(false);
  37. Test(LessEq(-1, -1)) as Expect(true);
  38. Test(LessEq(-1, 1)) as Expect(true);
  39. Test(LessEq(1, -1)) as Expect(false);
  40. }
  41. // --- mixed.carbon
  42. library "[[@TEST_NAME]]";
  43. fn LessEq(a: Core.IntLiteral(), b: i32) -> bool = "int.less_eq";
  44. class Expect(B:! bool) {}
  45. fn Test(B:! bool) -> Expect(B) { return {}; }
  46. fn F() {
  47. Test(LessEq(5, 5)) as Expect(true);
  48. Test(LessEq(5, 6)) as Expect(true);
  49. Test(LessEq(6, 5)) as Expect(false);
  50. Test(LessEq(-1, -1)) as Expect(true);
  51. Test(LessEq(-1, 1)) as Expect(true);
  52. Test(LessEq(1, -1)) as Expect(false);
  53. }
  54. // --- fail_runtime_literal.carbon
  55. library "[[@TEST_NAME]]";
  56. fn LessEq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.less_eq";
  57. fn Test(n: Core.IntLiteral()) {
  58. // OK
  59. LessEq(1, 1);
  60. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE+7]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  61. // CHECK:STDERR: LessEq(n, 1);
  62. // CHECK:STDERR: ^~~~~~~~~~~~
  63. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE-8]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  64. // CHECK:STDERR: fn LessEq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.less_eq";
  65. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  66. // CHECK:STDERR:
  67. LessEq(n, 1);
  68. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE+7]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  69. // CHECK:STDERR: LessEq(1, n);
  70. // CHECK:STDERR: ^~~~~~~~~~~~
  71. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE-16]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  72. // CHECK:STDERR: fn LessEq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.less_eq";
  73. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  74. // CHECK:STDERR:
  75. LessEq(1, n);
  76. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE+7]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  77. // CHECK:STDERR: LessEq(n, n);
  78. // CHECK:STDERR: ^~~~~~~~~~~~
  79. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE-24]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  80. // CHECK:STDERR: fn LessEq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.less_eq";
  81. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  82. // CHECK:STDERR:
  83. LessEq(n, n);
  84. }