Skip to content

Formatting with {s} on a struct produces confusing error #21965

@DonIsaac

Description

@DonIsaac

Zig Version

0.13.0

Steps to Reproduce and Observed Output

I'm writing a graphical formatter that pretty-prints custom Error objects. Note that Error is a struct, not an error union. Be advised, all NOTE comments have been added to this issue but do not exist in my code. They exist solely as clarification.

// NOTE: const Error = @import("Error.zig");
const GraphicalFormatter = struct {
    fn format(self: *GraphicalFormatter, w: *std.fs.File.Writer, e: Error) !void {
        // NOTE: .message refactored from `[]const u8` to `PossiblyStaticStr`, but
        // format string hasn't been changed yet. This is the userland bug.
        try w.print("{s}", .{e.message});
        _ = self; // rest is omitted
    }
};

I've just refactored my Error's message field, changing its type from []const str to PossiblyStaticStr.

// src/Error.zig
message: PossiblyStaticStr,

// NOTE: unrelated fields, functions, and structs are omitted
pub const PossiblyStaticStr = struct {
    static: bool = true,
    str: string,
};

When I run zig build check (basically just zig build install but artifacts aren't installed), I get this error:

check
└─ zig build-exe <project-name> Debug native 1 errors
/opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/fmt.zig:471:5: error: invalid format string 's' for type 'Error.PossiblyStaticStr'
    @compileError("invalid format string '" ++ fmt ++ "' for type '" ++ @typeName(@TypeOf(value)) ++ "'");
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    formatType__anon_9105: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/fmt.zig:578:53
    format__anon_5787: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/fmt.zig:185:23
    print__anon_4037: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/io/Writer.zig:24:26
    print: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/io.zig:324:47
    dumpSegfaultInfoPosix: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/debug.zig:2643:32
    handleSegfaultPosix: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/debug.zig:2617:13
    attachSegfaultHandler: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/debug.zig:2558:36
    maybeEnableSegfaultHandler: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/debug.zig:2535:18
    callMainWithArgs: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/start.zig:479:14
    main: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/start.zig:497:12
error: the following command failed with 1 compilation errors:
/opt/homebrew/Cellar/zig/0.13.0/bin/zig build-exe -freference-trace=256 -ODebug --dep util --dep smart-pointers -Mroot=<omitted>/<project-name>/src/main.zig -ODebug -Mutil=<omitted>/<project-name>/src/util.zig -ODebug -Msmart-pointers=<omitted>/.cache/zig/p/1220d71e011c53ca52d972f5b085339f113e471ba4e4922d9ecc5c10f7b5cc998f19/src/root.zig -fno-emit-bin --cache-dir <omitted>/<project-name>/.zig-cache --global-cache-dir <omitted>/.cache/zig --name <project-name> --listen=- 

Expected Output

  1. I should see an error telling me I'm passing a struct to a {s} in a format string
  2. The stack trace should include frames within my source code. The offending line (in my code, not in the zig std library) should be more prominent to bring my attention to the bug I introduced. EDIT: fixed in master branch

Metadata

Metadata

Assignees

No one assigned

    Labels

    error messageThis issue points out an error message that is unhelpful and should be improved.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions