diff --git a/.gitignore b/.gitignore index 9fa6f71cc7..feda423c10 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ # -andrewrk zig-cache/ +zig-out/ /release/ /debug/ /build/ diff --git a/BRANCH_TODO b/BRANCH_TODO index 95211126fe..7b625204f6 100644 --- a/BRANCH_TODO +++ b/BRANCH_TODO @@ -1,5 +1,5 @@ - * decouple AstGen from Module, Compilation * modify dbg_stmt ZIR instructions to have line/column rather than node indexes + * decouple AstGen from Module, Compilation * AstGen threadlocal * extern "foo" for vars and for functions * namespace decls table can't reference ZIR memory because it can get modified on updates diff --git a/doc/langref.html.in b/doc/langref.html.in index 80080ca146..65adab57d4 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2861,6 +2861,37 @@ fn dump(args: anytype) void { expect(args.b); expect(args.s[0] == 'h'); expect(args.s[1] == 'i'); +} + {#code_end#} +

+ Anonymous structs can be created without specifying field names, and are referred to as "tuples". +

+

+ The fields are implicitly named using numbers starting from 0. Because their names are integers, + the {#syntax#}@"0"{#endsyntax#} syntax must be used to access them. Names inside {#syntax#}@""{#endsyntax#} are always recognised as identifiers. +

+

+ Like arrays, tuples have a .len field, can be indexed and work with the ++ and ** operators. They can also be iterated over with {#link|inline for#}. +

+ {#code_begin|test|tuple#} +const std = @import("std"); +const expect = std.testing.expect; + +test "tuple" { + const values = .{ + @as(u32, 1234), + @as(f64, 12.34), + true, + "hi", + } ++ .{false} ** 2; + expect(values[0] == 1234); + expect(values[4] == false); + inline for (values) |v, i| { + if (i != 2) continue; + expect(v); + } + expect(values.len == 6); + expect(values.@"3"[0] == 'h'); } {#code_end#} {#header_close#} @@ -9997,7 +10028,7 @@ pub fn build(b: *Builder) void { {#code_end#}

terminal

$ zig build
-$ ./zig-cache/bin/test
+$ ./zig-out/bin/test
 all your base are belong to us
{#see_also|Targets|Zig Build System#} {#header_close#} diff --git a/lib/std/build.zig b/lib/std/build.zig index ff2a16d2b9..4eeb4aad4d 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -195,7 +195,8 @@ pub const Builder = struct { self.install_prefix = install_prefix orelse "/usr"; self.install_path = fs.path.join(self.allocator, &[_][]const u8{ dest_dir, self.install_prefix }) catch unreachable; } else { - self.install_prefix = install_prefix orelse self.cache_root; + self.install_prefix = install_prefix orelse + (fs.path.join(self.allocator, &[_][]const u8{ self.build_root, "zig-out" }) catch unreachable); self.install_path = self.install_prefix; } self.lib_dir = fs.path.join(self.allocator, &[_][]const u8{ self.install_path, "lib" }) catch unreachable; diff --git a/lib/std/compress/deflate.zig b/lib/std/compress/deflate.zig index e680dc9e6f..09e162933c 100644 --- a/lib/std/compress/deflate.zig +++ b/lib/std/compress/deflate.zig @@ -384,6 +384,8 @@ pub fn InflateStream(comptime ReaderType: type) type { const last_length = lengths[i - 1]; const repeat = 3 + (try self.readBits(2)); const last_index = i + repeat; + if (last_index > lengths.len) + return error.InvalidLength; while (i < last_index) : (i += 1) { lengths[i] = last_length; } @@ -655,3 +657,19 @@ pub fn InflateStream(comptime ReaderType: type) type { pub fn inflateStream(reader: anytype, window_slice: []u8) InflateStream(@TypeOf(reader)) { return InflateStream(@TypeOf(reader)).init(reader, window_slice); } + +test "lengths overflow" { + // malformed final dynamic block, tries to write 321 code lengths (MAXCODES is 316) + // f dy hlit hdist hclen 16 17 18 0 (18) x138 (18) x138 (18) x39 (16) x6 + // 1 10 11101 11101 0000 010 010 010 010 (11) 1111111 (11) 1111111 (11) 0011100 (01) 11 + const stream = [_]u8{ + 0b11101101, 0b00011101, 0b00100100, 0b11101001, 0b11111111, 0b11111111, 0b00111001, 0b00001110 + }; + + const reader = std.io.fixedBufferStream(&stream).reader(); + var window: [0x8000]u8 = undefined; + var inflate = inflateStream(reader, &window); + + var buf: [1]u8 = undefined; + std.testing.expectError(error.InvalidLength, inflate.read(&buf)); +} diff --git a/lib/std/crypto/25519/field.zig b/lib/std/crypto/25519/field.zig index 57371d8290..3378191451 100644 --- a/lib/std/crypto/25519/field.zig +++ b/lib/std/crypto/25519/field.zig @@ -292,7 +292,7 @@ pub const Fe = struct { return _carry128(&r); } - fn _sq(a: Fe, double: bool) callconv(.Inline) Fe { + fn _sq(a: Fe, comptime double: bool) callconv(.Inline) Fe { var ax: [5]u128 = undefined; var r: [5]u128 = undefined; comptime var i = 0; diff --git a/lib/std/crypto/poly1305.zig b/lib/std/crypto/poly1305.zig index a775094c48..375cf0a3cb 100644 --- a/lib/std/crypto/poly1305.zig +++ b/lib/std/crypto/poly1305.zig @@ -39,7 +39,7 @@ pub const Poly1305 = struct { }; } - fn blocks(st: *Poly1305, m: []const u8, last: bool) void { + fn blocks(st: *Poly1305, m: []const u8, comptime last: bool) void { const hibit: u64 = if (last) 0 else 1 << 40; const r0 = st.r[0]; const r1 = st.r[1]; diff --git a/lib/std/os.zig b/lib/std/os.zig index fbee2fdcbe..6c6baf49f3 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -1049,7 +1049,7 @@ pub const OpenError = error{ } || UnexpectedError; /// Open and possibly create a file. Keeps trying if it gets interrupted. -/// See also `openC`. +/// See also `openZ`. pub fn open(file_path: []const u8, flags: u32, perm: mode_t) OpenError!fd_t { if (std.Target.current.os.tag == .windows) { const file_path_w = try windows.sliceToPrefixedFileW(file_path); @@ -1147,7 +1147,7 @@ pub fn openW(file_path_w: []const u16, flags: u32, perm: mode_t) OpenError!fd_t /// Open and possibly create a file. Keeps trying if it gets interrupted. /// `file_path` is relative to the open directory handle `dir_fd`. -/// See also `openatC`. +/// See also `openatZ`. pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) OpenError!fd_t { if (builtin.os.tag == .wasi) { @compileError("use openatWasi instead"); @@ -1754,7 +1754,7 @@ pub const UnlinkError = error{ } || UnexpectedError; /// Delete a name and possibly the file it refers to. -/// See also `unlinkC`. +/// See also `unlinkZ`. pub fn unlink(file_path: []const u8) UnlinkError!void { if (builtin.os.tag == .wasi) { @compileError("unlink is not supported in WASI; use unlinkat instead"); @@ -3419,7 +3419,7 @@ pub fn fstat(fd: fd_t) FStatError!Stat { } } -pub const FStatAtError = FStatError || error{ NameTooLong, FileNotFound }; +pub const FStatAtError = FStatError || error{ NameTooLong, FileNotFound, SymLinkLoop }; /// Similar to `fstat`, but returns stat of a resource pointed to by `pathname` /// which is relative to `dirfd` handle. @@ -3466,8 +3466,10 @@ pub fn fstatatZ(dirfd: fd_t, pathname: [*:0]const u8, flags: u32) FStatAtError!S EBADF => unreachable, // Always a race condition. ENOMEM => return error.SystemResources, EACCES => return error.AccessDenied, + EPERM => return error.AccessDenied, EFAULT => unreachable, ENAMETOOLONG => return error.NameTooLong, + ELOOP => return error.SymLinkLoop, ENOENT => return error.FileNotFound, ENOTDIR => return error.FileNotFound, else => |err| return unexpectedErrno(err), diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index 70aa3c8dc6..c6185ef093 100644 --- a/lib/std/special/build_runner.zig +++ b/lib/std/special/build_runner.zig @@ -82,9 +82,9 @@ pub fn main() !void { builder.verbose = true; } else if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { return usage(builder, false, stdout_stream); - } else if (mem.eql(u8, arg, "--prefix")) { + } else if (mem.eql(u8, arg, "-p") or mem.eql(u8, arg, "--prefix")) { install_prefix = nextArg(args, &arg_idx) orelse { - warn("Expected argument after --prefix\n\n", .{}); + warn("Expected argument after {s}\n\n", .{arg}); return usageAndErr(builder, false, stderr_stream); }; } else if (mem.eql(u8, arg, "--search-prefix")) { @@ -188,7 +188,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void \\General Options: \\ -h, --help Print this help and exit \\ --verbose Print commands before executing them - \\ --prefix [path] Override default install prefix + \\ -p, --prefix [path] Override default install prefix \\ --search-prefix [path] Add a path to look for binaries, libraries, headers \\ --color [auto|off|on] Enable or disable colored error messages \\ diff --git a/src/main.zig b/src/main.zig index d5a379c8b0..7094e693d4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -687,7 +687,7 @@ fn buildOutputType( extra_cflags.shrinkRetainingCapacity(0); while (true) { i += 1; - if (i + 1 >= args.len) fatal("expected -- after -cflags", .{}); + if (i >= args.len) fatal("expected -- after -cflags", .{}); if (mem.eql(u8, args[i], "--")) break; try extra_cflags.append(args[i]); }