Skip to content

Commit

Permalink
Move comparison operators to subclasses of BCOffsetBase
Browse files Browse the repository at this point in the history
Summary:
Newer compilers get unhappy with this use of comparison operators and CRTP.
They can't seem to wrap their heads around inferring the T in `BCOffsetBase<T>`
for these operators, something about how in C++20 the operators are tried twice,
once with LHS vs RHS and once with RHS vs LHS.

The easy solution is to move the operators to the concrete subclasses and call it
a day.  GCC also doesn't like the use of the `auto` return type so that's made explicit.

Reviewed By: mpage

Differential Revision: D48442851

fbshipit-source-id: 60aed0ef88d7ee1a85f14be87f7ce1bdbf682e39
  • Loading branch information
Alex Malyshev authored and facebook-github-bot committed Aug 18, 2023
1 parent 922bcd6 commit fb84e34
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions Jit/bytecode_offsets.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,6 @@ class BCOffsetBase {
return value_;
}

// Comparison operators.

constexpr auto operator<=>(const T& other) const {
return operator<=>(other.value());
}

template <class TInt, class = std::enable_if_t<std::is_integral_v<TInt>>>
constexpr auto operator<=>(const TInt& other) const {
return value() <=> other;
}

constexpr bool operator==(const T& other) const {
return operator==(other.value());
}

template <class TInt, class = std::enable_if_t<std::is_integral_v<TInt>>>
constexpr bool operator==(const TInt& other) const {
return value() == other;
}

// Arithmetic operators.
T operator+(Py_ssize_t other) const {
return T{value() + other};
Expand Down Expand Up @@ -132,6 +112,26 @@ class BCOffset : public BCOffsetBase<BCOffset> {
BCOffset(BCIndex idx);

BCIndex asIndex() const;

// Comparison operators.

constexpr std::strong_ordering operator<=>(const BCOffset& other) const {
return value() <=> other.value();
}

template <class TInt, class = std::enable_if_t<std::is_integral_v<TInt>>>
constexpr std::strong_ordering operator<=>(const TInt& other) const {
return value() <=> other;
}

constexpr bool operator==(const BCOffset& other) const {
return value() == other.value();
}

template <class TInt, class = std::enable_if_t<std::is_integral_v<TInt>>>
constexpr bool operator==(const TInt& other) const {
return value() == other;
}
};

class BCIndex : public BCOffsetBase<BCIndex> {
Expand All @@ -141,6 +141,26 @@ class BCIndex : public BCOffsetBase<BCIndex> {
BCIndex(BCOffset offset);

BCOffset asOffset() const;

// Comparison operators.

constexpr std::strong_ordering operator<=>(const BCIndex& other) const {
return value() <=> other.value();
}

template <class TInt, class = std::enable_if_t<std::is_integral_v<TInt>>>
constexpr std::strong_ordering operator<=>(const TInt& other) const {
return value() <=> other;
}

constexpr bool operator==(const BCIndex& other) const {
return value() == other.value();
}

template <class TInt, class = std::enable_if_t<std::is_integral_v<TInt>>>
constexpr bool operator==(const TInt& other) const {
return value() == other;
}
};

inline BCOffset::BCOffset(BCIndex idx)
Expand Down

0 comments on commit fb84e34

Please sign in to comment.