Skip to content

make else if conditions contain debug stmt info #11160

@andrewrk

Description

@andrewrk

Zig version: 0.10.0-dev.1311+5ea94e771

const std = @import("std");

pub fn main() void {
    var x: i32 = 1234;
    if (x + 1 < 10) {
        x += 1;
    } else if (x / 2 == 0) {
        x += 2;
    }
    var y = x;
    _ = y;
}

Here you can see in a debugging session, the debugger regrettably steps over the x / 2 == 0 expression:

$ ./stage2/bin/zig build-exe test3.zig -fLLVM
$ gdb ./test3
(gdb) break test3.main
Breakpoint 1 at 0x201b24: file test3.zig, line 4.
(gdb) run
Starting program: /home/andy/dev/zig/build-release/test3 

Breakpoint 1, test3.main () at test3.zig:4
4	    var x: i32 = 1234;
(gdb) step
5	    if (x + 1 < 10) {
(gdb) 
10	    var y = x;
(gdb) 

This is because we do not emit a dbg_stmt for it in the ZIR:

        %17 = condbr(%16, {
          %19 = dbg_stmt(4, 9)
          %20 = load(%7) node_offset:6:11
          %21 = typeof(%20) node_offset:6:11
          %22 = add(%20, @Ref.one) node_offset:6:11
          %23 = store(%7, %22)
          %39 = break(%18, @Ref.void_value)
        }, {
          %30 = block({
            %24 = load(%7) node_offset:7:16
            %25 = int(2)
            %26 = div(%24, %25) node_offset:7:18
            %27 = cmp_eq(%26, @Ref.zero) node_offset:7:22
            %28 = as_node(@Ref.bool_type, %27) node_offset:7:22
            %29 = condbr(%28, {
              %31 = dbg_stmt(6, 9)
              %32 = load(%7) node_offset:8:11
              %33 = typeof(%32) node_offset:8:11
              %34 = int(2)
              %35 = add(%32, %34) node_offset:8:11
              %36 = store(%7, %35)
              %37 = break(%30, @Ref.void_value)
            }, {
              %38 = break(%30, @Ref.void_value)
            }) node_offset:7:12
          }) node_offset:7:12

If you ctrl+f to highlight dbg_stmt you can see that one is missing from the else if branch, just before the condition.

Metadata

Metadata

Assignees

Labels

enhancementSolving this issue will likely involve adding new logic or components to the codebase.frontendTokenization, parsing, AstGen, Sema, and Liveness.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions