function.carbon 4.3 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/int.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/interop/cpp/reverse/function.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/reverse/function.carbon
  12. // --- other.carbon
  13. package Other;
  14. fn F1() {}
  15. fn F2() -> i32 { return 0; }
  16. fn F3(_: i32) {}
  17. fn HasGenericArg(T:! type, a: T) { a; }
  18. fn HasDeducedArg[T:! type](a: T) { a; }
  19. class C {
  20. fn Method[self: Self]() { self; }
  21. }
  22. // --- function.carbon
  23. library "[[@TEST_NAME]]";
  24. import Other;
  25. import Cpp inline '''
  26. void G() {
  27. Carbon::Other::F1();
  28. }
  29. ''';
  30. // --- fail_todo_non_void.carbon
  31. library "[[@TEST_NAME]]";
  32. // CHECK:STDERR: fail_todo_non_void.carbon:[[@LINE+5]]:1: in import [InImport]
  33. // CHECK:STDERR: other.carbon:4:1: error: semantics TODO: `unsupported: C++ calling a Carbon function with return type other than `()`` [SemanticsTodo]
  34. // CHECK:STDERR: fn F2() -> i32 { return 0; }
  35. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  36. // CHECK:STDERR:
  37. import Other;
  38. import Cpp inline '''
  39. void G() {
  40. // CHECK:STDERR: fail_todo_non_void.carbon:[[@LINE+4]]:18: error: no member named 'F2' in namespace 'Carbon::Other' [CppInteropParseError]
  41. // CHECK:STDERR: 16 | Carbon::Other::F2();
  42. // CHECK:STDERR: | ^~
  43. // CHECK:STDERR:
  44. Carbon::Other::F2();
  45. }
  46. ''';
  47. // --- args.carbon
  48. library "[[@TEST_NAME]]";
  49. import Other;
  50. import Cpp inline '''
  51. void G() {
  52. Carbon::Other::F3(123);
  53. }
  54. ''';
  55. // --- fail_todo_generic.carbon
  56. library "[[@TEST_NAME]]";
  57. // CHECK:STDERR: fail_todo_generic.carbon:[[@LINE+5]]:1: in import [InImport]
  58. // CHECK:STDERR: other.carbon:7:1: error: semantics TODO: `unsupported: C++ calling a Carbon function with generic parameters` [SemanticsTodo]
  59. // CHECK:STDERR: fn HasGenericArg(T:! type, a: T) { a; }
  60. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  61. // CHECK:STDERR:
  62. import Other;
  63. import Cpp inline '''
  64. void G() {
  65. // CHECK:STDERR: fail_todo_generic.carbon:[[@LINE+8]]:18: error: no member named 'HasGenericArg' in namespace 'Carbon::Other' [CppInteropParseError]
  66. // CHECK:STDERR: 20 | Carbon::Other::HasGenericArg<int>(123);
  67. // CHECK:STDERR: | ^~~~~~~~~~~~~
  68. // CHECK:STDERR:
  69. // CHECK:STDERR: fail_todo_generic.carbon:[[@LINE+4]]:35: error: expected '(' for function-style cast or type construction [CppInteropParseError]
  70. // CHECK:STDERR: 20 | Carbon::Other::HasGenericArg<int>(123);
  71. // CHECK:STDERR: | ~~~^
  72. // CHECK:STDERR:
  73. Carbon::Other::HasGenericArg<int>(123);
  74. }
  75. ''';
  76. // --- fail_todo_deduced.carbon
  77. library "[[@TEST_NAME]]";
  78. // CHECK:STDERR: fail_todo_deduced.carbon:[[@LINE+5]]:1: in import [InImport]
  79. // CHECK:STDERR: other.carbon:8:1: error: semantics TODO: `unsupported: C++ calling a Carbon function with generic parameters` [SemanticsTodo]
  80. // CHECK:STDERR: fn HasDeducedArg[T:! type](a: T) { a; }
  81. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  82. // CHECK:STDERR:
  83. import Other;
  84. import Cpp inline '''
  85. void G() {
  86. // CHECK:STDERR: fail_todo_deduced.carbon:[[@LINE+4]]:18: error: no member named 'HasDeducedArg' in namespace 'Carbon::Other' [CppInteropParseError]
  87. // CHECK:STDERR: 16 | Carbon::Other::HasDeducedArg(123);
  88. // CHECK:STDERR: | ^~~~~~~~~~~~~
  89. // CHECK:STDERR:
  90. Carbon::Other::HasDeducedArg(123);
  91. }
  92. ''';
  93. // --- fail_todo_method.carbon
  94. library "[[@TEST_NAME]]";
  95. import Other;
  96. import Cpp inline '''
  97. void G() {
  98. // CHECK:STDERR: fail_todo_method.carbon:[[@LINE+8]]:3: error: invalid use of incomplete type 'Carbon::Other::C' [CppInteropParseError]
  99. // CHECK:STDERR: 15 | Carbon::Other::C().Method();
  100. // CHECK:STDERR: | ^~~~~~~~~~~~~~~~~~
  101. // CHECK:STDERR:
  102. // CHECK:STDERR: fail_todo_method.carbon:[[@LINE+4]]:21: error: member access into incomplete type 'Carbon::Other::C' [CppInteropParseError]
  103. // CHECK:STDERR: 15 | Carbon::Other::C().Method();
  104. // CHECK:STDERR: | ^
  105. // CHECK:STDERR:
  106. Carbon::Other::C().Method();
  107. }
  108. ''';