c2compiler icon indicating copy to clipboard operation
c2compiler copied to clipboard

enum func checks can go wrong

Open bvdberg opened this issue 6 months ago • 2 comments

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;
}

bvdberg avatar Jul 09 '25 17:07 bvdberg

!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.

chqrlie avatar Jul 09 '25 18:07 chqrlie

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).

bvdberg avatar Jul 10 '25 05:07 bvdberg