Commit 7b454565 authored by Emmanuel Odeke's avatar Emmanuel Odeke Committed by Josh Bleecher Snyder

cmd/compile: improve error message for unknown fields in structs

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/32240Reviewed-by: default avatarJosh Bleecher Snyder <josharian@gmail.com>
parent 894abb5f
...@@ -3095,7 +3095,7 @@ func typecheckcomplit(n *Node) *Node { ...@@ -3095,7 +3095,7 @@ func typecheckcomplit(n *Node) *Node {
f := lookdot1(nil, l.Sym, t, t.Fields(), 0) f := lookdot1(nil, l.Sym, t, t.Fields(), 0)
if f == nil { 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 continue
} }
fielddup(f.Sym.Name, hash) fielddup(f.Sym.Name, hash)
......
...@@ -14,6 +14,6 @@ package main ...@@ -14,6 +14,6 @@ package main
type T struct{} type T struct{}
func main() { 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" var s string = 1 // ERROR "cannot use 1"
} }
// 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"
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment