From b9a026117b5be8e9fde921365d45ac65e963db93 Mon Sep 17 00:00:00 2001 From: Daniel Oliveira Date: Wed, 18 Nov 2026 14:30:06 +0100 Subject: [PATCH 1/2] Add AttributeBag.Clone A copied node shares its attribute map with the original unless the bag is cloned, so setting an attribute on the copy changed the original. --- pkg/ast/attribute_bag.go | 11 ++++++++++- pkg/ast/attribute_bag_test.go | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pkg/ast/attribute_bag.go b/pkg/ast/attribute_bag.go index eb1b311..fd7694b 100644 --- a/pkg/ast/attribute_bag.go +++ b/pkg/ast/attribute_bag.go @@ -1,6 +1,9 @@ package ast -import "reflect" +import ( + "maps" + "reflect" +) // AttributeBag is embedded in every AST node to carry metadata assigned after // parsing, such as a resolved type. Attributes are keyed by string and allocated @@ -28,3 +31,9 @@ func (bag *AttributeBag) GetAttribute(key string) (any, bool) { value, ok := bag.attributes[key] return value, ok } + +// Clone returns a bag with its own copy of the attributes, for a copied +// node whose attributes must not change the original's. +func (bag AttributeBag) Clone() AttributeBag { + return AttributeBag{attributes: maps.Clone(bag.attributes)} +} diff --git a/pkg/ast/attribute_bag_test.go b/pkg/ast/attribute_bag_test.go index c2ec03b..94780e3 100644 --- a/pkg/ast/attribute_bag_test.go +++ b/pkg/ast/attribute_bag_test.go @@ -48,3 +48,22 @@ func TestNodeCarriesAttributes(test *testing.T) { test.Fatalf("node attribute not stored: got %v, %v", value, ok) } } + +func TestAttributeBagClone(test *testing.T) { + var bag AttributeBag + bag.SetAttribute("type", "User") + + clone := bag.Clone() + clone.SetAttribute("type", "Admin") + if value, _ := bag.GetAttribute("type"); value != "User" { + test.Fatalf("setting the clone changed the original: got %v", value) + } + if value, _ := clone.GetAttribute("type"); value != "Admin" { + test.Fatalf("got %v; want Admin", value) + } + + var empty AttributeBag + if clone := empty.Clone(); !clone.Equal(empty) { + test.Fatal("the clone of an empty bag should equal it") + } +} From 129ca7c5917fd6f60bd30333f866d648571aa0f4 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 23 Nov 2026 07:49:04 +0100 Subject: [PATCH 2/2] Rename Clone to CloneAttributes, since every node embeds the bag; document the shallow copy --- pkg/ast/attribute_bag.go | 7 ++++--- pkg/ast/attribute_bag_test.go | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pkg/ast/attribute_bag.go b/pkg/ast/attribute_bag.go index fd7694b..04dd79b 100644 --- a/pkg/ast/attribute_bag.go +++ b/pkg/ast/attribute_bag.go @@ -32,8 +32,9 @@ func (bag *AttributeBag) GetAttribute(key string) (any, bool) { return value, ok } -// Clone returns a bag with its own copy of the attributes, for a copied -// node whose attributes must not change the original's. -func (bag AttributeBag) Clone() AttributeBag { +// CloneAttributes returns a bag with its own attribute map, for a copied +// node whose attributes must not change the original's. The copy is shallow: +// attribute values themselves (pointers, slices, maps) are shared. +func (bag AttributeBag) CloneAttributes() AttributeBag { return AttributeBag{attributes: maps.Clone(bag.attributes)} } diff --git a/pkg/ast/attribute_bag_test.go b/pkg/ast/attribute_bag_test.go index 94780e3..ac26344 100644 --- a/pkg/ast/attribute_bag_test.go +++ b/pkg/ast/attribute_bag_test.go @@ -49,11 +49,11 @@ func TestNodeCarriesAttributes(test *testing.T) { } } -func TestAttributeBagClone(test *testing.T) { +func TestAttributeBagCloneAttributes(test *testing.T) { var bag AttributeBag bag.SetAttribute("type", "User") - clone := bag.Clone() + clone := bag.CloneAttributes() clone.SetAttribute("type", "Admin") if value, _ := bag.GetAttribute("type"); value != "User" { test.Fatalf("setting the clone changed the original: got %v", value) @@ -63,7 +63,7 @@ func TestAttributeBagClone(test *testing.T) { } var empty AttributeBag - if clone := empty.Clone(); !clone.Equal(empty) { + if clone := empty.CloneAttributes(); !clone.Equal(empty) { test.Fatal("the clone of an empty bag should equal it") } }