From bb26c5c1bdf7d561402cebd37a8d3ad09a604f85 Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Thu, 11 Apr 2024 09:43:57 +0000 Subject: [PATCH] MaskedTransformer: Fix in-place rebuilding of scoped nodes --- loki/ir/transformer.py | 2 +- tests/test_visitor.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/loki/ir/transformer.py b/loki/ir/transformer.py index 140848bd6..4de3cfa11 100644 --- a/loki/ir/transformer.py +++ b/loki/ir/transformer.py @@ -485,7 +485,7 @@ def visit_ScopedNode(self, o, **kwargs): # Update rebuilt node if kwargs['parent_active']: - o._update(rebuilt) + o._update(*rebuilt) return o return tuple(i for i in rebuilt if i is not None) or None diff --git a/tests/test_visitor.py b/tests/test_visitor.py index 659542747..5fd9a941d 100644 --- a/tests/test_visitor.py +++ b/tests/test_visitor.py @@ -979,6 +979,14 @@ def test_masked_transformer_associates(frontend): assert len(FindNodes(Assignment).visit(body)) == 3 assert not FindNodes(Associate).visit(body) + # Retains all nodes but the last, but check with ``inplace=True`` + body = MaskedTransformer(start=None, stop=assignments[-1], active=True, inplace=True).visit(routine.body) + assert len(FindNodes(Assignment).visit(body)) == len(assignments) - 1 + assocs = FindNodes(Associate).visit(body) + assert len(assocs) == 1 + assert len(assocs[0].body) == len(assignments) - 1 + assert all(isinstance(n, Assignment) for n in assocs[0].body) + @pytest.mark.parametrize('frontend', available_frontends()) def test_nested_masked_transformer(frontend):