Skip to content

Conversation

ryanofsky
Copy link
Contributor

@ryanofsky ryanofsky commented Sep 1, 2022

Replace uses of std::optional<bilingual_str> with util::Result as suggested #25648 (comment), #27632 (comment), #27632 (comment), #24313 (comment)

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 1, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK MarcoFalke, TheCharlatan, hebasto

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #27300 (build: debug enable addrman consistency checks by willcl-ark)
  • #26762 (refactor: Make CCheckQueue RAII-styled by hebasto)
  • #25665 (refactor: Add util::Result failure values, multiple error and warning messages by ryanofsky)

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.

@ryanofsky
Copy link
Contributor Author

ryanofsky commented Sep 12, 2022

Updated 9d3918d -> ebc493e (pr/bresult-opt.1 -> pr/bresult-opt.2, compare) to fix CI compile error https://cirrus-ci.com/task/6195420723412992?logs=ci#L2419
Rebased ebc493e -> f1a9c71 (pr/bresult-opt.2 -> pr/bresult-opt.3, compare) on top of pr/bresult2.24 to fix conflicts in base PR #25665
Rebased f1a9c71 -> fb94363 (pr/bresult-opt.3 -> pr/bresult-opt.4, compare) due to conflict with #25667
Rebased fb94363 -> 080268d (pr/bresult-opt.4 -> pr/bresult-opt.5, compare) due to conflict with #26251 and #26286
Rebased 080268d -> 2804dd4 (pr/bresult-opt.5 -> pr/bresult-opt.6, compare)
Rebased 2804dd4 -> 4883d30 (pr/bresult-opt.6 -> pr/bresult-opt.7, compare) on top of conflicted pr/bresult2.30,
Rebased 4883d30 -> 59c96ae (pr/bresult-opt.7 -> pr/bresult-opt.8, compare) due to conflict with #27254
Rebased 59c96ae -> 74655e5 (pr/bresult-opt.8 -> pr/bresult-opt.9, compare) on top of pr/bresult2.33 #27254
Rebased 74655e5 -> b2bcba0 (pr/bresult-opt.9 -> pr/bresult-opt.10, compare) dropping dependency on #27254

MarcoFalke and others added 2 commits May 24, 2023 08:55
@ryanofsky ryanofsky marked this pull request as ready for review May 24, 2023 13:45
@maflcko
Copy link
Member

maflcko commented May 24, 2023

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 return addrman; with return {std::move(addrman)};, or something like that (temporarily).

godbolt for clang-12 (broken): https://godbolt.org/z/1hraPjxrf

@ryanofsky
Copy link
Contributor Author

Thanks, added std::move as suggested.

Updated b2bcba0 -> 8aa8f73 (pr/bresult-opt.10 -> pr/bresult-opt.11, compare) with workaround for CI errors https://cirrus-ci.com/task/6456067106799616?logs=ci#L1528 https://cirrus-ci.com/task/5048692223246336?logs=ci#L1240 https://cirrus-ci.com/task/6174592130088960?logs=ci#L1328

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.

@maflcko
Copy link
Member

maflcko commented May 25, 2023

very nice ACK 8aa8f73 🏏

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: very nice ACK 8aa8f73adce72e1ae855b43413c1f65504423cb 🏏
FlnWlOVGKZVAPgirGB1Me+n1IHxeaKXXl2LHKPIpsnt+N2jG0ILNCy/guKB9V+K/JzCbDLy9h/UJBgDsXbspAw==

@maflcko maflcko requested a review from TheCharlatan May 25, 2023 07:18
@@ -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);
Copy link
Member

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?

Copy link
Contributor

@TheCharlatan TheCharlatan left a 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.

Copy link
Member

@hebasto hebasto left a 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5f49cb1

nit: Add #include <utility>?

@fanquake fanquake merged commit 4d13fe4 into bitcoin:master May 26, 2023
@hebasto
Copy link
Member

hebasto commented May 26, 2023

Looks like there is a clang bug. Could be fixed by either bumping it again, see #27682, but I am not sure how to do that for macOS. Or by replacing return addrman; with return {std::move(addrman)};, or something like that (temporarily).

Although GCC complains:

$ ./configure CONFIG_SITE=$PWD/depends/arm-linux-gnueabihf/share/config.site --enable-suppress-external-warnings CXXFLAGS="-Wextra -Wno-psabi"
$ make > /dev/null 
addrdb.cpp: In function ‘util::Result<std::unique_ptr<AddrMan> > LoadAddrman(const NetGroupManager&, const ArgsManager&)’:
addrdb.cpp:213:21: warning: redundant move in return statement [-Wredundant-move]
  213 |     return std::move(addrman); // std::move should be unneccessary but is temporarily needed to work around clang bug (https://github.com/bitcoin/bitcoin/pull/25977#issuecomment-1561270092)
      |            ~~~~~~~~~^~~~~~~~~
addrdb.cpp:213:21: note: remove ‘std::move’ call

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request May 26, 2023
… 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
@maflcko
Copy link
Member

maflcko commented May 29, 2023

Yeah, that's why I suggested {std::move()}, not std::move(), see #25977 (comment)

fanquake added a commit to bitcoin-core/gui that referenced this pull request May 30, 2023
…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
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request May 30, 2023
…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
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Dec 15, 2023
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
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Dec 15, 2023
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
@bitcoin bitcoin locked and limited conversation to collaborators May 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants