go/types: don't permit implicit assignments to unexported struct fields via literals

Change-Id: I74eec172ba5a319f91c95e33b047f489dfce0c95
Reviewed-on: https://go-review.googlesource.com/4090
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Robert Griesemer 2015-02-06 12:39:19 -08:00
parent 9622599500
commit fa3649f71f
2 changed files with 20 additions and 5 deletions

View File

@ -1057,7 +1057,12 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
break // cannot continue
}
// i < len(fields)
etyp := fields[i].typ
fld := fields[i]
if !fld.Exported() && fld.pkg != check.pkg {
check.errorf(x.pos(), "implicit assignment to unexported field %s in %s literal", fld.name, typ)
continue
}
etyp := fld.typ
if !check.assignment(x, etyp) {
if x.mode != invalid {
check.errorf(x.pos(), "cannot use %s as %s value in struct literal", x, etyp)

View File

@ -4,6 +4,8 @@
package expr3
import "time"
func indexes() {
_ = 1 /* ERROR "cannot index" */ [0]
_ = indexes /* ERROR "cannot index" */ [0]
@ -101,7 +103,6 @@ func indexes() {
_, ok = m["bar"]
_ = ok
var t string
_ = t[- /* ERROR "negative" */ 1]
_ = t[- /* ERROR "negative" */ 1 :]
@ -201,6 +202,15 @@ func struct_literals() {
x int
}
_ = P /* ERROR "invalid composite literal type" */ {}
// unexported fields
_ = time.Time{}
_ = time.Time{sec /* ERROR "unknown field" */ : 0}
_ = time.Time{
0 /* ERROR implicit assignment to unexported field sec in time.Time literal */,
0 /* ERROR implicit assignment */ ,
nil /* ERROR implicit assignment */ ,
}
}
func array_literals() {