From 057a5d4898f70c6a8169c99375fbb8631e539051 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 14 May 2019 21:21:59 -0400 Subject: [PATCH] slice types no longer have field access * fix crash when doing field access of slice types. closes #2486 * remove the deprecated Child property from slice types * add -Dskip-non-native build option to build script --- build.zig | 7 ++++--- src/ir.cpp | 13 +------------ test/compile_errors.zig | 10 ++++++++++ test/stage1/behavior/slice.zig | 6 ------ test/tests.zig | 12 +++++++++++- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/build.zig b/build.zig index 274ea51436..2bc0229475 100644 --- a/build.zig +++ b/build.zig @@ -71,6 +71,7 @@ pub fn build(b: *Builder) !void { const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release; const skip_release_fast = b.option(bool, "skip-release-fast", "Main test suite skips release-fast builds") orelse skip_release; const skip_release_safe = b.option(bool, "skip-release-safe", "Main test suite skips release-safe builds") orelse skip_release; + const skip_non_native = b.option(bool, "skip-non-native", "Main test suite skips non-native builds") orelse false; const skip_self_hosted = b.option(bool, "skip-self-hosted", "Main test suite skips building self hosted compiler") orelse false; if (!skip_self_hosted) { test_step.dependOn(&exe.step); @@ -115,11 +116,11 @@ pub fn build(b: *Builder) !void { const fmt_step = b.step("test-fmt", "Run zig fmt against build.zig to make sure it works"); fmt_step.dependOn(&fmt_build_zig.step); - test_step.dependOn(tests.addPkgTests(b, test_filter, "test/stage1/behavior.zig", "behavior", "Run the behavior tests", modes)); + test_step.dependOn(tests.addPkgTests(b, test_filter, "test/stage1/behavior.zig", "behavior", "Run the behavior tests", modes, skip_non_native)); - test_step.dependOn(tests.addPkgTests(b, test_filter, "std/std.zig", "std", "Run the standard library tests", modes)); + test_step.dependOn(tests.addPkgTests(b, test_filter, "std/std.zig", "std", "Run the standard library tests", modes, skip_non_native)); - test_step.dependOn(tests.addPkgTests(b, test_filter, "std/special/compiler_rt.zig", "compiler-rt", "Run the compiler_rt tests", modes)); + test_step.dependOn(tests.addPkgTests(b, test_filter, "std/special/compiler_rt.zig", "compiler-rt", "Run the compiler_rt tests", modes, skip_non_native)); test_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes)); test_step.dependOn(tests.addBuildExampleTests(b, test_filter, modes)); diff --git a/src/ir.cpp b/src/ir.cpp index afdbc16b5f..f70d5d475c 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -16178,18 +16178,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc if (type_is_invalid(child_type)) { return ira->codegen->invalid_instruction; - } else if (is_container(child_type)) { - if (is_slice(child_type) && buf_eql_str(field_name, "Child")) { - bool ptr_is_const = true; - bool ptr_is_volatile = false; - TypeStructField *ptr_field = &child_type->data.structure.fields[slice_ptr_index]; - assert(ptr_field->type_entry->id == ZigTypeIdPointer); - ZigType *child_type = ptr_field->type_entry->data.pointer.child_type; - return ir_get_const_ptr(ira, &field_ptr_instruction->base, - create_const_type(ira->codegen, child_type), - ira->codegen->builtin_types.entry_type, - ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0); - } + } else if (is_container(child_type) && !is_slice(child_type)) { if (child_type->id == ZigTypeIdEnum) { if ((err = ensure_complete_type(ira->codegen, child_type))) return ira->codegen->invalid_instruction; diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 297673235d..13b48ce103 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2,6 +2,16 @@ const tests = @import("tests.zig"); const builtin = @import("builtin"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add( + "field access of slices", + \\export fn entry() void { + \\ var slice: []i32 = undefined; + \\ const info = @typeOf(slice).unknown; + \\} + , + "tmp.zig:3:32: error: type '[]i32' does not support field access", + ); + cases.add( "peer cast then implicit cast const pointer to mutable C pointer", \\export fn func() void { diff --git a/test/stage1/behavior/slice.zig b/test/stage1/behavior/slice.zig index 7f3d7c95ee..1d3acff101 100644 --- a/test/stage1/behavior/slice.zig +++ b/test/stage1/behavior/slice.zig @@ -13,12 +13,6 @@ test "compile time slice of pointer to hard coded address" { expect(y.len == 0x400); } -test "slice child property" { - var array: [5]i32 = undefined; - var slice = array[0..]; - expect(@typeOf(slice).Child == i32); -} - test "runtime safety lets us slice from len..len" { var an_array = []u8{ 1, diff --git a/test/tests.zig b/test/tests.zig index 4b67cf0f6c..a17938fc3c 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -165,10 +165,20 @@ pub fn addGenHTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step { return cases.step; } -pub fn addPkgTests(b: *build.Builder, test_filter: ?[]const u8, root_src: []const u8, name: []const u8, desc: []const u8, modes: []const Mode) *build.Step { +pub fn addPkgTests( + b: *build.Builder, + test_filter: ?[]const u8, + root_src: []const u8, + name: []const u8, + desc: []const u8, + modes: []const Mode, + skip_non_native: bool, +) *build.Step { const step = b.step(b.fmt("test-{}", name), desc); for (test_targets) |test_target| { const is_native = (test_target.os == builtin.os and test_target.arch == builtin.arch); + if (skip_non_native and !is_native) + continue; for (modes) |mode| { for ([]bool{ false, true }) |link_libc| { for ([]bool{ false, true }) |single_threaded| {