]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: stop generating garbage when checking map key types
authorMatthew Dempsky <mdempsky@google.com>
Thu, 31 Mar 2016 19:30:20 +0000 (12:30 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 31 Mar 2016 20:30:32 +0000 (20:30 +0000)
Change-Id: Ib500ee92ae1a3d15f7c9f3f46d238b75184b4304
Reviewed-on: https://go-review.googlesource.com/21382
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/compile/internal/gc/parser.go
src/cmd/compile/internal/gc/subr.go
src/cmd/compile/internal/gc/type.go
src/cmd/compile/internal/gc/typecheck.go

index 3627461099110543c9217030271032b3e405b8de..746af32b958c7e8bd9901c561701128a25c1d74d 100644 (file)
@@ -3018,7 +3018,7 @@ func (p *parser) hidden_type_misc() *Type {
                p.want(']')
                s5 := p.hidden_type()
 
-               return maptype(s3, s5)
+               return typMap(s3, s5)
 
        case LSTRUCT:
                // LSTRUCT '{' ohidden_structdcl_list '}'
index 83888be6f7733199eed49a35bbd451560d83c577..c40cda0aeea73083a8298963cdb12ade5cbbd2a5 100644 (file)
@@ -372,40 +372,37 @@ func saveorignode(n *Node) {
        n.Orig = norig
 }
 
-func maptype(key *Type, val *Type) *Type {
-       if key != nil {
-               var bad *Type
-               atype := algtype1(key, &bad)
-               var mtype EType
-               if bad == nil {
-                       mtype = key.Etype
-               } else {
-                       mtype = bad.Etype
+// checkMapKeyType checks that Type key is valid for use as a map key.
+func checkMapKeyType(key *Type) {
+       var bad *Type
+       atype := algtype1(key, &bad)
+       var mtype EType
+       if bad == nil {
+               mtype = key.Etype
+       } else {
+               mtype = bad.Etype
+       }
+       switch mtype {
+       default:
+               if atype == ANOEQ {
+                       Yyerror("invalid map key type %v", key)
                }
-               switch mtype {
-               default:
-                       if atype == ANOEQ {
-                               Yyerror("invalid map key type %v", key)
-                       }
 
-               case TANY:
-                       // will be resolved later.
-                       break
+       case TANY:
+               // will be resolved later.
+               break
 
-               case TFORW:
-                       // map[key] used during definition of key.
-                       // postpone check until key is fully defined.
-                       // if there are multiple uses of map[key]
-                       // before key is fully defined, the error
-                       // will only be printed for the first one.
-                       // good enough.
-                       if key.Maplineno == 0 {
-                               key.Maplineno = lineno
-                       }
+       case TFORW:
+               // map[key] used during definition of key.
+               // postpone check until key is fully defined.
+               // if there are multiple uses of map[key]
+               // before key is fully defined, the error
+               // will only be printed for the first one.
+               // good enough.
+               if key.Maplineno == 0 {
+                       key.Maplineno = lineno
                }
        }
-
-       return typMap(key, val)
 }
 
 // methcmp sorts by symbol, then by package path for unexported symbols.
index e94ec85e604508e4ef76b0199fddc081e06f3929..c452bcf3016f2c5e20b3cb21d1e315b585f54c30 100644 (file)
@@ -275,6 +275,10 @@ func typChan(elem *Type, dir uint8) *Type {
 
 // typMap returns a new map Type with key type k and element (aka value) type v.
 func typMap(k, v *Type) *Type {
+       if k != nil {
+               checkMapKeyType(k)
+       }
+
        t := typ(TMAP)
        t.Down = k
        t.Type = v
index 5bcc8c96160d7774c8acb7aff9eaf83b0285f4ae..043022ba97920b90cb7cacf23304b2cce7698717 100644 (file)
@@ -402,7 +402,7 @@ OpSwitch:
                        return n
                }
                n.Op = OTYPE
-               n.Type = maptype(l.Type, r.Type)
+               n.Type = typMap(l.Type, r.Type)
                n.Left = nil
                n.Right = nil
 
@@ -3595,7 +3595,7 @@ ret:
 
                for _, n := range mapqueue {
                        lineno = n.Type.Maplineno
-                       maptype(n.Type, Types[TBOOL])
+                       checkMapKeyType(n.Type)
                }
 
                lineno = lno