]> Cypherpunks repositories - gostls13.git/commitdiff
gc: fix typed constant declarations
authorAnthony Martin <ality@pbrane.org>
Tue, 18 Jan 2011 16:24:49 +0000 (11:24 -0500)
committerRuss Cox <rsc@golang.org>
Tue, 18 Jan 2011 16:24:49 +0000 (11:24 -0500)
Fixes #1411.

R=rsc, gri, ejsherry
CC=golang-dev
https://golang.org/cl/4035042

src/cmd/gc/walk.c
test/fixedbugs/bug318.go [new file with mode: 0644]

index fa3e5d5e4c31a87cdcab5d84a05cf2c2595c0073..5faf630b888a58c9d6a7880748ae8b6357739d9a 100644 (file)
@@ -269,9 +269,15 @@ walkdef(Node *n)
                }
                t = n->type;
                if(t != T) {
-                       convlit(&e, t);
-                       if(!okforconst[t->etype])
+                       if(!okforconst[t->etype]) {
                                yyerror("invalid constant type %T", t);
+                               goto ret;
+                       }
+                       if(!isideal(e->type) && !eqtype(t, e->type)) {
+                               yyerror("cannot use %+N as type %T in const initializer", e, t);
+                               goto ret;
+                       }
+                       convlit(&e, t);
                }
                n->val = e->val;
                n->type = e->type;
diff --git a/test/fixedbugs/bug318.go b/test/fixedbugs/bug318.go
new file mode 100644 (file)
index 0000000..9c46a04
--- /dev/null
@@ -0,0 +1,12 @@
+// errchk $G $D/$F.go
+
+// Copyright 2011 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 1411.
+
+package main
+
+const ui uint = 0
+const i int = ui // ERROR "type"