Skip to content

Commit

Permalink
go/ir: support unsafe.Add and unsafe.Slice
Browse files Browse the repository at this point in the history
This is a backport of x/tool's
640c1dea83015e5271a001c99370762fc63dc280.

Updates gh-1045

(cherry picked from commit e5c61cf)
  • Loading branch information
dominikh committed Aug 16, 2021
1 parent 1325373 commit fae7339
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
3 changes: 1 addition & 2 deletions go/ir/UPSTREAM
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ The changes are too many to list here, and it is best to consider this package i
Upstream changes still get applied when they address bugs in portions of code we have inherited.

The last upstream commit we've looked at was:
20dafe5d6055a2382de349face79f568e574d9fd

640c1dea83015e5271a001c99370762fc63dc280

4 changes: 4 additions & 0 deletions go/ir/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,10 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value {
case *ast.SelectorExpr:
sel, ok := fn.Pkg.info.Selections[e]
if !ok {
// builtin unsafe.{Add,Slice}
if obj, ok := fn.Pkg.info.Uses[e.Sel].(*types.Builtin); ok {
return &Builtin{name: "Unsafe" + obj.Name(), sig: tv.Type.(*types.Signature)}
}
// qualified identifier
return b.expr(fn, e.Sel)
}
Expand Down
43 changes: 27 additions & 16 deletions go/ir/builder_go117_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package ir_test

import (
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
Expand All @@ -18,23 +19,33 @@ import (
"honnef.co/go/tools/go/ir/irutil"
)

func TestSliceToArrayPtr(t *testing.T) {
src := `package p
func f() {
var s []byte
_ = (*[4]byte)(s)
}
`
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "p.go", src, parser.ParseComments)
if err != nil {
t.Fatal(err)
func TestBuildPackageGo117(t *testing.T) {
tests := []struct {
name string
src string
importer types.Importer
}{
{"slice to array pointer", "package p; var s []byte; var _ = (*[4]byte)(s)", nil},
{"unsafe slice", `package p; import "unsafe"; var _ = unsafe.Add(nil, 0)`, importer.Default()},
{"unsafe add", `package p; import "unsafe"; var _ = unsafe.Slice((*int)(nil), 0)`, importer.Default()},
}
files := []*ast.File{f}

pkg := types.NewPackage("p", "")
conf := &types.Config{}
if _, _, err := irutil.BuildPackage(conf, fset, pkg, files, ir.SanityCheckFunctions); err != nil {
t.Fatalf("unexpected error: %v", err)
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "p.go", tc.src, parser.ParseComments)
if err != nil {
t.Error(err)
}
files := []*ast.File{f}

pkg := types.NewPackage("p", "")
conf := &types.Config{Importer: tc.importer}
if _, _, err := irutil.BuildPackage(conf, fset, pkg, files, ir.SanityCheckFunctions); err != nil {
t.Errorf("unexpected error: %v", err)
}
})
}
}

0 comments on commit fae7339

Please sign in to comment.