authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-19 18:06:16-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-03-19 18:06:16-04:00
logdc04e97098010f590d109e6e70d4afe79cd8f01b
treebed11818fd80fe7b4557f4253c8d5562de773624
parent555a2c03286507ffe4bd3bea2154dbfb719ebef1
parent160367e0ddcb36b6957e603d869507b9d7542edc
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4752 from ziglang/slice-array

slicing with comptime start and end indexes results in pointer-to-array

41 files changed, 906 insertions(+), 431 deletions(-)

doc/langref.html.in+16-20
......@@ -2093,8 +2093,9 @@ var foo: u8 align(4) = 100;
20932093test "global variable alignment" {
20942094 assert(@TypeOf(&foo).alignment == 4);
20952095 assert(@TypeOf(&foo) == *align(4) u8);
2096 const slice = @as(*[1]u8, &foo)[0..];
2097 assert(@TypeOf(slice) == []align(4) u8);
2096 const as_pointer_to_array: *[1]u8 = &foo;
2097 const as_slice: []u8 = as_pointer_to_array;
2098 assert(@TypeOf(as_slice) == []align(4) u8);
20982099}
20992100
21002101fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; }
......@@ -2187,7 +2188,8 @@ test "basic slices" {
21872188 // a slice is that the array's length is part of the type and known at
21882189 // compile-time, whereas the slice's length is known at runtime.
21892190 // Both can be accessed with the `len` field.
2190 const slice = array[0..array.len];
2191 var known_at_runtime_zero: usize = 0;
2192 const slice = array[known_at_runtime_zero..array.len];
21912193 assert(&slice[0] == &array[0]);
21922194 assert(slice.len == array.len);
21932195
......@@ -2207,13 +2209,15 @@ test "basic slices" {
22072209 {#code_end#}
22082210 <p>This is one reason we prefer slices to pointers.</p>
22092211 {#code_begin|test|slices#}
2210const assert = @import("std").debug.assert;
2211const mem = @import("std").mem;
2212const fmt = @import("std").fmt;
2212const std = @import("std");
2213const assert = std.debug.assert;
2214const mem = std.mem;
2215const fmt = std.fmt;
22132216
22142217test "using slices for strings" {
2215 // Zig has no concept of strings. String literals are arrays of u8, and
2216 // in general the string type is []u8 (slice of u8).
2218 // Zig has no concept of strings. String literals are const pointers to
2219 // arrays of u8, and by convention parameters that are "strings" are
2220 // expected to be UTF-8 encoded slices of u8.
22172221 // Here we coerce [5]u8 to []const u8
22182222 const hello: []const u8 = "hello";
22192223 const world: []const u8 = "世界";
......@@ -2222,7 +2226,7 @@ test "using slices for strings" {
22222226 // You can use slice syntax on an array to convert an array into a slice.
22232227 const all_together_slice = all_together[0..];
22242228 // String concatenation example.
2225 const hello_world = try fmt.bufPrint(all_together_slice, "{} {}", .{hello, world});
2229 const hello_world = try fmt.bufPrint(all_together_slice, "{} {}", .{ hello, world });
22262230
22272231 // Generally, you can use UTF-8 and not worry about whether something is a
22282232 // string. If you don't need to deal with individual characters, no need
......@@ -2239,23 +2243,15 @@ test "slice pointer" {
22392243 slice[2] = 3;
22402244 assert(slice[2] == 3);
22412245 // The slice is mutable because we sliced a mutable pointer.
2242 assert(@TypeOf(slice) == []u8);
2246 // Furthermore, it is actually a pointer to an array, since the start
2247 // and end indexes were both comptime-known.
2248 assert(@TypeOf(slice) == *[5]u8);
22432249
22442250 // You can also slice a slice:
22452251 const slice2 = slice[2..3];
22462252 assert(slice2.len == 1);
22472253 assert(slice2[0] == 3);
22482254}
2249
2250test "slice widening" {
2251 // Zig supports slice widening and slice narrowing. Cast a slice of u8
2252 // to a slice of anything else, and Zig will perform the length conversion.
2253 const array align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13 };
2254 const slice = mem.bytesAsSlice(u32, array[0..]);
2255 assert(slice.len == 2);
2256 assert(slice[0] == 0x12121212);
2257 assert(slice[1] == 0x13131313);
2258}
22592255 {#code_end#}
22602256 {#see_also|Pointers|for|Arrays#}
22612257
lib/std/crypto/aes.zig+19-19
......@@ -15,10 +15,10 @@ fn rotw(w: u32) u32 {
1515
1616// Encrypt one block from src into dst, using the expanded key xk.
1717fn encryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {
18 var s0 = mem.readIntSliceBig(u32, src[0..4]);
19 var s1 = mem.readIntSliceBig(u32, src[4..8]);
20 var s2 = mem.readIntSliceBig(u32, src[8..12]);
21 var s3 = mem.readIntSliceBig(u32, src[12..16]);
18 var s0 = mem.readIntBig(u32, src[0..4]);
19 var s1 = mem.readIntBig(u32, src[4..8]);
20 var s2 = mem.readIntBig(u32, src[8..12]);
21 var s3 = mem.readIntBig(u32, src[12..16]);
2222
2323 // First round just XORs input with key.
2424 s0 ^= xk[0];
......@@ -58,18 +58,18 @@ fn encryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {
5858 s2 ^= xk[k + 2];
5959 s3 ^= xk[k + 3];
6060
61 mem.writeIntSliceBig(u32, dst[0..4], s0);
62 mem.writeIntSliceBig(u32, dst[4..8], s1);
63 mem.writeIntSliceBig(u32, dst[8..12], s2);
64 mem.writeIntSliceBig(u32, dst[12..16], s3);
61 mem.writeIntBig(u32, dst[0..4], s0);
62 mem.writeIntBig(u32, dst[4..8], s1);
63 mem.writeIntBig(u32, dst[8..12], s2);
64 mem.writeIntBig(u32, dst[12..16], s3);
6565}
6666
6767// Decrypt one block from src into dst, using the expanded key xk.
6868pub fn decryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {
69 var s0 = mem.readIntSliceBig(u32, src[0..4]);
70 var s1 = mem.readIntSliceBig(u32, src[4..8]);
71 var s2 = mem.readIntSliceBig(u32, src[8..12]);
72 var s3 = mem.readIntSliceBig(u32, src[12..16]);
69 var s0 = mem.readIntBig(u32, src[0..4]);
70 var s1 = mem.readIntBig(u32, src[4..8]);
71 var s2 = mem.readIntBig(u32, src[8..12]);
72 var s3 = mem.readIntBig(u32, src[12..16]);
7373
7474 // First round just XORs input with key.
7575 s0 ^= xk[0];
......@@ -109,10 +109,10 @@ pub fn decryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {
109109 s2 ^= xk[k + 2];
110110 s3 ^= xk[k + 3];
111111
112 mem.writeIntSliceBig(u32, dst[0..4], s0);
113 mem.writeIntSliceBig(u32, dst[4..8], s1);
114 mem.writeIntSliceBig(u32, dst[8..12], s2);
115 mem.writeIntSliceBig(u32, dst[12..16], s3);
112 mem.writeIntBig(u32, dst[0..4], s0);
113 mem.writeIntBig(u32, dst[4..8], s1);
114 mem.writeIntBig(u32, dst[8..12], s2);
115 mem.writeIntBig(u32, dst[12..16], s3);
116116}
117117
118118fn xorBytes(dst: []u8, a: []const u8, b: []const u8) usize {
......@@ -154,8 +154,8 @@ fn AES(comptime keysize: usize) type {
154154 var n: usize = 0;
155155 while (n < src.len) {
156156 ctx.encrypt(keystream[0..], ctrbuf[0..]);
157 var ctr_i = std.mem.readIntSliceBig(u128, ctrbuf[0..]);
158 std.mem.writeIntSliceBig(u128, ctrbuf[0..], ctr_i +% 1);
157 var ctr_i = std.mem.readIntBig(u128, ctrbuf[0..]);
158 std.mem.writeIntBig(u128, ctrbuf[0..], ctr_i +% 1);
159159
160160 n += xorBytes(dst[n..], src[n..], &keystream);
161161 }
......@@ -251,7 +251,7 @@ fn expandKey(key: []const u8, enc: []u32, dec: []u32) void {
251251 var i: usize = 0;
252252 var nk = key.len / 4;
253253 while (i < nk) : (i += 1) {
254 enc[i] = mem.readIntSliceBig(u32, key[4 * i .. 4 * i + 4]);
254 enc[i] = mem.readIntBig(u32, key[4 * i ..][0..4]);
255255 }
256256 while (i < enc.len) : (i += 1) {
257257 var t = enc[i - 1];
lib/std/crypto/blake2.zig+4-7
......@@ -123,8 +123,7 @@ fn Blake2s(comptime out_len: usize) type {
123123 const rr = d.h[0 .. out_len / 32];
124124
125125 for (rr) |s, j| {
126 // TODO https://github.com/ziglang/zig/issues/863
127 mem.writeIntSliceLittle(u32, out[4 * j .. 4 * j + 4], s);
126 mem.writeIntLittle(u32, out[4 * j ..][0..4], s);
128127 }
129128 }
130129
......@@ -135,8 +134,7 @@ fn Blake2s(comptime out_len: usize) type {
135134 var v: [16]u32 = undefined;
136135
137136 for (m) |*r, i| {
138 // TODO https://github.com/ziglang/zig/issues/863
139 r.* = mem.readIntSliceLittle(u32, b[4 * i .. 4 * i + 4]);
137 r.* = mem.readIntLittle(u32, b[4 * i ..][0..4]);
140138 }
141139
142140 var k: usize = 0;
......@@ -358,8 +356,7 @@ fn Blake2b(comptime out_len: usize) type {
358356 const rr = d.h[0 .. out_len / 64];
359357
360358 for (rr) |s, j| {
361 // TODO https://github.com/ziglang/zig/issues/863
362 mem.writeIntSliceLittle(u64, out[8 * j .. 8 * j + 8], s);
359 mem.writeIntLittle(u64, out[8 * j ..][0..8], s);
363360 }
364361 }
365362
......@@ -370,7 +367,7 @@ fn Blake2b(comptime out_len: usize) type {
370367 var v: [16]u64 = undefined;
371368
372369 for (m) |*r, i| {
373 r.* = mem.readIntSliceLittle(u64, b[8 * i .. 8 * i + 8]);
370 r.* = mem.readIntLittle(u64, b[8 * i ..][0..8]);
374371 }
375372
376373 var k: usize = 0;
lib/std/crypto/chacha20.zig+30-31
......@@ -61,8 +61,7 @@ fn salsa20_wordtobyte(out: []u8, input: [16]u32) void {
6161 }
6262
6363 for (x) |_, i| {
64 // TODO https://github.com/ziglang/zig/issues/863
65 mem.writeIntSliceLittle(u32, out[4 * i .. 4 * i + 4], x[i] +% input[i]);
64 mem.writeIntLittle(u32, out[4 * i ..][0..4], x[i] +% input[i]);
6665 }
6766}
6867
......@@ -73,10 +72,10 @@ fn chaCha20_internal(out: []u8, in: []const u8, key: [8]u32, counter: [4]u32) vo
7372
7473 const c = "expand 32-byte k";
7574 const constant_le = [_]u32{
76 mem.readIntSliceLittle(u32, c[0..4]),
77 mem.readIntSliceLittle(u32, c[4..8]),
78 mem.readIntSliceLittle(u32, c[8..12]),
79 mem.readIntSliceLittle(u32, c[12..16]),
75 mem.readIntLittle(u32, c[0..4]),
76 mem.readIntLittle(u32, c[4..8]),
77 mem.readIntLittle(u32, c[8..12]),
78 mem.readIntLittle(u32, c[12..16]),
8079 };
8180
8281 mem.copy(u32, ctx[0..], constant_le[0..4]);
......@@ -120,19 +119,19 @@ pub fn chaCha20IETF(out: []u8, in: []const u8, counter: u32, key: [32]u8, nonce:
120119 var k: [8]u32 = undefined;
121120 var c: [4]u32 = undefined;
122121
123 k[0] = mem.readIntSliceLittle(u32, key[0..4]);
124 k[1] = mem.readIntSliceLittle(u32, key[4..8]);
125 k[2] = mem.readIntSliceLittle(u32, key[8..12]);
126 k[3] = mem.readIntSliceLittle(u32, key[12..16]);
127 k[4] = mem.readIntSliceLittle(u32, key[16..20]);
128 k[5] = mem.readIntSliceLittle(u32, key[20..24]);
129 k[6] = mem.readIntSliceLittle(u32, key[24..28]);
130 k[7] = mem.readIntSliceLittle(u32, key[28..32]);
122 k[0] = mem.readIntLittle(u32, key[0..4]);
123 k[1] = mem.readIntLittle(u32, key[4..8]);
124 k[2] = mem.readIntLittle(u32, key[8..12]);
125 k[3] = mem.readIntLittle(u32, key[12..16]);
126 k[4] = mem.readIntLittle(u32, key[16..20]);
127 k[5] = mem.readIntLittle(u32, key[20..24]);
128 k[6] = mem.readIntLittle(u32, key[24..28]);
129 k[7] = mem.readIntLittle(u32, key[28..32]);
131130
132131 c[0] = counter;
133 c[1] = mem.readIntSliceLittle(u32, nonce[0..4]);
134 c[2] = mem.readIntSliceLittle(u32, nonce[4..8]);
135 c[3] = mem.readIntSliceLittle(u32, nonce[8..12]);
132 c[1] = mem.readIntLittle(u32, nonce[0..4]);
133 c[2] = mem.readIntLittle(u32, nonce[4..8]);
134 c[3] = mem.readIntLittle(u32, nonce[8..12]);
136135 chaCha20_internal(out, in, k, c);
137136}
138137
......@@ -147,19 +146,19 @@ pub fn chaCha20With64BitNonce(out: []u8, in: []const u8, counter: u64, key: [32]
147146 var k: [8]u32 = undefined;
148147 var c: [4]u32 = undefined;
149148
150 k[0] = mem.readIntSliceLittle(u32, key[0..4]);
151 k[1] = mem.readIntSliceLittle(u32, key[4..8]);
152 k[2] = mem.readIntSliceLittle(u32, key[8..12]);
153 k[3] = mem.readIntSliceLittle(u32, key[12..16]);
154 k[4] = mem.readIntSliceLittle(u32, key[16..20]);
155 k[5] = mem.readIntSliceLittle(u32, key[20..24]);
156 k[6] = mem.readIntSliceLittle(u32, key[24..28]);
157 k[7] = mem.readIntSliceLittle(u32, key[28..32]);
149 k[0] = mem.readIntLittle(u32, key[0..4]);
150 k[1] = mem.readIntLittle(u32, key[4..8]);
151 k[2] = mem.readIntLittle(u32, key[8..12]);
152 k[3] = mem.readIntLittle(u32, key[12..16]);
153 k[4] = mem.readIntLittle(u32, key[16..20]);
154 k[5] = mem.readIntLittle(u32, key[20..24]);
155 k[6] = mem.readIntLittle(u32, key[24..28]);
156 k[7] = mem.readIntLittle(u32, key[28..32]);
158157
159158 c[0] = @truncate(u32, counter);
160159 c[1] = @truncate(u32, counter >> 32);
161 c[2] = mem.readIntSliceLittle(u32, nonce[0..4]);
162 c[3] = mem.readIntSliceLittle(u32, nonce[4..8]);
160 c[2] = mem.readIntLittle(u32, nonce[0..4]);
161 c[3] = mem.readIntLittle(u32, nonce[4..8]);
163162
164163 const block_size = (1 << 6);
165164 // The full block size is greater than the address space on a 32bit machine
......@@ -463,8 +462,8 @@ pub fn chacha20poly1305Seal(dst: []u8, plaintext: []const u8, data: []const u8,
463462 mac.update(zeros[0..padding]);
464463 }
465464 var lens: [16]u8 = undefined;
466 mem.writeIntSliceLittle(u64, lens[0..8], data.len);
467 mem.writeIntSliceLittle(u64, lens[8..16], plaintext.len);
465 mem.writeIntLittle(u64, lens[0..8], data.len);
466 mem.writeIntLittle(u64, lens[8..16], plaintext.len);
468467 mac.update(lens[0..]);
469468 mac.final(dst[plaintext.len..]);
470469}
......@@ -500,8 +499,8 @@ pub fn chacha20poly1305Open(dst: []u8, msgAndTag: []const u8, data: []const u8,
500499 mac.update(zeros[0..padding]);
501500 }
502501 var lens: [16]u8 = undefined;
503 mem.writeIntSliceLittle(u64, lens[0..8], data.len);
504 mem.writeIntSliceLittle(u64, lens[8..16], ciphertext.len);
502 mem.writeIntLittle(u64, lens[0..8], data.len);
503 mem.writeIntLittle(u64, lens[8..16], ciphertext.len);
505504 mac.update(lens[0..]);
506505 var computedTag: [16]u8 = undefined;
507506 mac.final(computedTag[0..]);
lib/std/crypto/md5.zig+1-2
......@@ -112,8 +112,7 @@ pub const Md5 = struct {
112112 d.round(d.buf[0..]);
113113
114114 for (d.s) |s, j| {
115 // TODO https://github.com/ziglang/zig/issues/863
116 mem.writeIntSliceLittle(u32, out[4 * j .. 4 * j + 4], s);
115 mem.writeIntLittle(u32, out[4 * j ..][0..4], s);
117116 }
118117 }
119118
lib/std/crypto/poly1305.zig+14-15
......@@ -3,11 +3,11 @@
33// https://monocypher.org/
44
55const std = @import("../std.zig");
6const builtin = @import("builtin");
6const builtin = std.builtin;
77
88const Endian = builtin.Endian;
9const readIntSliceLittle = std.mem.readIntSliceLittle;
10const writeIntSliceLittle = std.mem.writeIntSliceLittle;
9const readIntLittle = std.mem.readIntLittle;
10const writeIntLittle = std.mem.writeIntLittle;
1111
1212pub const Poly1305 = struct {
1313 const Self = @This();
......@@ -59,19 +59,19 @@ pub const Poly1305 = struct {
5959 {
6060 var i: usize = 0;
6161 while (i < 1) : (i += 1) {
62 ctx.r[0] = readIntSliceLittle(u32, key[0..4]) & 0x0fffffff;
62 ctx.r[0] = readIntLittle(u32, key[0..4]) & 0x0fffffff;
6363 }
6464 }
6565 {
6666 var i: usize = 1;
6767 while (i < 4) : (i += 1) {
68 ctx.r[i] = readIntSliceLittle(u32, key[i * 4 .. i * 4 + 4]) & 0x0ffffffc;
68 ctx.r[i] = readIntLittle(u32, key[i * 4 ..][0..4]) & 0x0ffffffc;
6969 }
7070 }
7171 {
7272 var i: usize = 0;
7373 while (i < 4) : (i += 1) {
74 ctx.pad[i] = readIntSliceLittle(u32, key[i * 4 + 16 .. i * 4 + 16 + 4]);
74 ctx.pad[i] = readIntLittle(u32, key[i * 4 + 16 ..][0..4]);
7575 }
7676 }
7777
......@@ -168,10 +168,10 @@ pub const Poly1305 = struct {
168168 const nb_blocks = nmsg.len >> 4;
169169 var i: usize = 0;
170170 while (i < nb_blocks) : (i += 1) {
171 ctx.c[0] = readIntSliceLittle(u32, nmsg[0..4]);
172 ctx.c[1] = readIntSliceLittle(u32, nmsg[4..8]);
173 ctx.c[2] = readIntSliceLittle(u32, nmsg[8..12]);
174 ctx.c[3] = readIntSliceLittle(u32, nmsg[12..16]);
171 ctx.c[0] = readIntLittle(u32, nmsg[0..4]);
172 ctx.c[1] = readIntLittle(u32, nmsg[4..8]);
173 ctx.c[2] = readIntLittle(u32, nmsg[8..12]);
174 ctx.c[3] = readIntLittle(u32, nmsg[12..16]);
175175 polyBlock(ctx);
176176 nmsg = nmsg[16..];
177177 }
......@@ -210,11 +210,10 @@ pub const Poly1305 = struct {
210210 const uu2 = (uu1 >> 32) + ctx.h[2] + ctx.pad[2]; // <= 2_00000000
211211 const uu3 = (uu2 >> 32) + ctx.h[3] + ctx.pad[3]; // <= 2_00000000
212212
213 // TODO https://github.com/ziglang/zig/issues/863
214 writeIntSliceLittle(u32, out[0..], @truncate(u32, uu0));
215 writeIntSliceLittle(u32, out[4..], @truncate(u32, uu1));
216 writeIntSliceLittle(u32, out[8..], @truncate(u32, uu2));
217 writeIntSliceLittle(u32, out[12..], @truncate(u32, uu3));
213 writeIntLittle(u32, out[0..4], @truncate(u32, uu0));
214 writeIntLittle(u32, out[4..8], @truncate(u32, uu1));
215 writeIntLittle(u32, out[8..12], @truncate(u32, uu2));
216 writeIntLittle(u32, out[12..16], @truncate(u32, uu3));
218217
219218 ctx.secureZero();
220219 }
lib/std/crypto/sha1.zig+1-2
......@@ -109,8 +109,7 @@ pub const Sha1 = struct {
109109 d.round(d.buf[0..]);
110110
111111 for (d.s) |s, j| {
112 // TODO https://github.com/ziglang/zig/issues/863
113 mem.writeIntSliceBig(u32, out[4 * j .. 4 * j + 4], s);
112 mem.writeIntBig(u32, out[4 * j ..][0..4], s);
114113 }
115114 }
116115
lib/std/crypto/sha2.zig+2-4
......@@ -167,8 +167,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {
167167 const rr = d.s[0 .. params.out_len / 32];
168168
169169 for (rr) |s, j| {
170 // TODO https://github.com/ziglang/zig/issues/863
171 mem.writeIntSliceBig(u32, out[4 * j .. 4 * j + 4], s);
170 mem.writeIntBig(u32, out[4 * j ..][0..4], s);
172171 }
173172 }
174173
......@@ -509,8 +508,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {
509508 const rr = d.s[0 .. params.out_len / 64];
510509
511510 for (rr) |s, j| {
512 // TODO https://github.com/ziglang/zig/issues/863
513 mem.writeIntSliceBig(u64, out[8 * j .. 8 * j + 8], s);
511 mem.writeIntBig(u64, out[8 * j ..][0..8], s);
514512 }
515513 }
516514
lib/std/crypto/sha3.zig+2-3
......@@ -120,7 +120,7 @@ fn keccak_f(comptime F: usize, d: []u8) void {
120120 var c = [_]u64{0} ** 5;
121121
122122 for (s) |*r, i| {
123 r.* = mem.readIntSliceLittle(u64, d[8 * i .. 8 * i + 8]);
123 r.* = mem.readIntLittle(u64, d[8 * i ..][0..8]);
124124 }
125125
126126 comptime var x: usize = 0;
......@@ -167,8 +167,7 @@ fn keccak_f(comptime F: usize, d: []u8) void {
167167 }
168168
169169 for (s) |r, i| {
170 // TODO https://github.com/ziglang/zig/issues/863
171 mem.writeIntSliceLittle(u64, d[8 * i .. 8 * i + 8], r);
170 mem.writeIntLittle(u64, d[8 * i ..][0..8], r);
172171 }
173172}
174173
lib/std/crypto/x25519.zig+20-21
......@@ -7,8 +7,8 @@ const builtin = @import("builtin");
77const fmt = std.fmt;
88
99const Endian = builtin.Endian;
10const readIntSliceLittle = std.mem.readIntSliceLittle;
11const writeIntSliceLittle = std.mem.writeIntSliceLittle;
10const readIntLittle = std.mem.readIntLittle;
11const writeIntLittle = std.mem.writeIntLittle;
1212
1313// Based on Supercop's ref10 implementation.
1414pub const X25519 = struct {
......@@ -255,16 +255,16 @@ const Fe = struct {
255255
256256 var t: [10]i64 = undefined;
257257
258 t[0] = readIntSliceLittle(u32, s[0..4]);
259 t[1] = @as(u32, readIntSliceLittle(u24, s[4..7])) << 6;
260 t[2] = @as(u32, readIntSliceLittle(u24, s[7..10])) << 5;
261 t[3] = @as(u32, readIntSliceLittle(u24, s[10..13])) << 3;
262 t[4] = @as(u32, readIntSliceLittle(u24, s[13..16])) << 2;
263 t[5] = readIntSliceLittle(u32, s[16..20]);
264 t[6] = @as(u32, readIntSliceLittle(u24, s[20..23])) << 7;
265 t[7] = @as(u32, readIntSliceLittle(u24, s[23..26])) << 5;
266 t[8] = @as(u32, readIntSliceLittle(u24, s[26..29])) << 4;
267 t[9] = (@as(u32, readIntSliceLittle(u24, s[29..32])) & 0x7fffff) << 2;
258 t[0] = readIntLittle(u32, s[0..4]);
259 t[1] = @as(u32, readIntLittle(u24, s[4..7])) << 6;
260 t[2] = @as(u32, readIntLittle(u24, s[7..10])) << 5;
261 t[3] = @as(u32, readIntLittle(u24, s[10..13])) << 3;
262 t[4] = @as(u32, readIntLittle(u24, s[13..16])) << 2;
263 t[5] = readIntLittle(u32, s[16..20]);
264 t[6] = @as(u32, readIntLittle(u24, s[20..23])) << 7;
265 t[7] = @as(u32, readIntLittle(u24, s[23..26])) << 5;
266 t[8] = @as(u32, readIntLittle(u24, s[26..29])) << 4;
267 t[9] = (@as(u32, readIntLittle(u24, s[29..32])) & 0x7fffff) << 2;
268268
269269 carry1(h, t[0..]);
270270 }
......@@ -544,15 +544,14 @@ const Fe = struct {
544544 ut[i] = @bitCast(u32, @intCast(i32, t[i]));
545545 }
546546
547 // TODO https://github.com/ziglang/zig/issues/863
548 writeIntSliceLittle(u32, s[0..4], (ut[0] >> 0) | (ut[1] << 26));
549 writeIntSliceLittle(u32, s[4..8], (ut[1] >> 6) | (ut[2] << 19));
550 writeIntSliceLittle(u32, s[8..12], (ut[2] >> 13) | (ut[3] << 13));
551 writeIntSliceLittle(u32, s[12..16], (ut[3] >> 19) | (ut[4] << 6));
552 writeIntSliceLittle(u32, s[16..20], (ut[5] >> 0) | (ut[6] << 25));
553 writeIntSliceLittle(u32, s[20..24], (ut[6] >> 7) | (ut[7] << 19));
554 writeIntSliceLittle(u32, s[24..28], (ut[7] >> 13) | (ut[8] << 12));
555 writeIntSliceLittle(u32, s[28..], (ut[8] >> 20) | (ut[9] << 6));
547 writeIntLittle(u32, s[0..4], (ut[0] >> 0) | (ut[1] << 26));
548 writeIntLittle(u32, s[4..8], (ut[1] >> 6) | (ut[2] << 19));
549 writeIntLittle(u32, s[8..12], (ut[2] >> 13) | (ut[3] << 13));
550 writeIntLittle(u32, s[12..16], (ut[3] >> 19) | (ut[4] << 6));
551 writeIntLittle(u32, s[16..20], (ut[5] >> 0) | (ut[6] << 25));
552 writeIntLittle(u32, s[20..24], (ut[6] >> 7) | (ut[7] << 19));
553 writeIntLittle(u32, s[24..28], (ut[7] >> 13) | (ut[8] << 12));
554 writeIntLittle(u32, s[28..32], (ut[8] >> 20) | (ut[9] << 6));
556555
557556 std.mem.secureZero(i64, t[0..]);
558557 }
lib/std/fmt.zig+2-1
......@@ -1223,7 +1223,8 @@ test "slice" {
12231223 try testFmt("slice: abc\n", "slice: {}\n", .{value});
12241224 }
12251225 {
1226 const value = @intToPtr([*]align(1) const []const u8, 0xdeadbeef)[0..0];
1226 var runtime_zero: usize = 0;
1227 const value = @intToPtr([*]align(1) const []const u8, 0xdeadbeef)[runtime_zero..runtime_zero];
12271228 try testFmt("slice: []const u8@deadbeef\n", "slice: {}\n", .{value});
12281229 }
12291230
lib/std/fs.zig+1-1
......@@ -341,7 +341,7 @@ pub const Dir = struct {
341341 if (self.index >= self.end_index) {
342342 const rc = os.system.getdirentries(
343343 self.dir.fd,
344 self.buf[0..].ptr,
344 &self.buf,
345345 self.buf.len,
346346 &self.seek,
347347 );
lib/std/hash/auto_hash.zig+8-4
......@@ -40,7 +40,9 @@ pub fn hashPointer(hasher: var, key: var, comptime strat: HashStrategy) void {
4040 .DeepRecursive => hashArray(hasher, key, .DeepRecursive),
4141 },
4242
43 .Many, .C, => switch (strat) {
43 .Many,
44 .C,
45 => switch (strat) {
4446 .Shallow => hash(hasher, @ptrToInt(key), .Shallow),
4547 else => @compileError(
4648 \\ unknown-length pointers and C pointers cannot be hashed deeply.
......@@ -236,9 +238,11 @@ test "hash slice shallow" {
236238 defer std.testing.allocator.destroy(array1);
237239 array1.* = [_]u32{ 1, 2, 3, 4, 5, 6 };
238240 const array2 = [_]u32{ 1, 2, 3, 4, 5, 6 };
239 const a = array1[0..];
240 const b = array2[0..];
241 const c = array1[0..3];
241 // TODO audit deep/shallow - maybe it has the wrong behavior with respect to array pointers and slices
242 var runtime_zero: usize = 0;
243 const a = array1[runtime_zero..];
244 const b = array2[runtime_zero..];
245 const c = array1[runtime_zero..3];
242246 testing.expect(testHashShallow(a) == testHashShallow(a));
243247 testing.expect(testHashShallow(a) != testHashShallow(array1));
244248 testing.expect(testHashShallow(a) != testHashShallow(b));
lib/std/hash/siphash.zig+3-3
......@@ -39,8 +39,8 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
3939 pub fn init(key: []const u8) Self {
4040 assert(key.len >= 16);
4141
42 const k0 = mem.readIntSliceLittle(u64, key[0..8]);
43 const k1 = mem.readIntSliceLittle(u64, key[8..16]);
42 const k0 = mem.readIntLittle(u64, key[0..8]);
43 const k1 = mem.readIntLittle(u64, key[8..16]);
4444
4545 var d = Self{
4646 .v0 = k0 ^ 0x736f6d6570736575,
......@@ -111,7 +111,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
111111 fn round(self: *Self, b: []const u8) void {
112112 assert(b.len == 8);
113113
114 const m = mem.readIntSliceLittle(u64, b[0..]);
114 const m = mem.readIntLittle(u64, b[0..8]);
115115 self.v3 ^= m;
116116
117117 // TODO this is a workaround, should be able to supply the value without a separate variable
lib/std/hash/wyhash.zig+1-1
......@@ -11,7 +11,7 @@ const primes = [_]u64{
1111
1212fn read_bytes(comptime bytes: u8, data: []const u8) u64 {
1313 const T = std.meta.IntType(false, 8 * bytes);
14 return mem.readIntSliceLittle(T, data[0..bytes]);
14 return mem.readIntLittle(T, data[0..bytes]);
1515}
1616
1717fn read_8bytes_swapped(data: []const u8) u64 {
lib/std/json.zig+16-7
......@@ -2249,11 +2249,16 @@ pub const StringifyOptions = struct {
22492249 // TODO: allow picking if []u8 is string or array?
22502250};
22512251
2252pub const StringifyError = error{
2253 TooMuchData,
2254 DifferentData,
2255};
2256
22522257pub fn stringify(
22532258 value: var,
22542259 options: StringifyOptions,
22552260 out_stream: var,
2256) !void {
2261) StringifyError!void {
22572262 const T = @TypeOf(value);
22582263 switch (@typeInfo(T)) {
22592264 .Float, .ComptimeFloat => {
......@@ -2320,9 +2325,15 @@ pub fn stringify(
23202325 return;
23212326 },
23222327 .Pointer => |ptr_info| switch (ptr_info.size) {
2323 .One => {
2324 // TODO: avoid loops?
2325 return try stringify(value.*, options, out_stream);
2328 .One => switch (@typeInfo(ptr_info.child)) {
2329 .Array => {
2330 const Slice = []const std.meta.Elem(ptr_info.child);
2331 return stringify(@as(Slice, value), options, out_stream);
2332 },
2333 else => {
2334 // TODO: avoid loops?
2335 return stringify(value.*, options, out_stream);
2336 },
23262337 },
23272338 // TODO: .Many when there is a sentinel (waiting for https://github.com/ziglang/zig/pull/3972)
23282339 .Slice => {
......@@ -2381,9 +2392,7 @@ pub fn stringify(
23812392 },
23822393 else => @compileError("Unable to stringify type '" ++ @typeName(T) ++ "'"),
23832394 },
2384 .Array => |info| {
2385 return try stringify(value[0..], options, out_stream);
2386 },
2395 .Array => return stringify(&value, options, out_stream),
23872396 else => @compileError("Unable to stringify type '" ++ @typeName(T) ++ "'"),
23882397 }
23892398 unreachable;
lib/std/mem.zig+46-67
......@@ -560,7 +560,7 @@ pub fn span(ptr: var) Span(@TypeOf(ptr)) {
560560
561561test "span" {
562562 var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 };
563 const ptr = array[0..2 :3].ptr;
563 const ptr = @as([*:3]u16, array[0..2 :3]);
564564 testing.expect(eql(u16, span(ptr), &[_]u16{ 1, 2 }));
565565 testing.expect(eql(u16, span(&array), &[_]u16{ 1, 2, 3, 4, 5 }));
566566}
......@@ -602,7 +602,7 @@ test "len" {
602602 testing.expect(len(&array) == 5);
603603 testing.expect(len(array[0..3]) == 3);
604604 array[2] = 0;
605 const ptr = array[0..2 :0].ptr;
605 const ptr = @as([*:0]u16, array[0..2 :0]);
606606 testing.expect(len(ptr) == 2);
607607 }
608608 {
......@@ -824,8 +824,7 @@ pub const readIntBig = switch (builtin.endian) {
824824pub fn readIntSliceNative(comptime T: type, bytes: []const u8) T {
825825 const n = @divExact(T.bit_count, 8);
826826 assert(bytes.len >= n);
827 // TODO https://github.com/ziglang/zig/issues/863
828 return readIntNative(T, @ptrCast(*const [n]u8, bytes.ptr));
827 return readIntNative(T, bytes[0..n]);
829828}
830829
831830/// Asserts that bytes.len >= T.bit_count / 8. Reads the integer starting from index 0
......@@ -863,8 +862,7 @@ pub fn readInt(comptime T: type, bytes: *const [@divExact(T.bit_count, 8)]u8, en
863862pub fn readIntSlice(comptime T: type, bytes: []const u8, endian: builtin.Endian) T {
864863 const n = @divExact(T.bit_count, 8);
865864 assert(bytes.len >= n);
866 // TODO https://github.com/ziglang/zig/issues/863
867 return readInt(T, @ptrCast(*const [n]u8, bytes.ptr), endian);
865 return readInt(T, bytes[0..n], endian);
868866}
869867
870868test "comptime read/write int" {
......@@ -1586,24 +1584,24 @@ pub fn nativeToBig(comptime T: type, x: T) T {
15861584}
15871585
15881586fn AsBytesReturnType(comptime P: type) type {
1589 if (comptime !trait.isSingleItemPtr(P))
1587 if (!trait.isSingleItemPtr(P))
15901588 @compileError("expected single item pointer, passed " ++ @typeName(P));
15911589
1592 const size = @as(usize, @sizeOf(meta.Child(P)));
1593 const alignment = comptime meta.alignment(P);
1590 const size = @sizeOf(meta.Child(P));
1591 const alignment = meta.alignment(P);
15941592
15951593 if (alignment == 0) {
1596 if (comptime trait.isConstPtr(P))
1594 if (trait.isConstPtr(P))
15971595 return *const [size]u8;
15981596 return *[size]u8;
15991597 }
16001598
1601 if (comptime trait.isConstPtr(P))
1599 if (trait.isConstPtr(P))
16021600 return *align(alignment) const [size]u8;
16031601 return *align(alignment) [size]u8;
16041602}
16051603
1606///Given a pointer to a single item, returns a slice of the underlying bytes, preserving constness.
1604/// Given a pointer to a single item, returns a slice of the underlying bytes, preserving constness.
16071605pub fn asBytes(ptr: var) AsBytesReturnType(@TypeOf(ptr)) {
16081606 const P = @TypeOf(ptr);
16091607 return @ptrCast(AsBytesReturnType(P), ptr);
......@@ -1750,34 +1748,50 @@ fn BytesAsSliceReturnType(comptime T: type, comptime bytesType: type) type {
17501748}
17511749
17521750pub fn bytesAsSlice(comptime T: type, bytes: var) BytesAsSliceReturnType(T, @TypeOf(bytes)) {
1753 const bytesSlice = if (comptime trait.isPtrTo(.Array)(@TypeOf(bytes))) bytes[0..] else bytes;
1754
17551751 // let's not give an undefined pointer to @ptrCast
17561752 // it may be equal to zero and fail a null check
1757 if (bytesSlice.len == 0) {
1753 if (bytes.len == 0) {
17581754 return &[0]T{};
17591755 }
17601756
1761 const bytesType = @TypeOf(bytesSlice);
1762 const alignment = comptime meta.alignment(bytesType);
1757 const Bytes = @TypeOf(bytes);
1758 const alignment = comptime meta.alignment(Bytes);
17631759
1764 const castTarget = if (comptime trait.isConstPtr(bytesType)) [*]align(alignment) const T else [*]align(alignment) T;
1760 const cast_target = if (comptime trait.isConstPtr(Bytes)) [*]align(alignment) const T else [*]align(alignment) T;
17651761
1766 return @ptrCast(castTarget, bytesSlice.ptr)[0..@divExact(bytes.len, @sizeOf(T))];
1762 return @ptrCast(cast_target, bytes)[0..@divExact(bytes.len, @sizeOf(T))];
17671763}
17681764
17691765test "bytesAsSlice" {
1770 const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF };
1771 const slice = bytesAsSlice(u16, bytes[0..]);
1772 testing.expect(slice.len == 2);
1773 testing.expect(bigToNative(u16, slice[0]) == 0xDEAD);
1774 testing.expect(bigToNative(u16, slice[1]) == 0xBEEF);
1766 {
1767 const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF };
1768 const slice = bytesAsSlice(u16, bytes[0..]);
1769 testing.expect(slice.len == 2);
1770 testing.expect(bigToNative(u16, slice[0]) == 0xDEAD);
1771 testing.expect(bigToNative(u16, slice[1]) == 0xBEEF);
1772 }
1773 {
1774 const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF };
1775 var runtime_zero: usize = 0;
1776 const slice = bytesAsSlice(u16, bytes[runtime_zero..]);
1777 testing.expect(slice.len == 2);
1778 testing.expect(bigToNative(u16, slice[0]) == 0xDEAD);
1779 testing.expect(bigToNative(u16, slice[1]) == 0xBEEF);
1780 }
17751781}
17761782
17771783test "bytesAsSlice keeps pointer alignment" {
1778 var bytes = [_]u8{ 0x01, 0x02, 0x03, 0x04 };
1779 const numbers = bytesAsSlice(u32, bytes[0..]);
1780 comptime testing.expect(@TypeOf(numbers) == []align(@alignOf(@TypeOf(bytes))) u32);
1784 {
1785 var bytes = [_]u8{ 0x01, 0x02, 0x03, 0x04 };
1786 const numbers = bytesAsSlice(u32, bytes[0..]);
1787 comptime testing.expect(@TypeOf(numbers) == []align(@alignOf(@TypeOf(bytes))) u32);
1788 }
1789 {
1790 var bytes = [_]u8{ 0x01, 0x02, 0x03, 0x04 };
1791 var runtime_zero: usize = 0;
1792 const numbers = bytesAsSlice(u32, bytes[runtime_zero..]);
1793 comptime testing.expect(@TypeOf(numbers) == []align(@alignOf(@TypeOf(bytes))) u32);
1794 }
17811795}
17821796
17831797test "bytesAsSlice on a packed struct" {
......@@ -1813,21 +1827,19 @@ fn SliceAsBytesReturnType(comptime sliceType: type) type {
18131827}
18141828
18151829pub fn sliceAsBytes(slice: var) SliceAsBytesReturnType(@TypeOf(slice)) {
1816 const actualSlice = if (comptime trait.isPtrTo(.Array)(@TypeOf(slice))) slice[0..] else slice;
1817 const actualSliceTypeInfo = @typeInfo(@TypeOf(actualSlice)).Pointer;
1830 const Slice = @TypeOf(slice);
18181831
18191832 // let's not give an undefined pointer to @ptrCast
18201833 // it may be equal to zero and fail a null check
1821 if (actualSlice.len == 0 and actualSliceTypeInfo.sentinel == null) {
1834 if (slice.len == 0 and comptime meta.sentinel(Slice) == null) {
18221835 return &[0]u8{};
18231836 }
18241837
1825 const sliceType = @TypeOf(actualSlice);
1826 const alignment = comptime meta.alignment(sliceType);
1838 const alignment = comptime meta.alignment(Slice);
18271839
1828 const castTarget = if (comptime trait.isConstPtr(sliceType)) [*]align(alignment) const u8 else [*]align(alignment) u8;
1840 const cast_target = if (comptime trait.isConstPtr(Slice)) [*]align(alignment) const u8 else [*]align(alignment) u8;
18291841
1830 return @ptrCast(castTarget, actualSlice.ptr)[0 .. actualSlice.len * @sizeOf(comptime meta.Child(sliceType))];
1842 return @ptrCast(cast_target, slice)[0 .. slice.len * @sizeOf(meta.Elem(Slice))];
18311843}
18321844
18331845test "sliceAsBytes" {
......@@ -1897,39 +1909,6 @@ test "sliceAsBytes and bytesAsSlice back" {
18971909 testing.expect(bytes[11] == math.maxInt(u8));
18981910}
18991911
1900fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type {
1901 if (trait.isConstPtr(T))
1902 return *const [length]meta.Child(meta.Child(T));
1903 return *[length]meta.Child(meta.Child(T));
1904}
1905
1906/// Given a pointer to an array, returns a pointer to a portion of that array, preserving constness.
1907/// TODO this will be obsoleted by https://github.com/ziglang/zig/issues/863
1908pub fn subArrayPtr(
1909 ptr: var,
1910 comptime start: usize,
1911 comptime length: usize,
1912) SubArrayPtrReturnType(@TypeOf(ptr), length) {
1913 assert(start + length <= ptr.*.len);
1914
1915 const ReturnType = SubArrayPtrReturnType(@TypeOf(ptr), length);
1916 const T = meta.Child(meta.Child(@TypeOf(ptr)));
1917 return @ptrCast(ReturnType, &ptr[start]);
1918}
1919
1920test "subArrayPtr" {
1921 const a1: [6]u8 = "abcdef".*;
1922 const sub1 = subArrayPtr(&a1, 2, 3);
1923 testing.expect(eql(u8, sub1, "cde"));
1924
1925 var a2: [6]u8 = "abcdef".*;
1926 var sub2 = subArrayPtr(&a2, 2, 3);
1927
1928 testing.expect(eql(u8, sub2, "cde"));
1929 sub2[1] = 'X';
1930 testing.expect(eql(u8, &a2, "abcXef"));
1931}
1932
19331912/// Round an address up to the nearest aligned address
19341913/// The alignment must be a power of 2 and greater than 0.
19351914pub fn alignForward(addr: usize, alignment: usize) usize {
lib/std/meta.zig+50-15
......@@ -104,7 +104,7 @@ pub fn Child(comptime T: type) type {
104104 .Array => |info| info.child,
105105 .Pointer => |info| info.child,
106106 .Optional => |info| info.child,
107 else => @compileError("Expected pointer, optional, or array type, " ++ "found '" ++ @typeName(T) ++ "'"),
107 else => @compileError("Expected pointer, optional, or array type, found '" ++ @typeName(T) ++ "'"),
108108 };
109109}
110110
......@@ -115,30 +115,65 @@ test "std.meta.Child" {
115115 testing.expect(Child(?u8) == u8);
116116}
117117
118/// Given a type with a sentinel e.g. `[:0]u8`, returns the sentinel
119pub fn Sentinel(comptime T: type) Child(T) {
120 // comptime asserts that ptr has a sentinel
118/// Given a "memory span" type, returns the "element type".
119pub fn Elem(comptime T: type) type {
121120 switch (@typeInfo(T)) {
122 .Array => |arrayInfo| {
123 return comptime arrayInfo.sentinel.?;
121 .Array => |info| return info.child,
122 .Pointer => |info| switch (info.size) {
123 .One => switch (@typeInfo(info.child)) {
124 .Array => |array_info| return array_info.child,
125 else => {},
126 },
127 .Many, .C, .Slice => return info.child,
124128 },
125 .Pointer => |ptrInfo| {
126 switch (ptrInfo.size) {
127 .Many, .Slice => {
128 return comptime ptrInfo.sentinel.?;
129 else => {},
130 }
131 @compileError("Expected pointer, slice, or array, found '" ++ @typeName(T) ++ "'");
132}
133
134test "std.meta.Elem" {
135 testing.expect(Elem([1]u8) == u8);
136 testing.expect(Elem([*]u8) == u8);
137 testing.expect(Elem([]u8) == u8);
138 testing.expect(Elem(*[10]u8) == u8);
139}
140
141/// Given a type which can have a sentinel e.g. `[:0]u8`, returns the sentinel value,
142/// or `null` if there is not one.
143/// Types which cannot possibly have a sentinel will be a compile error.
144pub fn sentinel(comptime T: type) ?Elem(T) {
145 switch (@typeInfo(T)) {
146 .Array => |info| return info.sentinel,
147 .Pointer => |info| {
148 switch (info.size) {
149 .Many, .Slice => return info.sentinel,
150 .One => switch (@typeInfo(info.child)) {
151 .Array => |array_info| return array_info.sentinel,
152 else => {},
129153 },
130154 else => {},
131155 }
132156 },
133157 else => {},
134158 }
135 @compileError("not a sentinel type, found '" ++ @typeName(T) ++ "'");
159 @compileError("type '" ++ @typeName(T) ++ "' cannot possibly have a sentinel");
136160}
137161
138test "std.meta.Sentinel" {
139 testing.expectEqual(@as(u8, 0), Sentinel([:0]u8));
140 testing.expectEqual(@as(u8, 0), Sentinel([*:0]u8));
141 testing.expectEqual(@as(u8, 0), Sentinel([5:0]u8));
162test "std.meta.sentinel" {
163 testSentinel();
164 comptime testSentinel();
165}
166
167fn testSentinel() void {
168 testing.expectEqual(@as(u8, 0), sentinel([:0]u8).?);
169 testing.expectEqual(@as(u8, 0), sentinel([*:0]u8).?);
170 testing.expectEqual(@as(u8, 0), sentinel([5:0]u8).?);
171 testing.expectEqual(@as(u8, 0), sentinel(*const [5:0]u8).?);
172
173 testing.expect(sentinel([]u8) == null);
174 testing.expect(sentinel([*]u8) == null);
175 testing.expect(sentinel([5]u8) == null);
176 testing.expect(sentinel(*const [5]u8) == null);
142177}
143178
144179pub fn containerLayout(comptime T: type) TypeInfo.ContainerLayout {
lib/std/meta/trait.zig+7-5
......@@ -230,9 +230,10 @@ pub fn isSingleItemPtr(comptime T: type) bool {
230230
231231test "std.meta.trait.isSingleItemPtr" {
232232 const array = [_]u8{0} ** 10;
233 testing.expect(isSingleItemPtr(@TypeOf(&array[0])));
234 testing.expect(!isSingleItemPtr(@TypeOf(array)));
235 testing.expect(!isSingleItemPtr(@TypeOf(array[0..1])));
233 comptime testing.expect(isSingleItemPtr(@TypeOf(&array[0])));
234 comptime testing.expect(!isSingleItemPtr(@TypeOf(array)));
235 var runtime_zero: usize = 0;
236 testing.expect(!isSingleItemPtr(@TypeOf(array[runtime_zero..1])));
236237}
237238
238239pub fn isManyItemPtr(comptime T: type) bool {
......@@ -259,7 +260,8 @@ pub fn isSlice(comptime T: type) bool {
259260
260261test "std.meta.trait.isSlice" {
261262 const array = [_]u8{0} ** 10;
262 testing.expect(isSlice(@TypeOf(array[0..])));
263 var runtime_zero: usize = 0;
264 testing.expect(isSlice(@TypeOf(array[runtime_zero..])));
263265 testing.expect(!isSlice(@TypeOf(array)));
264266 testing.expect(!isSlice(@TypeOf(&array[0])));
265267}
......@@ -276,7 +278,7 @@ pub fn isIndexable(comptime T: type) bool {
276278
277279test "std.meta.trait.isIndexable" {
278280 const array = [_]u8{0} ** 10;
279 const slice = array[0..];
281 const slice = @as([]const u8, &array);
280282
281283 testing.expect(isIndexable(@TypeOf(array)));
282284 testing.expect(isIndexable(@TypeOf(&array)));
lib/std/net.zig+7-5
......@@ -612,8 +612,7 @@ fn linuxLookupName(
612612 } else {
613613 mem.copy(u8, &sa6.addr, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff");
614614 mem.copy(u8, &da6.addr, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff");
615 // TODO https://github.com/ziglang/zig/issues/863
616 mem.writeIntNative(u32, @ptrCast(*[4]u8, da6.addr[12..].ptr), addr.addr.in.addr);
615 mem.writeIntNative(u32, da6.addr[12..], addr.addr.in.addr);
617616 da4.addr = addr.addr.in.addr;
618617 da = @ptrCast(*os.sockaddr, &da4);
619618 dalen = @sizeOf(os.sockaddr_in);
......@@ -821,7 +820,7 @@ fn linuxLookupNameFromHosts(
821820 // Skip to the delimiter in the stream, to fix parsing
822821 try stream.skipUntilDelimiterOrEof('\n');
823822 // Use the truncated line. A truncated comment or hostname will be handled correctly.
824 break :blk line_buf[0..];
823 break :blk @as([]u8, &line_buf); // TODO the cast should not be necessary
825824 },
826825 else => |e| return e,
827826 }) |line| {
......@@ -958,7 +957,10 @@ fn linuxLookupNameFromDns(
958957 }
959958 }
960959
961 var ap = [2][]u8{ apbuf[0][0..0], apbuf[1][0..0] };
960 var ap = [2][]u8{ apbuf[0], apbuf[1] };
961 ap[0].len = 0;
962 ap[1].len = 0;
963
962964 try resMSendRc(qp[0..nq], ap[0..nq], apbuf[0..nq], rc);
963965
964966 var i: usize = 0;
......@@ -1015,7 +1017,7 @@ fn getResolvConf(allocator: *mem.Allocator, rc: *ResolvConf) !void {
10151017 // Skip to the delimiter in the stream, to fix parsing
10161018 try stream.skipUntilDelimiterOrEof('\n');
10171019 // Give an empty line to the while loop, which will be skipped.
1018 break :blk line_buf[0..0];
1020 break :blk @as([]u8, line_buf[0..0]); // TODO the cast should not be necessary
10191021 },
10201022 else => |e| return e,
10211023 }) |line| {
lib/std/os/windows.zig+9-1
......@@ -1276,7 +1276,15 @@ pub fn unexpectedError(err: Win32Error) std.os.UnexpectedError {
12761276 // 614 is the length of the longest windows error desciption
12771277 var buf_u16: [614]u16 = undefined;
12781278 var buf_u8: [614]u8 = undefined;
1279 var len = kernel32.FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, null, err, MAKELANGID(LANG.NEUTRAL, SUBLANG.DEFAULT), buf_u16[0..].ptr, buf_u16.len / @sizeOf(TCHAR), null);
1279 const len = kernel32.FormatMessageW(
1280 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1281 null,
1282 err,
1283 MAKELANGID(LANG.NEUTRAL, SUBLANG.DEFAULT),
1284 &buf_u16,
1285 buf_u16.len / @sizeOf(TCHAR),
1286 null,
1287 );
12801288 _ = std.unicode.utf16leToUtf8(&buf_u8, buf_u16[0..len]) catch unreachable;
12811289 std.debug.warn("error.Unexpected: GetLastError({}): {}\n", .{ @enumToInt(err), buf_u8[0..len] });
12821290 std.debug.dumpCurrentStackTrace(null);
lib/std/rand.zig+1-1
......@@ -5,7 +5,7 @@
55// ```
66// var buf: [8]u8 = undefined;
77// try std.crypto.randomBytes(buf[0..]);
8// const seed = mem.readIntSliceLittle(u64, buf[0..8]);
8// const seed = mem.readIntLittle(u64, buf[0..8]);
99//
1010// var r = DefaultPrng.init(seed);
1111//
lib/std/unicode.zig+10-10
......@@ -251,12 +251,12 @@ pub const Utf16LeIterator = struct {
251251 pub fn nextCodepoint(it: *Utf16LeIterator) !?u21 {
252252 assert(it.i <= it.bytes.len);
253253 if (it.i == it.bytes.len) return null;
254 const c0: u21 = mem.readIntSliceLittle(u16, it.bytes[it.i .. it.i + 2]);
254 const c0: u21 = mem.readIntLittle(u16, it.bytes[it.i..][0..2]);
255255 if (c0 & ~@as(u21, 0x03ff) == 0xd800) {
256256 // surrogate pair
257257 it.i += 2;
258258 if (it.i >= it.bytes.len) return error.DanglingSurrogateHalf;
259 const c1: u21 = mem.readIntSliceLittle(u16, it.bytes[it.i .. it.i + 2]);
259 const c1: u21 = mem.readIntLittle(u16, it.bytes[it.i..][0..2]);
260260 if (c1 & ~@as(u21, 0x03ff) != 0xdc00) return error.ExpectedSecondSurrogateHalf;
261261 it.i += 2;
262262 return 0x10000 + (((c0 & 0x03ff) << 10) | (c1 & 0x03ff));
......@@ -630,11 +630,11 @@ test "utf8ToUtf16LeWithNull" {
630630 }
631631}
632632
633/// Converts a UTF-8 string literal into a UTF-16LE string literal.
634pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16LeLen(utf8) :0] u16 {
633/// Converts a UTF-8 string literal into a UTF-16LE string literal.
634pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16LeLen(utf8):0]u16 {
635635 comptime {
636636 const len: usize = calcUtf16LeLen(utf8);
637 var utf16le: [len :0]u16 = [_ :0]u16{0} ** len;
637 var utf16le: [len:0]u16 = [_:0]u16{0} ** len;
638638 const utf16le_len = utf8ToUtf16Le(&utf16le, utf8[0..]) catch |err| @compileError(err);
639639 assert(len == utf16le_len);
640640 return &utf16le;
......@@ -660,8 +660,8 @@ fn calcUtf16LeLen(utf8: []const u8) usize {
660660}
661661
662662test "utf8ToUtf16LeStringLiteral" {
663{
664 const bytes = [_:0]u16{ 0x41 };
663 {
664 const bytes = [_:0]u16{0x41};
665665 const utf16 = utf8ToUtf16LeStringLiteral("A");
666666 testing.expectEqualSlices(u16, &bytes, utf16);
667667 testing.expect(utf16[1] == 0);
......@@ -673,19 +673,19 @@ test "utf8ToUtf16LeStringLiteral" {
673673 testing.expect(utf16[2] == 0);
674674 }
675675 {
676 const bytes = [_:0]u16{ 0x02FF };
676 const bytes = [_:0]u16{0x02FF};
677677 const utf16 = utf8ToUtf16LeStringLiteral("\u{02FF}");
678678 testing.expectEqualSlices(u16, &bytes, utf16);
679679 testing.expect(utf16[1] == 0);
680680 }
681681 {
682 const bytes = [_:0]u16{ 0x7FF };
682 const bytes = [_:0]u16{0x7FF};
683683 const utf16 = utf8ToUtf16LeStringLiteral("\u{7FF}");
684684 testing.expectEqualSlices(u16, &bytes, utf16);
685685 testing.expect(utf16[1] == 0);
686686 }
687687 {
688 const bytes = [_:0]u16{ 0x801 };
688 const bytes = [_:0]u16{0x801};
689689 const utf16 = utf8ToUtf16LeStringLiteral("\u{801}");
690690 testing.expectEqualSlices(u16, &bytes, utf16);
691691 testing.expect(utf16[1] == 0);
src-self-hosted/stage2.zig+1-1
......@@ -128,7 +128,7 @@ export fn stage2_translate_c(
128128 args_end: [*]?[*]const u8,
129129 resources_path: [*:0]const u8,
130130) Error {
131 var errors = @as([*]translate_c.ClangErrMsg, undefined)[0..0];
131 var errors: []translate_c.ClangErrMsg = &[0]translate_c.ClangErrMsg{};
132132 out_ast.* = translate_c.translate(std.heap.c_allocator, args_begin, args_end, &errors, resources_path) catch |err| switch (err) {
133133 error.SemanticAnalyzeFail => {
134134 out_errors_ptr.* = errors.ptr;
src-self-hosted/translate_c.zig+12-14
......@@ -1744,20 +1744,18 @@ fn writeEscapedString(buf: []u8, s: []const u8) void {
17441744// Returns either a string literal or a slice of `buf`.
17451745fn escapeChar(c: u8, char_buf: *[4]u8) []const u8 {
17461746 return switch (c) {
1747 '\"' => "\\\""[0..],
1748 '\'' => "\\'"[0..],
1749 '\\' => "\\\\"[0..],
1750 '\n' => "\\n"[0..],
1751 '\r' => "\\r"[0..],
1752 '\t' => "\\t"[0..],
1753 else => {
1754 // Handle the remaining escapes Zig doesn't support by turning them
1755 // into their respective hex representation
1756 if (std.ascii.isCntrl(c))
1757 return std.fmt.bufPrint(char_buf[0..], "\\x{x:0<2}", .{c}) catch unreachable
1758 else
1759 return std.fmt.bufPrint(char_buf[0..], "{c}", .{c}) catch unreachable;
1760 },
1747 '\"' => "\\\"",
1748 '\'' => "\\'",
1749 '\\' => "\\\\",
1750 '\n' => "\\n",
1751 '\r' => "\\r",
1752 '\t' => "\\t",
1753 // Handle the remaining escapes Zig doesn't support by turning them
1754 // into their respective hex representation
1755 else => if (std.ascii.isCntrl(c))
1756 std.fmt.bufPrint(char_buf, "\\x{x:0<2}", .{c}) catch unreachable
1757 else
1758 std.fmt.bufPrint(char_buf, "{c}", .{c}) catch unreachable,
17611759 };
17621760}
17631761
src/all_types.hpp+6
......@@ -231,6 +231,7 @@ enum ConstPtrSpecial {
231231 // The pointer is a reference to a single object.
232232 ConstPtrSpecialRef,
233233 // The pointer points to an element in an underlying array.
234 // Not to be confused with ConstPtrSpecialSubArray.
234235 ConstPtrSpecialBaseArray,
235236 // The pointer points to a field in an underlying struct.
236237 ConstPtrSpecialBaseStruct,
......@@ -257,6 +258,10 @@ enum ConstPtrSpecial {
257258 // types to be the same, so all optionals of pointer types use x_ptr
258259 // instead of x_optional.
259260 ConstPtrSpecialNull,
261 // The pointer points to a sub-array (not an individual element).
262 // Not to be confused with ConstPtrSpecialBaseArray. However, it uses the same
263 // union payload struct (base_array).
264 ConstPtrSpecialSubArray,
260265};
261266
262267enum ConstPtrMut {
......@@ -3706,6 +3711,7 @@ struct IrInstGenSlice {
37063711 IrInstGen *start;
37073712 IrInstGen *end;
37083713 IrInstGen *result_loc;
3714 ZigValue *sentinel;
37093715 bool safety_check_on;
37103716};
37113717
src/analyze.cpp+32-6
......@@ -780,6 +780,8 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
780780}
781781
782782ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, ZigValue *sentinel) {
783 Error err;
784
783785 TypeId type_id = {};
784786 type_id.id = ZigTypeIdArray;
785787 type_id.data.array.codegen = g;
......@@ -791,7 +793,11 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, Zi
791793 return existing_entry->value;
792794 }
793795
794 assert(type_is_resolved(child_type, ResolveStatusSizeKnown));
796 size_t full_array_size = array_size + ((sentinel != nullptr) ? 1 : 0);
797
798 if (full_array_size != 0 && (err = type_resolve(g, child_type, ResolveStatusSizeKnown))) {
799 codegen_report_errors_and_exit(g);
800 }
795801
796802 ZigType *entry = new_type_table_entry(ZigTypeIdArray);
797803
......@@ -803,9 +809,8 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, Zi
803809 }
804810 buf_appendf(&entry->name, "]%s", buf_ptr(&child_type->name));
805811
806 size_t full_array_size = array_size + ((sentinel != nullptr) ? 1 : 0);
807812 entry->size_in_bits = child_type->size_in_bits * full_array_size;
808 entry->abi_align = child_type->abi_align;
813 entry->abi_align = (full_array_size == 0) ? 0 : child_type->abi_align;
809814 entry->abi_size = child_type->abi_size * full_array_size;
810815
811816 entry->data.array.child_type = child_type;
......@@ -4483,7 +4488,14 @@ static uint32_t get_async_frame_align_bytes(CodeGen *g) {
44834488}
44844489
44854490uint32_t get_ptr_align(CodeGen *g, ZigType *type) {
4486 ZigType *ptr_type = get_src_ptr_type(type);
4491 ZigType *ptr_type;
4492 if (type->id == ZigTypeIdStruct) {
4493 assert(type->data.structure.special == StructSpecialSlice);
4494 TypeStructField *ptr_field = type->data.structure.fields[slice_ptr_index];
4495 ptr_type = resolve_struct_field_type(g, ptr_field);
4496 } else {
4497 ptr_type = get_src_ptr_type(type);
4498 }
44874499 if (ptr_type->id == ZigTypeIdPointer) {
44884500 return (ptr_type->data.pointer.explicit_alignment == 0) ?
44894501 get_abi_alignment(g, ptr_type->data.pointer.child_type) : ptr_type->data.pointer.explicit_alignment;
......@@ -4500,8 +4512,15 @@ uint32_t get_ptr_align(CodeGen *g, ZigType *type) {
45004512 }
45014513}
45024514
4503bool get_ptr_const(ZigType *type) {
4504 ZigType *ptr_type = get_src_ptr_type(type);
4515bool get_ptr_const(CodeGen *g, ZigType *type) {
4516 ZigType *ptr_type;
4517 if (type->id == ZigTypeIdStruct) {
4518 assert(type->data.structure.special == StructSpecialSlice);
4519 TypeStructField *ptr_field = type->data.structure.fields[slice_ptr_index];
4520 ptr_type = resolve_struct_field_type(g, ptr_field);
4521 } else {
4522 ptr_type = get_src_ptr_type(type);
4523 }
45054524 if (ptr_type->id == ZigTypeIdPointer) {
45064525 return ptr_type->data.pointer.is_const;
45074526 } else if (ptr_type->id == ZigTypeIdFn) {
......@@ -5277,6 +5296,11 @@ static uint32_t hash_const_val_ptr(ZigValue *const_val) {
52775296 hash_val += hash_ptr(const_val->data.x_ptr.data.base_array.array_val);
52785297 hash_val += hash_size(const_val->data.x_ptr.data.base_array.elem_index);
52795298 return hash_val;
5299 case ConstPtrSpecialSubArray:
5300 hash_val += (uint32_t)2643358777;
5301 hash_val += hash_ptr(const_val->data.x_ptr.data.base_array.array_val);
5302 hash_val += hash_size(const_val->data.x_ptr.data.base_array.elem_index);
5303 return hash_val;
52805304 case ConstPtrSpecialBaseStruct:
52815305 hash_val += (uint32_t)3518317043;
52825306 hash_val += hash_ptr(const_val->data.x_ptr.data.base_struct.struct_val);
......@@ -6743,6 +6767,7 @@ bool const_values_equal_ptr(ZigValue *a, ZigValue *b) {
67436767 return false;
67446768 return true;
67456769 case ConstPtrSpecialBaseArray:
6770 case ConstPtrSpecialSubArray:
67466771 if (a->data.x_ptr.data.base_array.array_val != b->data.x_ptr.data.base_array.array_val) {
67476772 return false;
67486773 }
......@@ -7000,6 +7025,7 @@ static void render_const_val_ptr(CodeGen *g, Buf *buf, ZigValue *const_val, ZigT
70007025 render_const_value(g, buf, const_ptr_pointee(nullptr, g, const_val, nullptr));
70017026 return;
70027027 case ConstPtrSpecialBaseArray:
7028 case ConstPtrSpecialSubArray:
70037029 buf_appendf(buf, "*");
70047030 // TODO we need a source node for const_ptr_pointee because it can generate compile errors
70057031 render_const_value(g, buf, const_ptr_pointee(nullptr, g, const_val, nullptr));
src/analyze.hpp+1-1
......@@ -76,7 +76,7 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool all
7676
7777ZigType *get_src_ptr_type(ZigType *type);
7878uint32_t get_ptr_align(CodeGen *g, ZigType *type);
79bool get_ptr_const(ZigType *type);
79bool get_ptr_const(CodeGen *g, ZigType *type);
8080ZigType *validate_var_type(CodeGen *g, AstNode *source_node, ZigType *type_entry);
8181ZigType *container_ref_type(ZigType *type_entry);
8282bool type_is_complete(ZigType *type_entry);
src/codegen.cpp+59-25
......@@ -5418,12 +5418,16 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
54185418 ZigType *array_type = array_ptr_type->data.pointer.child_type;
54195419 LLVMValueRef array_ptr = get_handle_value(g, array_ptr_ptr, array_type, array_ptr_type);
54205420
5421 LLVMValueRef tmp_struct_ptr = ir_llvm_value(g, instruction->result_loc);
5422
54235421 bool want_runtime_safety = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base);
54245422
5425 ZigType *res_slice_ptr_type = instruction->base.value->type->data.structure.fields[slice_ptr_index]->type_entry;
5426 ZigValue *sentinel = res_slice_ptr_type->data.pointer.sentinel;
5423 ZigType *result_type = instruction->base.value->type;
5424 if (!type_has_bits(g, result_type)) {
5425 return nullptr;
5426 }
5427
5428 // This is not whether the result type has a sentinel, but whether there should be a sentinel check,
5429 // e.g. if they used [a..b :s] syntax.
5430 ZigValue *sentinel = instruction->sentinel;
54275431
54285432 if (array_type->id == ZigTypeIdArray ||
54295433 (array_type->id == ZigTypeIdPointer && array_type->data.pointer.ptr_len == PtrLenSingle))
......@@ -5458,6 +5462,8 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
54585462 }
54595463 }
54605464 if (!type_has_bits(g, array_type)) {
5465 LLVMValueRef tmp_struct_ptr = ir_llvm_value(g, instruction->result_loc);
5466
54615467 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_len_index, "");
54625468
54635469 // TODO if runtime safety is on, store 0xaaaaaaa in ptr field
......@@ -5466,20 +5472,26 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
54665472 return tmp_struct_ptr;
54675473 }
54685474
5469
5470 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_ptr_index, "");
54715475 LLVMValueRef indices[] = {
54725476 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
54735477 start_val,
54745478 };
54755479 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
5476 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
5480 if (result_type->id == ZigTypeIdPointer) {
5481 ir_assert(instruction->result_loc == nullptr, &instruction->base);
5482 LLVMTypeRef result_ptr_type = get_llvm_type(g, result_type);
5483 return LLVMBuildBitCast(g->builder, slice_start_ptr, result_ptr_type, "");
5484 } else {
5485 LLVMValueRef tmp_struct_ptr = ir_llvm_value(g, instruction->result_loc);
5486 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_ptr_index, "");
5487 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
54775488
5478 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_len_index, "");
5479 LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
5480 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
5489 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_len_index, "");
5490 LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
5491 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
54815492
5482 return tmp_struct_ptr;
5493 return tmp_struct_ptr;
5494 }
54835495 } else if (array_type->id == ZigTypeIdPointer) {
54845496 assert(array_type->data.pointer.ptr_len != PtrLenSingle);
54855497 LLVMValueRef start_val = ir_llvm_value(g, instruction->start);
......@@ -5493,24 +5505,39 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
54935505 }
54945506 }
54955507
5496 if (type_has_bits(g, array_type)) {
5497 size_t gen_ptr_index = instruction->base.value->type->data.structure.fields[slice_ptr_index]->gen_index;
5498 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, gen_ptr_index, "");
5499 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, &start_val, 1, "");
5500 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
5508 if (!type_has_bits(g, array_type)) {
5509 LLVMValueRef tmp_struct_ptr = ir_llvm_value(g, instruction->result_loc);
5510 size_t gen_len_index = result_type->data.structure.fields[slice_len_index]->gen_index;
5511 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, gen_len_index, "");
5512 LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
5513 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
5514 return tmp_struct_ptr;
5515 }
5516
5517 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, &start_val, 1, "");
5518 if (result_type->id == ZigTypeIdPointer) {
5519 ir_assert(instruction->result_loc == nullptr, &instruction->base);
5520 LLVMTypeRef result_ptr_type = get_llvm_type(g, result_type);
5521 return LLVMBuildBitCast(g->builder, slice_start_ptr, result_ptr_type, "");
55015522 }
55025523
5503 size_t gen_len_index = instruction->base.value->type->data.structure.fields[slice_len_index]->gen_index;
5524 LLVMValueRef tmp_struct_ptr = ir_llvm_value(g, instruction->result_loc);
5525
5526 size_t gen_ptr_index = result_type->data.structure.fields[slice_ptr_index]->gen_index;
5527 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, gen_ptr_index, "");
5528 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
5529
5530 size_t gen_len_index = result_type->data.structure.fields[slice_len_index]->gen_index;
55045531 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, gen_len_index, "");
55055532 LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
55065533 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
55075534
55085535 return tmp_struct_ptr;
5536
55095537 } else if (array_type->id == ZigTypeIdStruct) {
55105538 assert(array_type->data.structure.special == StructSpecialSlice);
55115539 assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMPointerTypeKind);
55125540 assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(array_ptr))) == LLVMStructTypeKind);
5513 assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(tmp_struct_ptr))) == LLVMStructTypeKind);
55145541
55155542 size_t ptr_index = array_type->data.structure.fields[slice_ptr_index]->gen_index;
55165543 assert(ptr_index != SIZE_MAX);
......@@ -5547,15 +5574,22 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
55475574 }
55485575 }
55495576
5550 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, (unsigned)ptr_index, "");
55515577 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, src_ptr, &start_val, 1, "");
5552 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
5578 if (result_type->id == ZigTypeIdPointer) {
5579 ir_assert(instruction->result_loc == nullptr, &instruction->base);
5580 LLVMTypeRef result_ptr_type = get_llvm_type(g, result_type);
5581 return LLVMBuildBitCast(g->builder, slice_start_ptr, result_ptr_type, "");
5582 } else {
5583 LLVMValueRef tmp_struct_ptr = ir_llvm_value(g, instruction->result_loc);
5584 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, (unsigned)ptr_index, "");
5585 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
55535586
5554 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, (unsigned)len_index, "");
5555 LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
5556 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
5587 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, (unsigned)len_index, "");
5588 LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
5589 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
55575590
5558 return tmp_struct_ptr;
5591 return tmp_struct_ptr;
5592 }
55595593 } else {
55605594 zig_unreachable();
55615595 }
......@@ -6640,7 +6674,6 @@ static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ZigValue *array_co
66406674 };
66416675 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
66426676 } else {
6643 assert(parent->id == ConstParentIdScalar);
66446677 return base_ptr;
66456678 }
66466679}
......@@ -6868,6 +6901,7 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ZigValue *const_val, const cha
68686901 return const_val->llvm_value;
68696902 }
68706903 case ConstPtrSpecialBaseArray:
6904 case ConstPtrSpecialSubArray:
68716905 {
68726906 ZigValue *array_const_val = const_val->data.x_ptr.data.base_array.array_val;
68736907 assert(array_const_val->type->id == ZigTypeIdArray);
src/ir.cpp+324-91
......@@ -784,14 +784,32 @@ static ZigValue *const_ptr_pointee_unchecked_no_isf(CodeGen *g, ZigValue *const_
784784 break;
785785 case ConstPtrSpecialBaseArray: {
786786 ZigValue *array_val = const_val->data.x_ptr.data.base_array.array_val;
787 if (const_val->data.x_ptr.data.base_array.elem_index == array_val->type->data.array.len) {
787 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;
788 if (elem_index == array_val->type->data.array.len) {
788789 result = array_val->type->data.array.sentinel;
789790 } else {
790791 expand_undef_array(g, array_val);
791 result = &array_val->data.x_array.data.s_none.elements[const_val->data.x_ptr.data.base_array.elem_index];
792 result = &array_val->data.x_array.data.s_none.elements[elem_index];
792793 }
793794 break;
794795 }
796 case ConstPtrSpecialSubArray: {
797 ZigValue *array_val = const_val->data.x_ptr.data.base_array.array_val;
798 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;
799
800 // TODO handle sentinel terminated arrays
801 expand_undef_array(g, array_val);
802 result = g->pass1_arena->create<ZigValue>();
803 result->special = array_val->special;
804 result->type = get_array_type(g, array_val->type->data.array.child_type,
805 array_val->type->data.array.len - elem_index, nullptr);
806 result->data.x_array.special = ConstArraySpecialNone;
807 result->data.x_array.data.s_none.elements = &array_val->data.x_array.data.s_none.elements[elem_index];
808 result->parent.id = ConstParentIdArray;
809 result->parent.data.p_array.array_val = array_val;
810 result->parent.data.p_array.elem_index = elem_index;
811 break;
812 }
795813 case ConstPtrSpecialBaseStruct: {
796814 ZigValue *struct_val = const_val->data.x_ptr.data.base_struct.struct_val;
797815 expand_undef_struct(g, struct_val);
......@@ -849,11 +867,6 @@ static bool is_slice(ZigType *type) {
849867 return type->id == ZigTypeIdStruct && type->data.structure.special == StructSpecialSlice;
850868}
851869
852static bool slice_is_const(ZigType *type) {
853 assert(is_slice(type));
854 return type->data.structure.fields[slice_ptr_index]->type_entry->data.pointer.is_const;
855}
856
857870// This function returns true when you can change the type of a ZigValue and the
858871// value remains meaningful.
859872static bool types_have_same_zig_comptime_repr(CodeGen *codegen, ZigType *expected, ZigType *actual) {
......@@ -3719,7 +3732,8 @@ static IrInstSrc *ir_build_slice_src(IrBuilderSrc *irb, Scope *scope, AstNode *s
37193732}
37203733
37213734static IrInstGen *ir_build_slice_gen(IrAnalyze *ira, IrInst *source_instruction, ZigType *slice_type,
3722 IrInstGen *ptr, IrInstGen *start, IrInstGen *end, bool safety_check_on, IrInstGen *result_loc)
3735 IrInstGen *ptr, IrInstGen *start, IrInstGen *end, bool safety_check_on, IrInstGen *result_loc,
3736 ZigValue *sentinel)
37233737{
37243738 IrInstGenSlice *instruction = ir_build_inst_gen<IrInstGenSlice>(
37253739 &ira->new_irb, source_instruction->scope, source_instruction->source_node);
......@@ -3729,11 +3743,12 @@ static IrInstGen *ir_build_slice_gen(IrAnalyze *ira, IrInst *source_instruction,
37293743 instruction->end = end;
37303744 instruction->safety_check_on = safety_check_on;
37313745 instruction->result_loc = result_loc;
3746 instruction->sentinel = sentinel;
37323747
37333748 ir_ref_inst_gen(ptr, ira->new_irb.current_basic_block);
37343749 ir_ref_inst_gen(start, ira->new_irb.current_basic_block);
3735 if (end) ir_ref_inst_gen(end, ira->new_irb.current_basic_block);
3736 ir_ref_inst_gen(result_loc, ira->new_irb.current_basic_block);
3750 if (end != nullptr) ir_ref_inst_gen(end, ira->new_irb.current_basic_block);
3751 if (result_loc != nullptr) ir_ref_inst_gen(result_loc, ira->new_irb.current_basic_block);
37373752
37383753 return &instruction->base;
37393754}
......@@ -12677,41 +12692,80 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc
1267712692 Error err;
1267812693
1267912694 assert(array_ptr->value->type->id == ZigTypeIdPointer);
12695 assert(array_ptr->value->type->data.pointer.child_type->id == ZigTypeIdArray);
12696
12697 ZigType *array_type = array_ptr->value->type->data.pointer.child_type;
12698 size_t array_len = array_type->data.array.len;
12699
12700 // A zero-sized array can be casted regardless of the destination alignment, or
12701 // whether the pointer is undefined, and the result is always comptime known.
12702 // TODO However, this is exposing a result location bug that I failed to solve on the first try.
12703 // If you want to try to fix the bug, uncomment this block and get the tests passing.
12704 //if (array_len == 0 && array_type->data.array.sentinel == nullptr) {
12705 // ZigValue *undef_array = ira->codegen->pass1_arena->create<ZigValue>();
12706 // undef_array->special = ConstValSpecialUndef;
12707 // undef_array->type = array_type;
12708
12709 // IrInstGen *result = ir_const(ira, source_instr, wanted_type);
12710 // init_const_slice(ira->codegen, result->value, undef_array, 0, 0, false);
12711 // result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = ConstPtrMutComptimeConst;
12712 // result->value->type = wanted_type;
12713 // return result;
12714 //}
1268012715
1268112716 if ((err = type_resolve(ira->codegen, array_ptr->value->type, ResolveStatusAlignmentKnown))) {
1268212717 return ira->codegen->invalid_inst_gen;
1268312718 }
1268412719
12685 assert(array_ptr->value->type->data.pointer.child_type->id == ZigTypeIdArray);
12686
12687 const size_t array_len = array_ptr->value->type->data.pointer.child_type->data.array.len;
12688
12689 // A zero-sized array can always be casted irregardless of the destination
12690 // alignment
1269112720 if (array_len != 0) {
1269212721 wanted_type = adjust_slice_align(ira->codegen, wanted_type,
1269312722 get_ptr_align(ira->codegen, array_ptr->value->type));
1269412723 }
1269512724
1269612725 if (instr_is_comptime(array_ptr)) {
12697 ZigValue *array_ptr_val = ir_resolve_const(ira, array_ptr, UndefBad);
12726 UndefAllowed undef_allowed = (array_len == 0) ? UndefOk : UndefBad;
12727 ZigValue *array_ptr_val = ir_resolve_const(ira, array_ptr, undef_allowed);
1269812728 if (array_ptr_val == nullptr)
1269912729 return ira->codegen->invalid_inst_gen;
12700 ZigValue *pointee = const_ptr_pointee(ira, ira->codegen, array_ptr_val, source_instr->source_node);
12701 if (pointee == nullptr)
12702 return ira->codegen->invalid_inst_gen;
12703 if (pointee->special != ConstValSpecialRuntime) {
12704 assert(array_ptr_val->type->id == ZigTypeIdPointer);
12705 ZigType *array_type = array_ptr_val->type->data.pointer.child_type;
12706 assert(is_slice(wanted_type));
12707 bool is_const = wanted_type->data.structure.fields[slice_ptr_index]->type_entry->data.pointer.is_const;
12730 ir_assert(is_slice(wanted_type), source_instr);
12731 if (array_ptr_val->special == ConstValSpecialUndef) {
12732 ZigValue *undef_array = ira->codegen->pass1_arena->create<ZigValue>();
12733 undef_array->special = ConstValSpecialUndef;
12734 undef_array->type = array_type;
1270812735
1270912736 IrInstGen *result = ir_const(ira, source_instr, wanted_type);
12710 init_const_slice(ira->codegen, result->value, pointee, 0, array_type->data.array.len, is_const);
12711 result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = array_ptr_val->data.x_ptr.mut;
12737 init_const_slice(ira->codegen, result->value, undef_array, 0, 0, false);
12738 result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = ConstPtrMutComptimeConst;
1271212739 result->value->type = wanted_type;
1271312740 return result;
1271412741 }
12742 bool wanted_const = wanted_type->data.structure.fields[slice_ptr_index]->type_entry->data.pointer.is_const;
12743 // Optimization to avoid creating unnecessary ZigValue in const_ptr_pointee
12744 if (array_ptr_val->data.x_ptr.special == ConstPtrSpecialSubArray) {
12745 ZigValue *array_val = array_ptr_val->data.x_ptr.data.base_array.array_val;
12746 if (array_val->special != ConstValSpecialRuntime) {
12747 IrInstGen *result = ir_const(ira, source_instr, wanted_type);
12748 init_const_slice(ira->codegen, result->value, array_val,
12749 array_ptr_val->data.x_ptr.data.base_array.elem_index,
12750 array_type->data.array.len, wanted_const);
12751 result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = array_ptr_val->data.x_ptr.mut;
12752 result->value->type = wanted_type;
12753 return result;
12754 }
12755 } else {
12756 ZigValue *pointee = const_ptr_pointee(ira, ira->codegen, array_ptr_val, source_instr->source_node);
12757 if (pointee == nullptr)
12758 return ira->codegen->invalid_inst_gen;
12759 if (pointee->special != ConstValSpecialRuntime) {
12760 assert(array_ptr_val->type->id == ZigTypeIdPointer);
12761
12762 IrInstGen *result = ir_const(ira, source_instr, wanted_type);
12763 init_const_slice(ira->codegen, result->value, pointee, 0, array_type->data.array.len, wanted_const);
12764 result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = array_ptr_val->data.x_ptr.mut;
12765 result->value->type = wanted_type;
12766 return result;
12767 }
12768 }
1271512769 }
1271612770
1271712771 if (result_loc == nullptr) result_loc = no_result_loc();
......@@ -14581,7 +14635,7 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,
1458114635 return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type);
1458214636 }
1458314637
14584 // *[N]T to ?[]const T
14638 // *[N]T to ?[]T
1458514639 if (wanted_type->id == ZigTypeIdOptional &&
1458614640 is_slice(wanted_type->data.maybe.child_type) &&
1458714641 actual_type->id == ZigTypeIdPointer &&
......@@ -19917,6 +19971,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source
1991719971 buf_write_value_bytes(codegen, (uint8_t*)buf_ptr(&buf), pointee);
1991819972 if ((err = buf_read_value_bytes(ira, codegen, source_node, (uint8_t*)buf_ptr(&buf), out_val)))
1991919973 return err;
19974 buf_deinit(&buf);
1992019975 return ErrorNone;
1992119976 }
1992219977
......@@ -19936,6 +19991,31 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source
1993619991 dst_size, buf_ptr(&pointee->type->name), src_size));
1993719992 return ErrorSemanticAnalyzeFail;
1993819993 }
19994 case ConstPtrSpecialSubArray: {
19995 ZigValue *array_val = ptr_val->data.x_ptr.data.base_array.array_val;
19996 assert(array_val->type->id == ZigTypeIdArray);
19997 if (array_val->data.x_array.special != ConstArraySpecialNone)
19998 zig_panic("TODO");
19999 if (dst_size > src_size) {
20000 size_t elem_index = ptr_val->data.x_ptr.data.base_array.elem_index;
20001 opt_ir_add_error_node(ira, codegen, source_node,
20002 buf_sprintf("attempt to read %" ZIG_PRI_usize " bytes from %s at index %" ZIG_PRI_usize " which is %" ZIG_PRI_usize " bytes",
20003 dst_size, buf_ptr(&array_val->type->name), elem_index, src_size));
20004 return ErrorSemanticAnalyzeFail;
20005 }
20006 size_t elem_size = src_size;
20007 size_t elem_count = (dst_size % elem_size == 0) ? (dst_size / elem_size) : (dst_size / elem_size + 1);
20008 Buf buf = BUF_INIT;
20009 buf_resize(&buf, elem_count * elem_size);
20010 for (size_t i = 0; i < elem_count; i += 1) {
20011 ZigValue *elem_val = &array_val->data.x_array.data.s_none.elements[i];
20012 buf_write_value_bytes(codegen, (uint8_t*)buf_ptr(&buf) + (i * elem_size), elem_val);
20013 }
20014 if ((err = buf_read_value_bytes(ira, codegen, source_node, (uint8_t*)buf_ptr(&buf), out_val)))
20015 return err;
20016 buf_deinit(&buf);
20017 return ErrorNone;
20018 }
1993920019 case ConstPtrSpecialBaseArray: {
1994020020 ZigValue *array_val = ptr_val->data.x_ptr.data.base_array.array_val;
1994120021 assert(array_val->type->id == ZigTypeIdArray);
......@@ -19959,6 +20039,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source
1995920039 }
1996020040 if ((err = buf_read_value_bytes(ira, codegen, source_node, (uint8_t*)buf_ptr(&buf), out_val)))
1996120041 return err;
20042 buf_deinit(&buf);
1996220043 return ErrorNone;
1996320044 }
1996420045 case ConstPtrSpecialBaseStruct:
......@@ -20538,6 +20619,44 @@ static ZigType *adjust_ptr_allow_zero(CodeGen *g, ZigType *ptr_type, bool allow_
2053820619 allow_zero);
2053920620}
2054020621
20622static Error compute_elem_align(IrAnalyze *ira, ZigType *elem_type, uint32_t base_ptr_align,
20623 uint64_t elem_index, uint32_t *result)
20624{
20625 Error err;
20626
20627 if (base_ptr_align == 0) {
20628 *result = 0;
20629 return ErrorNone;
20630 }
20631
20632 // figure out the largest alignment possible
20633 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusSizeKnown)))
20634 return err;
20635
20636 uint64_t elem_size = type_size(ira->codegen, elem_type);
20637 uint64_t abi_align = get_abi_alignment(ira->codegen, elem_type);
20638 uint64_t ptr_align = base_ptr_align;
20639
20640 uint64_t chosen_align = abi_align;
20641 if (ptr_align >= abi_align) {
20642 while (ptr_align > abi_align) {
20643 if ((elem_index * elem_size) % ptr_align == 0) {
20644 chosen_align = ptr_align;
20645 break;
20646 }
20647 ptr_align >>= 1;
20648 }
20649 } else if (elem_size >= ptr_align && elem_size % ptr_align == 0) {
20650 chosen_align = ptr_align;
20651 } else {
20652 // can't get here because guaranteed elem_size >= abi_align
20653 zig_unreachable();
20654 }
20655
20656 *result = chosen_align;
20657 return ErrorNone;
20658}
20659
2054120660static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemPtr *elem_ptr_instruction) {
2054220661 Error err;
2054320662 IrInstGen *array_ptr = elem_ptr_instruction->array_ptr->child;
......@@ -20676,29 +20795,11 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
2067620795 get_ptr_align(ira->codegen, ptr_type), 0, host_vec_len, false, (uint32_t)index,
2067720796 nullptr, nullptr);
2067820797 } else if (return_type->data.pointer.explicit_alignment != 0) {
20679 // figure out the largest alignment possible
20680
20681 if ((err = type_resolve(ira->codegen, return_type->data.pointer.child_type, ResolveStatusSizeKnown)))
20798 uint32_t chosen_align;
20799 if ((err = compute_elem_align(ira, return_type->data.pointer.child_type,
20800 return_type->data.pointer.explicit_alignment, index, &chosen_align)))
20801 {
2068220802 return ira->codegen->invalid_inst_gen;
20683
20684 uint64_t elem_size = type_size(ira->codegen, return_type->data.pointer.child_type);
20685 uint64_t abi_align = get_abi_alignment(ira->codegen, return_type->data.pointer.child_type);
20686 uint64_t ptr_align = get_ptr_align(ira->codegen, return_type);
20687
20688 uint64_t chosen_align = abi_align;
20689 if (ptr_align >= abi_align) {
20690 while (ptr_align > abi_align) {
20691 if ((index * elem_size) % ptr_align == 0) {
20692 chosen_align = ptr_align;
20693 break;
20694 }
20695 ptr_align >>= 1;
20696 }
20697 } else if (elem_size >= ptr_align && elem_size % ptr_align == 0) {
20698 chosen_align = ptr_align;
20699 } else {
20700 // can't get here because guaranteed elem_size >= abi_align
20701 zig_unreachable();
2070220803 }
2070320804 return_type = adjust_ptr_align(ira->codegen, return_type, chosen_align);
2070420805 }
......@@ -20819,6 +20920,7 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
2081920920 }
2082020921 break;
2082120922 case ConstPtrSpecialBaseArray:
20923 case ConstPtrSpecialSubArray:
2082220924 {
2082320925 size_t offset = array_ptr_val->data.x_ptr.data.base_array.elem_index;
2082420926 new_index = offset + index;
......@@ -20889,6 +20991,7 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
2088920991 out_val->data.x_ptr.special = ConstPtrSpecialRef;
2089020992 out_val->data.x_ptr.data.ref.pointee = ptr_field->data.x_ptr.data.ref.pointee;
2089120993 break;
20994 case ConstPtrSpecialSubArray:
2089220995 case ConstPtrSpecialBaseArray:
2089320996 {
2089420997 size_t offset = ptr_field->data.x_ptr.data.base_array.elem_index;
......@@ -25440,11 +25543,22 @@ static IrInstGen *ir_analyze_instruction_err_set_cast(IrAnalyze *ira, IrInstSrcE
2544025543static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align) {
2544125544 Error err;
2544225545
25443 ZigType *ptr_type = get_src_ptr_type(ty);
25546 ZigType *ptr_type;
25547 if (is_slice(ty)) {
25548 TypeStructField *ptr_field = ty->data.structure.fields[slice_ptr_index];
25549 ptr_type = resolve_struct_field_type(ira->codegen, ptr_field);
25550 } else {
25551 ptr_type = get_src_ptr_type(ty);
25552 }
2544425553 assert(ptr_type != nullptr);
2544525554 if (ptr_type->id == ZigTypeIdPointer) {
2544625555 if ((err = type_resolve(ira->codegen, ptr_type->data.pointer.child_type, ResolveStatusAlignmentKnown)))
2544725556 return err;
25557 } else if (is_slice(ptr_type)) {
25558 TypeStructField *ptr_field = ptr_type->data.structure.fields[slice_ptr_index];
25559 ZigType *slice_ptr_type = resolve_struct_field_type(ira->codegen, ptr_field);
25560 if ((err = type_resolve(ira->codegen, slice_ptr_type->data.pointer.child_type, ResolveStatusAlignmentKnown)))
25561 return err;
2544825562 }
2544925563
2545025564 *result_align = get_ptr_align(ira->codegen, ty);
......@@ -25899,6 +26013,7 @@ static IrInstGen *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstSrcMemset
2589926013 start = 0;
2590026014 bound_end = 1;
2590126015 break;
26016 case ConstPtrSpecialSubArray:
2590226017 case ConstPtrSpecialBaseArray:
2590326018 {
2590426019 ZigValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
......@@ -26032,6 +26147,7 @@ static IrInstGen *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstSrcMemcpy
2603226147 dest_start = 0;
2603326148 dest_end = 1;
2603426149 break;
26150 case ConstPtrSpecialSubArray:
2603526151 case ConstPtrSpecialBaseArray:
2603626152 {
2603726153 ZigValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
......@@ -26075,6 +26191,7 @@ static IrInstGen *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstSrcMemcpy
2607526191 src_start = 0;
2607626192 src_end = 1;
2607726193 break;
26194 case ConstPtrSpecialSubArray:
2607826195 case ConstPtrSpecialBaseArray:
2607926196 {
2608026197 ZigValue *array_val = src_ptr_val->data.x_ptr.data.base_array.array_val;
......@@ -26118,7 +26235,19 @@ static IrInstGen *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstSrcMemcpy
2611826235 return ir_build_memcpy_gen(ira, &instruction->base.base, casted_dest_ptr, casted_src_ptr, casted_count);
2611926236}
2612026237
26238static ZigType *get_result_loc_type(IrAnalyze *ira, ResultLoc *result_loc) {
26239 if (result_loc == nullptr) return nullptr;
26240
26241 if (result_loc->id == ResultLocIdCast) {
26242 return ir_resolve_type(ira, result_loc->source_instruction->child);
26243 }
26244
26245 return nullptr;
26246}
26247
2612126248static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *instruction) {
26249 Error err;
26250
2612226251 IrInstGen *ptr_ptr = instruction->ptr->child;
2612326252 if (type_is_invalid(ptr_ptr->value->type))
2612426253 return ira->codegen->invalid_inst_gen;
......@@ -26148,6 +26277,7 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2614826277 end = nullptr;
2614926278 }
2615026279
26280 ZigValue *slice_sentinel_val = nullptr;
2615126281 ZigType *non_sentinel_slice_ptr_type;
2615226282 ZigType *elem_type;
2615326283
......@@ -26198,6 +26328,7 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2619826328 }
2619926329 } else if (is_slice(array_type)) {
2620026330 ZigType *maybe_sentineled_slice_ptr_type = array_type->data.structure.fields[slice_ptr_index]->type_entry;
26331 slice_sentinel_val = maybe_sentineled_slice_ptr_type->data.pointer.sentinel;
2620126332 non_sentinel_slice_ptr_type = adjust_ptr_sentinel(ira->codegen, maybe_sentineled_slice_ptr_type, nullptr);
2620226333 elem_type = non_sentinel_slice_ptr_type->data.pointer.child_type;
2620326334 } else {
......@@ -26206,7 +26337,6 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2620626337 return ira->codegen->invalid_inst_gen;
2620726338 }
2620826339
26209 ZigType *return_type;
2621026340 ZigValue *sentinel_val = nullptr;
2621126341 if (instruction->sentinel) {
2621226342 IrInstGen *uncasted_sentinel = instruction->sentinel->child;
......@@ -26218,11 +26348,76 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2621826348 sentinel_val = ir_resolve_const(ira, sentinel, UndefBad);
2621926349 if (sentinel_val == nullptr)
2622026350 return ira->codegen->invalid_inst_gen;
26221 ZigType *slice_ptr_type = adjust_ptr_sentinel(ira->codegen, non_sentinel_slice_ptr_type, sentinel_val);
26351 }
26352
26353 ZigType *child_array_type = (array_type->id == ZigTypeIdPointer &&
26354 array_type->data.pointer.ptr_len == PtrLenSingle) ? array_type->data.pointer.child_type : array_type;
26355
26356 ZigType *return_type;
26357
26358 // If start index and end index are both comptime known, then the result type is a pointer to array
26359 // not a slice. However, if the start or end index is a lazy value, and the result location is a slice,
26360 // then the pointer-to-array would be casted to a slice anyway. So, we preserve the laziness of these
26361 // values by making the return type a slice.
26362 ZigType *res_loc_type = get_result_loc_type(ira, instruction->result_loc);
26363 bool result_loc_is_slice = (res_loc_type != nullptr && is_slice(res_loc_type));
26364 bool end_is_known = !result_loc_is_slice &&
26365 ((end != nullptr && value_is_comptime(end->value)) ||
26366 (end == nullptr && child_array_type->id == ZigTypeIdArray));
26367
26368 ZigValue *array_sentinel = sentinel_val;
26369 if (end_is_known) {
26370 uint64_t end_scalar;
26371 if (end != nullptr) {
26372 ZigValue *end_val = ir_resolve_const(ira, end, UndefBad);
26373 if (!end_val)
26374 return ira->codegen->invalid_inst_gen;
26375 end_scalar = bigint_as_u64(&end_val->data.x_bigint);
26376 } else {
26377 end_scalar = child_array_type->data.array.len;
26378 }
26379 array_sentinel = (child_array_type->id == ZigTypeIdArray && end_scalar == child_array_type->data.array.len)
26380 ? child_array_type->data.array.sentinel : sentinel_val;
26381
26382 if (value_is_comptime(casted_start->value)) {
26383 ZigValue *start_val = ir_resolve_const(ira, casted_start, UndefBad);
26384 if (!start_val)
26385 return ira->codegen->invalid_inst_gen;
26386
26387 uint64_t start_scalar = bigint_as_u64(&start_val->data.x_bigint);
26388
26389 if (start_scalar > end_scalar) {
26390 ir_add_error(ira, &instruction->base.base, buf_sprintf("out of bounds slice"));
26391 return ira->codegen->invalid_inst_gen;
26392 }
26393
26394 uint32_t base_ptr_align = non_sentinel_slice_ptr_type->data.pointer.explicit_alignment;
26395 uint32_t ptr_byte_alignment = 0;
26396 if (end_scalar > start_scalar) {
26397 if ((err = compute_elem_align(ira, elem_type, base_ptr_align, start_scalar, &ptr_byte_alignment)))
26398 return ira->codegen->invalid_inst_gen;
26399 }
26400
26401 ZigType *return_array_type = get_array_type(ira->codegen, elem_type, end_scalar - start_scalar,
26402 array_sentinel);
26403 return_type = get_pointer_to_type_extra(ira->codegen, return_array_type,
26404 non_sentinel_slice_ptr_type->data.pointer.is_const,
26405 non_sentinel_slice_ptr_type->data.pointer.is_volatile,
26406 PtrLenSingle, ptr_byte_alignment, 0, 0, false);
26407 goto done_with_return_type;
26408 }
26409 } else if (array_sentinel == nullptr && end == nullptr) {
26410 array_sentinel = slice_sentinel_val;
26411 }
26412 if (array_sentinel != nullptr) {
26413 // TODO deal with non-abi-alignment here
26414 ZigType *slice_ptr_type = adjust_ptr_sentinel(ira->codegen, non_sentinel_slice_ptr_type, array_sentinel);
2622226415 return_type = get_slice_type(ira->codegen, slice_ptr_type);
2622326416 } else {
26417 // TODO deal with non-abi-alignment here
2622426418 return_type = get_slice_type(ira->codegen, non_sentinel_slice_ptr_type);
2622526419 }
26420done_with_return_type:
2622626421
2622726422 if (instr_is_comptime(ptr_ptr) &&
2622826423 value_is_comptime(casted_start->value) &&
......@@ -26233,12 +26428,8 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2623326428 size_t abs_offset;
2623426429 size_t rel_end;
2623526430 bool ptr_is_undef = false;
26236 if (array_type->id == ZigTypeIdArray ||
26237 (array_type->id == ZigTypeIdPointer && array_type->data.pointer.ptr_len == PtrLenSingle))
26238 {
26431 if (child_array_type->id == ZigTypeIdArray) {
2623926432 if (array_type->id == ZigTypeIdPointer) {
26240 ZigType *child_array_type = array_type->data.pointer.child_type;
26241 assert(child_array_type->id == ZigTypeIdArray);
2624226433 parent_ptr = const_ptr_pointee(ira, ira->codegen, ptr_ptr->value, instruction->base.base.source_node);
2624326434 if (parent_ptr == nullptr)
2624426435 return ira->codegen->invalid_inst_gen;
......@@ -26249,6 +26440,10 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2624926440 abs_offset = 0;
2625026441 rel_end = SIZE_MAX;
2625126442 ptr_is_undef = true;
26443 } else if (parent_ptr->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
26444 array_val = nullptr;
26445 abs_offset = 0;
26446 rel_end = SIZE_MAX;
2625226447 } else {
2625326448 array_val = const_ptr_pointee(ira, ira->codegen, parent_ptr, instruction->base.base.source_node);
2625426449 if (array_val == nullptr)
......@@ -26291,6 +26486,7 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2629126486 rel_end = 1;
2629226487 }
2629326488 break;
26489 case ConstPtrSpecialSubArray:
2629426490 case ConstPtrSpecialBaseArray:
2629526491 array_val = parent_ptr->data.x_ptr.data.base_array.array_val;
2629626492 abs_offset = parent_ptr->data.x_ptr.data.base_array.elem_index;
......@@ -26341,6 +26537,7 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2634126537 abs_offset = SIZE_MAX;
2634226538 rel_end = 1;
2634326539 break;
26540 case ConstPtrSpecialSubArray:
2634426541 case ConstPtrSpecialBaseArray:
2634526542 array_val = parent_ptr->data.x_ptr.data.base_array.array_val;
2634626543 abs_offset = parent_ptr->data.x_ptr.data.base_array.elem_index;
......@@ -26401,15 +26598,28 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2640126598 }
2640226599
2640326600 IrInstGen *result = ir_const(ira, &instruction->base.base, return_type);
26404 ZigValue *out_val = result->value;
26405 out_val->data.x_struct.fields = alloc_const_vals_ptrs(ira->codegen, 2);
2640626601
26407 ZigValue *ptr_val = out_val->data.x_struct.fields[slice_ptr_index];
26602 ZigValue *ptr_val;
26603 if (return_type->id == ZigTypeIdPointer) {
26604 // pointer to array
26605 ptr_val = result->value;
26606 } else {
26607 // slice
26608 result->value->data.x_struct.fields = alloc_const_vals_ptrs(ira->codegen, 2);
26609
26610 ptr_val = result->value->data.x_struct.fields[slice_ptr_index];
26611
26612 ZigValue *len_val = result->value->data.x_struct.fields[slice_len_index];
26613 init_const_usize(ira->codegen, len_val, end_scalar - start_scalar);
26614 }
2640826615
26616 bool return_type_is_const = non_sentinel_slice_ptr_type->data.pointer.is_const;
2640926617 if (array_val) {
2641026618 size_t index = abs_offset + start_scalar;
26411 bool is_const = slice_is_const(return_type);
26412 init_const_ptr_array(ira->codegen, ptr_val, array_val, index, is_const, PtrLenUnknown);
26619 init_const_ptr_array(ira->codegen, ptr_val, array_val, index, return_type_is_const, PtrLenUnknown);
26620 if (return_type->id == ZigTypeIdPointer) {
26621 ptr_val->data.x_ptr.special = ConstPtrSpecialSubArray;
26622 }
2641326623 if (array_type->id == ZigTypeIdArray) {
2641426624 ptr_val->data.x_ptr.mut = ptr_ptr->value->data.x_ptr.mut;
2641526625 } else if (is_slice(array_type)) {
......@@ -26419,16 +26629,17 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2641926629 }
2642026630 } else if (ptr_is_undef) {
2642126631 ptr_val->type = get_pointer_to_type(ira->codegen, parent_ptr->type->data.pointer.child_type,
26422 slice_is_const(return_type));
26632 return_type_is_const);
2642326633 ptr_val->special = ConstValSpecialUndef;
2642426634 } else switch (parent_ptr->data.x_ptr.special) {
2642526635 case ConstPtrSpecialInvalid:
2642626636 case ConstPtrSpecialDiscard:
2642726637 zig_unreachable();
2642826638 case ConstPtrSpecialRef:
26429 init_const_ptr_ref(ira->codegen, ptr_val,
26430 parent_ptr->data.x_ptr.data.ref.pointee, slice_is_const(return_type));
26639 init_const_ptr_ref(ira->codegen, ptr_val, parent_ptr->data.x_ptr.data.ref.pointee,
26640 return_type_is_const);
2643126641 break;
26642 case ConstPtrSpecialSubArray:
2643226643 case ConstPtrSpecialBaseArray:
2643326644 zig_unreachable();
2643426645 case ConstPtrSpecialBaseStruct:
......@@ -26443,7 +26654,7 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2644326654 init_const_ptr_hard_coded_addr(ira->codegen, ptr_val,
2644426655 parent_ptr->type->data.pointer.child_type,
2644526656 parent_ptr->data.x_ptr.data.hard_coded_addr.addr + start_scalar,
26446 slice_is_const(return_type));
26657 return_type_is_const);
2644726658 break;
2644826659 case ConstPtrSpecialFunction:
2644926660 zig_panic("TODO");
......@@ -26451,26 +26662,11 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2645126662 zig_panic("TODO");
2645226663 }
2645326664
26454 ZigValue *len_val = out_val->data.x_struct.fields[slice_len_index];
26455 init_const_usize(ira->codegen, len_val, end_scalar - start_scalar);
26456
26665 // In the case of pointer-to-array, we must restore this because above it overwrites ptr_val->type
26666 result->value->type = return_type;
2645726667 return result;
2645826668 }
2645926669
26460 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
26461 return_type, nullptr, true, true);
26462 if (result_loc != nullptr) {
26463 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
26464 return result_loc;
26465 }
26466 IrInstGen *dummy_value = ir_const(ira, &instruction->base.base, return_type);
26467 dummy_value->value->special = ConstValSpecialRuntime;
26468 IrInstGen *dummy_result = ir_implicit_cast2(ira, &instruction->base.base,
26469 dummy_value, result_loc->value->type->data.pointer.child_type);
26470 if (type_is_invalid(dummy_result->value->type))
26471 return ira->codegen->invalid_inst_gen;
26472 }
26473
2647426670 if (generate_non_null_assert) {
2647526671 IrInstGen *ptr_val = ir_get_deref(ira, &instruction->base.base, ptr_ptr, nullptr);
2647626672
......@@ -26480,8 +26676,26 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2648026676 ir_build_assert_non_null(ira, &instruction->base.base, ptr_val);
2648126677 }
2648226678
26679 IrInstGen *result_loc = nullptr;
26680
26681 if (return_type->id != ZigTypeIdPointer) {
26682 result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
26683 return_type, nullptr, true, true);
26684 if (result_loc != nullptr) {
26685 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
26686 return result_loc;
26687 }
26688 IrInstGen *dummy_value = ir_const(ira, &instruction->base.base, return_type);
26689 dummy_value->value->special = ConstValSpecialRuntime;
26690 IrInstGen *dummy_result = ir_implicit_cast2(ira, &instruction->base.base,
26691 dummy_value, result_loc->value->type->data.pointer.child_type);
26692 if (type_is_invalid(dummy_result->value->type))
26693 return ira->codegen->invalid_inst_gen;
26694 }
26695 }
26696
2648326697 return ir_build_slice_gen(ira, &instruction->base.base, return_type, ptr_ptr,
26484 casted_start, end, instruction->safety_check_on, result_loc);
26698 casted_start, end, instruction->safety_check_on, result_loc, sentinel_val);
2648526699}
2648626700
2648726701static IrInstGen *ir_analyze_instruction_has_field(IrAnalyze *ira, IrInstSrcHasField *instruction) {
......@@ -27507,10 +27721,18 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
2750727721 // We have a check for zero bits later so we use get_src_ptr_type to
2750827722 // validate src_type and dest_type.
2750927723
27510 ZigType *src_ptr_type = get_src_ptr_type(src_type);
27511 if (src_ptr_type == nullptr) {
27512 ir_add_error(ira, ptr_src, buf_sprintf("expected pointer, found '%s'", buf_ptr(&src_type->name)));
27513 return ira->codegen->invalid_inst_gen;
27724 ZigType *if_slice_ptr_type;
27725 if (is_slice(src_type)) {
27726 TypeStructField *ptr_field = src_type->data.structure.fields[slice_ptr_index];
27727 if_slice_ptr_type = resolve_struct_field_type(ira->codegen, ptr_field);
27728 } else {
27729 if_slice_ptr_type = src_type;
27730
27731 ZigType *src_ptr_type = get_src_ptr_type(src_type);
27732 if (src_ptr_type == nullptr) {
27733 ir_add_error(ira, ptr_src, buf_sprintf("expected pointer, found '%s'", buf_ptr(&src_type->name)));
27734 return ira->codegen->invalid_inst_gen;
27735 }
2751427736 }
2751527737
2751627738 ZigType *dest_ptr_type = get_src_ptr_type(dest_type);
......@@ -27520,7 +27742,7 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
2752027742 return ira->codegen->invalid_inst_gen;
2752127743 }
2752227744
27523 if (get_ptr_const(src_type) && !get_ptr_const(dest_type)) {
27745 if (get_ptr_const(ira->codegen, src_type) && !get_ptr_const(ira->codegen, dest_type)) {
2752427746 ir_add_error(ira, source_instr, buf_sprintf("cast discards const qualifier"));
2752527747 return ira->codegen->invalid_inst_gen;
2752627748 }
......@@ -27538,7 +27760,10 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
2753827760 if ((err = type_resolve(ira->codegen, src_type, ResolveStatusZeroBitsKnown)))
2753927761 return ira->codegen->invalid_inst_gen;
2754027762
27541 if (type_has_bits(ira->codegen, dest_type) && !type_has_bits(ira->codegen, src_type) && safety_check_on) {
27763 if (safety_check_on &&
27764 type_has_bits(ira->codegen, dest_type) &&
27765 !type_has_bits(ira->codegen, if_slice_ptr_type))
27766 {
2754227767 ErrorMsg *msg = ir_add_error(ira, source_instr,
2754327768 buf_sprintf("'%s' and '%s' do not have the same in-memory representation",
2754427769 buf_ptr(&src_type->name), buf_ptr(&dest_type->name)));
......@@ -27549,6 +27774,14 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
2754927774 return ira->codegen->invalid_inst_gen;
2755027775 }
2755127776
27777 // For slices, follow the `ptr` field.
27778 if (is_slice(src_type)) {
27779 TypeStructField *ptr_field = src_type->data.structure.fields[slice_ptr_index];
27780 IrInstGen *ptr_ref = ir_get_ref(ira, source_instr, ptr, true, false);
27781 IrInstGen *ptr_ptr = ir_analyze_struct_field_ptr(ira, source_instr, ptr_field, ptr_ref, src_type, false);
27782 ptr = ir_get_deref(ira, source_instr, ptr_ptr, nullptr);
27783 }
27784
2755227785 if (instr_is_comptime(ptr)) {
2755327786 bool dest_allows_addr_zero = ptr_allows_addr_zero(dest_type);
2755427787 UndefAllowed is_undef_allowed = dest_allows_addr_zero ? UndefOk : UndefBad;
test/compare_output.zig+1-1
......@@ -292,7 +292,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
292292 \\pub export fn main() c_int {
293293 \\ var array = [_]u32{ 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };
294294 \\
295 \\ c.qsort(@ptrCast(?*c_void, array[0..].ptr), @intCast(c_ulong, array.len), @sizeOf(i32), compare_fn);
295 \\ c.qsort(@ptrCast(?*c_void, &array), @intCast(c_ulong, array.len), @sizeOf(i32), compare_fn);
296296 \\
297297 \\ for (array) |item, i| {
298298 \\ if (item != i) {
test/compile_errors.zig+2-14
......@@ -103,18 +103,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
103103 "tmp.zig:3:23: error: pointer to size 0 type has no address",
104104 });
105105
106 cases.addTest("slice to pointer conversion mismatch",
107 \\pub fn bytesAsSlice(bytes: var) [*]align(1) const u16 {
108 \\ return @ptrCast([*]align(1) const u16, bytes.ptr)[0..1];
109 \\}
110 \\test "bytesAsSlice" {
111 \\ const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF };
112 \\ const slice = bytesAsSlice(bytes[0..]);
113 \\}
114 , &[_][]const u8{
115 "tmp.zig:2:54: error: expected type '[*]align(1) const u16', found '[]align(1) const u16'",
116 });
117
118106 cases.addTest("access invalid @typeInfo decl",
119107 \\const A = B;
120108 \\test "Crash" {
......@@ -1918,8 +1906,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
19181906 cases.add("reading past end of pointer casted array",
19191907 \\comptime {
19201908 \\ const array: [4]u8 = "aoeu".*;
1921 \\ const slice = array[1..];
1922 \\ const int_ptr = @ptrCast(*const u24, slice.ptr);
1909 \\ const sub_array = array[1..];
1910 \\ const int_ptr = @ptrCast(*const u24, sub_array);
19231911 \\ const deref = int_ptr.*;
19241912 \\}
19251913 , &[_][]const u8{
test/runtime_safety.zig+1-1
......@@ -69,7 +69,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
6969 \\}
7070 \\pub fn main() void {
7171 \\ var buf: [4]u8 = undefined;
72 \\ const ptr = buf[0..].ptr;
72 \\ const ptr: [*]u8 = &buf;
7373 \\ const slice = ptr[0..3 :0];
7474 \\}
7575 );
test/stage1/behavior/align.zig+22-14
......@@ -5,10 +5,17 @@ const builtin = @import("builtin");
55var foo: u8 align(4) = 100;
66
77test "global variable alignment" {
8 expect(@TypeOf(&foo).alignment == 4);
9 expect(@TypeOf(&foo) == *align(4) u8);
10 const slice = @as(*[1]u8, &foo)[0..];
11 expect(@TypeOf(slice) == []align(4) u8);
8 comptime expect(@TypeOf(&foo).alignment == 4);
9 comptime expect(@TypeOf(&foo) == *align(4) u8);
10 {
11 const slice = @as(*[1]u8, &foo)[0..];
12 comptime expect(@TypeOf(slice) == *align(4) [1]u8);
13 }
14 {
15 var runtime_zero: usize = 0;
16 const slice = @as(*[1]u8, &foo)[runtime_zero..];
17 comptime expect(@TypeOf(slice) == []align(4) u8);
18 }
1219}
1320
1421fn derp() align(@sizeOf(usize) * 2) i32 {
......@@ -171,18 +178,19 @@ test "runtime known array index has best alignment possible" {
171178
172179 // because pointer is align 2 and u32 align % 2 == 0 we can assume align 2
173180 var smaller align(2) = [_]u32{ 1, 2, 3, 4 };
174 comptime expect(@TypeOf(smaller[0..]) == []align(2) u32);
175 comptime expect(@TypeOf(smaller[0..].ptr) == [*]align(2) u32);
176 testIndex(smaller[0..].ptr, 0, *align(2) u32);
177 testIndex(smaller[0..].ptr, 1, *align(2) u32);
178 testIndex(smaller[0..].ptr, 2, *align(2) u32);
179 testIndex(smaller[0..].ptr, 3, *align(2) u32);
181 var runtime_zero: usize = 0;
182 comptime expect(@TypeOf(smaller[runtime_zero..]) == []align(2) u32);
183 comptime expect(@TypeOf(smaller[runtime_zero..].ptr) == [*]align(2) u32);
184 testIndex(smaller[runtime_zero..].ptr, 0, *align(2) u32);
185 testIndex(smaller[runtime_zero..].ptr, 1, *align(2) u32);
186 testIndex(smaller[runtime_zero..].ptr, 2, *align(2) u32);
187 testIndex(smaller[runtime_zero..].ptr, 3, *align(2) u32);
180188
181189 // has to use ABI alignment because index known at runtime only
182 testIndex2(array[0..].ptr, 0, *u8);
183 testIndex2(array[0..].ptr, 1, *u8);
184 testIndex2(array[0..].ptr, 2, *u8);
185 testIndex2(array[0..].ptr, 3, *u8);
190 testIndex2(array[runtime_zero..].ptr, 0, *u8);
191 testIndex2(array[runtime_zero..].ptr, 1, *u8);
192 testIndex2(array[runtime_zero..].ptr, 2, *u8);
193 testIndex2(array[runtime_zero..].ptr, 3, *u8);
186194}
187195fn testIndex(smaller: [*]align(2) u32, index: usize, comptime T: type) void {
188196 comptime expect(@TypeOf(&smaller[index]) == T);
test/stage1/behavior/cast.zig+2-1
......@@ -435,7 +435,8 @@ fn incrementVoidPtrValue(value: ?*c_void) void {
435435
436436test "implicit cast from [*]T to ?*c_void" {
437437 var a = [_]u8{ 3, 2, 1 };
438 incrementVoidPtrArray(a[0..].ptr, 3);
438 var runtime_zero: usize = 0;
439 incrementVoidPtrArray(a[runtime_zero..].ptr, 3);
439440 expect(std.mem.eql(u8, &a, &[_]u8{ 4, 3, 2 }));
440441}
441442
test/stage1/behavior/eval.zig+1-1
......@@ -524,7 +524,7 @@ test "comptime slice of slice preserves comptime var" {
524524test "comptime slice of pointer preserves comptime var" {
525525 comptime {
526526 var buff: [10]u8 = undefined;
527 var a = buff[0..].ptr;
527 var a = @ptrCast([*]u8, &buff);
528528 a[0..1][0] = 1;
529529 expect(buff[0..][0..][0] == 1);
530530 }
test/stage1/behavior/misc.zig+9-5
......@@ -102,8 +102,8 @@ test "memcpy and memset intrinsics" {
102102 var foo: [20]u8 = undefined;
103103 var bar: [20]u8 = undefined;
104104
105 @memset(foo[0..].ptr, 'A', foo.len);
106 @memcpy(bar[0..].ptr, foo[0..].ptr, bar.len);
105 @memset(&foo, 'A', foo.len);
106 @memcpy(&bar, &foo, bar.len);
107107
108108 if (bar[11] != 'A') unreachable;
109109}
......@@ -565,12 +565,16 @@ test "volatile load and store" {
565565 expect(ptr.* == 1235);
566566}
567567
568test "slice string literal has type []const u8" {
568test "slice string literal has correct type" {
569569 comptime {
570 expect(@TypeOf("aoeu"[0..]) == []const u8);
570 expect(@TypeOf("aoeu"[0..]) == *const [4:0]u8);
571571 const array = [_]i32{ 1, 2, 3, 4 };
572 expect(@TypeOf(array[0..]) == []const i32);
572 expect(@TypeOf(array[0..]) == *const [4]i32);
573573 }
574 var runtime_zero: usize = 0;
575 comptime expect(@TypeOf("aoeu"[runtime_zero..]) == [:0]const u8);
576 const array = [_]i32{ 1, 2, 3, 4 };
577 comptime expect(@TypeOf(array[runtime_zero..]) == []const i32);
574578}
575579
576580test "pointer child field" {
test/stage1/behavior/pointers.zig+5-4
......@@ -159,12 +159,13 @@ test "allowzero pointer and slice" {
159159 var opt_ptr: ?[*]allowzero i32 = ptr;
160160 expect(opt_ptr != null);
161161 expect(@ptrToInt(ptr) == 0);
162 var slice = ptr[0..10];
163 expect(@TypeOf(slice) == []allowzero i32);
162 var runtime_zero: usize = 0;
163 var slice = ptr[runtime_zero..10];
164 comptime expect(@TypeOf(slice) == []allowzero i32);
164165 expect(@ptrToInt(&slice[5]) == 20);
165166
166 expect(@typeInfo(@TypeOf(ptr)).Pointer.is_allowzero);
167 expect(@typeInfo(@TypeOf(slice)).Pointer.is_allowzero);
167 comptime expect(@typeInfo(@TypeOf(ptr)).Pointer.is_allowzero);
168 comptime expect(@typeInfo(@TypeOf(slice)).Pointer.is_allowzero);
168169}
169170
170171test "assign null directly to C pointer and test null equality" {
test/stage1/behavior/ptrcast.zig+1-1
......@@ -13,7 +13,7 @@ fn testReinterpretBytesAsInteger() void {
1313 builtin.Endian.Little => 0xab785634,
1414 builtin.Endian.Big => 0x345678ab,
1515 };
16 expect(@ptrCast(*align(1) const u32, bytes[1..5].ptr).* == expected);
16 expect(@ptrCast(*align(1) const u32, bytes[1..5]).* == expected);
1717}
1818
1919test "reinterpret bytes of an array into an extern struct" {
test/stage1/behavior/slice.zig+155-4
......@@ -7,10 +7,10 @@ const mem = std.mem;
77const x = @intToPtr([*]i32, 0x1000)[0..0x500];
88const y = x[0x100..];
99test "compile time slice of pointer to hard coded address" {
10 expect(@ptrToInt(x.ptr) == 0x1000);
10 expect(@ptrToInt(x) == 0x1000);
1111 expect(x.len == 0x500);
1212
13 expect(@ptrToInt(y.ptr) == 0x1100);
13 expect(@ptrToInt(y) == 0x1100);
1414 expect(y.len == 0x400);
1515}
1616
......@@ -47,7 +47,9 @@ test "C pointer slice access" {
4747 var buf: [10]u32 = [1]u32{42} ** 10;
4848 const c_ptr = @ptrCast([*c]const u32, &buf);
4949
50 comptime expectEqual([]const u32, @TypeOf(c_ptr[0..1]));
50 var runtime_zero: usize = 0;
51 comptime expectEqual([]const u32, @TypeOf(c_ptr[runtime_zero..1]));
52 comptime expectEqual(*const [1]u32, @TypeOf(c_ptr[0..1]));
5153
5254 for (c_ptr[0..5]) |*cl| {
5355 expectEqual(@as(u32, 42), cl.*);
......@@ -107,7 +109,9 @@ test "obtaining a null terminated slice" {
107109 const ptr2 = buf[0..runtime_len :0];
108110 // ptr2 is a null-terminated slice
109111 comptime expect(@TypeOf(ptr2) == [:0]u8);
110 comptime expect(@TypeOf(ptr2[0..2]) == []u8);
112 comptime expect(@TypeOf(ptr2[0..2]) == *[2]u8);
113 var runtime_zero: usize = 0;
114 comptime expect(@TypeOf(ptr2[runtime_zero..2]) == []u8);
111115}
112116
113117test "empty array to slice" {
......@@ -126,3 +130,150 @@ test "empty array to slice" {
126130 S.doTheTest();
127131 comptime S.doTheTest();
128132}
133
134test "@ptrCast slice to pointer" {
135 const S = struct {
136 fn doTheTest() void {
137 var array align(@alignOf(u16)) = [5]u8{ 0xff, 0xff, 0xff, 0xff, 0xff };
138 var slice: []u8 = &array;
139 var ptr = @ptrCast(*u16, slice);
140 expect(ptr.* == 65535);
141 }
142 };
143
144 S.doTheTest();
145 comptime S.doTheTest();
146}
147
148test "slice syntax resulting in pointer-to-array" {
149 const S = struct {
150 fn doTheTest() void {
151 testArray();
152 testArrayZ();
153 testArray0();
154 testArrayAlign();
155 testPointer();
156 testPointerZ();
157 testPointer0();
158 testPointerAlign();
159 testSlice();
160 testSliceZ();
161 testSlice0();
162 testSliceAlign();
163 }
164
165 fn testArray() void {
166 var array = [5]u8{ 1, 2, 3, 4, 5 };
167 var slice = array[1..3];
168 comptime expect(@TypeOf(slice) == *[2]u8);
169 expect(slice[0] == 2);
170 expect(slice[1] == 3);
171 }
172
173 fn testArrayZ() void {
174 var array = [5:0]u8{ 1, 2, 3, 4, 5 };
175 comptime expect(@TypeOf(array[1..3]) == *[2]u8);
176 comptime expect(@TypeOf(array[1..5]) == *[4:0]u8);
177 comptime expect(@TypeOf(array[1..]) == *[4:0]u8);
178 comptime expect(@TypeOf(array[1..3 :4]) == *[2:4]u8);
179 }
180
181 fn testArray0() void {
182 {
183 var array = [0]u8{};
184 var slice = array[0..0];
185 comptime expect(@TypeOf(slice) == *[0]u8);
186 }
187 {
188 var array = [0:0]u8{};
189 var slice = array[0..0];
190 comptime expect(@TypeOf(slice) == *[0:0]u8);
191 expect(slice[0] == 0);
192 }
193 }
194
195 fn testArrayAlign() void {
196 var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };
197 var slice = array[4..5];
198 comptime expect(@TypeOf(slice) == *align(4) [1]u8);
199 expect(slice[0] == 5);
200 comptime expect(@TypeOf(array[0..2]) == *align(4) [2]u8);
201 }
202
203 fn testPointer() void {
204 var array = [5]u8{ 1, 2, 3, 4, 5 };
205 var pointer: [*]u8 = &array;
206 var slice = pointer[1..3];
207 comptime expect(@TypeOf(slice) == *[2]u8);
208 expect(slice[0] == 2);
209 expect(slice[1] == 3);
210 }
211
212 fn testPointerZ() void {
213 var array = [5:0]u8{ 1, 2, 3, 4, 5 };
214 var pointer: [*:0]u8 = &array;
215 comptime expect(@TypeOf(pointer[1..3]) == *[2]u8);
216 comptime expect(@TypeOf(pointer[1..3 :4]) == *[2:4]u8);
217 }
218
219 fn testPointer0() void {
220 var pointer: [*]u0 = &[1]u0{0};
221 var slice = pointer[0..1];
222 comptime expect(@TypeOf(slice) == *[1]u0);
223 expect(slice[0] == 0);
224 }
225
226 fn testPointerAlign() void {
227 var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };
228 var pointer: [*]align(4) u8 = &array;
229 var slice = pointer[4..5];
230 comptime expect(@TypeOf(slice) == *align(4) [1]u8);
231 expect(slice[0] == 5);
232 comptime expect(@TypeOf(pointer[0..2]) == *align(4) [2]u8);
233 }
234
235 fn testSlice() void {
236 var array = [5]u8{ 1, 2, 3, 4, 5 };
237 var src_slice: []u8 = &array;
238 var slice = src_slice[1..3];
239 comptime expect(@TypeOf(slice) == *[2]u8);
240 expect(slice[0] == 2);
241 expect(slice[1] == 3);
242 }
243
244 fn testSliceZ() void {
245 var array = [5:0]u8{ 1, 2, 3, 4, 5 };
246 var slice: [:0]u8 = &array;
247 comptime expect(@TypeOf(slice[1..3]) == *[2]u8);
248 comptime expect(@TypeOf(slice[1..]) == [:0]u8);
249 comptime expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8);
250 }
251
252 fn testSlice0() void {
253 {
254 var array = [0]u8{};
255 var src_slice: []u8 = &array;
256 var slice = src_slice[0..0];
257 comptime expect(@TypeOf(slice) == *[0]u8);
258 }
259 {
260 var array = [0:0]u8{};
261 var src_slice: [:0]u8 = &array;
262 var slice = src_slice[0..0];
263 comptime expect(@TypeOf(slice) == *[0]u8);
264 }
265 }
266
267 fn testSliceAlign() void {
268 var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };
269 var src_slice: []align(4) u8 = &array;
270 var slice = src_slice[4..5];
271 comptime expect(@TypeOf(slice) == *align(4) [1]u8);
272 expect(slice[0] == 5);
273 comptime expect(@TypeOf(src_slice[0..2]) == *align(4) [2]u8);
274 }
275 };
276
277 S.doTheTest();
278 comptime S.doTheTest();
279}
test/stage1/behavior/struct.zig+2-2
......@@ -409,8 +409,8 @@ const Bitfields = packed struct {
409409test "native bit field understands endianness" {
410410 var all: u64 = 0x7765443322221111;
411411 var bytes: [8]u8 = undefined;
412 @memcpy(bytes[0..].ptr, @ptrCast([*]u8, &all), 8);
413 var bitfields = @ptrCast(*Bitfields, bytes[0..].ptr).*;
412 @memcpy(&bytes, @ptrCast([*]u8, &all), 8);
413 var bitfields = @ptrCast(*Bitfields, &bytes).*;
414414
415415 expect(bitfields.f1 == 0x1111);
416416 expect(bitfields.f2 == 0x2222);