Skip to content

Commit 2d357d8

Browse files
cuonglmmdempsky
authored andcommitted
cmd/compile: fix typecheck type alias makes wrong export symbol metadata
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 1d1ba85 commit 2d357d8

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

src/cmd/compile/internal/gc/typecheck.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -3671,7 +3671,11 @@ func typecheckdef(n *Node) {
36713671
n.SetDiag(true)
36723672
goto ret
36733673
}
3674-
n.Sym.Def = asTypesNode(p.Ntype)
3674+
// For package-level type aliases, set n.Sym.Def so we can identify
3675+
// it as a type alias during export. See also #31959.
3676+
if n.Name.Curfn == nil {
3677+
n.Sym.Def = asTypesNode(p.Ntype)
3678+
}
36753679
}
36763680
break
36773681
}

test/fixedbugs/issue31959.dir/a.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
type T struct{}
8+
9+
func F() {
10+
type T = int
11+
println(T(0))
12+
}

test/fixedbugs/issue31959.dir/main.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run
2+
3+
// Copyright 2019 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Check import package contains type alias in function
8+
// with the same name with an export type not panic
9+
10+
package main
11+
12+
import (
13+
"fmt"
14+
15+
"a"
16+
)
17+
18+
func main() {
19+
fmt.Println(a.T{})
20+
a.F()
21+
}

test/fixedbugs/issue31959.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rundir
2+
3+
// Copyright 2019 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package ignored

test/fixedbugs/issue31959.out

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{}
2+
0

0 commit comments

Comments
 (0)