Skip to content

Sema: support coercing ref to anonymous array init to many-pointer#18925

Merged
andrewrk merged 1 commit intoziglang:masterfrom
castholm:coerce-to-many-ptr
Apr 7, 2024
Merged

Sema: support coercing ref to anonymous array init to many-pointer#18925
andrewrk merged 1 commit intoziglang:masterfrom
castholm:coerce-to-many-ptr

Conversation

@castholm
Copy link
Contributor

Fixes #18924

Makes &.{ a, b, c } coerce to [*]const T/[*:x]const T:

const std = @import("std");
const expectEqual = std.testing.expectEqual;

const Foo = struct { bar: [*]const i32 };

test "ref to anon array init coerced to many-ptr" {
    const foo: Foo = .{ .bar = &.{ 1, -2 } };
    try expectEqual(1, foo.bar[0]);
    try expectEqual(-2, foo.bar[1]);
}

fn checkSentinelManyPtr(values: [*:0]const i32) !void {
    try expectEqual(3, values[0]);
    try expectEqual(-4, values[1]);
    try expectEqual(0, values[2]);
}

test "ref to anon array init coerced to sentinel many-ptr" {
    try checkSentinelManyPtr(&.{ 3, -4 });
}

@castholm castholm force-pushed the coerce-to-many-ptr branch from 1269c23 to 20e5e7f Compare April 7, 2024 13:07
@andrewrk andrewrk added the release notes This PR should be mentioned in the release notes. label Apr 7, 2024
@andrewrk andrewrk merged commit 278db0a into ziglang:master Apr 7, 2024
@castholm castholm deleted the coerce-to-many-ptr branch April 8, 2024 14:37
@andrewrk andrewrk added this to the 0.12.0 milestone Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release notes This PR should be mentioned in the release notes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: Allow coercing ref to anonymous array init to [*] and [*:x]

2 participants