In commit "refactor: Mark pimpls as NotNull" (fa071a2c451d2cab12e181711b2dd64d61f3ea18)
With the current NotNull constructor, PRs like this one that introduce NotNull can look safer than they are, because changing a member to a NotNull type adds Assert calls that don't show up in the diff. It would be better to write NotNull initializers so the check is visible, using MakeNotNullUnique/MakeNotNullShared or util::NotNull{...}.
For example, with m_impl a NotNullUniquePtr, this aborts if LoadImpl() returns null, but nothing on the line says so:
Foo::Foo() : m_impl{LoadImpl()} {}
Forwarding functions do the same: vec.emplace_back(LoadImpl()) into a vector of NotNull.
Most other places don't have this problem, by design, because the NotNull constructor is explicit. Member initializer lists and forwarding functions are the exceptions.
This PR has seven initializers like the one above. They all use make_unique, so they're fine, but checking them means looking outside the diff. In this PR, they could have been made clearer with:
<details><summary>diff</summary>
diff --git a/src/addrman.cpp b/src/addrman.cpp
index f830acdbe3f..14f53936e3e 100644
--- a/src/addrman.cpp
+++ b/src/addrman.cpp
@@ -1259,7 +1259,7 @@ std::optional<AddressPosition> AddrManImpl::FindAddressEntry(const CAddress& add
}
AddrMan::AddrMan(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio)
- : m_impl(std::make_unique<AddrManImpl>(netgroupman, deterministic, consistency_check_ratio)) {}
+ : m_impl(util::MakeNotNullUnique<AddrManImpl>(netgroupman, deterministic, consistency_check_ratio)) {}
AddrMan::~AddrMan() = default;
diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp
index 77c57964a9f..ee68cefa579 100644
--- a/src/dbwrapper.cpp
+++ b/src/dbwrapper.cpp
@@ -183,7 +183,7 @@ struct CDBBatch::WriteBatchImpl {
CDBBatch::CDBBatch(const CDBWrapper& _parent)
: parent{_parent},
- m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()}
+ m_impl_batch{util::MakeNotNullUnique<CDBBatch::WriteBatchImpl>()}
{
m_key_scratch.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
m_value_scratch.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
diff --git a/src/musig.cpp b/src/musig.cpp
index 38a49fa2f37..d344e6e34da 100644
--- a/src/musig.cpp
+++ b/src/musig.cpp
@@ -100,7 +100,7 @@ public:
bool IsValid() { return m_nonce != nullptr; }
};
-MuSig2SecNonce::MuSig2SecNonce() : m_impl{std::make_unique<MuSig2SecNonceImpl>()} {}
+MuSig2SecNonce::MuSig2SecNonce() : m_impl{util::MakeNotNullUnique<MuSig2SecNonceImpl>()} {}
MuSig2SecNonce::MuSig2SecNonce(MuSig2SecNonce&&) noexcept = default;
MuSig2SecNonce& MuSig2SecNonce::operator=(MuSig2SecNonce&&) noexcept = default;
diff --git a/src/node/txdownloadman_impl.cpp b/src/node/txdownloadman_impl.cpp
index 7443367b6bf..4b8f0a54543 100644
--- a/src/node/txdownloadman_impl.cpp
+++ b/src/node/txdownloadman_impl.cpp
@@ -15,7 +15,7 @@
namespace node {
// TxDownloadManager wrappers
TxDownloadManager::TxDownloadManager(const TxDownloadOptions& options) :
- m_impl{std::make_unique<TxDownloadManagerImpl>(options)}
+ m_impl{util::MakeNotNullUnique<TxDownloadManagerImpl>(options)}
{}
TxDownloadManager::~TxDownloadManager() = default;
diff --git a/src/node/txreconciliation.cpp b/src/node/txreconciliation.cpp
index 9a1f640f5a4..fb6e2417ec3 100644
--- a/src/node/txreconciliation.cpp
+++ b/src/node/txreconciliation.cpp
@@ -144,7 +144,7 @@ public:
}
};
-TxReconciliationTracker::TxReconciliationTracker(uint32_t recon_version) : m_impl{std::make_unique<TxReconciliationTracker::Impl>(recon_version)} {}
+TxReconciliationTracker::TxReconciliationTracker(uint32_t recon_version) : m_impl{util::MakeNotNullUnique<TxReconciliationTracker::Impl>(recon_version)} {}
TxReconciliationTracker::~TxReconciliationTracker() = default;
diff --git a/src/test/fuzz/util/check_globals.cpp b/src/test/fuzz/util/check_globals.cpp
index 431519dc269..11a2a34dd62 100644
--- a/src/test/fuzz/util/check_globals.cpp
+++ b/src/test/fuzz/util/check_globals.cpp
@@ -56,5 +56,5 @@ struct CheckGlobalsImpl {
}
};
-CheckGlobals::CheckGlobals() : m_impl(std::make_unique<CheckGlobalsImpl>()) {}
+CheckGlobals::CheckGlobals() : m_impl(util::MakeNotNullUnique<CheckGlobalsImpl>()) {}
CheckGlobals::~CheckGlobals() = default;
diff --git a/src/txrequest.cpp b/src/txrequest.cpp
index 53136a8e710..0fb25f592e2 100644
--- a/src/txrequest.cpp
+++ b/src/txrequest.cpp
@@ -709,7 +709,7 @@ public:
};
TxRequestTracker::TxRequestTracker(bool deterministic) :
- m_impl{std::make_unique<TxRequestTracker::Impl>(deterministic)} {}
+ m_impl{util::MakeNotNullUnique<TxRequestTracker::Impl>(deterministic)} {}
TxRequestTracker::~TxRequestTracker() = default;
</details>
Other PRs could follow the same convention.
The compiler could also catch these cases by making the constructor that takes a plain smart pointer private, so a NotNull could only be created with MakeNotNullUnique/MakeNotNullShared or a checking function like util::CheckNotNull(expr).