aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2019-11-06 14:54:34 +0100
committerRon Evans <[email protected]>2019-11-06 16:30:07 +0100
commit473576e75637f6cc5ed3c7ccca0441870168a617 (patch)
tree2dba5d35a1077cd5241c23942f39e8e07eedf232
parent913131bf627e9df0a87b1dc6699779faaf70ca5d (diff)
downloadtinygo-473576e75637f6cc5ed3c7ccca0441870168a617.tar.gz
tinygo-473576e75637f6cc5ed3c7ccca0441870168a617.zip
cgo: do type checking in CGo testing
Such type checking should hopefully catch more bugs. This commit also fixes some existing type errors.
-rw-r--r--cgo/cgo_test.go35
-rw-r--r--cgo/testdata/types.go8
2 files changed, 39 insertions, 4 deletions
diff --git a/cgo/cgo_test.go b/cgo/cgo_test.go
index fad71cf4d..3034f012f 100644
--- a/cgo/cgo_test.go
+++ b/cgo/cgo_test.go
@@ -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)
+ }
+}
diff --git a/cgo/testdata/types.go b/cgo/testdata/types.go
index bd1f0ffbe..d7276dcb2 100644
--- a/cgo/testdata/types.go
+++ b/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()
}