github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/go/ir/builder_go117_test.go (about)

     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  //go:build go1.17
     6  // +build go1.17
     7  
     8  package ir_test
     9  
    10  import (
    11  	"go/ast"
    12  	"go/importer"
    13  	"go/parser"
    14  	"go/token"
    15  	"go/types"
    16  	"testing"
    17  
    18  	"github.com/amarpal/go-tools/go/ir"
    19  	"github.com/amarpal/go-tools/go/ir/irutil"
    20  )
    21  
    22  func TestBuildPackageGo117(t *testing.T) {
    23  	tests := []struct {
    24  		name     string
    25  		src      string
    26  		importer types.Importer
    27  	}{
    28  		{"slice to array pointer", "package p; var s []byte; var _ = (*[4]byte)(s)", nil},
    29  		{"unsafe slice", `package p; import "unsafe"; var _ = unsafe.Add(nil, 0)`, importer.Default()},
    30  		{"unsafe add", `package p; import "unsafe"; var _ = unsafe.Slice((*int)(nil), 0)`, importer.Default()},
    31  	}
    32  
    33  	for _, tc := range tests {
    34  		tc := tc
    35  		t.Run(tc.name, func(t *testing.T) {
    36  			t.Parallel()
    37  			fset := token.NewFileSet()
    38  			f, err := parser.ParseFile(fset, "p.go", tc.src, parser.ParseComments|parser.SkipObjectResolution)
    39  			if err != nil {
    40  				t.Error(err)
    41  			}
    42  			files := []*ast.File{f}
    43  
    44  			pkg := types.NewPackage("p", "")
    45  			conf := &types.Config{Importer: tc.importer}
    46  			if _, _, err := irutil.BuildPackage(conf, fset, pkg, files, ir.SanityCheckFunctions); err != nil {
    47  				t.Errorf("unexpected error: %v", err)
    48  			}
    49  		})
    50  	}
    51  }