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

Fmt throws compile-time error on a different call reference #22307

Open
xgallom opened this issue Dec 24, 2024 · 5 comments
Open

Fmt throws compile-time error on a different call reference #22307

xgallom opened this issue Dec 24, 2024 · 5 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@xgallom
Copy link

xgallom commented Dec 24, 2024

Zig Version

0.13.0

Steps to Reproduce and Observed Behavior

In my source code, in a deinit function, I deinitialize a gpa:

pub const Allocators = struct {
    core: std.mem.Allocator = undefined,
    gpa_state: GPA = undefined,
    arena_state: Arena = undefined,

    const Self = @This();

    pub fn init(self: *Self, core_allocator: std.mem.Allocator, memory_limit: usize) void {
        self.core = core_allocator;

        self.gpa_state = GPA{
            .backing_allocator = self.core,
            .requested_memory_limit = memory_limit,
        };

        self.arena_state = Arena.init(self.gpa_state.allocator());
    }

    pub fn deinit(self: *Self) std.heap.Check {
        self.arena_state.deinit();
        const result = self.gpa_state.deinit(); // <- error happens here
        self.* = Self{};
        return result;
    }
};

the code produces following compile-time error:

run
└─ run zeng
   └─ zig build-exe zeng Debug native 1 errors
/opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/fmt.zig:632:21: error: cannot format slice without a specifier (i.e. {s} or {any})
                    @compileError("cannot format slice without a specifier (i.e. {s} or {any})");
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    format__anon_18435: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/fmt.zig:185:23
    print__anon_18003: /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
    defaultLog__anon_18523: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/log.zig:158:15
    log__anon_18405: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/log.zig:125:22
    err__anon_17781: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/log.zig:175:16
    detectLeaksInBucket: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/heap/general_purpose_allocator.zig:391:36
    detectLeaks: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/heap/general_purpose_allocator.zig:415:29
    deinit: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/heap/general_purpose_allocator.zig:482:50
    deinit: src/zengine/allocators.zig:32:38
    deinit: src/zengine/allocators.zig:51:32
    main_impl: src/main.zig:29:21
    main: src/main.zig:24:12
    callMain: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/start.zig:524:32
    callMainWithArgs: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/start.zig:482:12
    main: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/start.zig:497:12

from the stack trace one can find out the line where the bug happens at general_purpose_allocator.zig:391:36:

log.err("memory address 0x{x} leaked: {}", .{
    @intFromPtr(addr), stack_trace,
});

my thoughts are that from the StackTrace struct, the slice

    instruction_addresses: []usize,

can be interpreted as a slice of numbers, or a character string. The fmt should contain a specifier.

Did not test if this occurs in a clean project, or if this occurs due to some outside code, but by nature of the bug it seems not.

Expected Behavior

The code compiles without an error. The error was not there before, but none of the changes point to anything having to do with this code being or not being compiled, or being altered in any way.

@xgallom xgallom added the bug Observed behavior contradicts documented or intended behavior label Dec 24, 2024
@xgallom
Copy link
Author

xgallom commented Dec 24, 2024

Update: every fmt in the application now throws a compile error, even the ones that have specifiers:

std.log.err("failed creating shader: {s}", .{sdl.SDL_GetError()});

this code now errors, even with the correct specifiers:

run
└─ run zeng
   └─ zig build-exe zeng Debug native 1 errors
/opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/fmt.zig:632:21: error: cannot format slice without a specifier (i.e. {s} or {any})
                    @compileError("cannot format slice without a specifier (i.e. {s} or {any})");
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    format__anon_18403: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/fmt.zig:185:23
    print__anon_17979: /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
    defaultLog__anon_17890: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/log.zig:158:15
    log__anon_17464: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/log.zig:125:22
    err__anon_16462: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/log.zig:175:16
    open: src/zengine/gfx/shader.zig:79:20
    init: src/zengine/gfx/renderer.zig:67:39
    main_impl: src/main.zig:37:36
    main: src/main.zig:24:12
    callMain: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/start.zig:524:32
    callMainWithArgs: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/start.zig:482:12
    main: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/start.zig:497:12

this suddenly happened and when I remove a log call, the error just moves to whichever other fmt call is in the compilation order.

I am sorry that there I provide no useful ways to reproduce this, I can setup a github repo with the current code.

@xgallom
Copy link
Author

xgallom commented Dec 24, 2024

Problem solved:

std.log.err("failed loading mesh from obj file: {}", .{@errorName(err)});

This code, without a specifier was incorrectly traced and showed up as an error in a different fmt call.
I am not sure if this is some bug in the compiler or fmt implementation.

@xgallom xgallom changed the title General purpose allocator fmt throws on format of stack trace Fmt throws compile-time error on a different call reference Dec 24, 2024
@ianprime0509
Copy link
Contributor

Given you're using 0.13.0, reference traces pointing to the wrong location sounds like #18900, which has since been fixed. Assuming there aren't too many incompatibilities with your current code due to breaking changes, it might be a good idea to see if Zig master fixes your issue.

@xgallom
Copy link
Author

xgallom commented Dec 25, 2024

Can't test master due to breaking changes, wanted to migrate on major release.
Is there some public packages good practice, such as develop packages for 0.13.0 or master or both?

@zxubian
Copy link

zxubian commented Dec 26, 2024

@xgallom that last question is probably best suited for e.g the community discord.
You might be interested in the Mach "nominated zig" versions.

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
Projects
None yet
Development

No branches or pull requests

3 participants