c2compiler
c2compiler copied to clipboard
enum func checks can go wrong
The following fragments passes the analyser, but fails in C compilation.
module test;
type Kind enum u8 {
A, B, C
}
fn bool Kind.isA(Kind k) {
return k == A;
}
public fn i32 main() {
Kind k = A;
bool is_a = !k.isA; // forget ()
// Note bool is_a = k.isA; does give a c2c error
return 0;
}
!k.isA is equivalent to k.isA == nil, which is OK if we allow taking the address of a type function.
If allowed, k.isA should be the same as Kind.isA.
The C code generated is incorrect: _Bool is_a = !k.test_Kind_isA;
bool aa = !Kind.isA; works fine, but generates a warnings with clang because the result is always true. We might want to flag flag as well in the analyser.
The issue I sometimes have is that you forget the parentheses. I think c2c should generate an error if assigning a (type-)function to something other than a Function type (for callbacks).