-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Sema: implement @splat for arrays #21635
Conversation
src/Sema.zig
Outdated
@memset(elems[0..len], scalar_val.toIntern()); | ||
elems[len] = sentinel.toIntern(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these need to be @intCast
ed, prob just cast the const len =
src/Sema.zig
Outdated
const elems = try sema.arena.alloc(Air.Inst.Ref, len + @intFromBool(maybe_sentinel != null)); | ||
@memset(elems[0..len], scalar); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these two will also need to be int casted. ditto above.
8d150aa
to
e261adc
Compare
Resolves: ziglang#20433
Release notes please! |
Release NotesZig 0.14.0 expands the const Rgba = struct {
r: u8,
b: u8,
g: u8,
a: u8,
pub const black: Rgba = .{ .r = 0, .g = 0, .b = 0, .a = 255 };
};
var pixels: [width][height]Rgba = @splat(@splat(.black)); The operand may be comptime-known or runtime-known. In addition, this builtin can also be used to initialize sentinel-terminated arrays. const std = @import("std");
const assert = std.debug.assert;
const expect = std.testing.expect;
test "initialize sentinel-terminated array" {
// the sentinel does not need to match the value
const arr: [2:0]u8 = @splat(10);
try expect(arr[0] == 10);
try expect(arr[1] == 10);
try expect(arr[2] == 0);
}
test "initialize runtime array" {
var runtime_known: u8 = undefined;
runtime_known = 123;
// the operand can be runtime-known, giving a runtime-known array
const arr: [2]u8 = @splat(runtime_known);
try expect(arr[0] == 123);
try expect(arr[1] == 123);
}
test "initialize zero-length sentinel-terminated array" {
var runtime_known: u8 = undefined;
runtime_known = 123;
const arr: [0:10]u8 = @splat(runtime_known);
// the operand was runtime-known, but since the array length was zero, the result is comptime-known
comptime assert(arr[0] == 10);
} |
Resolves: #20433