pkg/compiler: detect duplicate fields in template structs

This commit is contained in:
Dmitry Vyukov 2018-05-17 11:46:45 +02:00
parent 256b70f9cf
commit 90c54c496b
2 changed files with 13 additions and 3 deletions

View File

@ -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 {

View File

@ -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] {