mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-03 18:47:47 +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.
50 lines
922 B
Go
50 lines
922 B
Go
package main
|
|
|
|
/*
|
|
#warning some warning
|
|
|
|
typedef struct {
|
|
int x;
|
|
int y;
|
|
} point_t;
|
|
|
|
typedef someType noType; // undefined type
|
|
|
|
#define SOME_CONST_1 5) // invalid const syntax
|
|
#define SOME_CONST_2 6) // const not used (so no error)
|
|
#define SOME_CONST_3 1234 // const too large for byte
|
|
*/
|
|
//
|
|
//
|
|
// #define SOME_CONST_4 8) // after some empty lines
|
|
import "C"
|
|
|
|
// #warning another warning
|
|
import "C"
|
|
|
|
// #include <stdint.h>
|
|
import "C"
|
|
|
|
// Make sure that errors for the following lines won't change with future
|
|
// additions to the CGo preamble.
|
|
//
|
|
//line errors.go:100
|
|
var (
|
|
// constant too large
|
|
_ C.char = 2 << 10
|
|
|
|
// z member does not exist
|
|
_ C.point_t = C.point_t{z: 3}
|
|
|
|
// constant has syntax error
|
|
_ = C.SOME_CONST_1
|
|
|
|
_ byte = C.SOME_CONST_3
|
|
|
|
_ = C.SOME_CONST_4
|
|
|
|
// This must result in a type error. Previously, TinyGo would allow this
|
|
// code (which is not allowed by upstream Go).
|
|
_ int8 = C.int8_t(5)
|
|
)
|