]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: improve error message for unknown fields in structs
authorEmmanuel Odeke <emm.odeke@gmail.com>
Fri, 28 Oct 2016 04:52:33 +0000 (21:52 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Sat, 29 Oct 2016 05:33:14 +0000 (05:33 +0000)
Improves the error message by moving the field name before the body
of a struct, in the error message for unknown fields for structs.

* Exhibit:
Given program:

package main

import "time"

func main() {
  _ = struct {
    about      string
    before     map[string]uint
    update     map[string]int
    updateTime time.Time
    expect     map[string]int
  }{
    about:   "this one",
    updates: map[string]int{"gopher": 10},
  }
}

* Before:
./issue17631.go:20: unknown struct { about string; before map[string]uint;
 update map[string]int; updateTime time.Time; expect map[string]int } field
'updates' in struct literal

* After:
./issue17631.go:20: unknown field 'updates' in struct literal of type { about string;
before map[string]uint; update map[string]int; updateTime time.Time;
expect map[string]int }

Fixes #17631

Change-Id: I76842616411b931b5ad7a76bd42860dfde7739f4
Reviewed-on: https://go-review.googlesource.com/32240
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
src/cmd/compile/internal/gc/typecheck.go
test/fixedbugs/issue14136.go
test/fixedbugs/issue17631.go [new file with mode: 0644]

index 94ae32ec2c837f6045b7ff6e7532281e2f82caf2..1a8056a2a4dd88369f59a8c0db9c324b98a5205a 100644 (file)
@@ -3095,7 +3095,7 @@ func typecheckcomplit(n *Node) *Node {
 
                                f := lookdot1(nil, l.Sym, t, t.Fields(), 0)
                                if f == nil {
-                                       yyerror("unknown %v field '%v' in struct literal", t, l.Sym)
+                                       yyerror("unknown field '%v' in struct literal of type %v", l.Sym, t)
                                        continue
                                }
                                fielddup(f.Sym.Name, hash)
index 928a60bf6b5ab85789d002998aca5e0fe0d05940..f9efd05f96c8cbb904305cd007ed08d6c73102b7 100644 (file)
@@ -14,6 +14,6 @@ package main
 type T struct{}
 
 func main() {
-       t := T{X: 1, X: 1, X: 1, X: 1, X: 1, X: 1, X: 1, X: 1, X: 1, X: 1} // ERROR "unknown T field"
+       t := T{X: 1, X: 1, X: 1, X: 1, X: 1, X: 1, X: 1, X: 1, X: 1, X: 1} // ERROR "unknown field 'X' in struct literal of type T"
        var s string = 1 // ERROR "cannot use 1"
 }
diff --git a/test/fixedbugs/issue17631.go b/test/fixedbugs/issue17631.go
new file mode 100644 (file)
index 0000000..79b7e8a
--- /dev/null
@@ -0,0 +1,22 @@
+// errorcheck
+
+// Copyright 2016 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import "time"
+
+func main() {
+       _ = struct {
+               about      string
+               before     map[string]uint
+               update     map[string]int
+               updateTime time.Time
+               expect     map[string]int
+       }{
+               about:   "this one",
+               updates: map[string]int{"gopher": 10}, // ERROR "unknown field 'updates' in struct literal of type"
+       }
+}