Skip to content

Wrong line position when using non-usize type in allocation #16601

@cgbur

Description

@cgbur

Zig Version

0.11.0-dev.4296+7e25fb4a4

Steps to Reproduce and Observed Output

const std = @import("std");
const Allocator = std.mem.Allocator;

const Configuration = struct {
    dimension: usize,
};

const State = struct {
    const Self = @This();

    test_buffer: []f32,
    other_buffer: []f32,

    fn init(allocator: Allocator, config: *const Configuration) !Self {
        var xxX: i32 = 0;
        return Self{
            .test_buffer = try allocator.alloc(f32, xxX),
            .other_buffer = try allocator.alloc(f32, config.dimension),
        };
    }
};

pub fn main() !void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    const allocator = arena.allocator();

    const config = Configuration{
        .dimension = 100,
    };

    var state = try State.init(allocator, &config);
    _ = state;
}

zig run example.zig

❯ zig run example.zig
example.zig:2:19: error: expected type 'usize', found 'i32'
const Allocator = std.mem.Allocator;
                  ^~~
example.zig:2:19: note: unsigned 64-bit int cannot represent all 
possible signed 32-bit values
referenced by:
    main: example.zig:31:26
    callMain: /home/cgbur/zig/0.11.0-dev.4296+7e25fb4a4/files/lib
/std/start.zig:615:32
    remaining reference traces hidden; use '-freference-trace' to
 see all reference traces

In a project I am working on with fairly large structs it is pointing at a field in a completely different struct nearly a hundred lines above.

Expected Output

Something about

.test_buffer = try allocator.alloc(f32, xxX),
                                        ^^^^

highlighting the xxX.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorerror messageThis issue points out an error message that is unhelpful and should be improved.frontendTokenization, parsing, AstGen, Sema, and Liveness.regressionIt worked in a previous version of Zig, but stopped working.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions