-
Notifications
You must be signed in to change notification settings - Fork 37.7k
refactor: Replace std::optional<bilingual_str>
with util::Result
#25977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
9d3918d
to
ebc493e
Compare
ebc493e
to
f1a9c71
Compare
f1a9c71
to
fb94363
Compare
fb94363
to
080268d
Compare
A minimal (but hacky) way to add support for void to Result originally posted bitcoin#27632 (comment)
Looks like there is a clang bug. Could be fixed by either bumping it again (to clang-13), see #27682, but I am not sure how to do that for macOS. Or by replacing godbolt for clang-12 (broken): https://godbolt.org/z/1hraPjxrf |
Thanks, added std::move as suggested. Updated b2bcba0 -> 8aa8f73 ( addrdb.cpp:213:12: error: call to implicitly-deleted copy constructor of 'util::Result<std::__1::unique_ptr<AddrMan, std::__1::default_delete<AddrMan> > >::T' (aka 'std::__1::unique_ptr<AddrMan, std::__1::default_delete<AddrMan> >')
return addrman;
^~~~~~~
/usr/lib/llvm-10/bin/../include/c++/v1/memory:2513:3: note: copy constructor is implicitly deleted because 'unique_ptr<AddrMan, std::__1::default_delete<AddrMan> >' has a user-declared move constructor
unique_ptr(unique_ptr&& __u) _NOEXCEPT
^
./util/result.h:47:14: note: passing argument to parameter 'obj' here
Result(T obj) : m_variant{std::in_place_index_t<1>{}, std::move(obj)} {}
^
1 error generated. |
very nice ACK 8aa8f73 🏏 Show signatureSignature:
|
@@ -98,6 +99,6 @@ class CBlockTreeDB : public CDBWrapper | |||
EXCLUSIVE_LOCKS_REQUIRED(::cs_main); | |||
}; | |||
|
|||
std::optional<bilingual_str> CheckLegacyTxindex(CBlockTreeDB& block_tree_db); | |||
util::Result<void> CheckLegacyTxindex(CBlockTreeDB& block_tree_db); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit (if you retouch): Could add [[nodiscard]]
(either as fixup or new commit), while touching all those header files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 8aa8f73
I think [[nodiscard]
qualifiers for all the util::Result
return types would be nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 8aa8f73, I have reviewed the code and it looks OK.
std::variant<bilingual_str, T> m_variant; | ||
|
||
template <typename FT> | ||
friend bilingual_str ErrorString(const Result<FT>& result); | ||
|
||
public: | ||
Result() : m_variant{std::in_place_index_t<1>{}, std::monostate{}} {} // constructor for void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Add #include <utility>
?
Although GCC complains:
|
… with `util::Result` 8aa8f73 refactor: Replace std::optional<bilingual_str> with util::Result (Ryan Ofsky) 5f49cb1 util: Add void support to util::Result (MarcoFalke) Pull request description: Replace uses of `std::optional<bilingual_str>` with `util::Result` as suggested bitcoin#25648 (comment), bitcoin#27632 (comment), bitcoin#27632 (comment), bitcoin#24313 (comment) ACKs for top commit: MarcoFalke: very nice ACK 8aa8f73 🏏 TheCharlatan: ACK 8aa8f73 hebasto: ACK 8aa8f73, I have reviewed the code and it looks OK. Tree-SHA512: 6c2f218bc445184ce93ec2b907e61666a05f26f2c9447f69fcb504aa8291b0c693d913f659dfd19813a9e24615546c72cbe2ca419218fd867ff0694c4a1b6a30
Yeah, that's why I suggested |
…ng a Result return type is an error fa5680b fix includes for touched header files (iwyu) (MarcoFalke) dddde27 Add [[nodiscard]] where ignoring a Result return type is an error (MarcoFalke) Pull request description: Only add it for those where it is an error to ignore. Also, fix the gcc compile warning bitcoin/bitcoin#25977 (comment). Also, fix iwyu for touched header files. ACKs for top commit: TheCharlatan: ACK fa5680b stickies-v: ACK fa5680b Tree-SHA512: c3509103bfeae246e2cf565bc561fcd68d8118515bac5431ba5304c3a63c8254b9c4f40e268b6f6d6b79405675c5a960db9b4eb3bdd14aedca333dc1c9e76415
…ult return type is an error fa5680b fix includes for touched header files (iwyu) (MarcoFalke) dddde27 Add [[nodiscard]] where ignoring a Result return type is an error (MarcoFalke) Pull request description: Only add it for those where it is an error to ignore. Also, fix the gcc compile warning bitcoin#25977 (comment). Also, fix iwyu for touched header files. ACKs for top commit: TheCharlatan: ACK fa5680b stickies-v: ACK fa5680b Tree-SHA512: c3509103bfeae246e2cf565bc561fcd68d8118515bac5431ba5304c3a63c8254b9c4f40e268b6f6d6b79405675c5a960db9b4eb3bdd14aedca333dc1c9e76415
Summary: > Useful to encapsulate the function result object (in case of having it) or, in case of failure, the failure reason. > > This let us clean lot of boilerplate code, as now instead of returning a boolean and having to add a ref arg for the > return object and another ref for the error string. We can simply return a 'BResult<Obj>'. > > Example of what we currently have: > ``` > bool doSomething(arg1, arg2, arg3, arg4, &result, &error_string) { > do something... > if (error) { > error_string = "something bad happened"; > return false; > } > > result = goodResult; > return true; > } > ``` > > Example of what we will get with this commit: > ``` > BResult<Obj> doSomething(arg1, arg2, arg3, arg4) { > do something... > if (error) return {"something happened"}; > > // good > return {goodResult}; > } > ``` > > This allows a similar boilerplate cleanup on the function callers side as well. They don't have to add the extra > pre-function-call error string and result object declarations to pass the references to the function. bitcoin/bitcoin@7a45c33 ---- > Prepare BResult for non-copyable types bitcoin/bitcoin@fa8de09 ---- > Refactor: Replace BResult with util::Result > > Rename `BResult` class to `util::Result` and update the class interface to be > more compatible with `std::optional` and with a full-featured result class > implemented in bitcoin/bitcoin#25665. Motivation for > this change is to update existing `BResult` usages now so they don't have to > change later when more features are added in #25665. > > This change makes the following improvements originally implemented in #25665: > > - More explicit API. Drops potentially misleading `BResult` constructor that > treats any bilingual string argument as an error. Adds `util::Error` > constructor so it is never ambiguous when a result is being assigned an error > or non-error value. > > - Better type compatibility. Supports `util::Result<bilingual_str>` return > values to hold translated messages which are not errors. > > - More standard and consistent API. `util::Result` supports most of the same > operators and methods as `std::optional`. `BResult` had a less familiar > interface with `HasRes`/`GetObj`/`ReleaseObj` methods. The Result/Res/Obj > naming was also not internally consistent. > > - Better code organization. Puts `src/util/` code in the `util::` namespace so > naming reflects code organization and it is obvious where the class is coming > from. Drops "B" from name because it is undocumented what it stands for > (bilingual?) > > - Has unit tests. bitcoin/bitcoin@a23cca5 ---- > util: Add void support to util::Result > > A minimal (but hacky) way to add support for void to Result > originally posted bitcoin/bitcoin#27632 (comment) bitcoin/bitcoin@5f49cb1 ---- This is a partial backport of [[bitcoin/bitcoin#25218 | core#25218]], [[bitcoin/bitcoin#25594 | core#25594]], [[bitcoin/bitcoin#25721 | core#25721]] and [[bitcoin/bitcoin#25977 | core#25977]] The wallet use cases in the first 3 pull requests are not yet applicable to Bitcoin ABC due to missing dependencies. The `std::optional<bilingual_str>` use cases from [[bitcoin/bitcoin#25977 | core#25977]] will be backported in their own diff. Test Plan: `ninja && ninja check` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Subscribers: Fabien Differential Revision: https://reviews.bitcoinabc.org/D14991
Summary: > refactor: Replace std::optional<bilingual_str> with util::Result bitcoin/bitcoin@8aa8f73 > Add [[nodiscard]] where ignoring a Result return type is an error bitcoin/bitcoin@dddde27 > fix includes for touched header files (iwyu) bitcoin/bitcoin@fa5680b This is a partial backport of [[bitcoin/bitcoin#25977 | core#25977]] and [[bitcoin/bitcoin#27774 | core#27774]] Test Plan: `ninja all check-all` `grep -r 'std::optional<bilingual_str>' src/` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D14992
Replace uses of
std::optional<bilingual_str>
withutil::Result
as suggested #25648 (comment), #27632 (comment), #27632 (comment), #24313 (comment)