commit 80a138ca12d65bc0bd39cfdf50b056c15b678645
parent 58640982c4ce1ff31792afb88187048eba648511
Author: Jan de Mooij <jdemooij@mozilla.com>
Date: Thu, 13 Nov 2025 19:00:01 +0000
Bug 2000015 - Use the C++20 spaceship operator in some places instead of defining operator{<,<=,>,>=} manually. r=arai
Also use `operator==(..) = default` in a few places.
Differential Revision: https://phabricator.services.mozilla.com/D272500
Diffstat:
7 files changed, 24 insertions(+), 102 deletions(-)
diff --git a/js/src/frontend/BytecodeOffset.h b/js/src/frontend/BytecodeOffset.h
@@ -10,6 +10,7 @@
#include "mozilla/Assertions.h" // MOZ_ASSERT
#include "mozilla/CheckedInt.h" // mozilla::CheckedInt
+#include <compare> // std::strong_ordering
#include <stddef.h> // ptrdiff_t
namespace js {
@@ -38,11 +39,7 @@ class BytecodeOffset {
return BytecodeOffset(Invalid());
}
- bool operator==(const BytecodeOffset& rhs) const {
- return value_ == rhs.value_;
- }
-
- bool operator!=(const BytecodeOffset& rhs) const { return !(*this == rhs); }
+ constexpr bool operator==(const BytecodeOffset& rhs) const = default;
inline BytecodeOffsetDiff operator-(const BytecodeOffset& rhs) const;
inline BytecodeOffset operator+(const BytecodeOffsetDiff& diff) const;
@@ -50,25 +47,10 @@ class BytecodeOffset {
inline BytecodeOffset& operator+=(const BytecodeOffsetDiff& diff);
inline BytecodeOffset& operator-=(const BytecodeOffsetDiff& diff);
- bool operator<(const BytecodeOffset& rhs) const {
- MOZ_ASSERT(valid());
- MOZ_ASSERT(rhs.valid());
- return value_ < rhs.value_;
- }
- bool operator<=(const BytecodeOffset& rhs) const {
- MOZ_ASSERT(valid());
- MOZ_ASSERT(rhs.valid());
- return value_ <= rhs.value_;
- }
- bool operator>(const BytecodeOffset& rhs) const {
- MOZ_ASSERT(valid());
- MOZ_ASSERT(rhs.valid());
- return value_ > rhs.value_;
- }
- bool operator>=(const BytecodeOffset& rhs) const {
+ auto operator<=>(const BytecodeOffset& rhs) const {
MOZ_ASSERT(valid());
MOZ_ASSERT(rhs.valid());
- return value_ >= rhs.value_;
+ return value_ <=> rhs.value_;
}
ptrdiff_t value() const { return value_; }
@@ -91,13 +73,7 @@ class BytecodeOffsetDiff {
explicit constexpr BytecodeOffsetDiff(ptrdiff_t value_) : value_(value_) {}
- bool operator==(const BytecodeOffsetDiff& rhs) const {
- return value_ == rhs.value_;
- }
-
- bool operator!=(const BytecodeOffsetDiff& rhs) const {
- return !(*this == rhs);
- }
+ constexpr bool operator==(const BytecodeOffsetDiff& rhs) const = default;
BytecodeOffsetDiff operator+(const BytecodeOffsetDiff& rhs) const {
mozilla::CheckedInt<ptrdiff_t> result = value_;
diff --git a/js/src/frontend/Token.h b/js/src/frontend/Token.h
@@ -14,6 +14,7 @@
#include "mozilla/Assertions.h" // MOZ_ASSERT
+#include <compare> // std::strong_ordering
#include <stdint.h> // uint32_t
#include "frontend/ParserAtom.h" // TaggedParserAtomIndex, TrivialTaggedParserAtomIndex
@@ -39,22 +40,12 @@ struct TokenPos {
return TokenPos(left.begin, right.end);
}
- bool operator==(const TokenPos& bpos) const {
- return begin == bpos.begin && end == bpos.end;
- }
+ constexpr bool operator==(const TokenPos& bpos) const = default;
- bool operator!=(const TokenPos& bpos) const {
- return begin != bpos.begin || end != bpos.end;
+ constexpr auto operator<=>(const TokenPos& bpos) const {
+ return begin <=> bpos.begin;
}
- bool operator<(const TokenPos& bpos) const { return begin < bpos.begin; }
-
- bool operator<=(const TokenPos& bpos) const { return begin <= bpos.begin; }
-
- bool operator>(const TokenPos& bpos) const { return !(*this <= bpos); }
-
- bool operator>=(const TokenPos& bpos) const { return !(*this < bpos); }
-
bool encloses(const TokenPos& pos) const {
return begin <= pos.begin && pos.end <= end;
}
diff --git a/js/src/frontend/TypedIndex.h b/js/src/frontend/TypedIndex.h
@@ -7,6 +7,7 @@
#ifndef frontend_TypedIndex_h
#define frontend_TypedIndex_h
+#include <compare> // std::strong_ordering
#include <cstdint>
#include <stddef.h>
@@ -30,10 +31,7 @@ struct TypedIndex {
return *this;
}
- bool operator<(TypedIndex other) const { return index < other.index; }
- bool operator<=(TypedIndex other) const { return index <= other.index; }
- bool operator>(TypedIndex other) const { return index > other.index; }
- bool operator>=(TypedIndex other) const { return index >= other.index; }
+ constexpr auto operator<=>(const TypedIndex& other) const = default;
};
} // namespace frontend
diff --git a/js/src/jit/RegisterAllocator.h b/js/src/jit/RegisterAllocator.h
@@ -9,6 +9,8 @@
#include "mozilla/MathAlgorithms.h"
+#include <compare> // std::strong_ordering
+
#include "jit/LIR.h"
#include "jit/MIRGenerator.h"
#include "jit/MIRGraph.h"
@@ -164,17 +166,7 @@ class CodePosition {
SubPosition subpos() const { return (SubPosition)(bits_ & SUBPOSITION_MASK); }
- bool operator<(CodePosition other) const { return bits_ < other.bits_; }
-
- bool operator<=(CodePosition other) const { return bits_ <= other.bits_; }
-
- bool operator!=(CodePosition other) const { return bits_ != other.bits_; }
-
- bool operator==(CodePosition other) const { return bits_ == other.bits_; }
-
- bool operator>(CodePosition other) const { return bits_ > other.bits_; }
-
- bool operator>=(CodePosition other) const { return bits_ >= other.bits_; }
+ constexpr auto operator<=>(const CodePosition& other) const = default;
uint32_t operator-(CodePosition other) const {
MOZ_ASSERT(bits_ >= other.bits_);
diff --git a/js/src/jit/shared/IonAssemblerBuffer.h b/js/src/jit/shared/IonAssemblerBuffer.h
@@ -11,6 +11,7 @@
#include "mozilla/MathAlgorithms.h"
#include <algorithm>
+#include <compare> // std::strong_ordering
#include "jit/ProcessExecutableMemory.h"
#include "jit/shared/Assembler-shared.h"
@@ -25,7 +26,7 @@ class BufferOffset {
public:
friend BufferOffset nextOffset();
- BufferOffset() : offset(INT_MIN) {}
+ constexpr BufferOffset() : offset(INT_MIN) {}
explicit BufferOffset(int offset_) : offset(offset_) {
MOZ_ASSERT(offset >= 0);
@@ -59,31 +60,9 @@ class BufferOffset {
}
return BOffImm(offset - other->offset());
}
-};
-
-inline bool operator<(BufferOffset a, BufferOffset b) {
- return a.getOffset() < b.getOffset();
-}
-
-inline bool operator>(BufferOffset a, BufferOffset b) {
- return a.getOffset() > b.getOffset();
-}
-
-inline bool operator<=(BufferOffset a, BufferOffset b) {
- return a.getOffset() <= b.getOffset();
-}
-inline bool operator>=(BufferOffset a, BufferOffset b) {
- return a.getOffset() >= b.getOffset();
-}
-
-inline bool operator==(BufferOffset a, BufferOffset b) {
- return a.getOffset() == b.getOffset();
-}
-
-inline bool operator!=(BufferOffset a, BufferOffset b) {
- return a.getOffset() != b.getOffset();
-}
+ constexpr auto operator<=>(const BufferOffset& other) const = default;
+};
template <int SliceSize>
class BufferSlice {
diff --git a/js/src/vm/BytecodeLocation.h b/js/src/vm/BytecodeLocation.h
@@ -7,6 +7,8 @@
#ifndef vm_BytecodeLocation_h
#define vm_BytecodeLocation_h
+#include <compare> // std::strong_ordering
+
#include "frontend/NameAnalysisTypes.h"
#include "js/TypeDecls.h"
#include "vm/BuiltinObjectKind.h"
@@ -140,21 +142,9 @@ class BytecodeLocation {
return !(other == *this);
}
- bool operator<(const BytecodeLocation& other) const {
+ auto operator<=>(const BytecodeLocation& other) const {
MOZ_ASSERT(this->debugOnlyScript_ == other.debugOnlyScript_);
- return rawBytecode_ < other.rawBytecode_;
- }
-
- // It is traditional to represent the rest of the relational operators
- // using operator<, so we don't need to assert for these.
- bool operator>(const BytecodeLocation& other) const { return other < *this; }
-
- bool operator<=(const BytecodeLocation& other) const {
- return !(other < *this);
- }
-
- bool operator>=(const BytecodeLocation& other) const {
- return !(*this < other);
+ return rawBytecode_ <=> other.rawBytecode_;
}
// Return the next bytecode
diff --git a/js/src/wasm/WasmMemory.h b/js/src/wasm/WasmMemory.h
@@ -22,6 +22,7 @@
#include "mozilla/CheckedInt.h"
#include "mozilla/Maybe.h"
+#include <compare> // std::strong_ordering
#include <stdint.h>
#include "js/Value.h"
@@ -124,12 +125,7 @@ struct Pages {
// Implement pass-through comparison operators so that Pages can be compared.
- bool operator==(Pages other) const { return value_ == other.value_; }
- bool operator!=(Pages other) const { return value_ != other.value_; }
- bool operator<=(Pages other) const { return value_ <= other.value_; }
- bool operator<(Pages other) const { return value_ < other.value_; }
- bool operator>=(Pages other) const { return value_ >= other.value_; }
- bool operator>(Pages other) const { return value_ > other.value_; }
+ constexpr auto operator<=>(const Pages& other) const = default;
};
// The largest number of pages the application can request.