From d76609c3862b8310f0220cd832f3bcf04211a9bb Mon Sep 17 00:00:00 2001 From: Peter Ebden Date: Mon, 1 Jul 2024 10:21:55 +0100 Subject: [PATCH] Fix list concat overwriting (#3167) * add test case * Fix * here too --- src/parse/asp/interpreter_test.go | 14 ++++++++++++ src/parse/asp/objects.go | 7 +++--- .../test_data/interpreter/list_concat.build | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/parse/asp/test_data/interpreter/list_concat.build diff --git a/src/parse/asp/interpreter_test.go b/src/parse/asp/interpreter_test.go index e49bf5f304..0d88547b04 100644 --- a/src/parse/asp/interpreter_test.go +++ b/src/parse/asp/interpreter_test.go @@ -686,3 +686,17 @@ func TestOperatorPrecedence(t *testing.T) { assert.EqualValues(t, False, s.Lookup("m")) assert.EqualValues(t, True, s.Lookup("n")) } + +func TestListConcatenation(t *testing.T) { + s, err := parseFile("src/parse/asp/test_data/interpreter/list_concat.build") + assert.NoError(t, err) + assert.EqualValues(t, pyList{ + pyString("apple"), + pyString("banana"), + pyString("edamame"), + pyString("fennel"), + pyString("tuna"), + pyString("baked beans"), + pyString("haribo"), + }, s.Lookup("fruit_veg_canned_food_and_sweets")) +} diff --git a/src/parse/asp/objects.go b/src/parse/asp/objects.go index 1992ddc9a0..cb93d3e7fa 100644 --- a/src/parse/asp/objects.go +++ b/src/parse/asp/objects.go @@ -3,6 +3,7 @@ package asp import ( "encoding/json" "fmt" + "slices" "sort" "strconv" "strings" @@ -359,11 +360,11 @@ func (l pyList) Operator(operator Operator, operand pyObject) pyObject { l2, ok := operand.(pyList) if !ok { if l2, ok := operand.(pyFrozenList); ok { - return append(l, l2.pyList...) + return slices.Clip(append(l, l2.pyList...)) } panic("Cannot add list and " + operand.Type()) } - return append(l, l2...) + return slices.Clip(append(l, l2...)) case In, NotIn: for _, item := range l { if item == operand { @@ -429,7 +430,7 @@ func (l pyList) Freeze() pyObject { // Repeat returns a copy of this list, repeated n times func (l pyList) Repeat(n pyInt) pyList { - var ret pyList + ret := make(pyList, 0, int(n)*len(l)) for i := 0; i < int(n); i++ { ret = append(ret, l...) } diff --git a/src/parse/asp/test_data/interpreter/list_concat.build b/src/parse/asp/test_data/interpreter/list_concat.build new file mode 100644 index 0000000000..526c9189ee --- /dev/null +++ b/src/parse/asp/test_data/interpreter/list_concat.build @@ -0,0 +1,22 @@ +fruit = [ + "apple", + "banana", +] + +fruit_and_veg = fruit + [ + "edamame", + "fennel", +] + +fruit_veg_and_canned_food = fruit_and_veg + [ + "tuna", + "baked beans", +] + +fruit_veg_canned_food_and_sweets = fruit_veg_and_canned_food + [ + "haribo", +] + +fruit_veg_canned_food_and_drinks = fruit_veg_and_canned_food + [ + "matcha latte", +]