From 90c54c496b268f0977517448e8f7de54e93b975d Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Thu, 17 May 2018 11:46:45 +0200 Subject: pkg/compiler: detect duplicate fields in template structs --- pkg/compiler/check.go | 15 ++++++++++++--- pkg/compiler/testdata/errors.txt | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'pkg') diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go index 906d7e0de..496490dff 100644 --- a/pkg/compiler/check.go +++ b/pkg/compiler/check.go @@ -137,9 +137,11 @@ func (comp *compiler) checkFields() { switch n := decl.(type) { case *ast.Struct: _, typ, name := n.Info() - comp.checkFieldGroup(n.Fields, "field", typ+" "+name) - if len(n.Fields) < 1 { - comp.error(n.Pos, "%v %v has no fields, need at least 1 field", typ, name) + comp.checkStructFields(n, typ, name) + case *ast.TypeDef: + if n.Struct != nil { + _, typ, _ := n.Struct.Info() + comp.checkStructFields(n.Struct, "template "+typ, n.Name.Name) } case *ast.Call: name := n.Name.Name @@ -152,6 +154,13 @@ func (comp *compiler) checkFields() { } } +func (comp *compiler) checkStructFields(n *ast.Struct, typ, name string) { + comp.checkFieldGroup(n.Fields, "field", typ+" "+name) + if len(n.Fields) < 1 { + comp.error(n.Pos, "%v %v has no fields, need at least 1 field", typ, name) + } +} + func (comp *compiler) checkFieldGroup(fields []*ast.Field, what, ctx string) { existing := make(map[string]bool) for _, f := range fields { diff --git a/pkg/compiler/testdata/errors.txt b/pkg/compiler/testdata/errors.txt index 2d61d9e76..750916a83 100644 --- a/pkg/compiler/testdata/errors.txt +++ b/pkg/compiler/testdata/errors.txt @@ -288,6 +288,7 @@ type templ_struct0[A, B] { type templ_struct1[STR] { f string[STR, 40] + f int32 ### duplicate field f in template struct templ_struct1 } type templ_struct2[A] { -- cgit mrf-deployment