Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile-time error VS. (Run-time) Detectable illegal behaviour. #7388

Closed
fonghou opened this issue Dec 10, 2020 · 4 comments
Closed

Compile-time error VS. (Run-time) Detectable illegal behaviour. #7388

fonghou opened this issue Dec 10, 2020 · 4 comments
Milestone

Comments

@fonghou
Copy link

fonghou commented Dec 10, 2020

Hi, just started learning zig, and like the design very much!

One thing I'm a little puzzled, what is the difference between Compile-time error VS. (Run-time) Detectable illegal behaviour.

For example https://ziglang.org/documentation/master/#Exhaustive-Switching seems saying "Non-exhaustive Switching is a compile error".

However, the example there only gives an error when running zig test test.zig, which I'd say it's a run-time Detectable illegal behaviour. For non-exhaustive swithing to be a true compile-time error, I'd expect "zig build-exe test.zig" to detect the error (without running the test. Notice, I just changed the test to a fn definition).

test.zig

const Color = enum {
    auto,
    off,
    on,
};

// test "exhaustive switching" {
fn nonExhanstiveSwitch() struct{} {
    const color = Color.off;
    switch (color) {
        Color.auto => {},
        Color.on => {},
    }
}

Other places I found where zig doc is vague w.r.t compile-time vs. run-time (including test-time) are const/immutable usage check. The initiution from other languages, I'd expect they are true compile-time check without writing/running any tests.

Am I misunderstanding some fundamental principal of zig here, or this is just a tranisent state before zig reaches 1.0?

Thanks!

@fonghou
Copy link
Author

fonghou commented Dec 11, 2020

Hmm, this may be the answer I'm looking for

Zig has lazy top level declaration analysis, which means that if a function is not called, or otherwise used, it is not analyzed. This means that there may be an undiscovered compile error in a function because it is never called.

I'm still wondering why? compiler speed? dead code elimination? or some other reasons.

If these are just trade-offs, is there (or should there be) a build mode that prefer compile-time correctness as well?

@SpexGuy
Copy link
Contributor

SpexGuy commented Dec 11, 2020

The primary reason for this is that Zig doesn't have a preprocessor. This is the replacement feature that allows one library to have code supporting multiple operating systems, without causing problems when values are undefined. As long as you don't call the windows functions when compiling for linux, they won't get compiled, and you won't get errors about missing functions.

@SpexGuy
Copy link
Contributor

SpexGuy commented Dec 11, 2020

There are several proposals for ways to validate code that isn't always referenced, the most promising being #3028 and #5208.

@fonghou
Copy link
Author

fonghou commented Dec 11, 2020

@SpexGuy, thank you for the anwser and references. Interesting... I'll read.

@fonghou fonghou closed this as completed Dec 11, 2020
@andrewrk andrewrk added this to the 0.8.0 milestone Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants