Skip to content

Commit 7838db1

Browse files
committed
Merge bitcoin#20495: sync: Use decltype(auto) return type for WITH_LOCK
3eb94ec sync: Use decltype(auto) return type for WITH_LOCK (Carl Dong) Pull request description: > Now that we're using C++17, we can use the decltype(auto) return type > for functions and lambda expressions. > > As demonstrated in this commit, this can simplify cases where previously > the compiler failed to deduce the correct return type. > > Just for reference, for the "assign to ref" cases fixed here, there are > 3 possible solutions: > > - Return a pointer and immediately deref as used before this commit > - Make sure the function/lambda returns declspec(auto) as used after > this commit > - Class& i = WITH_LOCK(..., return std::ref(...)); > > ----- > > References: > 1. https://en.cppreference.com/w/cpp/language/function#Return_type_deduction > 2. https://en.cppreference.com/w/cpp/language/template_argument_deduction#Other_contexts > 3. https://en.cppreference.com/w/cpp/language/auto > 4. https://en.cppreference.com/w/cpp/language/decltype > > Explanations: > 1. https://stackoverflow.com/a/21369192 > 2. https://stackoverflow.com/a/21369170 Thanks to sipa and ryanofsky for helping me understand this ACKs for top commit: jnewbery: utACK 3eb94ec hebasto: ACK 3eb94ec, I have reviewed the code and it looks OK, I agree it can be merged. I have verified possible warnings: ryanofsky: Code review ACK 3eb94ec Tree-SHA512: 5f55c7722aeca8ea70e5c1a8db93e93ba0e356e8967e7f607ada38003df4b153d73c29bd2cea8d7ec1344720d37d857ea7dbfd2a88da1d92e0e9cbb9abd287df
2 parents 6af0137 + 3eb94ec commit 7838db1

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

Diff for: src/sync.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,22 @@ using DebugLock = UniqueLock<typename std::remove_reference<typename std::remove
258258
//!
259259
//! int val = WITH_LOCK(cs, return shared_val);
260260
//!
261-
#define WITH_LOCK(cs, code) [&] { LOCK(cs); code; }()
261+
//! Note:
262+
//!
263+
//! Since the return type deduction follows that of decltype(auto), while the
264+
//! deduced type of:
265+
//!
266+
//! WITH_LOCK(cs, return {int i = 1; return i;});
267+
//!
268+
//! is int, the deduced type of:
269+
//!
270+
//! WITH_LOCK(cs, return {int j = 1; return (j);});
271+
//!
272+
//! is &int, a reference to a local variable
273+
//!
274+
//! The above is detectable at compile-time with the -Wreturn-local-addr flag in
275+
//! gcc and the -Wreturn-stack-address flag in clang, both enabled by default.
276+
#define WITH_LOCK(cs, code) [&]() -> decltype(auto) { LOCK(cs); code; }()
262277

263278
class CSemaphore
264279
{

Diff for: src/test/validation_chainstate_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
3535
return outp;
3636
};
3737

38-
CChainState& c1 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate(mempool));
38+
CChainState& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(mempool));
3939
c1.InitCoinsDB(
4040
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
4141
WITH_LOCK(::cs_main, c1.InitCoinsCache(1 << 23));

Diff for: src/test/validation_chainstatemanager_tests.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
3030

3131
// Create a legacy (IBD) chainstate.
3232
//
33-
CChainState& c1 = *WITH_LOCK(::cs_main, return &manager.InitializeChainstate(mempool));
33+
CChainState& c1 = WITH_LOCK(::cs_main, return manager.InitializeChainstate(mempool));
3434
chainstates.push_back(&c1);
3535
c1.InitCoinsDB(
3636
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
@@ -56,7 +56,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
5656

5757
// Create a snapshot-based chainstate.
5858
//
59-
CChainState& c2 = *WITH_LOCK(::cs_main, return &manager.InitializeChainstate(mempool, GetRandHash()));
59+
CChainState& c2 = WITH_LOCK(::cs_main, return manager.InitializeChainstate(mempool, GetRandHash()));
6060
chainstates.push_back(&c2);
6161
c2.InitCoinsDB(
6262
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
@@ -116,7 +116,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
116116

117117
// Create a legacy (IBD) chainstate.
118118
//
119-
CChainState& c1 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate(mempool));
119+
CChainState& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(mempool));
120120
chainstates.push_back(&c1);
121121
c1.InitCoinsDB(
122122
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
@@ -134,7 +134,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
134134

135135
// Create a snapshot-based chainstate.
136136
//
137-
CChainState& c2 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate(mempool, GetRandHash()));
137+
CChainState& c2 = WITH_LOCK(cs_main, return manager.InitializeChainstate(mempool, GetRandHash()));
138138
chainstates.push_back(&c2);
139139
c2.InitCoinsDB(
140140
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);

0 commit comments

Comments
 (0)