eq.carbon 4.7 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/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/eq.carbon
  12. // --- int_eq.carbon
  13. library "[[@TEST_NAME]]";
  14. fn Eq(a: i32, b: i32) -> bool = "int.eq";
  15. class True {}
  16. class False {}
  17. fn F(true_: True, false_: False) {
  18. true_ as (if Eq(1, 1) then True else False);
  19. false_ as (if Eq(1, 2) then True else False);
  20. }
  21. fn RuntimeCallIsValid(a: i32, b: i32) -> bool {
  22. //@dump-sem-ir-begin
  23. return Eq(a, b);
  24. //@dump-sem-ir-end
  25. }
  26. // --- fail_bad_decl.carbon
  27. library "[[@TEST_NAME]]";
  28. // CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: error: invalid signature for builtin function "int.eq" [InvalidBuiltinSignature]
  29. // CHECK:STDERR: fn WrongResult(a: i32, b: i32) -> i32 = "int.eq";
  30. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  31. // CHECK:STDERR:
  32. fn WrongResult(a: i32, b: i32) -> i32 = "int.eq";
  33. // --- literal.carbon
  34. library "[[@TEST_NAME]]";
  35. fn Eq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.eq";
  36. class Expect(B:! bool) {}
  37. fn Test(B:! bool) -> Expect(B) { return {}; }
  38. fn F() {
  39. Test(Eq(5, 5)) as Expect(true);
  40. Test(Eq(5, 6)) as Expect(false);
  41. Test(Eq(-1, -1)) as Expect(true);
  42. Test(Eq(-1, 1)) as Expect(false);
  43. }
  44. // --- mixed.carbon
  45. library "[[@TEST_NAME]]";
  46. fn Eq(a: Core.IntLiteral(), b: i32) -> bool = "int.eq";
  47. class Expect(B:! bool) {}
  48. fn Test(B:! bool) -> Expect(B) { return {}; }
  49. fn F() {
  50. Test(Eq(5, 5)) as Expect(true);
  51. Test(Eq(5, 6)) as Expect(false);
  52. Test(Eq(-1, -1)) as Expect(true);
  53. Test(Eq(-1, 1)) as Expect(false);
  54. }
  55. // --- fail_runtime_literal.carbon
  56. library "[[@TEST_NAME]]";
  57. fn Eq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.eq";
  58. fn Test(n: Core.IntLiteral()) {
  59. // OK
  60. Eq(1, 1);
  61. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE+7]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  62. // CHECK:STDERR: Eq(n, 1);
  63. // CHECK:STDERR: ^~~~~~~~
  64. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE-8]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  65. // CHECK:STDERR: fn Eq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.eq";
  66. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. // CHECK:STDERR:
  68. Eq(n, 1);
  69. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE+7]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  70. // CHECK:STDERR: Eq(1, n);
  71. // CHECK:STDERR: ^~~~~~~~
  72. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE-16]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  73. // CHECK:STDERR: fn Eq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.eq";
  74. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  75. // CHECK:STDERR:
  76. Eq(1, n);
  77. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE+7]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  78. // CHECK:STDERR: Eq(n, n);
  79. // CHECK:STDERR: ^~~~~~~~
  80. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE-24]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  81. // CHECK:STDERR: fn Eq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.eq";
  82. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. // CHECK:STDERR:
  84. Eq(n, n);
  85. }
  86. // CHECK:STDOUT: --- int_eq.carbon
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: constants {
  89. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  90. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  91. // CHECK:STDOUT: %Eq.type: type = fn_type @Eq [concrete]
  92. // CHECK:STDOUT: %Eq: %Eq.type = struct_value () [concrete]
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: imports {
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: fn @RuntimeCallIsValid(%a.param: %i32, %b.param: %i32) -> bool {
  99. // CHECK:STDOUT: !entry:
  100. // CHECK:STDOUT: %Eq.ref: %Eq.type = name_ref Eq, file.%Eq.decl [concrete = constants.%Eq]
  101. // CHECK:STDOUT: %a.ref: %i32 = name_ref a, %a
  102. // CHECK:STDOUT: %b.ref: %i32 = name_ref b, %b
  103. // CHECK:STDOUT: %Eq.call: init bool = call %Eq.ref(%a.ref, %b.ref)
  104. // CHECK:STDOUT: return %Eq.call to %return
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT: