field.carbon 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/class/export/field.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/export/field.carbon
  12. // --- field.carbon
  13. library "[[@TEST_NAME]]";
  14. import Cpp;
  15. class A {
  16. var x: i32;
  17. var y: i32;
  18. }
  19. inline Cpp '''
  20. void F() {
  21. Carbon::A a;
  22. a.x = 12;
  23. a.y = 34;
  24. }
  25. ''';
  26. // --- field_inheritence.carbon
  27. library "[[@TEST_NAME]]";
  28. import Cpp;
  29. base class A {
  30. var x: i32;
  31. }
  32. class B {
  33. extend base: A;
  34. var y: i32;
  35. }
  36. inline Cpp '''
  37. void F() {
  38. Carbon::B b;
  39. b.x = 12;
  40. b.y = 34;
  41. }
  42. ''';
  43. // --- fail_nonexistent_field.carbon
  44. library "[[@TEST_NAME]]";
  45. import Cpp;
  46. class A {}
  47. inline Cpp '''
  48. void F() {
  49. Carbon::A a;
  50. // CHECK:STDERR: fail_nonexistent_field.carbon:[[@LINE+4]]:5: error: no member named 'x' in 'Carbon::A' [CppInteropParseError]
  51. // CHECK:STDERR: 13 | a.x = 12;
  52. // CHECK:STDERR: | ~ ^
  53. // CHECK:STDERR:
  54. a.x = 12;
  55. // CHECK:STDERR: fail_nonexistent_field.carbon:[[@LINE+4]]:5: error: no member named 'y' in 'Carbon::A' [CppInteropParseError]
  56. // CHECK:STDERR: 18 | a.y = 34;
  57. // CHECK:STDERR: | ~ ^
  58. // CHECK:STDERR:
  59. a.y = 34;
  60. }
  61. ''';
  62. // --- fail_unsupported_field_type.carbon
  63. library "[[@TEST_NAME]]";
  64. import Cpp;
  65. class A {
  66. // CHECK:STDERR: fail_unsupported_field_type.carbon:[[@LINE+4]]:7: error: semantics TODO: `failed to map Carbon type to C++` [SemanticsTodo]
  67. // CHECK:STDERR: var x: ();
  68. // CHECK:STDERR: ^~~~~
  69. // CHECK:STDERR:
  70. var x: ();
  71. // CHECK:STDERR: fail_unsupported_field_type.carbon:[[@LINE+4]]:7: error: semantics TODO: `failed to map Carbon type to C++` [SemanticsTodo]
  72. // CHECK:STDERR: var y: ();
  73. // CHECK:STDERR: ^~~~~
  74. // CHECK:STDERR:
  75. var y: ();
  76. }
  77. inline Cpp '''
  78. void F() {
  79. Carbon::A a;
  80. // CHECK:STDERR: fail_unsupported_field_type.carbon:[[@LINE+4]]:5: error: no member named 'x' in 'Carbon::A' [CppInteropParseError]
  81. // CHECK:STDERR: 24 | a.x;
  82. // CHECK:STDERR: | ~ ^
  83. // CHECK:STDERR:
  84. a.x;
  85. // CHECK:STDERR: fail_unsupported_field_type.carbon:[[@LINE+4]]:5: error: no member named 'y' in 'Carbon::A' [CppInteropParseError]
  86. // CHECK:STDERR: 29 | a.y;
  87. // CHECK:STDERR: | ~ ^
  88. // CHECK:STDERR:
  89. a.y;
  90. }
  91. ''';
  92. // --- fail_adapter.carbon
  93. library "[[@TEST_NAME]]";
  94. import Cpp;
  95. class A {
  96. adapt ();
  97. }
  98. inline Cpp '''
  99. void F() {
  100. Carbon::A a;
  101. // CHECK:STDERR: fail_adapter.carbon:[[@LINE+4]]:5: error: no member named 'x' in 'Carbon::A' [CppInteropParseError]
  102. // CHECK:STDERR: 15 | a.x = 12;
  103. // CHECK:STDERR: | ~ ^
  104. // CHECK:STDERR:
  105. a.x = 12;
  106. }
  107. ''';