enum.carbon 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/full.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/lower/testdata/interop/cpp/enum.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/interop/cpp/enum.carbon
  12. // TODO: Tests marked as `fail_todo_5891_` to fixed as a follow-up of https://github.com/carbon-language/carbon-lang/pull/5891.
  13. // --- enum_member.h
  14. struct C {
  15. enum E : short {
  16. a,
  17. b = 7,
  18. c
  19. };
  20. static void F(E);
  21. };
  22. // --- fail_todo_5891_pass_as_arg.carbon
  23. library "[[@TEST_NAME]]";
  24. import Cpp library "enum_member.h";
  25. fn Pass() {
  26. Cpp.C.F(Cpp.C.a);
  27. // CHECK:STDERR: fail_todo_5891_pass_as_arg.carbon:[[@LINE+7]]:3: error: no matching function for call to `F` [CppOverloadingNoViableFunctionFound]
  28. // CHECK:STDERR: Cpp.C.F(Cpp.C.b);
  29. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  30. // CHECK:STDERR: fail_todo_5891_pass_as_arg.carbon:[[@LINE+4]]:3: note: in call to Cpp function here [InCallToCppFunction]
  31. // CHECK:STDERR: Cpp.C.F(Cpp.C.b);
  32. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  33. // CHECK:STDERR:
  34. Cpp.C.F(Cpp.C.b);
  35. // CHECK:STDERR: fail_todo_5891_pass_as_arg.carbon:[[@LINE+7]]:3: error: no matching function for call to `F` [CppOverloadingNoViableFunctionFound]
  36. // CHECK:STDERR: Cpp.C.F(Cpp.C.c);
  37. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  38. // CHECK:STDERR: fail_todo_5891_pass_as_arg.carbon:[[@LINE+4]]:3: note: in call to Cpp function here [InCallToCppFunction]
  39. // CHECK:STDERR: Cpp.C.F(Cpp.C.c);
  40. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  41. // CHECK:STDERR:
  42. Cpp.C.F(Cpp.C.c);
  43. // CHECK:STDERR: fail_todo_5891_pass_as_arg.carbon:[[@LINE+7]]:3: error: no matching function for call to `F` [CppOverloadingNoViableFunctionFound]
  44. // CHECK:STDERR: Cpp.C.F(Cpp.C.E.a);
  45. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
  46. // CHECK:STDERR: fail_todo_5891_pass_as_arg.carbon:[[@LINE+4]]:3: note: in call to Cpp function here [InCallToCppFunction]
  47. // CHECK:STDERR: Cpp.C.F(Cpp.C.E.a);
  48. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
  49. // CHECK:STDERR:
  50. Cpp.C.F(Cpp.C.E.a);
  51. // CHECK:STDERR: fail_todo_5891_pass_as_arg.carbon:[[@LINE+7]]:3: error: no matching function for call to `F` [CppOverloadingNoViableFunctionFound]
  52. // CHECK:STDERR: Cpp.C.F(Cpp.C.E.b);
  53. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
  54. // CHECK:STDERR: fail_todo_5891_pass_as_arg.carbon:[[@LINE+4]]:3: note: in call to Cpp function here [InCallToCppFunction]
  55. // CHECK:STDERR: Cpp.C.F(Cpp.C.E.b);
  56. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
  57. // CHECK:STDERR:
  58. Cpp.C.F(Cpp.C.E.b);
  59. // CHECK:STDERR: fail_todo_5891_pass_as_arg.carbon:[[@LINE+7]]:3: error: no matching function for call to `F` [CppOverloadingNoViableFunctionFound]
  60. // CHECK:STDERR: Cpp.C.F(Cpp.C.E.c);
  61. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
  62. // CHECK:STDERR: fail_todo_5891_pass_as_arg.carbon:[[@LINE+4]]:3: note: in call to Cpp function here [InCallToCppFunction]
  63. // CHECK:STDERR: Cpp.C.F(Cpp.C.E.c);
  64. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
  65. // CHECK:STDERR:
  66. Cpp.C.F(Cpp.C.E.c);
  67. }
  68. // --- fail_todo_5891_convert.carbon
  69. library "[[@TEST_NAME]]";
  70. import Cpp library "enum_member.h";
  71. fn TakeI16(n: i16);
  72. fn PassEnum() {
  73. TakeI16(Cpp.C.b as i16);
  74. }
  75. fn ConvertEnumToI16(e: Cpp.C.E) {
  76. TakeI16(e as i16);
  77. }
  78. fn ConvertI16ToEnum(n: i16) {
  79. // CHECK:STDERR: fail_todo_5891_convert.carbon:[[@LINE+7]]:3: error: no matching function for call to `F` [CppOverloadingNoViableFunctionFound]
  80. // CHECK:STDERR: Cpp.C.F(n as Cpp.C.E);
  81. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~
  82. // CHECK:STDERR: fail_todo_5891_convert.carbon:[[@LINE+4]]:3: note: in call to Cpp function here [InCallToCppFunction]
  83. // CHECK:STDERR: Cpp.C.F(n as Cpp.C.E);
  84. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~
  85. // CHECK:STDERR:
  86. Cpp.C.F(n as Cpp.C.E);
  87. }
  88. // --- bitmask.h
  89. enum Bits {
  90. A = 1 << 0,
  91. B = 1 << 1,
  92. C = 1 << 2,
  93. };
  94. void Take(Bits b);
  95. // --- fail_todo_use_5891_bitmask.carbon
  96. library "[[@TEST_NAME]]";
  97. import Cpp library "bitmask.h";
  98. // TODO: Once we support interop with C++-defined operators, we should be able
  99. // to use `|` below rather than declaring our own builtin.
  100. fn BitOr(a: Cpp.Bits, b: Cpp.Bits) -> Cpp.Bits = "int.or";
  101. fn Call() {
  102. // CHECK:STDERR: fail_todo_use_5891_bitmask.carbon:[[@LINE+7]]:3: error: no matching function for call to `Take` [CppOverloadingNoViableFunctionFound]
  103. // CHECK:STDERR: Cpp.Take(BitOr(Cpp.A, Cpp.C));
  104. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  105. // CHECK:STDERR: fail_todo_use_5891_bitmask.carbon:[[@LINE+4]]:3: note: in call to Cpp function here [InCallToCppFunction]
  106. // CHECK:STDERR: Cpp.Take(BitOr(Cpp.A, Cpp.C));
  107. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  108. // CHECK:STDERR:
  109. Cpp.Take(BitOr(Cpp.A, Cpp.C));
  110. }