Backtestshugorun-20261013-011256#15323

Issue #15323 Merged fix PR # Run 13 Oct 2026 · run-20261013-011256 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 removes the root cause: the early return that silently produced an empty slice whenever the two slice types differed. It replaces it with conversion where possible and widening to []any where not. This fixes every case in the issue. int,float gives [1], lists give [1 2 3], and int,string gives [1 a]. The uncomparable-types check now runs per element of l2, so `union (slice (slice 1)) (slice 2)` returns the expected error instead of []. A also handles arrays as l1 by making the result a slice, and fixes the empty-l1 prototype by using l1's element type. Its tests cover each issue scenario in an integration test that would fail before the change, plus unit cases for lossy floats, empty l1, arrays, and mixed []any. It updates the two existing expectations that encoded the buggy empty result. Patch B only exempts number-to-number pairs from the early return. int,string still silently returns [], and the uncomparable-check ordering bug is untouched. For []int{} with []float64{1.0} it explicitly keeps returning [] silently, and it leaves the old tests asserting empty results for string/int mixes. So B fixes only part of the symptom and leaves the issue's core complaint, silent empty results, in place. A is somewhat more invasive, with a widening helper and some risk around string-conversion semantics, but it is well tested and fully addresses the issue.

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

38m 50s

Wall-clock time in the container

Full test suite

No new failures

244 passed · 0 skipped · 244 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

128vs 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+102 −26
tpl/collections/collections.goindex a44603b53..3d4b4a2fc 100644@@ -630,9 +630,24 @@ func (i *intersector) handleValuePair(l1vv, l2vv reflect.Value) { 	} } +// appendWidening appends v if not seen, first converting the result to a+// []any if v is not assignable to its current element type.+func (i *intersector) appendWidening(v reflect.Value) {+	if !v.Type().AssignableTo(i.r.Type().Elem()) {+		widened := reflect.MakeSlice(reflect.TypeFor[[]any](), 0, i.r.Len()+1)+		for j := range i.r.Len() {+			widened = reflect.Append(widened, i.r.Index(j))+		}+		i.r = widened+	}+	i.appendIfNotSeen(v)+}+ // 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.+// Elements of l2 are converted to the element type of l1 when possible+// (e.g. 2.0 to 2 or 2 to 2.0; floats may be narrowed to float32); if any+// element can't be converted to the element type of l1, a []any is returned. // 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,13 +667,11 @@ 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() &&-				l1v.Type().Elem().Kind() != reflect.Interface &&-				l2v.Type().Elem().Kind() != reflect.Interface {-				return ins.r.Interface(), nil+			sliceType := l1v.Type()+			if sliceType.Kind() == reflect.Array {+				sliceType = reflect.SliceOf(sliceType.Elem()) 			}+			ins = &intersector{r: reflect.MakeSlice(sliceType, 0, 0), seen: make(map[any]bool)}  			var ( 				l1vv  reflect.Value@@ -673,38 +686,48 @@ func (ns *Namespace) Union(l1, l2 any) (any, error) { 				}  				if !isNil {-					ins.appendIfNotSeen(l1vv)+					ins.appendWidening(l1vv) 				} 			}  			if !l1vv.IsValid() {-				// The first slice may be empty. Pick the first value of the second-				// to use as a prototype.-				if l2v.Len() > 0 {-					l1vv = l2v.Index(0)+				// The first slice may be empty. Use its element type as the+				// prototype, or, if that is an interface, the first value of+				// the second.+				if elemType := l1v.Type().Elem(); elemType.Kind() != reflect.Interface {+					l1vv = reflect.Zero(elemType)+				} else if l2v.Len() > 0 {+					l1vv, _ = hreflect.Indirect(l2v.Index(0)) 				} 			}  			for j := range l2v.Len() {-				l2vv := l2v.Index(j)+				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 {+					continue+				}+ 				typ := l1vv.Type()  				switch kind := l1vv.Kind(); { 				case kind == reflect.String:-					l2t, err := hreflect.ToStringE(l2vv)-					if err == nil {-						ins.appendIfNotSeen(reflect.ValueOf(l2t))+					if l2s, ok := hreflect.ConvertIfPossible(l2vv, typ); ok {+						l2vv = l2s 					} 				case hreflect.IsNumber(kind):-					var err error-					l2vv, err = convertNumber(l2vv, typ)-					if err == nil {-						ins.appendIfNotSeen(l2vv)+					if l2n, err := convertNumber(l2vv, typ); err == nil {+						l2vv = l2n 					}-				case kind == reflect.Interface, kind == reflect.Struct, kind == reflect.Pointer:-					ins.appendIfNotSeen(l2vv)- 				}++				// Values that could not be converted are kept as is,+				// widening the result to []any if needed.+				ins.appendWidening(l2vv) 			}  			return ins.r.Interface(), niltpl/collections/collections_integration_test.goindex 52939f3c3..a5195441e 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"@@ -289,6 +290,44 @@ disableKinds = ['rss','sitemap', 'taxonomy', 'term', 'page'] 	b.AssertFileContentExact("public/index.html", "0: /a3_b1.html\n\n1: /b2.html\n\n2: /a1.html\n\n3: /a2.html\n$") } +func TestUnionMixedSliceTypes(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+disableKinds = ['rss','sitemap', 'taxonomy', 'term', 'page']+-- layouts/home.html --+union int,int: {{ union (slice 1) (slice 2) }}|+union int,float: {{ union (slice 1) (slice 1.0) }}|+union int,float lossy: {{ union (slice 1) (slice 1.5) }}|+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) }}|+`++	b := hugolib.Test(t, files)++	b.AssertFileContent("public/index.html",+		"union int,int: [1 2]|",+		"union int,float: [1]|",+		"union int,float lossy: [1 1.5]|",+		"union int,string: [1 a]|",+		"union lists: [1 2 3]|",+		"intersect lists: [2]|",+	)++	files = `+-- hugo.toml --+disableKinds = ['rss','sitemap', 'taxonomy', 'term', 'page']+-- 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")+}+ // Issue 14777. func TestWhereWithPageEqualityIssue14777(t *testing.T) { 	t.Parallel()tpl/collections/collections_test.goindex 5562a80d6..3eb7ab036 100644@@ -664,8 +664,20 @@ 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}, []int{1}, false},+		{[]int{1, 2}, []float64{2.0, 3.0}, []int{1, 2, 3}, false},+		{[]int{1}, []float64{1.5}, []any{1, 1.5}, false},+		{[]int{1}, []string{"a"}, []any{1, "a"}, false},+		{[]float64{1.5}, []int{1, 2}, []float64{1.5, 1, 2}, false},+		{[]int{}, []float64{2.0}, []int{2}, false},+		{[]string{}, []int{3}, []any{3}, false},+		{[2]int{1, 2}, []int{2, 3}, []int{1, 2, 3}, false},++		// []T ∪ []interface{} with values not representable as T+		{[]int{1}, []any{"a", 2.0, 1}, []any{1, "a", 2}, false},+		{[]string{"a"}, []any{"b", 3}, []any{"a", "b", 3}, false},  		// []T ∪ []T 		{[]string{"a", "b", "c", "c"}, []string{"a", "b", "b"}, []string{"a", "b", "c"}, false},@@ -720,6 +732,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}, []any{[]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},