mirror of
https://github.com/golang/go
synced 2024-11-13 19:10:22 -07:00
go/types: unresolved literal keys must be looked up in universe.
Fixes #4888. R=golang-dev, gri CC=golang-dev https://golang.org/cl/7383051
This commit is contained in:
parent
d97b975d5c
commit
670f6b602d
@ -543,6 +543,8 @@ func (check *checker) compositeLitKey(key ast.Expr) {
|
|||||||
if ident, ok := key.(*ast.Ident); ok && ident.Obj == nil {
|
if ident, ok := key.(*ast.Ident); ok && ident.Obj == nil {
|
||||||
if obj := check.pkg.Scope.Lookup(ident.Name); obj != nil {
|
if obj := check.pkg.Scope.Lookup(ident.Name); obj != nil {
|
||||||
check.register(ident, obj)
|
check.register(ident, obj)
|
||||||
|
} else if obj := Universe.Lookup(ident.Name); obj != nil {
|
||||||
|
check.register(ident, obj)
|
||||||
} else {
|
} else {
|
||||||
check.errorf(ident.Pos(), "undeclared name: %s", ident.Name)
|
check.errorf(ident.Pos(), "undeclared name: %s", ident.Name)
|
||||||
}
|
}
|
||||||
|
7
src/pkg/go/types/testdata/expr3.src
vendored
7
src/pkg/go/types/testdata/expr3.src
vendored
@ -259,6 +259,8 @@ var index2 int = 2
|
|||||||
|
|
||||||
func map_literals() {
|
func map_literals() {
|
||||||
type M0 map[string]int
|
type M0 map[string]int
|
||||||
|
type M1 map[bool]int
|
||||||
|
type M2 map[*int]int
|
||||||
|
|
||||||
_ = M0{}
|
_ = M0{}
|
||||||
_ = M0{1 /* ERROR "missing key" */ }
|
_ = M0{1 /* ERROR "missing key" */ }
|
||||||
@ -267,11 +269,14 @@ func map_literals() {
|
|||||||
_ = M0{"foo": 1, "bar": 2, "foo" /* ERROR "duplicate key" */ : 3 }
|
_ = M0{"foo": 1, "bar": 2, "foo" /* ERROR "duplicate key" */ : 3 }
|
||||||
|
|
||||||
// map keys must be resolved correctly
|
// map keys must be resolved correctly
|
||||||
// (for detials, see comment in go/parser/parser.go, method parseElement)
|
// (for details, see comment in go/parser/parser.go, method parseElement)
|
||||||
key1 := "foo"
|
key1 := "foo"
|
||||||
_ = M0{key1: 1}
|
_ = M0{key1: 1}
|
||||||
_ = M0{key2: 2}
|
_ = M0{key2: 2}
|
||||||
_ = M0{key3 /* ERROR "undeclared name" */ : 2}
|
_ = M0{key3 /* ERROR "undeclared name" */ : 2}
|
||||||
|
|
||||||
|
_ = M1{true: 1, false: 0}
|
||||||
|
_ = M2{nil: 0, &index2: 1}
|
||||||
}
|
}
|
||||||
|
|
||||||
var key2 string = "bar"
|
var key2 string = "bar"
|
||||||
|
Loading…
Reference in New Issue
Block a user