Backtestshugorun-20261005-133023#15323

Issue #15323 Merged fix PR # Run 5 Oct 2026 · run-20261005-133023 gohugoio/hugo

tpl/collections: union silently returns an empty list when the two slice types differ

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Patch A fixes the reported bug; Patch B only fixes part of it.

**The bug.** `union` returns an empty slice, with no error, whenever the two slice types differ and neither has an interface element type. The issue lists several such cases:
- int with float
- int with string
- mixed numeric lists
- an uncomparable case that should raise an error but returns `[]` instead

**Patch A** removes the silent early return.
- For mixed types it builds a `[]any` result. It walks l1 with the existing loop, then walks l2 and appends unseen values, so cross-type numeric duplicates collapse. The tests expect `[1]` for int ∪ float 1.0 and `[1 2 3]` for the mixed lists, which match how `intersect` treats numbers.
- It adds the uncomparable check to the l2 walk, so `union (slice (slice 1)) (slice 2)` now errors as the issue expects.
- Every scenario in the issue is covered: int/float, int/string, the mixed lists, and the uncomparable error.
- Tests: it updates the existing unit cases that encoded the old empty-result behavior. It adds unit cases for empty l1, empty l2, the pages types and uncomparable mixes. It also adds an integration test that reproduces the issue's template.
- Cost: the result type changes to `[]any` for mixed inputs. That is a reasonable design choice, and the doc comment is updated to say so.

**Patch B** only bypasses the early return when both element types are numeric.
- int ∪ string still returns `[]` silently, as do all other non-numeric mismatches. This is one of the exact cases in the issue.
- The uncomparable-before-check ordering problem is not addressed.
- Its new assignability guard makes `[]int{} ∪ []float64{1.0}` return `[]int{}`. That is again silently dropping data, and the patch enshrines it in a test.
- It also keeps the old tests asserting empty results for string/int mixes, which locks in the reported bug.

A resolves the root cause across all reported cases and has stronger tests. B is a partial fix that leaves the core silent-empty behavior in place.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

1h 2m

Wall-clock time in the container

Full test suite

No new failures

3,769 passed · 101 skipped · 3,872 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

85vs 17 merged

Changed lines, ours vs merged

What version of Hugo are you using (hugo version)?

<pre>
$ hugo version
hugo v0.166.0-DEV-ae07063fcc9a59c95838e83e7fdf12e326ebee7d darwin/arm64
</pre>

Does this issue reproduce with the latest release?

I built master at ae07063 and reproduced it there. I did not run a tagged release binary, so I have not confirmed it against one directly, though Union is unchanged in tpl/collections/collections.go.

What did you expect to see?

union either returning the union of its two arguments, or returning an error. It already errors for the other argument shapes it cannot handle, for example union does not support slices or arrays of uncomparable types and can't iterate over ....

What did you see instead?

union returns an empty slice, with no error, whenever the two arguments have different slice types and neither has an interface element type.

layouts/index.html:

{{ printf "%T" (slice 1) }}
{{ printf "%T" (slice 1.0) }}
union int,int    -> {{ union (slice 1) (slice 2) }}
union int,float  -> {{ union (slice 1) (slice 1.0) }}
union int,string -> {{ union (slice 1) (slice "a") }}
union lists      -> {{ union (slice 1 2) (slice 2.0 3.0) }}
intersect lists  -> {{ intersect (slice 1 2) (slice 2.0 3.0) }}

Output:

[]int
[]float64
union int,int    -> [1 2]
union int,float  -> []
union int,string -> []
union lists      -> []
intersect lists  -> [2]

intersect handles the same two mixed-type lists and returns [2], so the sibling functions disagree about whether these arguments are usable at all.

The cause is this early return in Union:

if l1v.Type() != l2v.Type() &&
	l1v.Type().Elem().Kind() != reflect.Interface &&
	l2v.Type().Elem().Kind() != reflect.Interface {
	return ins.r.Interface(), nil
}

ins.r is the empty slice made just above, so a caller gets an empty list rather than an error. It is reachable from ordinary templates because slice 1 is []int and slice 1.0 is []float64, and it also runs before the uncomparable-types check, so union (slice (slice 1)) (slice 2) returns [] instead of the error that check would raise.

Adding one element of another type to the first argument avoids it, since the slice then becomes []interface {}: union (slice 1 "x") (slice 1.0) returns [1 x].

Stack traceback or browser console output

