fix: detect cyclic validation aliases #1675

coverage: 72.575% (+0.006%) from 72.569% — Harshil-1603:fix/cyclic-alias-expansion into go-playground:master
How about this test?
func TestCyclicAliasMaskedByCachedTag(t *testing.T) {
v := New()
// Compile and cache the literal tag "base" while it is still acyclic.
v.RegisterAlias("base", "required")
Equal(t, v.Var("value", "base"), nil)
// Introduce a cycle: base -> next -> base.
v.RegisterAlias("next", "base")
v.RegisterAlias("base", "next")
// This should detect the newly introduced cycle.
PanicMatches(t, func() {
_ = v.Var("value", "base")
}, "Cyclic alias 'base' on field ''")
}
[AI] I reproduced this test: Var keeps using the cached required expansion, so no panic occurs. The same behavior exists on current master; the cache implementation is unchanged by this PR. Re-registering an acyclic alias after validation also retains its earlier cached expansion.
RegisterAlias currently documents that aliases should be registered before any validation. Changing this behavior would require deciding how alias re-registration interacts with both tag and struct caches, beyond detecting a cycle while compiling tags.
I suggest keeping this PR scoped to cycle detection during compilation and handling post-validation alias re-registration separately. Would you prefer that broader cache behavior included here? As a control, I checked that a cycle registered before any validation is still detected after an unrelated tag has been cached.
nodivbyzero
Harshil-1603
Fixes Or Enhances
Cyclic aliases currently recurse until Go terminates the process with a stack overflow. Track the aliases on the current expansion path and report a recoverable configuration panic naming the alias and field, consistent with other invalid-tag handling. Reusing an alias in separate branches remains valid; forward registration still works.
Fixes #1673.
VarandStruct, and repeated acyclic aliases.The isolated reproduction terminates with a fatal stack overflow before the fix and recovers afterward.
make test(all packages, coverage and race detector) passes on Go 1.27.3. Golangci-lint 2.13.1 passes with Go 1.26.0, matching the lint job; that older linter cannot read Go 1.27 export data.git diff --checkpasses.AI-assisted contribution: OpenAI Codex (GPT-6) implemented and verified this patch.