less_eq.carbon 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/primitives.carbon
  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. //@dump-sem-ir-begin
  27. return LessEq(a, b);
  28. //@dump-sem-ir-end
  29. }
  30. // --- literal.carbon
  31. library "[[@TEST_NAME]]";
  32. fn LessEq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.less_eq";
  33. class Expect(B:! bool) {}
  34. fn Test(B:! bool) -> Expect(B) { return {}; }
  35. fn F() {
  36. Test(LessEq(5, 5)) as Expect(true);
  37. Test(LessEq(5, 6)) as Expect(true);
  38. Test(LessEq(6, 5)) as Expect(false);
  39. Test(LessEq(-1, -1)) as Expect(true);
  40. Test(LessEq(-1, 1)) as Expect(true);
  41. Test(LessEq(1, -1)) as Expect(false);
  42. }
  43. // --- mixed.carbon
  44. library "[[@TEST_NAME]]";
  45. fn LessEq(a: Core.IntLiteral(), b: i32) -> bool = "int.less_eq";
  46. class Expect(B:! bool) {}
  47. fn Test(B:! bool) -> Expect(B) { return {}; }
  48. fn F() {
  49. Test(LessEq(5, 5)) as Expect(true);
  50. Test(LessEq(5, 6)) as Expect(true);
  51. Test(LessEq(6, 5)) as Expect(false);
  52. Test(LessEq(-1, -1)) as Expect(true);
  53. Test(LessEq(-1, 1)) as Expect(true);
  54. Test(LessEq(1, -1)) as Expect(false);
  55. }
  56. // --- fail_runtime_literal.carbon
  57. library "[[@TEST_NAME]]";
  58. fn LessEq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.less_eq";
  59. fn Test(n: Core.IntLiteral()) {
  60. // OK
  61. LessEq(1, 1);
  62. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE+7]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  63. // CHECK:STDERR: LessEq(n, 1);
  64. // CHECK:STDERR: ^~~~~~~~~~~~
  65. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE-8]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  66. // CHECK:STDERR: fn LessEq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.less_eq";
  67. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  68. // CHECK:STDERR:
  69. LessEq(n, 1);
  70. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE+7]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  71. // CHECK:STDERR: LessEq(1, n);
  72. // CHECK:STDERR: ^~~~~~~~~~~~
  73. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE-16]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  74. // CHECK:STDERR: fn LessEq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.less_eq";
  75. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  76. // CHECK:STDERR:
  77. LessEq(1, n);
  78. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE+7]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  79. // CHECK:STDERR: LessEq(n, n);
  80. // CHECK:STDERR: ^~~~~~~~~~~~
  81. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE-24]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  82. // CHECK:STDERR: fn LessEq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.less_eq";
  83. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  84. // CHECK:STDERR:
  85. LessEq(n, n);
  86. }
  87. // CHECK:STDOUT: --- int_less_eq.carbon
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: constants {
  90. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  91. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  92. // CHECK:STDOUT: %LessEq.type: type = fn_type @LessEq [concrete]
  93. // CHECK:STDOUT: %LessEq: %LessEq.type = struct_value () [concrete]
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: imports {
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: fn @RuntimeCallIsValid(%a.param: %i32, %b.param: %i32) -> bool {
  100. // CHECK:STDOUT: !entry:
  101. // CHECK:STDOUT: %LessEq.ref: %LessEq.type = name_ref LessEq, file.%LessEq.decl [concrete = constants.%LessEq]
  102. // CHECK:STDOUT: %a.ref: %i32 = name_ref a, %a
  103. // CHECK:STDOUT: %b.ref: %i32 = name_ref b, %b
  104. // CHECK:STDOUT: %int.less_eq: init bool = call %LessEq.ref(%a.ref, %b.ref)
  105. // CHECK:STDOUT: %.loc20_22.1: bool = value_of_initializer %int.less_eq
  106. // CHECK:STDOUT: %.loc20_22.2: bool = converted %int.less_eq, %.loc20_22.1
  107. // CHECK:STDOUT: return %.loc20_22.2
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT: