]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix typecheck type alias makes wrong export symbol metadata
authorLE Manh Cuong <cuong.manhle.vn@gmail.com>
Wed, 15 May 2019 19:28:47 +0000 (02:28 +0700)
committerMatthew Dempsky <mdempsky@google.com>
Tue, 21 May 2019 17:44:21 +0000 (17:44 +0000)
typecheck type alias always replaces the original definition of the symbol.
This is wrong behavior because if the symbol's definition is replaced by a
local type alias, it ends up being written to compiled file as an alias,
instead of the original type.

To fix, only replace the definition of symbol with global type alias.

Fixes #31959

Change-Id: Id85a15e8a9d6a4b06727e655a95dc81e63df633a
Reviewed-on: https://go-review.googlesource.com/c/go/+/177378
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/typecheck.go
test/fixedbugs/issue31959.dir/a.go [new file with mode: 0644]
test/fixedbugs/issue31959.dir/main.go [new file with mode: 0644]
test/fixedbugs/issue31959.go [new file with mode: 0644]
test/fixedbugs/issue31959.out [new file with mode: 0644]

index 81f59013f4ef02813c25c54a0f09f65da983e17e..4cb28d6100b1495be8165e50360ca7639b6d3485 100644 (file)
@@ -3671,7 +3671,11 @@ func typecheckdef(n *Node) {
                                        n.SetDiag(true)
                                        goto ret
                                }
-                               n.Sym.Def = asTypesNode(p.Ntype)
+                               // For package-level type aliases, set n.Sym.Def so we can identify
+                               // it as a type alias during export. See also #31959.
+                               if n.Name.Curfn == nil {
+                                       n.Sym.Def = asTypesNode(p.Ntype)
+                               }
                        }
                        break
                }
diff --git a/test/fixedbugs/issue31959.dir/a.go b/test/fixedbugs/issue31959.dir/a.go
new file mode 100644 (file)
index 0000000..6c7ffa3
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2019 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.
+
+package a
+
+type T struct{}
+
+func F() {
+       type T = int
+       println(T(0))
+}
diff --git a/test/fixedbugs/issue31959.dir/main.go b/test/fixedbugs/issue31959.dir/main.go
new file mode 100644 (file)
index 0000000..895c4e5
--- /dev/null
@@ -0,0 +1,21 @@
+// run
+
+// Copyright 2019 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.
+
+// Check import package contains type alias in function
+// with the same name with an export type not panic
+
+package main
+
+import (
+       "fmt"
+
+       "a"
+)
+
+func main() {
+       fmt.Println(a.T{})
+       a.F()
+}
diff --git a/test/fixedbugs/issue31959.go b/test/fixedbugs/issue31959.go
new file mode 100644 (file)
index 0000000..af6f134
--- /dev/null
@@ -0,0 +1,7 @@
+// rundir
+
+// Copyright 2019 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.
+
+package ignored
diff --git a/test/fixedbugs/issue31959.out b/test/fixedbugs/issue31959.out
new file mode 100644 (file)
index 0000000..8ddcb67
--- /dev/null
@@ -0,0 +1,2 @@
+{}
+0