diff --git a/src/cmd/compile/internal/gc/typecheck.go b/src/cmd/compile/internal/gc/typecheck.go index 0b8eb8c75bd..688936e9268 100644 --- a/src/cmd/compile/internal/gc/typecheck.go +++ b/src/cmd/compile/internal/gc/typecheck.go @@ -3207,6 +3207,11 @@ func checkassign(stmt *Node, n *Node) { return } + if n.Op == ODOT && n.Left.Op == OINDEXMAP { + Yyerror("cannot directly assign to struct field %v in map", n) + return + } + Yyerror("cannot assign to %v", n) } diff --git a/test/fixedbugs/issue13779.go b/test/fixedbugs/issue13779.go new file mode 100644 index 00000000000..94cf9c68de0 --- /dev/null +++ b/test/fixedbugs/issue13779.go @@ -0,0 +1,15 @@ +// 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. + +// Issue 13779: provide better error message when directly assigning to struct field in map + +package main + +func main() { + type person struct{ age, weight, height int } + students := map[string]person{"sally": person{12, 50, 32}} + students["sally"].age = 3 // ERROR "cannot directly assign to struct field .* in map" +}