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

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 #19158

Closed
orvly opened this issue Mar 2, 2024 · 10 comments
Labels
error message This issue points out an error message that is unhelpful and should be improved.
Milestone

Comments

@orvly
Copy link

orvly commented Mar 2, 2024

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 orvly added the error message This issue points out an error message that is unhelpful and should be improved. label Mar 2, 2024
@RossComputerGuy
Copy link
Contributor

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

@xdBronch
Copy link
Contributor

xdBronch commented Mar 2, 2024

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

@rohlem
Copy link
Contributor

rohlem commented Mar 3, 2024

looks similar to #18900 to me

@RossComputerGuy
Copy link
Contributor

RossComputerGuy commented Mar 3, 2024

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

@rohlem
Copy link
Contributor

rohlem commented Mar 4, 2024

@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).

@gwenzek
Copy link
Contributor

gwenzek commented Jun 17, 2024

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

@gwenzek
Copy link
Contributor

gwenzek commented Jun 24, 2024

This is probably caused by #18900

@WillLillis
Copy link
Contributor

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

@andrewrk andrewrk added this to the 0.14.0 milestone Jul 26, 2024
@gwenzek
Copy link
Contributor

gwenzek commented Jul 26, 2024

thanks !

@yuyuasm
Copy link

yuyuasm commented Jan 2, 2025

const std = @import("std");

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error message This issue points out an error message that is unhelpful and should be improved.
Projects
None yet
Development

No branches or pull requests

8 participants