Skip to content

Commit f27d6a2

Browse files
committed
cmd/compile: builtins may be in the unsafe package
Now that unsafe.Sizeof and friends can operate on generic parameters, and evaluate to non-constants, we need to export/import them correctly. Fixes #48094 Change-Id: If3ebf77255385cd5462e13fb7ced8b157ba3cf5b Reviewed-on: https://go-review.googlesource.com/c/go/+/346469 Trust: Keith Randall <[email protected]> Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]>
1 parent 6815235 commit f27d6a2

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,7 @@ func (w *exportWriter) expr(n ir.Node) {
16921692
isBuiltin := n.BuiltinOp != ir.OXXX
16931693
w.bool(isBuiltin)
16941694
if isBuiltin {
1695+
w.bool(n.Sym().Pkg == types.UnsafePkg)
16951696
w.string(n.Sym().Name)
16961697
break
16971698
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,11 @@ func (r *importReader) node() ir.Node {
12691269
case ir.ONAME:
12701270
isBuiltin := r.bool()
12711271
if isBuiltin {
1272-
return types.BuiltinPkg.Lookup(r.string()).Def.(*ir.Name)
1272+
pkg := types.BuiltinPkg
1273+
if r.bool() {
1274+
pkg = types.UnsafePkg
1275+
}
1276+
return pkg.Lookup(r.string()).Def.(*ir.Name)
12731277
}
12741278
return r.localName()
12751279

test/typeparam/issue48094.dir/a.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2021 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+
import "unsafe"
8+
9+
func F[T any]() uintptr {
10+
var t T
11+
return unsafe.Sizeof(t)
12+
}
13+
14+
func G[T any]() uintptr {
15+
var t T
16+
return unsafe.Alignof(t)
17+
}
18+
19+
//func H[T any]() uintptr {
20+
// type S struct {
21+
// a T
22+
// b T
23+
// }
24+
// var s S
25+
// return unsafe.Offsetof(s.b)
26+
//}

test/typeparam/issue48094.dir/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2021 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 main
6+
7+
import "a"
8+
9+
func main() {
10+
if a.F[int64]() != 8 {
11+
panic("bad")
12+
}
13+
if a.G[int8]() != 1 {
14+
panic("bad")
15+
}
16+
// TODO: enable once 47631 is fixed.
17+
//if a.H[int64]() != 8 {
18+
// panic("bad")
19+
//}
20+
}

test/typeparam/issue48094.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rundir -G=3
2+
3+
// Copyright 2021 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

0 commit comments

Comments
 (0)