cgo: do type checking in CGo testing

Such type checking should hopefully catch more bugs.

This commit also fixes some existing type errors.
Этот коммит содержится в:
Ayke van Laethem 2019-11-06 14:54:34 +01:00 коммит произвёл Ron Evans
родитель 913131bf62
коммит 473576e756
2 изменённых файлов: 39 добавлений и 4 удалений

Просмотреть файл

@ -2,10 +2,12 @@ package cgo
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"path/filepath"
"strings"
@ -34,6 +36,23 @@ func TestCGo(t *testing.T) {
t.Errorf("error during CGo processing: %v", err)
}
// Check the AST for type errors.
hasTypeError := false
config := types.Config{
Error: func(err error) {
t.Error("typecheck error:", err)
hasTypeError = true
},
Importer: simpleImporter{},
Sizes: types.SizesFor("gccgo", "arm"),
}
_, err = config.Check("", fset, []*ast.File{f, cgoAST}, nil)
if err != nil && !hasTypeError {
// Only report errors when no type errors are found (an
// unexpected condition).
t.Error(err)
}
// Store the (formatted) output in a buffer. Format it, so it
// becomes easier to read (and will hopefully change less with CGo
// changes).
@ -60,3 +79,19 @@ func TestCGo(t *testing.T) {
})
}
}
// simpleImporter implements the types.Importer interface, but only allows
// importing the unsafe package.
type simpleImporter struct {
}
// Import implements the Importer interface. For testing usage only: it only
// supports importing the unsafe package.
func (i simpleImporter) Import(path string) (*types.Package, error) {
switch path {
case "unsafe":
return types.Unsafe, nil
default:
return nil, fmt.Errorf("importer not implemented for package %s", path)
}
}

8
cgo/testdata/types.go предоставленный
Просмотреть файл

@ -92,16 +92,16 @@ var (
// Arrays.
_ C.myIntArray
_ C.myIntArrayPtr
)
// Test bitfield accesses.
func foo() {
var x C.bitfield_t
x.start = 3
x.a = 4
x.b = 1
x.c = 2
x.set_bitfield_a(4)
x.set_bitfield_b(1)
x.set_bitfield_c(2)
x.d = 10
x.e = 5
var _ C.uchar = x.bitfield_a()
}