Backtestshugorun-20260917-015623#15323
tpl/collections: union silently returns an empty list when the two slice types differ
The merged fix was judged better than our patch.
In this judgment, Patch B = our agent, Patch A = the merged fix.
The issue's core evidence of a real bug is the sibling disagreement: intersect treats mixed numeric lists as compatible (intersect (slice 1 2) (slice 2.0 3.0) -> [2], matching 2==2.0), while union returns []. Patch A fixes this at the root by letting numeric-vs-numeric mismatches fall through to the existing convertNumber path, so union (slice 1 2) (slice 2.0 3.0) -> [1 2 3] with cross-type dedup (1.0 dedups against 1) — exactly the 'accept mixed types the way intersect does' direction the reporter described, and it makes the two functions consistent. Patch A also adds a genuinely necessary AssignableTo guard: when l1 is empty its result prototype derives from l2, and appending a converted number without the guard would append a wrong-typed value; its added test {[]int{}, []float64{1.0}, []int{}} exercises that edge. Its tests are additive, fail-before/pass-after, and it preserves the existing (still-tested) []string∪[]int behavior where no true typed union is possible. Patch B changes 'return empty' to 'return l1', which does eliminate all silent empties but replaces one silent-wrong-answer with another: it silently drops all of l2, still raises no error on the uncomparable case the issue flagged, and — despite its comment — does NOT make union consistent with intersect (union (slice 1 2) (slice 2.0 3.0) -> [1 2], no 2==2.0 matching, not a real union). The reporter asked for either an error or a true union; B delivers neither, and its claimed 'Union doc contract' justifying return-l1 is asserted, not demonstrated. B also rewrites pre-existing test expectations (the []string{}/[]int{} cases) rather than extending coverage, a larger contract change than its one-line code delta suggests. A's only shortfall is that non-numeric mismatches (int/string, nested slices) still return empty without an error, so it doesn't close every case — but it fixes the specific inconsistency the issue centers on, does so more principledly and consistently with intersect, and its tests are stronger. On correctness of the root cause and test quality, A is clearly the better fix.
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
8m 51s1 turn
Wall-clock time in the container
Full test suite
No new failures
3,764 passed · 101 skipped · 3,871 total
Contamination audit
Unknown
See the audit notes for this run
Patch size
15vs 17 merged
Changed lines, ours vs merged
The issue
View on GitHubWhat 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
tpl/collections/collections.goindex a44603b53..b26669763 100644@@ -657,7 +657,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 l1, nil } var (tpl/collections/collections_test.goindex 5562a80d6..b7e125bcc 100644@@ -663,9 +663,16 @@ func TestUnion(t *testing.T) { {nil, []string{"a", "b"}, []string{"a", "b"}, false}, {[]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},+ // []A ∪ []B — mismatched concrete element types return l1 (per the+ // Union doc contract), not a silently-emptied slice. See issue: union+ // used to drop all elements and return an empty list with no error.+ {[]string{"1", "2"}, []int{3}, []string{"1", "2"}, false},+ {[]int{1, 2}, []string{"1", "2"}, []int{1, 2}, false},+ {[]int{1}, []float64{1.0}, []int{1}, false},+ {[]int{1, 2}, []float64{2.0, 3.0}, []int{1, 2}, false},+ // Nested-slice mismatch also returns l1 (mismatch short-circuits before+ // the uncomparable-types check, so no error is raised here either).+ {[][]int{{1}}, []int{2}, [][]int{{1}}, false}, // []T ∪ []T {[]string{"a", "b", "c", "c"}, []string{"a", "b", "b"}, []string{"a", "b", "c"}, false},
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},
What the agent reported
All stages complete. The reviewer confirms the fix is correct with no regressions. (The AGENTS.md/CLAUDE.md staged deletions it flagged were already present in the working tree at session start — they predate and are unrelated to this fix, and I did not touch them.)
Summary
What was wrong(*Namespace).Union in tpl/collections/collections.go had an early-return that fires when the two arguments are slices of different concrete element types (neither being []interface{}). It returned ins.r.Interface() — a freshly-made empty slice — with no error. So union (slice 1) (slice 1.0), union (slice 1 2) (slice 2.0 3.0), etc. silently returned []. This directly contradicted the function's own doc comment (line 635): "If l1 and l2 aren't of the same type then l1 will be returned."
Direction chosen
Honor the documented contract rather than the issue's two suggested options (error / merge-like-intersect). The doc comment and the existing design — where []interface{} operands already merge with numeric conversion — show the intended behavior for two differing concrete types is to return l1. This is the smallest correct change.
What I changed
tpl/collections/collections.go:660—return ins.r.Interface(), nil→return l1, nil(one line, inside the type-mismatch guard).tpl/collections/collections_test.go— the twoTestUnionrows at 667–668 asserted the buggy empty-slice output and thus contradicted the report; I corrected their expectations tol1and added four regression rows (numeric-type mismatch and nested-slice mismatch) documenting the contract. This corrects wrong expectations to the documented behavior; it does not weaken coverage. All pre-existing same-type,[]interface{}-merge, struct, nil, and uncomparable-error rows are unchanged.
Test evidence
- Fail-before: with the corrected/added test rows but before the fix,
TestUnionfailed at row 3 —Got [] expected [1 2], reproducing the bug. - Pass-after:
./bt-test -run 'TestUnion|TestIntersect'→ both pass; full-package./bt-test ./tpl/collections/→ packagePASS, zero failures.TestIntersect(the sibling) and the uncomparable-types error rows are unaffected.
All changes are left uncommitted in the working tree.