Skip to content

Conversation

@elichai
Copy link
Contributor

@elichai elichai commented Jan 17, 2020

Hi,
This introduces a level of abstractions for boost::variant such that when we switch to C++17 we can drop in std::variant instead and everything should work modulo the visitors.

The reason I went for a class and not a typedef/using like with Optional is because I want to add the get function so "users" won't need to still include boost/variant/get.hpp and because I wanted to make it more explicit when get is throwing and when not (by splitting to get and get_if like stl does).

I also replaced the generic boost/variant.hpp with boost/variant/get.hpp and boost/variant/variant.hpp which are just 2 out of the 7 headers in boost/variant.hpp(https://www.boost.org/doc/libs/1_71_0/boost/variant.hpp) (Had to remove this since it broke a build, not sure if it's because a specific boost version or a clang version https://travis-ci.org/bitcoin/bitcoin/jobs/638552502)

template <typename T>
void operator==(const T&) const
{
static_assert(false && sizeof(T), "Compared a Variant directly with type T. this prevented an implicit conversion");
Copy link
Member

Choose a reason for hiding this comment

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

for reviewers, what's the sizeof(T) trick here?

Copy link
Contributor Author

@elichai elichai Jan 17, 2020

Choose a reason for hiding this comment

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

right forgot to mention that.
if I just do static_assert(false, msg) it won't compile no matter what.
now because the sizeof(T) is here then it can't evaluate the expression without monomorphising the function so it can know what T is. so as long as this function is never called the compiler won't fail compilation because it will never be able to evaluate it.

ie without: https://godbolt.org/z/ZGAFQB (fail compilation even though it isn't being called)
with: https://godbolt.org/z/JCDeeM (doesn't fail because can't evaluate)
with and used: https://godbolt.org/z/KCfNSV (fail compilation)

FYI I stole that trick from: https://www.boost.org/doc/libs/1_71_0/boost/variant/variant.hpp (see BOOST_STATIC_ASSERT)

@DrahtBot
Copy link
Contributor

DrahtBot commented Jan 18, 2020

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

Conflicts

Reviewers, 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.

@fanquake fanquake requested a review from ryanofsky January 23, 2020 02:16
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

Code review ACK 497a77358ff5a3baa62b96d8d83a8edf5d72ddd1

src/Makefile.am Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems consistent since our optional and span replacements are also at the top level. But I wonder if they should all move to src/util/ or some place to keep the top level less crowded. Probably something to deal with in a different PR

Copy link
Member

Choose a reason for hiding this comment

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

See also #15732

@elichai
Copy link
Contributor Author

elichai commented Jan 24, 2020

Not sure what's the problem in s390x, https://travis-ci.org/bitcoin/bitcoin/jobs/641327078 seems to be linker related. (Not happening anymore, CI related)

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

Code review ACK 08bf1e5e213a5926a41433b6c532867aca48ca12. Only change since last review is adding and removing some constructor and assignment overloads

Not sure what's the problem in s390x, https://travis-ci.org/bitcoin/bitcoin/jobs/641327078 seems to be linker related.

It looks like the build is just timing out during the link step and being terminated. I'd guess it's not related to this PR.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

Code review ACK 86870ff. Only change since last review is switching to std::decay

@maflcko
Copy link
Member

maflcko commented Jan 31, 2020

Not sure what the point of this is, but aren't we going to switch to c++17 soon anyway? In that case we could just replace it once with the std::variant instead of first wrapping it and then switching it around in the wrapper.

@elichai
Copy link
Contributor Author

elichai commented Jan 31, 2020

Not sure what the point of this is, but aren't we going to switch to c++17 soon anyway? In that case we could just replace it once with the std::variant instead of first wrapping it and then switching it around in the wrapper.

The point is to lay the ground by not depending directly on boost everywhere, and also making it more obvious when get and get_if is used.
that way the actual removal of boost::variant is just 2-3 lines in this file.

I think that's the same goal that fs.h and optional.h service.


#include <node/transaction.h>
#include <outputtype.h>
#include <variant.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

Alphabetical order of includes?

@@ -0,0 +1,153 @@
// Copyright (c) 2017-2019 The Bitcoin Core developers
Copy link
Contributor

Choose a reason for hiding this comment

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

2020?

#define BITCOIN_VARIANT_H

#include <utility>
#include <type_traits>
Copy link
Contributor

Choose a reason for hiding this comment

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

Alphabetical order of includes?

@DrahtBot
Copy link
Contributor

Needs rebase

@adamjonas
Copy link
Member

Checking in on the value of going forward with this since C++17 is ~5 months away and there is still some work to be done to get this merged (rebase).

@ryanofsky
Copy link
Contributor

Checking in on the value of going forward with this since C++17 is ~5 months away and there is still some work to be done to get this merged (rebase).

I guess other reviewers have to weight in, but I'm happy with the approach here and would re-ack if rebased. I think this PR still makes sense with even an upcoming transition to std::variant, because it brings existing variant code into alignment with the std::variant API so fewer changes to this code are needed later, even though some temporary util code is added now

@maflcko
Copy link
Member

maflcko commented May 13, 2020

it brings existing variant code into alignment with the std::variant API

It does not. The boost::variant and std::variant APIs are not identical. The API variant that is introduced in this pull is identical to neither of the "official" ones. So if this is merged, we will end up with three APIs to keep in mind and maintain. I just don't see the value of spending time on review when the changes here can be done (in theory) with a trivial scripted diff. See https://github.com/MarcoFalke/bitcoin-core/pull/new/2005-StdVariantScriptedDiff

The changes here are already complicated and they even miss the most complicated part (boost visitor).

Feel free to disagree, but going forward, I am hoping to see something like #18863 merged for 0.21, so that the diff in my branch is reduced for 0.22.

@maflcko
Copy link
Member

maflcko commented May 13, 2020

To compare the two branches:

  • 2005-StdVariantScriptedDiff: 107 additions and 127 deletions (removes boost completely in one go)
  • 2020-01-variant: Adds a file with 155 lines + other changes (is only the first step to remove boost, needs follow-ups and eternal maintenance)

@ryanofsky
Copy link
Contributor

That all makes sense. So it sounds like even though this PR has some merits, it conflicts with better alternatives and may not be worth effort to review and rebase now and update for future changes later.

@elichai elichai closed this May 14, 2020
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
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.

9 participants