Skip to content

peer type resolution for slice and single-pointer-to-array #4766

@andrewrk

Description

@andrewrk

Extracted from #4752.

Test case:

const std = @import("std");

fn foo(c: u8, char_buf: *[4]u8) []const u8 {
    return switch (c) {
        '\"' => "\\\"",
        '\'' => "\\'",
        '\\' => "\\\\",
        '\n' => "\\n",
        '\r' => "\\r",
        '\t' => "\\t",
        else => {
            if (c == 10) {
                return bar(char_buf);
            } else {
                return bar(char_buf);
            }
        },
    };
}

fn bar(buf: []u8) []u8 {
    buf[0..4].* = "abcd".*;
    return buf;
}

test "peer type resolution for slice and single-pointer-to-array" {
    var buf: [4]u8 = undefined;
    std.testing.expect(std.mem.eql(u8, foo('\\', &buf), "\\"));
    std.testing.expect(std.mem.eql(u8, foo(10, &buf), "abcd"));
}

Expected to pass. Instead:

./test2.zig:3:33: error: incompatible types: '[]u8' and '*const [2:0]u8'
fn foo(c: u8, char_buf: *[4]u8) []const u8 {
                                ^
./test2.zig:13:27: note: type '[]u8' here
                return bar(char_buf);
                          ^
./test2.zig:5:17: note: type '*const [2:0]u8' here
        '\"' => "\\\"",
                ^

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSolving this issue will likely involve adding new logic or components to the codebase.stage1The process of building from source via WebAssembly and the C backend.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions