From 4cdd1e095eda1fd5fd076e70d43411d16ea2e82c Mon Sep 17 00:00:00 2001 From: Scott Anderson <662325+scottanderson@users.noreply.github.com> Date: Sun, 8 Nov 2026 18:02:39 -0500 Subject: [PATCH] fix: Read match arm members from the current section A primitive in a match arm read its value from the main section. This occurred when its struct was placed in a custom section. The match statement did not set the section on its patterns. The if and try/catch statements already do this. --- .../pl/core/ast/ast_node_match_statement.cpp | 1 + tests/CMakeLists.txt | 1 + .../test_pattern_section_statements.hpp | 57 +++++++++++++++++++ tests/source/tests.cpp | 2 + 4 files changed, 61 insertions(+) create mode 100644 tests/include/test_patterns/test_pattern_section_statements.hpp diff --git a/lib/source/pl/core/ast/ast_node_match_statement.cpp b/lib/source/pl/core/ast/ast_node_match_statement.cpp index 15abe40f..a28618c4 100644 --- a/lib/source/pl/core/ast/ast_node_match_statement.cpp +++ b/lib/source/pl/core/ast/ast_node_match_statement.cpp @@ -33,6 +33,7 @@ namespace pl::core::ast { std::vector> newPatterns; node->createPatterns(evaluator, newPatterns); for (auto &pattern : newPatterns) { + pattern->setSection(evaluator->getSectionId()); scope.push_back(std::move(pattern)); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 292b15f5..94767010 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -69,6 +69,7 @@ set(AVAILABLE_TESTS DuplicateVariableFail StaticArrayRangeOverflowFail Dollar + SectionStatements InVariableDefault InVariableOverride SetEntriesSortedConsistency diff --git a/tests/include/test_patterns/test_pattern_section_statements.hpp b/tests/include/test_patterns/test_pattern_section_statements.hpp new file mode 100644 index 00000000..b240e791 --- /dev/null +++ b/tests/include/test_patterns/test_pattern_section_statements.hpp @@ -0,0 +1,57 @@ +#pragma once + +#include "test_pattern.hpp" + +namespace pl::test { + + class TestPatternSectionStatements : public TestPattern { + public: + TestPatternSectionStatements(core::Evaluator *evaluator) : TestPattern(evaluator, "SectionStatements") { + } + ~TestPatternSectionStatements() override = default; + + [[nodiscard]] std::string getSourceCode() const override { + return R"( + auto section = builtin::std::mem::create_section("data"); + builtin::std::mem::copy_value_to_section("ABCDEFGHIJKL", section, 0); + + struct Inner { + u8 a; + }; + + struct S { + u8 plain; + match (plain) { + (_): u8 inMatch; + } + if (true) { + u8 inIf; + } + try { + u8 inTry; + } catch { } + try { + u8 failing[1] @ 0x100; + u8 failed = failing[1]; + } catch { + u8 inCatch; + } + Inner inner; + match (plain) { + (_): Inner structInMatch; + } + }; + + S s @ 0x00 in section; + std::assert(s.plain == 0x41, "Struct member did not read from section"); + std::assert(s.inMatch == 0x42, "Match arm member did not read from section"); + std::assert(s.inIf == 0x43, "If body member did not read from section"); + std::assert(s.inTry == 0x44, "Try body member did not read from section"); + std::assert(s.inCatch == 0x45, "Catch body member did not read from section"); + std::assert(s.inner.a == 0x46, "Nested struct member did not read from section"); + std::assert(s.structInMatch.a == 0x47, "Struct in match arm did not read from section"); + )"; + } + }; + +} diff --git a/tests/source/tests.cpp b/tests/source/tests.cpp index 0e6510ef..07a60a07 100644 --- a/tests/source/tests.cpp +++ b/tests/source/tests.cpp @@ -39,6 +39,7 @@ #include "test_patterns/test_pattern_extended_semantics.hpp" #include "test_patterns/test_pattern_error_semantics.hpp" #include "test_patterns/test_pattern_dollar.hpp" +#include "test_patterns/test_pattern_section_statements.hpp" #include "test_patterns/test_pattern_in_variable_defaults.hpp" #include "test_patterns/test_pattern_set_entries_sorted_consistency.hpp" #include "test_patterns/test_pattern_allowed_redefinitions.hpp" @@ -112,6 +113,7 @@ std::array Tests = { TEST(DuplicateVariableFail), TEST(StaticArrayRangeOverflowFail), TEST(Dollar), + TEST(SectionStatements), TEST(InVariableDefault), TEST(InVariableOverride), TEST(SetEntriesSortedConsistency),