diff options
| -rw-r--r-- | src/Sema.zig | 19 | ||||
| -rw-r--r-- | src/Zir.zig | 2 | ||||
| -rw-r--r-- | test/behavior/eval.zig | 56 | ||||
| -rw-r--r-- | test/behavior/floatop.zig | 29 | ||||
| -rw-r--r-- | test/behavior/math.zig | 6 |
5 files changed, 65 insertions, 47 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 7326fd9bc6..9789ca8416 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -11098,24 +11098,33 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air break :blk try sema.resolveAlreadyCoercedInt(block, .unneeded, ref, u16); } else 0; - const bit_end = if (inst_data.flags.has_bit_range) blk: { + var host_size: u16 = if (inst_data.flags.has_bit_range) blk: { const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]); extra_i += 1; break :blk try sema.resolveAlreadyCoercedInt(block, .unneeded, ref, u16); } else 0; - if (bit_end != 0 and bit_start >= bit_end * 8) - return sema.fail(block, src, "bit offset starts after end of host integer", .{}); - const elem_type = try sema.resolveType(block, .unneeded, extra.data.elem_type); + if (host_size != 0) { + if (bit_start >= host_size * 8) { + return sema.fail(block, src, "bit offset starts after end of host integer", .{}); + } + const target = sema.mod.getTarget(); + const elem_type_bits = elem_type.bitSize(target); + if (host_size * 8 == elem_type_bits) { + assert(bit_start == 0); + host_size = 0; + } + } + const ty = try Type.ptr(sema.arena, .{ .pointee_type = elem_type, .sentinel = sentinel, .@"align" = abi_align, .@"addrspace" = address_space, .bit_offset = bit_start, - .host_size = bit_end, + .host_size = host_size, .mutable = inst_data.flags.is_mutable, .@"allowzero" = inst_data.flags.is_allowzero or inst_data.size == .C, .@"volatile" = inst_data.flags.is_volatile, diff --git a/src/Zir.zig b/src/Zir.zig index 3795c28daf..86fe44773e 100644 --- a/src/Zir.zig +++ b/src/Zir.zig @@ -2423,7 +2423,7 @@ pub const Inst = struct { /// 1. align: Ref // if `has_align` flag is set /// 2. address_space: Ref // if `has_addrspace` flag is set /// 3. bit_start: Ref // if `has_bit_range` flag is set - /// 4. bit_end: Ref // if `has_bit_range` flag is set + /// 4. host_size: Ref // if `has_bit_range` flag is set pub const PtrType = struct { elem_type: Ref, }; diff --git a/test/behavior/eval.zig b/test/behavior/eval.zig index a3313011a3..c7b5b016b4 100644 --- a/test/behavior/eval.zig +++ b/test/behavior/eval.zig @@ -522,20 +522,12 @@ test "inlined loop has array literal with elided runtime scope on first iteratio } } -test "eval @setFloatMode at compile-time" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - - const result = comptime fnWithFloatMode(); - try expect(result == 1234.0); -} - -fn fnWithFloatMode() f32 { - @setFloatMode(std.builtin.FloatMode.Strict); - return 1234.0; -} - test "call method on bound fn referring to var instance" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO + if (builtin.zig_backend != .stage1) { + // Let's delay solving this one; I want to try to eliminate bound functions from + // the language. + return error.SkipZigTest; // TODO + } try expect(bound_fn() == 1237); } @@ -596,19 +588,6 @@ fn assertEqualPtrs(ptr1: *const u8, ptr2: *const u8) !void { try expect(ptr1 == ptr2); } -test "float literal at compile time not lossy" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - - try expect(16777216.0 + 1.0 == 16777217.0); - try expect(9007199254740992.0 + 1.0 == 9007199254740993.0); -} - -test "f128 at compile time is lossy" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - - try expect(@as(f128, 10384593717069655257060992658440192.0) + 1 == 10384593717069655257060992658440192.0); -} - test "string literal used as comptime slice is memoized" { if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO @@ -714,20 +693,7 @@ fn testVarInsideInlineLoop(args: anytype) !void { } } -test "bit shift a u1" { - // note: when debugging this test case for stage2, be sure to run it - // in valgrind. I noticed the rhs value is undefined in the lowering - // of the const value. - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - - var x: u1 = 1; - var y = x << 0; - try expect(y == 1); -} - test "*align(1) u16 is the same as *align(1:0:2) u16" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - comptime { try expect(*align(1:0:2) u16 == *align(1) u16); try expect(*align(2:0:2) u16 == *u16); @@ -735,14 +701,22 @@ test "*align(1) u16 is the same as *align(1:0:2) u16" { } test "array concatenation forces comptime" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO + if (builtin.zig_backend != .stage1) { + // note: our plan is to change the language to support runtime array + // concatenation instead of making this test pass. + return error.SkipZigTest; // TODO + } var a = oneItem(3) ++ oneItem(4); try expect(std.mem.eql(i32, &a, &[_]i32{ 3, 4 })); } test "array multiplication forces comptime" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO + if (builtin.zig_backend != .stage1) { + // note: our plan is to change the language to support runtime array + // multiplication instead of making this test pass. + return error.SkipZigTest; // TODO + } var a = oneItem(3) ** scalar(2); try expect(std.mem.eql(i32, &a, &[_]i32{ 3, 3 })); diff --git a/test/behavior/floatop.zig b/test/behavior/floatop.zig index 22c576e5f5..85d5cec1e6 100644 --- a/test/behavior/floatop.zig +++ b/test/behavior/floatop.zig @@ -466,3 +466,32 @@ test "negation" { try S.doTheTest(); comptime try S.doTheTest(); } + +test "eval @setFloatMode at compile-time" { + if (builtin.zig_backend != .stage1) { + // let's delay solving this one; I want to re-evaluate this language feature, and + // we don't rely on it for self-hosted. + return error.SkipZigTest; // TODO + } + + const result = comptime fnWithFloatMode(); + try expect(result == 1234.0); +} + +fn fnWithFloatMode() f32 { + @setFloatMode(std.builtin.FloatMode.Strict); + return 1234.0; +} + +test "float literal at compile time not lossy" { + if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO + + try expect(16777216.0 + 1.0 == 16777217.0); + try expect(9007199254740992.0 + 1.0 == 9007199254740993.0); +} + +test "f128 at compile time is lossy" { + if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO + + try expect(@as(f128, 10384593717069655257060992658440192.0) + 1 == 10384593717069655257060992658440192.0); +} diff --git a/test/behavior/math.zig b/test/behavior/math.zig index fe5329ec81..597d004262 100644 --- a/test/behavior/math.zig +++ b/test/behavior/math.zig @@ -488,6 +488,12 @@ const DivResult = struct { remainder: u64, }; +test "bit shift a u1" { + var x: u1 = 1; + var y = x << 0; + try expect(y == 1); +} + test "truncating shift right" { try testShrTrunc(maxInt(u16)); comptime try testShrTrunc(maxInt(u16)); |
