mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-26 06:38:42 +00:00
3ecefd6a0a
Code like this is not allowed by the upstream Go CGo implementation, but
was allowed by TinyGo:
var _ int8 = C.int8_t(5)
The reason it shouldn't be allowed is a little bit complicated. While
it is true that C.int8_t is always the same underlying data type as Go
int8 (signed 8-bit integer), the C type is actually a typedef of one of
the base C types (usually unsigned char or signed char) which in turn do
_not_ map cleanly to Go types: the 'char' type is ambiguous (it may be
either signed or unsigned depending on the ABI) and types like 'int'
vary in size by ABI as well.
To make code more portable, I think it's better to match the upstream
implementation.
64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
// CGo errors:
|
|
// testdata/errors.go:4:2: warning: some warning
|
|
// testdata/errors.go:11:9: error: unknown type name 'someType'
|
|
// testdata/errors.go:22:5: warning: another warning
|
|
// testdata/errors.go:13:23: unexpected token ), expected end of expression
|
|
// testdata/errors.go:19:26: unexpected token ), expected end of expression
|
|
|
|
// Type checking errors after CGo processing:
|
|
// testdata/errors.go:102: cannot use 2 << 10 (untyped int constant 2048) as C.char value in variable declaration (overflows)
|
|
// testdata/errors.go:105: unknown field z in struct literal
|
|
// testdata/errors.go:108: undefined: C.SOME_CONST_1
|
|
// testdata/errors.go:110: cannot use C.SOME_CONST_3 (untyped int constant 1234) as byte value in variable declaration (overflows)
|
|
// testdata/errors.go:112: undefined: C.SOME_CONST_4
|
|
// testdata/errors.go:116: cannot use C.int8_t(5) (constant 5 of type C.schar) as int8 value in variable declaration
|
|
|
|
package main
|
|
|
|
import "unsafe"
|
|
|
|
var _ unsafe.Pointer
|
|
|
|
//go:linkname C.CString runtime.cgo_CString
|
|
func C.CString(string) *C.char
|
|
|
|
//go:linkname C.GoString runtime.cgo_GoString
|
|
func C.GoString(*C.char) string
|
|
|
|
//go:linkname C.__GoStringN runtime.cgo_GoStringN
|
|
func C.__GoStringN(*C.char, uintptr) string
|
|
|
|
func C.GoStringN(cstr *C.char, length C.int) string {
|
|
return C.__GoStringN(cstr, uintptr(length))
|
|
}
|
|
|
|
//go:linkname C.__GoBytes runtime.cgo_GoBytes
|
|
func C.__GoBytes(unsafe.Pointer, uintptr) []byte
|
|
|
|
func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
|
|
return C.__GoBytes(ptr, uintptr(length))
|
|
}
|
|
|
|
type (
|
|
C.char uint8
|
|
C.schar int8
|
|
C.uchar uint8
|
|
C.short int16
|
|
C.ushort uint16
|
|
C.int int32
|
|
C.uint uint32
|
|
C.long int32
|
|
C.ulong uint32
|
|
C.longlong int64
|
|
C.ulonglong uint64
|
|
)
|
|
type C.struct_point_t struct {
|
|
x C.int
|
|
y C.int
|
|
}
|
|
type C.point_t = C.struct_point_t
|
|
|
|
const C.SOME_CONST_3 = 1234
|
|
|
|
type C.int8_t = C.schar
|