void.carbon 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/check/testdata/interop/cpp/void.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/void.carbon
  12. // --- void_not_incomplete.carbon
  13. library "[[@TEST_NAME]]";
  14. import Cpp;
  15. // This is valid, unlike in C++, because `Cpp.void` is complete but abstract in
  16. // Carbon. Values of type `Cpp.void` exist, but objects of that type do not.
  17. fn F(unused x: Cpp.void) {}
  18. // --- fail_void_abstract.carbon
  19. library "[[@TEST_NAME]]";
  20. import Cpp;
  21. fn G() {
  22. // CHECK:STDERR: fail_void_abstract.carbon:[[@LINE+7]]:17: error: binding pattern has abstract type `Cpp.void` in `var` pattern [AbstractTypeInVarPattern]
  23. // CHECK:STDERR: var unused x: Cpp.void;
  24. // CHECK:STDERR: ^~~~~~~~
  25. // CHECK:STDERR: {{.*}}/prelude/types/cpp/void.carbon:24:1: note: class was declared abstract here [ClassAbstractHere]
  26. // CHECK:STDERR: abstract class CppCompat.VoidBase {
  27. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. // CHECK:STDERR:
  29. var unused x: Cpp.void;
  30. }
  31. // --- fail_void_argument.carbon
  32. library "[[@TEST_NAME]]";
  33. import Cpp inline "void F(int);";
  34. fn G(v: Cpp.void) {
  35. // CHECK:STDERR: fail_void_argument.carbon:[[@LINE+7]]:10: error: no matching function for call to 'F' [CppInteropParseError]
  36. // CHECK:STDERR: 14 | Cpp.F(v);
  37. // CHECK:STDERR: | ^
  38. // CHECK:STDERR: fail_void_argument.carbon:[[@LINE-6]]:6: note: candidate function not viable: cannot convert argument of incomplete type 'void' to 'int' for 1st argument [CppInteropParseNote]
  39. // CHECK:STDERR: 4 | void F(int);
  40. // CHECK:STDERR: | ^ ~~~
  41. // CHECK:STDERR:
  42. Cpp.F(v);
  43. }
  44. fn H(p: Cpp.void*) {
  45. // CHECK:STDERR: fail_void_argument.carbon:[[@LINE+7]]:11: error: no matching function for call to 'F' [CppInteropParseError]
  46. // CHECK:STDERR: 25 | Cpp.F(*p);
  47. // CHECK:STDERR: | ^
  48. // CHECK:STDERR: fail_void_argument.carbon:[[@LINE-17]]:6: note: candidate function not viable: cannot convert argument of incomplete type 'void' to 'int' for 1st argument [CppInteropParseNote]
  49. // CHECK:STDERR: 4 | void F(int);
  50. // CHECK:STDERR: | ^ ~~~
  51. // CHECK:STDERR:
  52. Cpp.F(*p);
  53. }