zig icon indicating copy to clipboard operation
zig copied to clipboard

Compiler error message for calling std.debug.print with the wrong format string doesn't include the line number of the error, even when compiling with -freference-trace

Open orvly opened this issue 2 years ago • 8 comments

Zig Version

0.12.0-dev.3126+136d7c213

Steps to Reproduce and Observed Output

Zig Version 0.12.0-dev

Steps to Reproduce and Observed Output

When you call std.debug.print with too few arguments:

pub fn main() void {
 const myconst : i32 = 42;
 std.debug.print("Hello {s}\n", .{myconst});
}

The error message doesn't include the line number, even when compiling with -freference-trace

In #18485 , which looks very similar, @xdBronch says that is fixed in master, and I'm on the latest version, AFAIK.

Command line

zig run main.zig -freference-trace

Output Notice how nowhere in this output is the actual source line in my own code specified.

D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\fmt.zig:470:5: error: invalid format string 's' for type 'i32'
    @compileError("invalid format string '" ++ fmt ++ "' for type '" ++ @typeName(@TypeOf(value)) ++ "'");
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    formatIntValue__anon_8030: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\fmt.zig:752:24
    formatValue__anon_7838: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\fmt.zig:704:52
    formatType__anon_6885: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\fmt.zig:498:31
    format__anon_3722: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\fmt.zig:184:23
    print__anon_3693: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\io\Writer.zig:23:26
    print: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\io.zig:322:47
    dumpStackTraceFromBase: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\debug.zig:284:19
    dumpSegfaultInfoWindows: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\debug.zig:2707:5
    handleSegfaultWindowsExtra: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\debug.zig:2679:17
    handleSegfaultWindows: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\debug.zig:2651:45
    attachSegfaultHandler: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\debug.zig:2540:83
    maybeEnableSegfaultHandler: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\debug.zig:2521:18
    WinStartup: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\start.zig:348:14

Note: I'm new to zig (just started learning today), so I might be misunderstanding something.

Expected Output

I'd expect the error message to include the line number of the offending statement.

orvly avatar Mar 02 '24 20:03 orvly

The error trace does say where it comes from, strange that it says its with the segfault handling.

    dumpStackTraceFromBase: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\debug.zig:284:19
    dumpSegfaultInfoWindows: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\debug.zig:2707:5
    handleSegfaultWindowsExtra: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\debug.zig:2679:17
    handleSegfaultWindows: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\debug.zig:2651:45
    attachSegfaultHandler: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\debug.zig:2540:83
    maybeEnableSegfaultHandler: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\debug.zig:2521:18
    WinStartup: D:\zig\zig-windows-x86_64-0.12.0-dev.3126+136d7c213\lib\std\start.zig:348:14

RossComputerGuy avatar Mar 02 '24 20:03 RossComputerGuy

i saw this happen to someone the other day where it points at the segfault handler, i have no idea why thats happening but im pretty sure its fairly recent

xdBronch avatar Mar 02 '24 20:03 xdBronch

looks similar to #18900 to me

rohlem avatar Mar 03 '24 19:03 rohlem

@rohlem That one occurs by calling logging methods, this is occurring in the stack trace printing code in std.

RossComputerGuy avatar Mar 03 '24 19:03 RossComputerGuy

@RossComputerGuy Right, and as far as I can see both cases trigger the @compileError at std/fmt.zig:470, "invalid format string 's' for type '<other>'". I wanted to link the issues together to suggest re-testing both against a fix (which I assume may apply to /fix both issues).

rohlem avatar Mar 04 '24 13:03 rohlem

Proposed a solution in #20292 : tweak fmt.format to log the culprit formatting string.

gwenzek avatar Jun 17 '24 19:06 gwenzek

This is probably caused by https://github.com/ziglang/zig/issues/18900

gwenzek avatar Jun 24 '24 14:06 gwenzek

Running the sample program with -freference-trace has the line number from main.zig included again:

❯ zig run src/main.zig -freference-trace
/home/lillis/zig/lib/std/fmt.zig:483:5: error: invalid format string 's' for type 'i32'
    @compileError("invalid format string '" ++ fmt ++ "' for type '" ++ @typeName(@TypeOf(value)) ++ "'");
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    formatIntValue__anon_8891: /home/lillis/zig/lib/std/fmt.zig:765:24
    formatValue__anon_8614: /home/lillis/zig/lib/std/fmt.zig:717:52
    formatType__anon_8478: /home/lillis/zig/lib/std/fmt.zig:511:31
    format__anon_7837: /home/lillis/zig/lib/std/fmt.zig:188:23
    print__anon_3748: /home/lillis/zig/lib/std/io/Writer.zig:24:26
    print__anon_3343: /home/lillis/zig/lib/std/io.zig:324:47
    main: src/main.zig:5:20
    posixCallMainAndExit: /home/lillis/zig/lib/std/start.zig:523:22
    _start: /home/lillis/zig/lib/std/start.zig:361:40
    comptime_0: /home/lillis/zig/lib/std/start.zig:99:54

WillLillis avatar Jul 24 '24 22:07 WillLillis

thanks !

gwenzek avatar Jul 26 '24 09:07 gwenzek

const std = @import("std");

pub fn main() void {
    const myconst : i32 = 42;
    std.debug.print("Hello {d}\n", .{myconst});
}

你应该使用 {d}, 这是一个int !!!

yuyuasm avatar Jan 02 '25 11:01 yuyuasm