None, no error is raised. That is the problem.


This is a different mechanism from #3411, which was closed in 2017 and is now locked with a note asking that related bugs be opened as new issues.

Happy to send a PR. Returning an error here would be a small change; making union accept mixed types the way intersect does is bigger, so I would rather you pick the direction first.

This issue was written primarily by Claude Code, including the reproduction above.

The two patches

Our patch+76 −9
tpl/collections/collections.goindex a44603b53..33bb8724d 100644@@ -631,8 +631,9 @@ func (i *intersector) handleValuePair(l1vv, l2vv reflect.Value) { }  // Union returns the union of the given sets, l1 and l2. l1 and-// l2 must be of the same type and may be either arrays or slices.-// If l1 and l2 aren't of the same type then l1 will be returned.+// l2 may be either arrays or slices.+// If l1 and l2 are of different types and neither has an interface+// element type, the result is a []any holding the elements of both. // If either l1 or l2 is nil then the non-nil list will be returned. func (ns *Namespace) Union(l1, l2 any) (any, error) { 	if l1 == nil && l2 == nil {@@ -652,14 +653,20 @@ func (ns *Namespace) Union(l1, l2 any) (any, error) { 	case reflect.Array, reflect.Slice: 		switch l2v.Kind() { 		case reflect.Array, reflect.Slice:-			ins = &intersector{r: reflect.MakeSlice(l1v.Type(), 0, 0), seen: make(map[any]bool)}--			if l1v.Type() != l2v.Type() &&+			// When the element types differ and neither is an interface, values+			// from l2 can not in general be stored in a slice of l1's type,+			// so collect the union of both into a []any.+			mixed := l1v.Type() != l2v.Type() && 				l1v.Type().Elem().Kind() != reflect.Interface &&-				l2v.Type().Elem().Kind() != reflect.Interface {-				return ins.r.Interface(), nil+				l2v.Type().Elem().Kind() != reflect.Interface++			rt := l1v.Type()+			if mixed {+				rt = reflect.TypeFor[[]any]() 			} +			ins = &intersector{r: reflect.MakeSlice(rt, 0, 0), seen: make(map[any]bool)}+ 			var ( 				l1vv  reflect.Value 				isNil bool@@ -685,6 +692,22 @@ func (ns *Namespace) Union(l1, l2 any) (any, error) { 				} 			} +			if mixed {+				for j := range l2v.Len() {+					l2vv, isNil := hreflect.Indirect(l2v.Index(j))++					if !l2vv.Type().Comparable() {+						return []any{}, errors.New("union does not support slices or arrays of uncomparable types")+					}++					if !isNil {+						ins.appendIfNotSeen(l2vv)+					}+				}++				return ins.r.Interface(), nil+			}+ 			for j := range l2v.Len() { 				l2vv := l2v.Index(j) 				typ := l1vv.Type()tpl/collections/collections_integration_test.goindex 52939f3c3..b49e3204d 100644@@ -19,6 +19,7 @@ import ( 	"strings" 	"testing" +	qt "github.com/frankban/quicktest" 	"github.com/gohugoio/hugo/hugolib" 	"github.com/gohugoio/hugo/resources/page" 	"github.com/gohugoio/hugo/tpl/collections"@@ -486,6 +487,40 @@ tags_weight: 30 	) } +func TestUnionMixedSliceTypes(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+disableKinds = ["page", "section", "taxonomy", "term", "rss", "sitemap"]+-- layouts/home.html --+union int,int: {{ union (slice 1) (slice 2) }}|+union int,float: {{ union (slice 1) (slice 1.0) }}|+union int,string: {{ union (slice 1) (slice "a") }}|+union lists: {{ union (slice 1 2) (slice 2.0 3.0) }}|+intersect lists: {{ intersect (slice 1 2) (slice 2.0 3.0) }}|+`++	hugolib.Test(t, files).AssertFileContent("public/index.html",+		"union int,int: [1 2]|",+		"union int,float: [1]|",+		"union int,string: [1 a]|",+		"union lists: [1 2 3]|",+		"intersect lists: [2]|",+	)++	files = `+-- hugo.toml --+disableKinds = ["page", "section", "taxonomy", "term", "rss", "sitemap"]+-- layouts/home.html --+{{ union (slice (slice 1)) (slice 2) }}+`++	b, err := hugolib.TestE(t, files)+	b.Assert(err, qt.IsNotNil)+	b.Assert(err.Error(), qt.Contains, "union does not support slices or arrays of uncomparable types")+}+ func TestCollectionsFuncs(t *testing.T) { 	t.Parallel() tpl/collections/collections_test.goindex 5562a80d6..6ed6feab7 100644@@ -664,8 +664,15 @@ func TestUnion(t *testing.T) { 		{[]string{"a", "b"}, nil, []string{"a", "b"}, false},  		// []A ∪ []B-		{[]string{"1", "2"}, []int{3}, []string{}, false},-		{[]int{1, 2}, []string{"1", "2"}, []int{}, false},+		{[]string{"1", "2"}, []int{3}, []any{"1", "2", 3}, false},+		{[]int{1, 2}, []string{"1", "2"}, []any{1, 2, "1", "2"}, false},+		{[]int{1}, []float64{1.0}, []any{1}, false},+		{[]int{1}, []string{"a"}, []any{1, "a"}, false},+		{[]int{1, 2}, []float64{2.0, 3.0}, []any{1, 2, 3.0}, false},+		{[]int{1, 2}, []float64{2.5}, []any{1, 2, 2.5}, false},+		{[]int{}, []float64{1.5}, []any{1.5}, false},+		{[]int{1}, []float64{}, []any{1}, false},+		{pagesPtr{p1}, pagesVals{p3v}, []any{p1, p3v}, false},  		// []T ∪ []T 		{[]string{"a", "b", "c", "c"}, []string{"a", "b", "b"}, []string{"a", "b", "c"}, false},@@ -720,6 +727,8 @@ func TestUnion(t *testing.T) { 		// uncomparable types - #3820 		{[]map[string]int{{"K1": 1}}, []map[string]int{{"K2": 2}, {"K2": 2}}, false, true}, 		{[][]int{{1, 1}, {1, 2}}, [][]int{{2, 1}, {2, 2}}, false, true},+		{[][]int{{1}}, []int{2}, false, true},+		{[]int{1}, [][]int{{2}}, false, true}, 	} {  		errMsg := qt.Commentf("[%d] %v", i, test) 
Merged fix · PR #+15 −2
tpl/collections/collections.goindex a44603b53..fdb2501f6 100644@@ -657,7 +657,12 @@ func (ns *Namespace) Union(l1, l2 any) (any, error) { 			if l1v.Type() != l2v.Type() && 				l1v.Type().Elem().Kind() != reflect.Interface && 				l2v.Type().Elem().Kind() != reflect.Interface {-				return ins.r.Interface(), nil+				// Numeric element types unify through the conversion below,+				// mirroring how intersect matches numbers across types.+				if !hreflect.IsNumber(l1v.Type().Elem().Kind()) ||+					!hreflect.IsNumber(l2v.Type().Elem().Kind()) {+					return ins.r.Interface(), nil+				} 			}  			var (@@ -698,7 +703,8 @@ func (ns *Namespace) Union(l1, l2 any) (any, error) { 				case hreflect.IsNumber(kind): 					var err error 					l2vv, err = convertNumber(l2vv, typ)-					if err == nil {+					// The empty l1 prototype comes from l2 and may not match l1's element type.+					if err == nil && l2vv.Type().AssignableTo(l1v.Type().Elem()) { 						ins.appendIfNotSeen(l2vv) 					} 				case kind == reflect.Interface, kind == reflect.Struct, kind == reflect.Pointer:tpl/collections/collections_test.goindex 5562a80d6..47c51e01d 100644@@ -667,6 +667,13 @@ func TestUnion(t *testing.T) { 		{[]string{"1", "2"}, []int{3}, []string{}, false}, 		{[]int{1, 2}, []string{"1", "2"}, []int{}, false}, +		// numeric []A ∪ []B unify. See issue 15323.+		{[]int{1}, []float64{1.0}, []int{1}, false},+		{[]int{1, 2}, []float64{2.0, 3.0}, []int{1, 2, 3}, false},+		{[]float64{1.0}, []int{1, 2}, []float64{1.0, 2.0}, false},+		// the empty l1 prototype comes from l2, so appends must stay assignable to l1's element type.+		{[]int{}, []float64{1.0}, []int{}, false},+ 		// []T ∪ []T 		{[]string{"a", "b", "c", "c"}, []string{"a", "b", "b"}, []string{"a", "b", "c"}, false}, 		{[]string{"a", "b"}, []string{"a", "b", "c"}, []string{"a", "b", "c"}, false},