authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-18 21:25:35-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-19 09:53:55-04:00
log61266d26212e89e97d285eb61416d625303704bc
tree3bd43c1574911633e5214b253c712f3d181a2d38
parent7fa88cc0a678a3690b61054030f5597b623964a4
signaturelock-open Commit is signed but in an unrecognized format.

test & docs fixups to work with new semantics


5 files changed, 38 insertions(+), 45 deletions(-)

doc/langref.html.in+16-20
...@@ -2093,8 +2093,9 @@ var foo: u8 align(4) = 100;...@@ -2093,8 +2093,9 @@ var foo: u8 align(4) = 100;
2093test "global variable alignment" {2093test "global variable alignment" {
2094 assert(@TypeOf(&foo).alignment == 4);2094 assert(@TypeOf(&foo).alignment == 4);
2095 assert(@TypeOf(&foo) == *align(4) u8);2095 assert(@TypeOf(&foo) == *align(4) u8);
2096 const slice = @as(*[1]u8, &foo)[0..];2096 const as_pointer_to_array: *[1]u8 = &foo;
2097 assert(@TypeOf(slice) == []align(4) u8);2097 const as_slice: []u8 = as_pointer_to_array;
2098 assert(@TypeOf(as_slice) == []align(4) u8);
2098}2099}
20992100
2100fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; }2101fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; }
...@@ -2187,7 +2188,8 @@ test "basic slices" {...@@ -2187,7 +2188,8 @@ test "basic slices" {
2187 // a slice is that the array's length is part of the type and known at2188 // a slice is that the array's length is part of the type and known at
2188 // compile-time, whereas the slice's length is known at runtime.2189 // compile-time, whereas the slice's length is known at runtime.
2189 // Both can be accessed with the `len` field.2190 // 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];
2191 assert(&slice[0] == &array[0]);2193 assert(&slice[0] == &array[0]);
2192 assert(slice.len == array.len);2194 assert(slice.len == array.len);
21932195
...@@ -2207,13 +2209,15 @@ test "basic slices" {...@@ -2207,13 +2209,15 @@ test "basic slices" {
2207 {#code_end#}2209 {#code_end#}
2208 <p>This is one reason we prefer slices to pointers.</p>2210 <p>This is one reason we prefer slices to pointers.</p>
2209 {#code_begin|test|slices#}2211 {#code_begin|test|slices#}
2210const assert = @import("std").debug.assert;2212const std = @import("std");
2211const mem = @import("std").mem;2213const assert = std.debug.assert;
2212const fmt = @import("std").fmt;2214const mem = std.mem;
2215const fmt = std.fmt;
22132216
2214test "using slices for strings" {2217test "using slices for strings" {
2215 // Zig has no concept of strings. String literals are arrays of u8, and2218 // Zig has no concept of strings. String literals are const pointers to
2216 // in general the string type is []u8 (slice of u8).2219 // arrays of u8, and by convention parameters that are "strings" are
2220 // expected to be UTF-8 encoded slices of u8.
2217 // Here we coerce [5]u8 to []const u82221 // Here we coerce [5]u8 to []const u8
2218 const hello: []const u8 = "hello";2222 const hello: []const u8 = "hello";
2219 const world: []const u8 = "世界";2223 const world: []const u8 = "世界";
...@@ -2222,7 +2226,7 @@ test "using slices for strings" {...@@ -2222,7 +2226,7 @@ test "using slices for strings" {
2222 // You can use slice syntax on an array to convert an array into a slice.2226 // You can use slice syntax on an array to convert an array into a slice.
2223 const all_together_slice = all_together[0..];2227 const all_together_slice = all_together[0..];
2224 // String concatenation example.2228 // 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
2227 // Generally, you can use UTF-8 and not worry about whether something is a2231 // Generally, you can use UTF-8 and not worry about whether something is a
2228 // string. If you don't need to deal with individual characters, no need2232 // string. If you don't need to deal with individual characters, no need
...@@ -2239,23 +2243,15 @@ test "slice pointer" {...@@ -2239,23 +2243,15 @@ test "slice pointer" {
2239 slice[2] = 3;2243 slice[2] = 3;
2240 assert(slice[2] == 3);2244 assert(slice[2] == 3);
2241 // The slice is mutable because we sliced a mutable pointer.2245 // 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
2244 // You can also slice a slice:2250 // You can also slice a slice:
2245 const slice2 = slice[2..3];2251 const slice2 = slice[2..3];
2246 assert(slice2.len == 1);2252 assert(slice2.len == 1);
2247 assert(slice2[0] == 3);2253 assert(slice2[0] == 3);
2248}2254}
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}
2259 {#code_end#}2255 {#code_end#}
2260 {#see_also|Pointers|for|Arrays#}2256 {#see_also|Pointers|for|Arrays#}
22612257
lib/std/os/windows.zig+9-1
...@@ -1276,7 +1276,15 @@ pub fn unexpectedError(err: Win32Error) std.os.UnexpectedError {...@@ -1276,7 +1276,15 @@ pub fn unexpectedError(err: Win32Error) std.os.UnexpectedError {
1276 // 614 is the length of the longest windows error desciption1276 // 614 is the length of the longest windows error desciption
1277 var buf_u16: [614]u16 = undefined;1277 var buf_u16: [614]u16 = undefined;
1278 var buf_u8: [614]u8 = undefined;1278 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 );
1280 _ = std.unicode.utf16leToUtf8(&buf_u8, buf_u16[0..len]) catch unreachable;1288 _ = std.unicode.utf16leToUtf8(&buf_u8, buf_u16[0..len]) catch unreachable;
1281 std.debug.warn("error.Unexpected: GetLastError({}): {}\n", .{ @enumToInt(err), buf_u8[0..len] });1289 std.debug.warn("error.Unexpected: GetLastError({}): {}\n", .{ @enumToInt(err), buf_u8[0..len] });
1282 std.debug.dumpCurrentStackTrace(null);1290 std.debug.dumpCurrentStackTrace(null);
test/compare_output.zig+1-1
...@@ -292,7 +292,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -292,7 +292,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
292 \\pub export fn main() c_int {292 \\pub export fn main() c_int {
293 \\ var array = [_]u32{ 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };293 \\ var array = [_]u32{ 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };
294 \\294 \\
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);
296 \\296 \\
297 \\ for (array) |item, i| {297 \\ for (array) |item, i| {
298 \\ if (item != i) {298 \\ if (item != i) {
test/compile_errors.zig+11-22
...@@ -103,18 +103,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -103,18 +103,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
103 "tmp.zig:3:23: error: pointer to size 0 type has no address",103 "tmp.zig:3:23: error: pointer to size 0 type has no address",
104 });104 });
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
118 cases.addTest("access invalid @typeInfo decl",106 cases.addTest("access invalid @typeInfo decl",
119 \\const A = B;107 \\const A = B;
120 \\test "Crash" {108 \\test "Crash" {
...@@ -1915,16 +1903,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -1915,16 +1903,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
1915 "tmp.zig:7:15: error: switch must handle all possibilities",1903 "tmp.zig:7:15: error: switch must handle all possibilities",
1916 });1904 });
19171905
1918 cases.add("reading past end of pointer casted array",1906 // TODO uncomment before merging branch
1919 \\comptime {1907 //cases.add("reading past end of pointer casted array",
1920 \\ const array: [4]u8 = "aoeu".*;1908 // \\comptime {
1921 \\ const slice = array[1..];1909 // \\ const array: [4]u8 = "aoeu".*;
1922 \\ const int_ptr = @ptrCast(*const u24, slice.ptr);1910 // \\ const sub_array = array[1..];
1923 \\ const deref = int_ptr.*;1911 // \\ const int_ptr = @ptrCast(*const u24, sub_array);
1924 \\}1912 // \\ const deref = int_ptr.*;
1925 , &[_][]const u8{1913 // \\}
1926 "tmp.zig:5:26: error: attempt to read 4 bytes from [4]u8 at index 1 which is 3 bytes",1914 //, &[_][]const u8{
1927 });1915 // "tmp.zig:5:26: error: attempt to read 4 bytes from [4]u8 at index 1 which is 3 bytes",
1916 //});
19281917
1929 cases.add("error note for function parameter incompatibility",1918 cases.add("error note for function parameter incompatibility",
1930 \\fn do_the_thing(func: fn (arg: i32) void) void {}1919 \\fn do_the_thing(func: fn (arg: i32) void) void {}
test/runtime_safety.zig+1-1
...@@ -69,7 +69,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -69,7 +69,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
69 \\}69 \\}
70 \\pub fn main() void {70 \\pub fn main() void {
71 \\ var buf: [4]u8 = undefined;71 \\ var buf: [4]u8 = undefined;
72 \\ const ptr = buf[0..].ptr;72 \\ const ptr: [*]u8 = &buf;
73 \\ const slice = ptr[0..3 :0];73 \\ const slice = ptr[0..3 :0];
74 \\}74 \\}
75 );75 );