Skip to content

Compiler reports incorrect type error on the wrong line #10663

@Bodhizafa

Description

@Bodhizafa

Steps to reproduce:
Attempt to compile the following code, which tries to return an OPCMessage containing one too many levels of indirection for data:

const std = @import("std");
const ArrayList = std.ArrayList;

pub const io_mode = .evented;

const OPCMessage = struct {
    data: []const u8,
};

pub const Client = struct {
    stream: std.net.Stream,
    handle_frame: @Frame(handle),
    allocator: std.mem.Allocator,
    fn read_message(self: *Client) !OPCMessage {
        const data_len = 10;
        const data_buf = try self.allocator.alloc(u8, data_len); // The compiler reports the error here
        // Adding more code here does not affect the output
        return OPCMessage{
            .data = &data_buf, // The error is the & shouldn't be here
        };
    }
    fn handle(self: *Client) !void {
        defer {
            self.stream.close();
        }
        while (true) {
            _ = self.read_message() catch |err| {
                std.log.info("Client Lost: {any}\n", .{err});
                return;
            };
        }
    }
};

pub fn main() anyerror!void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    var heap = gpa.allocator();
    var server = std.net.StreamServer.init(.{ .reuse_address = true });
    defer server.deinit();
    var clients = std.AutoHashMap(*Client, void).init(heap);
    try server.listen(std.net.Address.parseIp("127.0.0.1", 42024) catch unreachable);
    std.log.info("Listening on {}\n", .{server.listen_address});
    while(true) {
        const client = try heap.create(Client);
        client.* = Client {
            .stream = (try server.accept()).stream,
            .handle_frame = async client.handle(),
            .allocator = heap
        };
        try clients.putNoClobber(client, {});
    }
}

Compiler output:

 zig build run
./src/main.zig:16:9: error: expected type '[]const u8', found '*const []u8'
        const data_buf = try self.allocator.alloc(u8, data_len); // The compiler reports the error here
        ^
./src/main.zig:12:5: note: while checking this field
    handle_frame: @Frame(handle),
    ^

Expected compiler output:
Something indicating that the error is in the return statement rather than the allocation.

System info:

$ zig version
0.10.0-dev.347+f763000dc
$ uname -a
Linux BOUZOUKI 5.10.60.1-microsoft-standard-WSL2 #1 SMP Wed Aug 25 23:20:18 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorcontributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.frontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions