Backtestshugorun-20261002-200313#15323

Issue #15323 Merged fix PR # Run 2 Oct 2026 · run-20261002-200313 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.

The issue is that `union` silently returns an empty slice when the two slice types differ. It asks for either a real union or an error.

Patch A replaces the early `return ins.r.Interface(), nil` with an explicit error naming both types. That removes the silent-empty behaviour in every case the issue lists: int/float, int/string, the mixed numeric lists, and `union (slice (slice 1)) (slice 2)`, which previously skipped the uncomparable check. Its tests cover all of these at two levels: unit tests in `TestUnion`, updating the old cases that encoded the buggy `[]string{}` / `[]int{}` results, and an integration test through real templates. It also confirms that same-type and interface-element cases still work (`[1 2]|[1 x]`). It updates the doc comment and the user docs to match.

Patch B only unifies numeric element types. For any non-numeric mismatch it keeps the original `return ins.r.Interface(), nil`, so `union (slice 1) (slice "a")` still silently returns `[]`, and so does the slice-of-slices case. That is exactly the behaviour the issue reports, so B fixes only part of it. B also needed an extra `AssignableTo` guard to handle a prototype-type quirk, which hints at fragility in that path. Its tests cover only the numeric cases and leave the silent-empty expectations for string/int in place.

B matches `intersect` more closely for numbers, but A resolves the root complaint (silent failure) completely and consistently, with stronger tests.

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

17m 59s

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

45vs 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+40 −5
docs/content/en/functions/collections/Union.mdindex da993561c..a670fe50b 100644@@ -20,6 +20,8 @@ aliases: [/functions/union] {{ union nil nil }}                     → [] ``` +Both slices must be of the same type unless one of them is a slice of mixed types, otherwise the function returns an error. For example, `union (slice 1) (slice 1.0)` fails because `slice 1` is a `[]int` and `slice 1.0` is a `[]float64`.+ ## OR filter in where query  This is also useful as `OR` filters when combined with where:tpl/collections/collections.goindex a44603b53..04a71996d 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 must be of the same type and may be either arrays or slices,+// unless one of them has an interface element type (e.g. []any).+// An error is returned if l1 and l2 are of different types. // 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 {@@ -657,7 +658,7 @@ 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+				return nil, fmt.Errorf("union does not support slices or arrays of different types: %s and %s", l1v.Type(), l2v.Type()) 			}  			var (tpl/collections/collections_integration_test.goindex 52939f3c3..b8177c67f 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"@@ -653,3 +654,30 @@ All. 		} 	}) }++func TestUnionDifferentSliceTypes(t *testing.T) {+	t.Parallel()++	filesTemplate := `+-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- layouts/home.html --+{{ UNION }}+`++	for _, union := range []string{+		`{{ union (slice 1) (slice 1.0) }}`,+		`{{ union (slice 1) (slice "a") }}`,+		`{{ union (slice 1 2) (slice 2.0 3.0) }}`,+		`{{ union (slice (slice 1)) (slice 2) }}`,+	} {+		files := strings.ReplaceAll(filesTemplate, "{{ UNION }}", union)+		b, err := hugolib.TestE(t, files)+		b.Assert(err, qt.IsNotNil, qt.Commentf(union))+		b.Assert(err.Error(), qt.Contains, "union does not support slices or arrays of different types", qt.Commentf(union))+	}++	files := strings.ReplaceAll(filesTemplate, "{{ UNION }}", `{{ union (slice 1) (slice 2) }}|{{ union (slice 1 "x") (slice 1.0) }}`)+	b := hugolib.Test(t, files)+	b.AssertFileContent("public/index.html", "[1 2]|[1 x]")+}tpl/collections/collections_test.goindex 5562a80d6..0ceb7452a 100644@@ -664,8 +664,12 @@ 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}, false, true},+		{[]int{1, 2}, []string{"1", "2"}, false, true},+		{[]int{1}, []float64{1.0}, false, true},+		{[]int{1, 2}, []float64{2.0, 3.0}, false, true},+		{[]int{1}, []string{"a"}, false, true},+		{[][]int{{1}}, []int{2}, false, true},  		// []T ∪ []T 		{[]string{"a", "b", "c", "c"}, []string{"a", "b", "b"}, []string{"a", "b", "c"}, false}, 
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},