deref.carbon 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // EXTRA-ARGS: --target=aarch64-unknown-linux --clang-arg=--std=c++20
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/deref.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/deref.carbon
  14. // --- fail_todo_const_deref.carbon
  15. library "[[@TEST_NAME]]";
  16. import Cpp inline '''c++
  17. class ConstDeref {
  18. public:
  19. auto operator*() const -> int&;
  20. };
  21. ''';
  22. fn TestDerefPass(var int_ptr: Cpp.ConstDeref) -> ref i32 {
  23. // CHECK:STDERR: fail_todo_const_deref.carbon:[[@LINE+4]]:10: error: semantics TODO: `Associated constant in interface with synthesized impl` [SemanticsTodo]
  24. // CHECK:STDERR: return int_ptr.(Core.CppUnsafeDeref.Op)();
  25. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. // CHECK:STDERR:
  27. return int_ptr.(Core.CppUnsafeDeref.Op)();
  28. }
  29. // --- fail_todo_mutable_deref.carbon
  30. library "[[@TEST_NAME]]";
  31. import Cpp inline '''c++
  32. class MutableDeref {
  33. public:
  34. auto operator*() -> int&;
  35. };
  36. ''';
  37. fn TestDerefPass(var int_ptr: Cpp.MutableDeref) -> ref i32 {
  38. // CHECK:STDERR: fail_todo_mutable_deref.carbon:[[@LINE+4]]:10: error: semantics TODO: `Associated constant in interface with synthesized impl` [SemanticsTodo]
  39. // CHECK:STDERR: return int_ptr.(Core.CppUnsafeDeref.Op)();
  40. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  41. // CHECK:STDERR:
  42. return int_ptr.(Core.CppUnsafeDeref.Op)();
  43. }
  44. // --- fail_todo_multi_deref.carbon
  45. library "[[@TEST_NAME]]";
  46. import Cpp inline '''c++
  47. class MultiDeref {
  48. public:
  49. auto operator*() & -> int&;
  50. auto operator*() const& -> const int&;
  51. auto operator*() && -> int&&;
  52. auto operator*() const&& -> int const&&;
  53. };
  54. ''';
  55. fn TestDerefPass(var int_ptr: Cpp.MultiDeref) -> ref i32 {
  56. // CHECK:STDERR: fail_todo_multi_deref.carbon:[[@LINE+4]]:10: error: semantics TODO: `operator* overload sets not implemented yet` [SemanticsTodo]
  57. // CHECK:STDERR: return int_ptr.(Core.CppUnsafeDeref.Op)();
  58. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. // CHECK:STDERR:
  60. return int_ptr.(Core.CppUnsafeDeref.Op)();
  61. }
  62. // --- fail_todo_maybe_deref.carbon
  63. library "[[@TEST_NAME]]";
  64. import Cpp inline '''c++
  65. template<class T, class U>
  66. concept SameImpl = __is_same(T, U);
  67. template<class T, class U>
  68. concept SameAs = SameImpl<T, U> && SameImpl<U, T>;
  69. template<class T>
  70. class MaybeDeref {
  71. public:
  72. auto operator*() const -> int&
  73. requires SameAs<T, int>;
  74. auto operator*() const -> const T&;
  75. };
  76. ''';
  77. fn TestDerefPass(var int_ptr: Cpp.MaybeDeref(i32)) -> ref i32 {
  78. // CHECK:STDERR: fail_todo_maybe_deref.carbon:[[@LINE+4]]:10: error: semantics TODO: `operator* overload sets not implemented yet` [SemanticsTodo]
  79. // CHECK:STDERR: return int_ptr.(Core.CppUnsafeDeref.Op)();
  80. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  81. // CHECK:STDERR:
  82. return int_ptr.(Core.CppUnsafeDeref.Op)();
  83. }
  84. // --- fail_not_a_ptr.carbon
  85. library "[[@TEST_NAME]]";
  86. import Cpp inline '''c++
  87. class NotAPtr {};
  88. ''';
  89. fn TestDerefFail(not_ptr: Cpp.NotAPtr) {
  90. // CHECK:STDERR: fail_not_a_ptr.carbon:[[@LINE+4]]:3: error: cannot access member of interface `Core.CppUnsafeDeref` in type `Cpp.NotAPtr` that does not implement that interface [MissingImplInMemberAccess]
  91. // CHECK:STDERR: not_ptr.(Core.CppUnsafeDeref.Op)();
  92. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. // CHECK:STDERR:
  94. not_ptr.(Core.CppUnsafeDeref.Op)();
  95. }