From f27d6a23b0b9d2cb41441a5dd2bd6d65dd94acf0 Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Tue, 31 Aug 2021 09:31:56 -0700 Subject: [PATCH] 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 Run-TryBot: Keith Randall TryBot-Result: Go Bot Reviewed-by: Robert Griesemer Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/typecheck/iexport.go | 1 + src/cmd/compile/internal/typecheck/iimport.go | 6 ++++- test/typeparam/issue48094.dir/a.go | 26 +++++++++++++++++++ test/typeparam/issue48094.dir/main.go | 20 ++++++++++++++ test/typeparam/issue48094.go | 7 +++++ 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test/typeparam/issue48094.dir/a.go create mode 100644 test/typeparam/issue48094.dir/main.go create mode 100644 test/typeparam/issue48094.go diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go index 89eab4df16..f001017a86 100644 --- a/src/cmd/compile/internal/typecheck/iexport.go +++ b/src/cmd/compile/internal/typecheck/iexport.go @@ -1692,6 +1692,7 @@ func (w *exportWriter) expr(n ir.Node) { isBuiltin := n.BuiltinOp != ir.OXXX w.bool(isBuiltin) if isBuiltin { + w.bool(n.Sym().Pkg == types.UnsafePkg) w.string(n.Sym().Name) break } diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go index 0dfc33db20..45bf2563aa 100644 --- a/src/cmd/compile/internal/typecheck/iimport.go +++ b/src/cmd/compile/internal/typecheck/iimport.go @@ -1269,7 +1269,11 @@ func (r *importReader) node() ir.Node { case ir.ONAME: isBuiltin := r.bool() if isBuiltin { - return types.BuiltinPkg.Lookup(r.string()).Def.(*ir.Name) + pkg := types.BuiltinPkg + if r.bool() { + pkg = types.UnsafePkg + } + return pkg.Lookup(r.string()).Def.(*ir.Name) } return r.localName() diff --git a/test/typeparam/issue48094.dir/a.go b/test/typeparam/issue48094.dir/a.go new file mode 100644 index 0000000000..dd8c16f3ae --- /dev/null +++ b/test/typeparam/issue48094.dir/a.go @@ -0,0 +1,26 @@ +// Copyright 2021 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 + +import "unsafe" + +func F[T any]() uintptr { + var t T + return unsafe.Sizeof(t) +} + +func G[T any]() uintptr { + var t T + return unsafe.Alignof(t) +} + +//func H[T any]() uintptr { +// type S struct { +// a T +// b T +// } +// var s S +// return unsafe.Offsetof(s.b) +//} diff --git a/test/typeparam/issue48094.dir/main.go b/test/typeparam/issue48094.dir/main.go new file mode 100644 index 0000000000..eb1ddbe231 --- /dev/null +++ b/test/typeparam/issue48094.dir/main.go @@ -0,0 +1,20 @@ +// Copyright 2021 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 main + +import "a" + +func main() { + if a.F[int64]() != 8 { + panic("bad") + } + if a.G[int8]() != 1 { + panic("bad") + } + // TODO: enable once 47631 is fixed. + //if a.H[int64]() != 8 { + // panic("bad") + //} +} diff --git a/test/typeparam/issue48094.go b/test/typeparam/issue48094.go new file mode 100644 index 0000000000..76930e5e4f --- /dev/null +++ b/test/typeparam/issue48094.go @@ -0,0 +1,7 @@ +// rundir -G=3 + +// Copyright 2021 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 -- 2.48.1