diff --git a/AK/Variant.h b/AK/Variant.h index ad73d7bd23e..b872233ff72 100644 --- a/AK/Variant.h +++ b/AK/Variant.h @@ -80,16 +80,16 @@ struct Variant { template struct VisitImpl { - template - ALWAYS_INLINE static constexpr decltype(auto) visit(IndexType id, const void* data, Visitor&& visitor) requires(CurrentIndex < sizeof...(Ts)) + template + ALWAYS_INLINE static constexpr decltype(auto) visit(Self& self, IndexType id, const void* data, Visitor&& visitor) requires(CurrentIndex < sizeof...(Ts)) { using T = typename TypeList::template Type; if (id == CurrentIndex) - return visitor(*bit_cast(data)); + return visitor(*bit_cast*>(data)); if constexpr ((CurrentIndex + 1) < sizeof...(Ts)) - return visit(id, data, forward(visitor)); + return visit(self, id, data, forward(visitor)); else VERIFY_NOT_REACHED(); } @@ -367,14 +367,14 @@ public: ALWAYS_INLINE decltype(auto) visit(Fs&&... functions) { Visitor visitor { forward(functions)... }; - return VisitHelper::visit(m_index, m_data, move(visitor)); + return VisitHelper::visit(*this, m_index, m_data, move(visitor)); } template ALWAYS_INLINE decltype(auto) visit(Fs&&... functions) const { Visitor visitor { forward(functions)... }; - return VisitHelper::visit(m_index, m_data, move(visitor)); + return VisitHelper::visit(*this, m_index, m_data, move(visitor)); } template diff --git a/Tests/AK/TestVariant.cpp b/Tests/AK/TestVariant.cpp index f05c629022a..af87669671c 100644 --- a/Tests/AK/TestVariant.cpp +++ b/Tests/AK/TestVariant.cpp @@ -31,9 +31,22 @@ TEST_CASE(visit) bool correct = false; Variant the_value { 42.0f }; the_value.visit( - [&](const int&) { correct = false; }, - [&](const String&) { correct = false; }, - [&](const float&) { correct = true; }); + [&](int const&) { correct = false; }, + [&](String const&) { correct = false; }, + [&](float const&) { correct = true; }); + EXPECT(correct); +} + +TEST_CASE(visit_const) +{ + bool correct = false; + Variant const the_value { "42"sv }; + + the_value.visit( + [&](String const&) { correct = true; }, + [&](auto&) {}, + [&](auto const&) {}); + EXPECT(correct); } @@ -139,9 +152,9 @@ TEST_CASE(return_values) MyVariant the_value { 42.0f }; float value = the_value.visit( - [&](const int&) { return 1.0f; }, - [&](const String&) { return 2.0f; }, - [&](const float& f) { return f; }); + [&](int const&) { return 1.0f; }, + [&](String const&) { return 2.0f; }, + [&](float const& f) { return f; }); EXPECT_EQ(value, 42.0f); } { @@ -157,9 +170,9 @@ TEST_CASE(return_values) const MyVariant the_value { "str" }; String value = the_value.visit( - [&](const int&) { return String { "wrong" }; }, - [&](const String& s) { return s; }, - [&](const float&) { return String { "wrong" }; }); + [&](int const&) { return String { "wrong" }; }, + [&](String const& s) { return s; }, + [&](float const&) { return String { "wrong" }; }); EXPECT_EQ(value, "str"); } } @@ -170,9 +183,9 @@ TEST_CASE(return_values_by_reference) Variant the_value { 42.0f }; auto& value = the_value.visit( - [&](const int&) -> RefPtr& { return ref; }, - [&](const String&) -> RefPtr& { return ref; }, - [&](const float&) -> RefPtr& { return ref; }); + [&](int const&) -> RefPtr& { return ref; }, + [&](String const&) -> RefPtr& { return ref; }, + [&](float const&) -> RefPtr& { return ref; }); EXPECT_EQ(ref, value); EXPECT_EQ(ref->ref_count(), 1u); diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index 653a0160968..dcf0c07477d 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -2507,7 +2507,7 @@ Completion AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& // AssignmentExpression : LeftHandSideExpression = AssignmentExpression return m_lhs.visit( // 1. If LeftHandSideExpression is neither an ObjectLiteral nor an ArrayLiteral, then - [&](NonnullRefPtr& lhs) -> ThrowCompletionOr { + [&](NonnullRefPtr const& lhs) -> ThrowCompletionOr { // a. Let lref be the result of evaluating LeftHandSideExpression. // b. ReturnIfAbrupt(lref). auto reference = TRY(lhs->to_reference(interpreter, global_object)); @@ -2534,7 +2534,7 @@ Completion AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& return rhs_result; }, // 2. Let assignmentPattern be the AssignmentPattern that is covered by LeftHandSideExpression. - [&](NonnullRefPtr& pattern) -> ThrowCompletionOr { + [&](NonnullRefPtr const& pattern) -> ThrowCompletionOr { // 3. Let rref be the result of evaluating AssignmentExpression. // 4. Let rval be ? GetValue(rref). auto rhs_result = TRY(m_rhs->execute(interpreter, global_object)).release_value(); diff --git a/Userland/Libraries/LibRegex/RegexMatch.h b/Userland/Libraries/LibRegex/RegexMatch.h index 846b24dec71..ddf402c9f75 100644 --- a/Userland/Libraries/LibRegex/RegexMatch.h +++ b/Userland/Libraries/LibRegex/RegexMatch.h @@ -201,7 +201,7 @@ public: views.empend(view); return views; }, - [](Utf8View& view) { + [](Utf8View const& view) { Vector views; auto it = view.begin(); auto previous_newline_position_it = it; diff --git a/Userland/Libraries/LibWeb/CSS/Length.cpp b/Userland/Libraries/LibWeb/CSS/Length.cpp index 2cb999f7b19..9f51e56fc76 100644 --- a/Userland/Libraries/LibWeb/CSS/Length.cpp +++ b/Userland/Libraries/LibWeb/CSS/Length.cpp @@ -123,10 +123,10 @@ static float resolve_calc_value(CalculatedStyleValue::CalcValue const& calc_valu { return calc_value.visit( [](float value) { return value; }, - [&](Length length) { + [&](Length const& length) { return length.resolved_or_zero(layout_node, reference_for_percent).to_px(layout_node); }, - [&](NonnullOwnPtr& calc_sum) { + [&](NonnullOwnPtr const& calc_sum) { return resolve_calc_sum(calc_sum, layout_node, reference_for_percent); }, [](auto&) { @@ -173,7 +173,7 @@ static float resolve_calc_number_value(CalculatedStyleValue::CalcNumberValue con { return number_value.visit( [](float number) { return number; }, - [](NonnullOwnPtr& calc_number_sum) { + [](NonnullOwnPtr const& calc_number_sum) { return resolve_calc_number_sum(calc_number_sum); }); } diff --git a/Userland/Libraries/LibWeb/CSS/MediaQuery.cpp b/Userland/Libraries/LibWeb/CSS/MediaQuery.cpp index f25aa62fdae..e872bf3b830 100644 --- a/Userland/Libraries/LibWeb/CSS/MediaQuery.cpp +++ b/Userland/Libraries/LibWeb/CSS/MediaQuery.cpp @@ -23,16 +23,16 @@ NonnullRefPtr MediaQuery::create_not_all() String MediaFeatureValue::to_string() const { return m_value.visit( - [](String& ident) { return serialize_an_identifier(ident); }, - [](Length& length) { return length.to_string(); }, + [](String const& ident) { return serialize_an_identifier(ident); }, + [](Length const& length) { return length.to_string(); }, [](double number) { return String::number(number); }); } bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const { return m_value.visit( - [&](String&) { return other.is_ident(); }, - [&](Length&) { return other.is_length(); }, + [&](String const&) { return other.is_ident(); }, + [&](Length const&) { return other.is_length(); }, [&](double) { return other.is_number(); }); } diff --git a/Userland/Libraries/LibWeb/CSS/Supports.cpp b/Userland/Libraries/LibWeb/CSS/Supports.cpp index ed375d7a115..97e4d7d0736 100644 --- a/Userland/Libraries/LibWeb/CSS/Supports.cpp +++ b/Userland/Libraries/LibWeb/CSS/Supports.cpp @@ -35,13 +35,13 @@ MatchResult Supports::Condition::evaluate() const MatchResult Supports::InParens::evaluate() const { return value.visit( - [&](NonnullOwnPtr& condition) { + [&](NonnullOwnPtr const& condition) { return condition->evaluate(); }, - [&](Feature& feature) { + [&](Feature const& feature) { return feature.evaluate(); }, - [&](GeneralEnclosed& general_enclosed) { + [&](GeneralEnclosed const& general_enclosed) { return general_enclosed.evaluate(); }); }