Skip to content

Commit bf3ac66

Browse files
committed
remove type coercion from array values to references
* Implements #3768. This is a sweeping breaking change that requires many (trivial) edits to Zig source code. Array values no longer coerced to slices; however one may use `&` to obtain a reference to an array value, which may then be coerced to a slice. * Adds `IrInstruction::dump`, for debugging purposes. It's useful to call to inspect the instruction when debugging Zig IR. * Fixes bugs with result location semantics. See the new behavior test cases, and compile error test cases. * Fixes bugs with `@typeInfo` not properly resolving const values. * Behavior tests are passing but std lib tests are not yet. There is more work to do before merging this branch.
1 parent 379d547 commit bf3ac66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+729
-839
lines changed

build.zig

+15-15
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ pub fn build(b: *Builder) !void {
2020
const rel_zig_exe = try fs.path.relative(b.allocator, b.build_root, b.zig_exe);
2121
const langref_out_path = fs.path.join(
2222
b.allocator,
23-
[_][]const u8{ b.cache_root, "langref.html" },
23+
&[_][]const u8{ b.cache_root, "langref.html" },
2424
) catch unreachable;
2525
var docgen_cmd = docgen_exe.run();
26-
docgen_cmd.addArgs([_][]const u8{
26+
docgen_cmd.addArgs(&[_][]const u8{
2727
rel_zig_exe,
2828
"doc" ++ fs.path.sep_str ++ "langref.html.in",
2929
langref_out_path,
@@ -36,7 +36,7 @@ pub fn build(b: *Builder) !void {
3636
const test_step = b.step("test", "Run all the tests");
3737

3838
// find the stage0 build artifacts because we're going to re-use config.h and zig_cpp library
39-
const build_info = try b.exec([_][]const u8{
39+
const build_info = try b.exec(&[_][]const u8{
4040
b.zig_exe,
4141
"BUILD_INFO",
4242
});
@@ -56,7 +56,7 @@ pub fn build(b: *Builder) !void {
5656
test_stage2.setBuildMode(builtin.Mode.Debug);
5757
test_stage2.addPackagePath("stage2_tests", "test/stage2/test.zig");
5858

59-
const fmt_build_zig = b.addFmt([_][]const u8{"build.zig"});
59+
const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"});
6060

6161
var exe = b.addExecutable("zig", "src-self-hosted/main.zig");
6262
exe.setBuildMode(mode);
@@ -88,7 +88,7 @@ pub fn build(b: *Builder) !void {
8888
.source_dir = "lib",
8989
.install_dir = .Lib,
9090
.install_subdir = "zig",
91-
.exclude_extensions = [_][]const u8{ "test.zig", "README.md" },
91+
.exclude_extensions = &[_][]const u8{ "test.zig", "README.md" },
9292
});
9393

9494
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
@@ -148,7 +148,7 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void {
148148
}
149149
const lib_dir = fs.path.join(
150150
b.allocator,
151-
[_][]const u8{ dep.prefix, "lib" },
151+
&[_][]const u8{ dep.prefix, "lib" },
152152
) catch unreachable;
153153
for (dep.system_libs.toSliceConst()) |lib| {
154154
const static_bare_name = if (mem.eql(u8, lib, "curses"))
@@ -157,7 +157,7 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void {
157157
b.fmt("lib{}.a", lib);
158158
const static_lib_name = fs.path.join(
159159
b.allocator,
160-
[_][]const u8{ lib_dir, static_bare_name },
160+
&[_][]const u8{ lib_dir, static_bare_name },
161161
) catch unreachable;
162162
const have_static = fileExists(static_lib_name) catch unreachable;
163163
if (have_static) {
@@ -183,7 +183,7 @@ fn fileExists(filename: []const u8) !bool {
183183
}
184184

185185
fn addCppLib(b: *Builder, lib_exe_obj: var, cmake_binary_dir: []const u8, lib_name: []const u8) void {
186-
lib_exe_obj.addObjectFile(fs.path.join(b.allocator, [_][]const u8{
186+
lib_exe_obj.addObjectFile(fs.path.join(b.allocator, &[_][]const u8{
187187
cmake_binary_dir,
188188
"zig_cpp",
189189
b.fmt("{}{}{}", lib_exe_obj.target.libPrefix(), lib_name, lib_exe_obj.target.staticLibSuffix()),
@@ -199,22 +199,22 @@ const LibraryDep = struct {
199199
};
200200

201201
fn findLLVM(b: *Builder, llvm_config_exe: []const u8) !LibraryDep {
202-
const shared_mode = try b.exec([_][]const u8{ llvm_config_exe, "--shared-mode" });
202+
const shared_mode = try b.exec(&[_][]const u8{ llvm_config_exe, "--shared-mode" });
203203
const is_static = mem.startsWith(u8, shared_mode, "static");
204204
const libs_output = if (is_static)
205-
try b.exec([_][]const u8{
205+
try b.exec(&[_][]const u8{
206206
llvm_config_exe,
207207
"--libfiles",
208208
"--system-libs",
209209
})
210210
else
211-
try b.exec([_][]const u8{
211+
try b.exec(&[_][]const u8{
212212
llvm_config_exe,
213213
"--libs",
214214
});
215-
const includes_output = try b.exec([_][]const u8{ llvm_config_exe, "--includedir" });
216-
const libdir_output = try b.exec([_][]const u8{ llvm_config_exe, "--libdir" });
217-
const prefix_output = try b.exec([_][]const u8{ llvm_config_exe, "--prefix" });
215+
const includes_output = try b.exec(&[_][]const u8{ llvm_config_exe, "--includedir" });
216+
const libdir_output = try b.exec(&[_][]const u8{ llvm_config_exe, "--libdir" });
217+
const prefix_output = try b.exec(&[_][]const u8{ llvm_config_exe, "--prefix" });
218218

219219
var result = LibraryDep{
220220
.prefix = mem.tokenize(prefix_output, " \r\n").next().?,
@@ -341,7 +341,7 @@ fn addCxxKnownPath(
341341
objname: []const u8,
342342
errtxt: ?[]const u8,
343343
) !void {
344-
const path_padded = try b.exec([_][]const u8{
344+
const path_padded = try b.exec(&[_][]const u8{
345345
ctx.cxx_compiler,
346346
b.fmt("-print-file-name={}", objname),
347347
});

lib/std/array_list.zig

+4-11
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
3535
/// Deinitialize with `deinit` or use `toOwnedSlice`.
3636
pub fn init(allocator: *Allocator) Self {
3737
return Self{
38-
.items = [_]T{},
38+
.items = &[_]T{},
3939
.len = 0,
4040
.allocator = allocator,
4141
};
@@ -306,18 +306,14 @@ test "std.ArrayList.basic" {
306306
testing.expect(list.pop() == 10);
307307
testing.expect(list.len == 9);
308308

309-
list.appendSlice([_]i32{
310-
1,
311-
2,
312-
3,
313-
}) catch unreachable;
309+
list.appendSlice(&[_]i32{ 1, 2, 3 }) catch unreachable;
314310
testing.expect(list.len == 12);
315311
testing.expect(list.pop() == 3);
316312
testing.expect(list.pop() == 2);
317313
testing.expect(list.pop() == 1);
318314
testing.expect(list.len == 9);
319315

320-
list.appendSlice([_]i32{}) catch unreachable;
316+
list.appendSlice(&[_]i32{}) catch unreachable;
321317
testing.expect(list.len == 9);
322318

323319
// can only set on indices < self.len
@@ -464,10 +460,7 @@ test "std.ArrayList.insertSlice" {
464460
try list.append(2);
465461
try list.append(3);
466462
try list.append(4);
467-
try list.insertSlice(1, [_]i32{
468-
9,
469-
8,
470-
});
463+
try list.insertSlice(1, &[_]i32{ 9, 8 });
471464
testing.expect(list.items[0] == 1);
472465
testing.expect(list.items[1] == 9);
473466
testing.expect(list.items[2] == 8);

lib/std/bloom_filter.zig

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ pub fn BloomFilter(
6262
}
6363

6464
pub fn getCell(self: Self, cell: Index) Cell {
65-
return Io.get(self.data, cell, 0);
65+
return Io.get(&self.data, cell, 0);
6666
}
6767

6868
pub fn incrementCell(self: *Self, cell: Index) void {
6969
if (Cell == bool or Cell == u1) {
7070
// skip the 'get' operation
7171
Io.set(&self.data, cell, 0, cellMax);
7272
} else {
73-
const old = Io.get(self.data, cell, 0);
73+
const old = Io.get(&self.data, cell, 0);
7474
if (old != cellMax) {
7575
Io.set(&self.data, cell, 0, old + 1);
7676
}
@@ -120,7 +120,7 @@ pub fn BloomFilter(
120120
} else if (newsize > n_items) {
121121
var copied: usize = 0;
122122
while (copied < r.data.len) : (copied += self.data.len) {
123-
std.mem.copy(u8, r.data[copied .. copied + self.data.len], self.data);
123+
std.mem.copy(u8, r.data[copied .. copied + self.data.len], &self.data);
124124
}
125125
}
126126
return r;

0 commit comments

Comments
 (0)