diff --git a/loki/transform/transform_inline.py b/loki/transform/transform_inline.py index 91f41b9bb..3f09e6ed0 100644 --- a/loki/transform/transform_inline.py +++ b/loki/transform/transform_inline.py @@ -239,7 +239,13 @@ def _map_unbound_dims(var, val): {v: v.clone(name=f'{member.name}_{v.name}') for v in duplicate_locals} ) member.spec = shadow_mapper.visit(member.spec) - member.body = shadow_mapper.visit(member.body) + + var_map = {} + duplicate_locals_names = {dl.name.lower() for dl in duplicate_locals} + for v in FindVariables(unique=False).visit(member.body): + if v.name.lower() in duplicate_locals_names: + var_map[v] = v.clone(name=f'{member.name}_{v.name}') + member.body = SubstituteExpressions(var_map).visit(member.body) # Get local variable declarations and hoist them decls = FindNodes(VariableDeclaration).visit(member.spec) diff --git a/tests/test_transform_inline.py b/tests/test_transform_inline.py index b5ac37bd0..30dcd683e 100644 --- a/tests/test_transform_inline.py +++ b/tests/test_transform_inline.py @@ -473,12 +473,11 @@ def test_inline_member_routines_variable_shadowing(frontend): fcode = """ subroutine outer() real :: x = 3 ! 'x' is real in outer. - real :: tmp = 0 real :: y y = 1.0 - call inner(tmp, y=y) - x = x + tmp + call inner(y) + x = x + y contains subroutine inner(y) @@ -515,7 +514,7 @@ def test_inline_member_routines_variable_shadowing(frontend): # Check inner 'y' was substituted, not renamed! assign = FindNodes(Assignment).visit(routine.body) assert routine.variable_map['y'] == 'y' - assert assign[2].lhs == 'y' and assign[2].rhs == 'y + sum(x)' + assert assign[2].lhs == 'y' and assign[2].rhs == 'y + sum(inner_x)' @pytest.mark.parametrize('frontend', available_frontends())