authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2020-06-05 00:48:36-04:00
committergravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2020-06-05 00:49:57-04:00
logc0c9d11d8c509fdd1e60492abb1215d9860c3d39
tree0fa83a1731000ea6d0c760817ccc5434652d713c
parentf839d34baa8213048a1f073a38bf9b54af74711d
signaturelock-open Commit is signed but in an unrecognized format.

stage1: fix constness in some corner cases

- for one-possible-value types, ir_analyze_struct_field_ptr() no longer hardcodes const/volatile - when slicing arrays, ir_analyze_instruction_slice() no longer consults ConstValSpecialStatic closes #5474

3 files changed, 62 insertions(+), 4 deletions(-)

src/ir.cpp+4-4
...@@ -21924,7 +21924,9 @@ static IrInstGen *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInst* source_ins...@@ -21924,7 +21924,9 @@ static IrInstGen *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInst* source_ins
21924 case OnePossibleValueYes: {21924 case OnePossibleValueYes: {
21925 IrInstGen *elem = ir_const_move(ira, source_instr,21925 IrInstGen *elem = ir_const_move(ira, source_instr,
21926 get_the_one_possible_value(ira->codegen, field_type));21926 get_the_one_possible_value(ira->codegen, field_type));
21927 return ir_get_ref(ira, source_instr, elem, false, false);21927 return ir_get_ref(ira, source_instr, elem,
21928 struct_ptr->value->type->data.pointer.is_const,
21929 struct_ptr->value->type->data.pointer.is_volatile);
21928 }21930 }
21929 case OnePossibleValueNo:21931 case OnePossibleValueNo:
21930 break;21932 break;
...@@ -27097,10 +27099,8 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i...@@ -27097,10 +27099,8 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2709727099
27098 if (array_type->id == ZigTypeIdArray) {27100 if (array_type->id == ZigTypeIdArray) {
27099 elem_type = array_type->data.array.child_type;27101 elem_type = array_type->data.array.child_type;
27100 bool is_comptime_const = ptr_ptr->value->special == ConstValSpecialStatic &&
27101 ptr_ptr->value->data.x_ptr.mut == ConstPtrMutComptimeConst;
27102 non_sentinel_slice_ptr_type = get_pointer_to_type_extra(ira->codegen, elem_type,27102 non_sentinel_slice_ptr_type = get_pointer_to_type_extra(ira->codegen, elem_type,
27103 ptr_ptr_type->data.pointer.is_const || is_comptime_const,27103 ptr_ptr_type->data.pointer.is_const,
27104 ptr_ptr_type->data.pointer.is_volatile,27104 ptr_ptr_type->data.pointer.is_volatile,
27105 PtrLenUnknown,27105 PtrLenUnknown,
27106 ptr_ptr_type->data.pointer.explicit_alignment, 0, 0, false);27106 ptr_ptr_type->data.pointer.explicit_alignment, 0, 0, false);
test/stage1/behavior.zig+1
...@@ -50,6 +50,7 @@ comptime {...@@ -50,6 +50,7 @@ comptime {
50 _ = @import("behavior/bugs/4769_b.zig");50 _ = @import("behavior/bugs/4769_b.zig");
51 _ = @import("behavior/bugs/4769_c.zig");51 _ = @import("behavior/bugs/4769_c.zig");
52 _ = @import("behavior/bugs/4954.zig");52 _ = @import("behavior/bugs/4954.zig");
53 _ = @import("behavior/bugs/5474.zig");
53 _ = @import("behavior/bugs/5487.zig");54 _ = @import("behavior/bugs/5487.zig");
54 _ = @import("behavior/bugs/394.zig");55 _ = @import("behavior/bugs/394.zig");
55 _ = @import("behavior/bugs/421.zig");56 _ = @import("behavior/bugs/421.zig");
test/stage1/behavior/bugs/5474.zig created+57
...@@ -0,0 +1,57 @@
1const std = @import("std");
2
3// baseline (control) struct with array of scalar
4const Box0 = struct {
5 items: [4]Item,
6
7 const Item = struct {
8 num: u32,
9 };
10};
11
12// struct with array of empty struct
13const Box1 = struct {
14 items: [4]Item,
15
16 const Item = struct {};
17};
18
19// struct with array of zero-size struct
20const Box2 = struct {
21 items: [4]Item,
22
23 const Item = struct {
24 nothing: void,
25 };
26};
27
28fn doTest() void {
29 // var
30 {
31 var box0: Box0 = .{ .items = undefined };
32 std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == false);
33
34 var box1: Box1 = .{ .items = undefined };
35 std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == false);
36
37 var box2: Box2 = .{ .items = undefined };
38 std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == false);
39 }
40
41 // const
42 {
43 const box0: Box0 = .{ .items = undefined };
44 std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == true);
45
46 const box1: Box1 = .{ .items = undefined };
47 std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == true);
48
49 const box2: Box2 = .{ .items = undefined };
50 std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == true);
51 }
52}
53
54test "pointer-to-array constness for zero-size elements" {
55 doTest();
56 comptime doTest();
57}