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

Invalid code compiles in comptime #21405

Closed
PanSashko opened this issue Sep 14, 2024 · 1 comment · Fixed by #21422
Closed

Invalid code compiles in comptime #21405

PanSashko opened this issue Sep 14, 2024 · 1 comment · Fixed by #21422
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@PanSashko
Copy link
Contributor

PanSashko commented Sep 14, 2024

Zig Version

0.14.0-dev.1550+4fba7336a

Steps to Reproduce and Observed Behavior

The following code compiles

test {
    const x: ?u32 = 10;
    _ = comptime if (x) x.f();
}

That might cause a confusing bug in more complex cases, when the function f actually exists in x.

Expected Behavior

Like without comptime the compilation should fail with

error: expected type 'bool', found '?u32'
@PanSashko PanSashko added the bug Observed behavior contradicts documented or intended behavior label Sep 14, 2024
@mlugg
Copy link
Member

mlugg commented Sep 14, 2024

Caused by .condbr_inline case (and equivalent code in the .condbr case) in Sema.analyzeBodyInner missing the required coercion to bool; see Sema.zirCondbr for the runtime context which performs the proper coercion.

@mlugg mlugg added this to the 0.14.0 milestone Sep 14, 2024
mlugg added a commit to mlugg/zig that referenced this issue Sep 15, 2024
Also, start using labeled switch statements when dispatching
maybe-runtime instructions like condbr to comptime-only variants like
condbr_inline.

This can't be merged until we get a zig1.wasm update due to ziglang#21385.

Resolves: ziglang#21405
mlugg added a commit to mlugg/zig that referenced this issue Oct 27, 2024
Also, start using labeled switch statements when dispatching
maybe-runtime instructions like condbr to comptime-only variants like
condbr_inline.

This can't be merged until we get a zig1.wasm update due to ziglang#21385.

Resolves: ziglang#21405
@mlugg mlugg closed this as completed in 05b445a Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants