function.carbon 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/function/export/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/function/export/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. // --- function.carbon
  20. library "[[@TEST_NAME]]";
  21. import Other;
  22. import Cpp inline '''
  23. void G() {
  24. Carbon::Other::F1();
  25. }
  26. ''';
  27. // --- return_int.carbon
  28. library "[[@TEST_NAME]]";
  29. import Other;
  30. import Cpp inline '''
  31. int G() {
  32. return Carbon::Other::F2();
  33. }
  34. ''';
  35. // --- args.carbon
  36. library "[[@TEST_NAME]]";
  37. import Other;
  38. import Cpp inline '''
  39. void G() {
  40. Carbon::Other::F3(123);
  41. }
  42. ''';
  43. // --- ref_arg.carbon
  44. library "[[@TEST_NAME]]";
  45. import Cpp;
  46. fn F(ref n: i32);
  47. inline Cpp '''
  48. void G() {
  49. int n = 0;
  50. Carbon::F(n);
  51. }
  52. ''';
  53. // --- using.carbon
  54. library "[[@TEST_NAME]]";
  55. import Cpp;
  56. fn F();
  57. inline Cpp '''
  58. using Carbon::F;
  59. void G() { F(); }
  60. ''';
  61. // --- fail_todo_generic.carbon
  62. library "[[@TEST_NAME]]";
  63. // CHECK:STDERR: fail_todo_generic.carbon:[[@LINE+5]]:1: in import [InImport]
  64. // CHECK:STDERR: other.carbon:7:1: error: semantics TODO: `unsupported: C++ calling a Carbon function with generic parameters` [SemanticsTodo]
  65. // CHECK:STDERR: fn HasGenericArg(T:! type, a: T) { a; }
  66. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. // CHECK:STDERR:
  68. import Other;
  69. import Cpp inline '''
  70. void G() {
  71. // CHECK:STDERR: fail_todo_generic.carbon:[[@LINE+8]]:18: error: no member named 'HasGenericArg' in namespace 'Carbon::Other' [CppInteropParseError]
  72. // CHECK:STDERR: 20 | Carbon::Other::HasGenericArg<int>(123);
  73. // CHECK:STDERR: | ^~~~~~~~~~~~~
  74. // CHECK:STDERR:
  75. // CHECK:STDERR: fail_todo_generic.carbon:[[@LINE+4]]:35: error: expected '(' for function-style cast or type construction [CppInteropParseError]
  76. // CHECK:STDERR: 20 | Carbon::Other::HasGenericArg<int>(123);
  77. // CHECK:STDERR: | ~~~^
  78. // CHECK:STDERR:
  79. Carbon::Other::HasGenericArg<int>(123);
  80. }
  81. ''';
  82. // --- fail_todo_deduced.carbon
  83. library "[[@TEST_NAME]]";
  84. // CHECK:STDERR: fail_todo_deduced.carbon:[[@LINE+5]]:1: in import [InImport]
  85. // CHECK:STDERR: other.carbon:8:1: error: semantics TODO: `unsupported: C++ calling a Carbon function with generic parameters` [SemanticsTodo]
  86. // CHECK:STDERR: fn HasDeducedArg[T:! type](a: T) { a; }
  87. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88. // CHECK:STDERR:
  89. import Other;
  90. import Cpp inline '''
  91. void G() {
  92. // CHECK:STDERR: fail_todo_deduced.carbon:[[@LINE+4]]:18: error: no member named 'HasDeducedArg' in namespace 'Carbon::Other' [CppInteropParseError]
  93. // CHECK:STDERR: 16 | Carbon::Other::HasDeducedArg(123);
  94. // CHECK:STDERR: | ^~~~~~~~~~~~~
  95. // CHECK:STDERR:
  96. Carbon::Other::HasDeducedArg(123);
  97. }
  98. ''';