#missing return not caught

1 messages · Page 1 of 1 (latest)

drowsy marsh
#

I was expecting this code...

pub fn intern(name: [] const u8) !Value {
    var hash: u32 = 0xdeadbeef;
    for (0..name.len) |i| {
        hash ^= hash << 13;
        hash ^= hash >> 17;
        hash ^= hash << 5;
        hash ^= name[i];
    }
}

to give a compile error (there's no return statement), instead it's not. Am I missing something?

surreal storm
#

is the code not used anywhere? zig does lazy analysis of functions so it won't find non-syntax errors in functions that aren't called

drowsy marsh
#

ah, ok... indeed it's not called; I'm very new to zig (even the syntax is still sort of weird for me) so I keep checking with the compiler if I'm writing things correctly.

surreal storm
#

makes sense, i usually try these things by including a main function or a test block that calls whatever code i'm experimenting with

drowsy marsh
#

I saw zig ast-check and bound that to a key in my editor. Is there a more comprehensive check that actually compiles everything (not down to actual machine code generation, just to find errors)? I'm using gcc -O3 -Wall -c with input from standard in for that when working with C/C++...

surreal storm
#

zig doesn't have warnings or configurable error levels so you don't need a -Wall equivalent. the pitfall is just code not getting analyzed. it's not practical to analyze all code whether or not it is reached, because there is often zig code that's not meant to be reached in some cases (e.g. code specific to one target might produce an error if it were analyzed when compiling for a different target)

#

if you want to speed up the generation of compile errors without needing to run the code yet you might try zig build-exe -fno-emit-bin, which performs full analysis of the given code for a chosen target, and produces errors if they occur, but doesn't generate machine code.

dull bough
#

you can also add

test {
    std.testing.refAllDecls(@This());
}

and run zig test which should work AFAIK

proud phoenix
drowsy marsh
#

I'm not sure I like the idea that zig really compiles only code being called... seems sort of a forcing towards "test-first" or TDD that I personally find a quite questionable practice as it slows down A LOT unless you've a crystal clear idea beforehand of what needs to be implemented and with what interface (rarely the case in my experience, unless you are just re-writing something already existing for weird reasons).
I must say feels like sort of aligned with the weird approach of having unused locals or non-mutated variables being errors (!?... those are not YET used locals and not YET mutated variables). So in a sense it's coherent... in a mistake, but coherent.

Anyway it's the way it is... zig doesn't check partial code. For now I'll just keep calling zig ast-check with the current file in memory... better than nothing.

dull bough
#

zig only using and compiling the stuff that's used is one of it's best features imo

#

it keeps binaries small and compiler fast

steady gull
#

its also fundamental to how comptime, cross-compilation, etc. work