From 4ae33302a4bfb136675050ed01bf075250e40160 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 30 Jul 2013 13:15:28 -0700 Subject: [PATCH] go.tools/go/types: map keys must be comparable R=adonovan CC=golang-dev https://golang.org/cl/12102044 --- go/types/stdlib_test.go | 3 --- go/types/testdata/cycles.src | 4 ++-- go/types/typexpr.go | 9 +++++++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/go/types/stdlib_test.go b/go/types/stdlib_test.go index f371ceaa023..faaf8160c5c 100644 --- a/go/types/stdlib_test.go +++ b/go/types/stdlib_test.go @@ -60,9 +60,6 @@ func TestStdtest(t *testing.T) { case "goto.go", "label1.go": // TODO(gri) implement missing label checks continue - case "map1.go": - // TODO(gri) fix map key checking - continue case "sizeof.go", "switch.go": // TODO(gri) tone down duplicate checking in expression switches continue diff --git a/go/types/testdata/cycles.src b/go/types/testdata/cycles.src index a043bc37cf6..10b6f190bac 100644 --- a/go/types/testdata/cycles.src +++ b/go/types/testdata/cycles.src @@ -63,7 +63,7 @@ type ( I6 interface{ I5 } // maps - M0 map[M0]M0 + M0 map[M0 /* ERROR "invalid map key" */ ]M0 // channels C0 chan C0 @@ -103,7 +103,7 @@ func _() { i0 /* ERROR "cycle" */ interface{ i0 } // maps - m0 map[m0]m0 + m0 map[m0 /* ERROR "invalid map key" */ ]m0 // channels c0 chan c0 diff --git a/go/types/typexpr.go b/go/types/typexpr.go index 8ddaf4ecbb5..cc480668b3d 100644 --- a/go/types/typexpr.go +++ b/go/types/typexpr.go @@ -293,6 +293,15 @@ func (check *checker) typ0(e ast.Expr, def *Named, cycleOk bool) Type { typ.key = check.typ(e.Key, nil, true) typ.elt = check.typ(e.Value, nil, true) + + // spec: "The comparison operators == and != must be fully defined + // for operands of the key type; thus the key type must not be a + // function, map, or slice." + if !isComparable(typ.key) { + check.errorf(e.Key.Pos(), "invalid map key type %s", typ.key) + // ok to continue + } + return typ case *ast.ChanType: