aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2019-11-06 14:52:35 +0100
committerRon Evans <[email protected]>2019-11-06 16:30:07 +0100
commit913131bf627e9df0a87b1dc6699779faaf70ca5d (patch)
treeffcf02cc975a3ed2bae16387e1f20b77341a290a
parentb41e58bcb4a769f0fbfd7d1c85aaeb239802dc4e (diff)
downloadtinygo-913131bf627e9df0a87b1dc6699779faaf70ca5d.tar.gz
tinygo-913131bf627e9df0a87b1dc6699779faaf70ca5d.zip
cgo: avoid '"unsafe" imported but not used' error
This can happen when not all CGo features are used.
-rw-r--r--cgo/cgo.go27
-rw-r--r--cgo/testdata/basic.out.go2
-rw-r--r--cgo/testdata/types.out.go2
3 files changed, 31 insertions, 0 deletions
diff --git a/cgo/cgo.go b/cgo/cgo.go
index 22666499a..e11b15ce3 100644
--- a/cgo/cgo.go
+++ b/cgo/cgo.go
@@ -185,6 +185,7 @@ func Process(files []*ast.File, dir string, fset *token.FileSet, cflags []string
Name: files[0].Name.Name,
},
Decls: []ast.Decl{
+ // import "unsafe"
&ast.GenDecl{
TokPos: p.generatedPos,
Tok: token.IMPORT,
@@ -192,6 +193,32 @@ func Process(files []*ast.File, dir string, fset *token.FileSet, cflags []string
unsafeImport,
},
},
+ // var _ unsafe.Pointer
+ // This avoids type errors when the unsafe package is never used.
+ &ast.GenDecl{
+ Tok: token.VAR,
+ Specs: []ast.Spec{
+ &ast.ValueSpec{
+ Names: []*ast.Ident{
+ &ast.Ident{
+ Name: "_",
+ Obj: &ast.Object{
+ Kind: ast.Var,
+ Name: "_",
+ },
+ },
+ },
+ Type: &ast.SelectorExpr{
+ X: &ast.Ident{
+ Name: "unsafe",
+ },
+ Sel: &ast.Ident{
+ Name: "Pointer",
+ },
+ },
+ },
+ },
+ },
},
Imports: []*ast.ImportSpec{unsafeImport},
}
diff --git a/cgo/testdata/basic.out.go b/cgo/testdata/basic.out.go
index 26126a163..1fb2c4d1a 100644
--- a/cgo/testdata/basic.out.go
+++ b/cgo/testdata/basic.out.go
@@ -2,6 +2,8 @@ package main
import "unsafe"
+var _ unsafe.Pointer
+
type C.int16_t = int16
type C.int32_t = int32
type C.int64_t = int64
diff --git a/cgo/testdata/types.out.go b/cgo/testdata/types.out.go
index 939c1a5a6..bdc2e57f7 100644
--- a/cgo/testdata/types.out.go
+++ b/cgo/testdata/types.out.go
@@ -2,6 +2,8 @@ package main
import "unsafe"
+var _ unsafe.Pointer
+
const C.option2A = 20
const C.optionA = 0
const C.optionB = 1