Skip to content

Commit 28606cd

Browse files
committed
test & docs fixups to work with new semantics
1 parent be7f98d commit 28606cd

File tree

5 files changed

+38
-45
lines changed

5 files changed

+38
-45
lines changed

doc/langref.html.in

+16-20
Original file line numberDiff line numberDiff line change
@@ -2093,8 +2093,9 @@ var foo: u8 align(4) = 100;
20932093
test "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

21002101
fn 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#}
2210-
const assert = @import("std").debug.assert;
2211-
const mem = @import("std").mem;
2212-
const fmt = @import("std").fmt;
2212+
const std = @import("std");
2213+
const assert = std.debug.assert;
2214+
const mem = std.mem;
2215+
const fmt = std.fmt;
22132216

22142217
test "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-
2250-
test "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/os/windows.zig

+9-1
Original file line numberDiff line numberDiff line change
@@ -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);

test/compare_output.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -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

+11-22
Original file line numberDiff line numberDiff line change
@@ -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" {
@@ -1915,16 +1903,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
19151903
"tmp.zig:7:15: error: switch must handle all possibilities",
19161904
});
19171905

1918-
cases.add("reading past end of pointer casted array",
1919-
\\comptime {
1920-
\\ const array: [4]u8 = "aoeu".*;
1921-
\\ const slice = array[1..];
1922-
\\ const int_ptr = @ptrCast(*const u24, slice.ptr);
1923-
\\ const deref = int_ptr.*;
1924-
\\}
1925-
, &[_][]const u8{
1926-
"tmp.zig:5:26: error: attempt to read 4 bytes from [4]u8 at index 1 which is 3 bytes",
1927-
});
1906+
// TODO uncomment before merging branch
1907+
//cases.add("reading past end of pointer casted array",
1908+
// \\comptime {
1909+
// \\ const array: [4]u8 = "aoeu".*;
1910+
// \\ const sub_array = array[1..];
1911+
// \\ const int_ptr = @ptrCast(*const u24, sub_array);
1912+
// \\ const deref = int_ptr.*;
1913+
// \\}
1914+
//, &[_][]const u8{
1915+
// "tmp.zig:5:26: error: attempt to read 4 bytes from [4]u8 at index 1 which is 3 bytes",
1916+
//});
19281917

19291918
cases.add("error note for function parameter incompatibility",
19301919
\\fn do_the_thing(func: fn (arg: i32) void) void {}

test/runtime_safety.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -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
);

0 commit comments

Comments
 (0)