eq.carbon 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/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. return Eq(a, b);
  23. }
  24. // --- fail_bad_decl.carbon
  25. package FailBadDecl;
  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. }