aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2018-05-17 11:46:45 +0200
committerDmitry Vyukov <dvyukov@google.com>2018-05-17 11:46:45 +0200
commit90c54c496b268f0977517448e8f7de54e93b975d (patch)
tree4484f5d55f49fc5a274602ed306356f4973740d1 /pkg
parent256b70f9cfff65292358cf8c0b9d2611db56c326 (diff)
pkg/compiler: detect duplicate fields in template structs
Diffstat (limited to 'pkg')
-rw-r--r--pkg/compiler/check.go15
-rw-r--r--pkg/compiler/testdata/errors.txt1
2 files changed, 13 insertions, 3 deletions
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] {