authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-18 20:35:19-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-19 09:53:55-04:00
log7fa88cc0a678a3690b61054030f5597b623964a4
treea1cdaabfbbe0fe8b240691a9b1921245973f5107
parentb5dba702fff35c2d9aa86c9d5dd93a4a38d3b75b
signaturelock-open Commit is signed but in an unrecognized format.

std lib fixups for new semantics

std lib tests are passing now

8 files changed, 47 insertions(+), 64 deletions(-)

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
......@@ -360,7 +360,7 @@ pub const Dir = struct {
360360 if (self.index >= self.end_index) {
361361 const rc = os.system.getdirentries(
362362 self.dir.fd,
363 self.buf[0..].ptr,
363 &self.buf,
364364 self.buf.len,
365365 &self.seek,
366366 );
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/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+7-40
......@@ -1586,24 +1586,24 @@ pub fn nativeToBig(comptime T: type, x: T) T {
15861586}
15871587
15881588fn AsBytesReturnType(comptime P: type) type {
1589 if (comptime !trait.isSingleItemPtr(P))
1589 if (!trait.isSingleItemPtr(P))
15901590 @compileError("expected single item pointer, passed " ++ @typeName(P));
15911591
1592 const size = @as(usize, @sizeOf(meta.Child(P)));
1593 const alignment = comptime meta.alignment(P);
1592 const size = @sizeOf(meta.Child(P));
1593 const alignment = meta.alignment(P);
15941594
15951595 if (alignment == 0) {
1596 if (comptime trait.isConstPtr(P))
1596 if (trait.isConstPtr(P))
15971597 return *const [size]u8;
15981598 return *[size]u8;
15991599 }
16001600
1601 if (comptime trait.isConstPtr(P))
1601 if (trait.isConstPtr(P))
16021602 return *align(alignment) const [size]u8;
16031603 return *align(alignment) [size]u8;
16041604}
16051605
1606///Given a pointer to a single item, returns a slice of the underlying bytes, preserving constness.
1606/// Given a pointer to a single item, returns a slice of the underlying bytes, preserving constness.
16071607pub fn asBytes(ptr: var) AsBytesReturnType(@TypeOf(ptr)) {
16081608 const P = @TypeOf(ptr);
16091609 return @ptrCast(AsBytesReturnType(P), ptr);
......@@ -1841,7 +1841,7 @@ pub fn sliceAsBytes(slice: var) SliceAsBytesReturnType(@TypeOf(slice)) {
18411841
18421842 const cast_target = if (comptime trait.isConstPtr(Slice)) [*]align(alignment) const u8 else [*]align(alignment) u8;
18431843
1844 return @ptrCast(cast_target, slice)[0 .. slice.len * @sizeOf(meta.Child(Slice))];
1844 return @ptrCast(cast_target, slice)[0 .. slice.len * @sizeOf(meta.Elem(Slice))];
18451845}
18461846
18471847test "sliceAsBytes" {
......@@ -1911,39 +1911,6 @@ test "sliceAsBytes and bytesAsSlice back" {
19111911 testing.expect(bytes[11] == math.maxInt(u8));
19121912}
19131913
1914fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type {
1915 if (trait.isConstPtr(T))
1916 return *const [length]meta.Child(meta.Child(T));
1917 return *[length]meta.Child(meta.Child(T));
1918}
1919
1920/// Given a pointer to an array, returns a pointer to a portion of that array, preserving constness.
1921/// TODO this will be obsoleted by https://github.com/ziglang/zig/issues/863
1922pub fn subArrayPtr(
1923 ptr: var,
1924 comptime start: usize,
1925 comptime length: usize,
1926) SubArrayPtrReturnType(@TypeOf(ptr), length) {
1927 assert(start + length <= ptr.*.len);
1928
1929 const ReturnType = SubArrayPtrReturnType(@TypeOf(ptr), length);
1930 const T = meta.Child(meta.Child(@TypeOf(ptr)));
1931 return @ptrCast(ReturnType, &ptr[start]);
1932}
1933
1934test "subArrayPtr" {
1935 const a1: [6]u8 = "abcdef".*;
1936 const sub1 = subArrayPtr(&a1, 2, 3);
1937 testing.expect(eql(u8, sub1, "cde"));
1938
1939 var a2: [6]u8 = "abcdef".*;
1940 var sub2 = subArrayPtr(&a2, 2, 3);
1941
1942 testing.expect(eql(u8, sub2, "cde"));
1943 sub2[1] = 'X';
1944 testing.expect(eql(u8, &a2, "abcXef"));
1945}
1946
19471914/// Round an address up to the nearest aligned address
19481915/// The alignment must be a power of 2 and greater than 0.
19491916pub fn alignForward(addr: usize, alignment: usize) usize {
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+5-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,8 @@ fn linuxLookupNameFromDns(
958957 }
959958 }
960959
961 var ap = [2][]u8{ apbuf[0][0..0], apbuf[1][0..0] };
960 var hack: usize = 0; // TODO remove this hack
961 var ap = [2][]u8{ apbuf[0][0..hack], apbuf[1][0..hack] };
962962 try resMSendRc(qp[0..nq], ap[0..nq], apbuf[0..nq], rc);
963963
964964 var i: usize = 0;
......@@ -1015,7 +1015,7 @@ fn getResolvConf(allocator: *mem.Allocator, rc: *ResolvConf) !void {
10151015 // Skip to the delimiter in the stream, to fix parsing
10161016 try stream.skipUntilDelimiterOrEof('\n');
10171017 // Give an empty line to the while loop, which will be skipped.
1018 break :blk line_buf[0..0];
1018 break :blk @as([]u8, line_buf[0..0]); // TODO the cast should not be necessary
10191019 },
10201020 else => |e| return e,
10211021 }) |line| {
src/ir.cpp+1-1
......@@ -14633,7 +14633,7 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,
1463314633 return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type);
1463414634 }
1463514635
14636 // *[N]T to ?[]const T
14636 // *[N]T to ?[]T
1463714637 if (wanted_type->id == ZigTypeIdOptional &&
1463814638 is_slice(wanted_type->data.maybe.child_type) &&
1463914639 actual_type->id == ZigTypeIdPointer &&