Skip to content

stage2: compiler panic when using @ptrToInt in bitwise AND #11211

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

Closed
topolarity opened this issue Mar 17, 2022 · 2 comments · Fixed by #11734
Closed

stage2: compiler panic when using @ptrToInt in bitwise AND #11211

topolarity opened this issue Mar 17, 2022 · 2 comments · Fixed by #11734
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@topolarity
Copy link
Contributor

Zig Version

0.10.0-dev.1393+291f5055f

Steps to Reproduce

test "comptime ptrToInt in AND expression" {
    comptime var x: u8 = 15;
    try std.testing.expect(@ptrToInt(&x) & 0x00 == 0);
}

Expected Behavior

Test should compile successfully and pass.

Actual Behavior

The test fails because we end up calling toBigInt on a decl_ref_mut, which is not an integer type.

We're probably missing a dereference here for the indirect Value.

Analyzing testme.zig: testme.zig:test.comptime ptrToInt in AND expression
      %28 = decl_ref("std")
      %29 = field_ptr(%28, "testing")
      %30 = field_call_bind(%29, "expect")
      %31 = dbg_stmt(3, 27)
      %32 = ptr_to_int(%23)
      %33 = int(255)
    > %34 = bit_and(%32, %33)
      %35 = cmp_eq(%34, @Ref.zero)
      %36 = call(.auto, %30, [%35])
      %37 = is_non_err(%36)
      %38 = condbr(%37, {
        %40 = err_union_payload_unsafe(%36)
        %43 = break(%39, %40)
      }, {
        %41 = err_union_code(%36)
        %42 = ret_node(%41)
      })
    For full context, use the command
      zig ast-check -t testme.zig

  in testme.zig: testme.zig:test.comptime ptrToInt in AND expression
    > %39 = block({%28..%38})

error(stdlib): metadata: 7fdb1aa25f68
/home/topolarity/repos/zig/src/value.zig:983:21: 0x3080801 in value.Value.toBigInt (zig)
            else => unreachable,
                    ^
/home/topolarity/repos/zig/src/value.zig:3007:40: 0x34dfe1b in value.Value.bitwiseAnd (zig)
        const lhs_bigint = lhs.toBigInt(&lhs_space);
                                       ^
/home/topolarity/repos/zig/src/Sema.zig:8222:51: 0x3323c31 in Sema.zirBitwise (zig)
                .bit_and => try lhs_val.bitwiseAnd(rhs_val, sema.arena),
                                                  ^
/home/topolarity/repos/zig/src/Sema.zig:645:65: 0x31e1c8a in Sema.analyzeBodyInner (zig)
            .bit_and                      => try sema.zirBitwise(block, inst, .bit_and),
                                                                ^
/home/topolarity/repos/zig/src/Sema.zig:3850:34: 0x35083c2 in Sema.resolveBlockBody (zig)
        if (sema.analyzeBodyInner(child_block, body)) |_| {
                                 ^
@topolarity topolarity added the bug Observed behavior contradicts documented or intended behavior label Mar 17, 2022
@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Mar 27, 2022
@Vexu Vexu added this to the 0.10.0 milestone Mar 27, 2022
@davidgmbb
Copy link
Contributor

This is no longer crashing

@Vexu
Copy link
Member

Vexu commented Sep 30, 2022

Now it just behaves (IMO) incorrectly.

@andrewrk andrewrk modified the milestones: 0.10.0, 0.10.1 Oct 12, 2022
@andrewrk andrewrk modified the milestones: 0.10.1, 0.11.0 Jan 10, 2023
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 frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants