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