raw_ptr_unittest.nc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // This is a "No Compile Test" suite.
  5. // http://dev.chromium.org/developers/testing/no-compile-tests
  6. #include <memory>
  7. #include <tuple> // for std::ignore
  8. #include <type_traits> // for std::remove_pointer_t
  9. #include "base/memory/raw_ptr.h"
  10. namespace {
  11. struct Producer {};
  12. struct DerivedProducer : Producer {};
  13. struct OtherDerivedProducer : Producer {};
  14. struct Unrelated {};
  15. struct DerivedUnrelated : Unrelated {};
  16. struct PmfTest {
  17. public:
  18. int Func(char, double) const { return 11; }
  19. };
  20. #if defined(NCTEST_AUTO_DOWNCAST) // [r"no viable conversion from 'raw_ptr<Producer>' to 'raw_ptr<DerivedProducer>'"]
  21. void WontCompile() {
  22. Producer f;
  23. raw_ptr<Producer> ptr = &f;
  24. raw_ptr<DerivedProducer> derived_ptr = ptr;
  25. }
  26. #elif defined(NCTEST_STATIC_DOWNCAST) // [r"no matching conversion for static_cast from 'raw_ptr<Producer>' to 'raw_ptr<DerivedProducer>'"]
  27. void WontCompile() {
  28. Producer f;
  29. raw_ptr<Producer> ptr = &f;
  30. raw_ptr<DerivedProducer> derived_ptr =
  31. static_cast<raw_ptr<DerivedProducer>>(ptr);
  32. }
  33. #elif defined(NCTEST_AUTO_REF_DOWNCAST) // [r"non-const lvalue reference to type 'raw_ptr<DerivedProducer>' cannot bind to a value of unrelated type 'raw_ptr<Producer>'"]
  34. void WontCompile() {
  35. Producer f;
  36. raw_ptr<Producer> ptr = &f;
  37. raw_ptr<DerivedProducer>& derived_ptr = ptr;
  38. }
  39. #elif defined(NCTEST_STATIC_REF_DOWNCAST) // [r"non-const lvalue reference to type 'raw_ptr<DerivedProducer>' cannot bind to a value of unrelated type 'raw_ptr<Producer>'"]
  40. void WontCompile() {
  41. Producer f;
  42. raw_ptr<Producer> ptr = &f;
  43. raw_ptr<DerivedProducer>& derived_ptr =
  44. static_cast<raw_ptr<DerivedProducer>&>(ptr);
  45. }
  46. #elif defined(NCTEST_AUTO_DOWNCAST_FROM_RAW) // [r"no viable conversion from 'Producer \*' to 'raw_ptr<DerivedProducer>'"]
  47. void WontCompile() {
  48. Producer f;
  49. raw_ptr<DerivedProducer> ptr = &f;
  50. }
  51. #elif defined(NCTEST_UNRELATED_FROM_RAW) // [r"no viable conversion from 'DerivedProducer \*' to 'raw_ptr<Unrelated>'"]
  52. void WontCompile() {
  53. DerivedProducer f;
  54. raw_ptr<Unrelated> ptr = &f;
  55. }
  56. #elif defined(NCTEST_UNRELATED_STATIC_FROM_WRAPPED) // [r"static_cast from '\(anonymous namespace\)::DerivedProducer \*' to '\(anonymous namespace\)::Unrelated \*', which are not related by inheritance, is not allowed"]
  57. void WontCompile() {
  58. DerivedProducer f;
  59. raw_ptr<DerivedProducer> ptr = &f;
  60. std::ignore = static_cast<Unrelated*>(ptr);
  61. }
  62. #elif defined(NCTEST_VOID_DEREFERENCE) // [r"indirection requires pointer operand \('raw_ptr<const void>' invalid\)"]
  63. void WontCompile() {
  64. const char foo[] = "42";
  65. raw_ptr<const void> ptr = foo;
  66. std::ignore = *ptr;
  67. }
  68. #elif defined(NCTEST_FUNCTION_POINTER) // [r"raw_ptr<T> doesn't work with this kind of pointee type T"]
  69. void WontCompile() {
  70. raw_ptr<void(int)> raw_ptr_var;
  71. std::ignore = raw_ptr_var.get();
  72. }
  73. #elif defined(NCTEST_POINTER_TO_MEMBER) // [r"overload resolution selected deleted operator '->\*'"]
  74. void WontCompile() {
  75. PmfTest object;
  76. int (PmfTest::*pmf_func)(char, double) const = &PmfTest::Func;
  77. raw_ptr<PmfTest> object_ptr = &object;
  78. std::ignore = object_ptr->*pmf_func;
  79. }
  80. #elif defined(NCTEST_DANGLING_GSL) // [r"object backing the pointer will be destroyed at the end of the full-expression"]
  81. void WontCompile() {
  82. [[maybe_unused]] raw_ptr<int> ptr = std::make_unique<int>(2).get();
  83. }
  84. #endif
  85. } // namespace