From c51b871c4516003e8d2c84e7e1c36124c3797f5c Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 1 Sep 2020 17:29:10 +0200 Subject: ir: Typecheck the sentinel value in *[N:S1]T to [S2]T casts Closes #6054 --- src/ir.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index 692dd392e1..36be78ed7c 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -15341,9 +15341,14 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr, ZigType *array_type = actual_type->data.pointer.child_type; bool const_ok = (slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 0 || !actual_type->data.pointer.is_const); + if (const_ok && types_match_const_cast_only(ira, slice_ptr_type->data.pointer.child_type, array_type->data.array.child_type, source_node, - !slice_ptr_type->data.pointer.is_const).id == ConstCastResultIdOk) + !slice_ptr_type->data.pointer.is_const).id == ConstCastResultIdOk && + (slice_ptr_type->data.pointer.sentinel == nullptr || + (array_type->data.array.sentinel != nullptr && + const_values_equal(ira->codegen, array_type->data.array.sentinel, + slice_ptr_type->data.pointer.sentinel)))) { // If the pointers both have ABI align, it works. // Or if the array length is 0, alignment doesn't matter. -- cgit v1.2.3 From 1b2154dfe2f9b5030f487e7c4be8c706ce6e59b5 Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Tue, 1 Sep 2020 18:55:36 -0600 Subject: builtin: Add TypeInfo.StructField.is_comptime --- lib/std/builtin.zig | 1 + lib/std/meta/trailer_flags.zig | 1 + src/ir.cpp | 6 ++++++ test/stage1/behavior/type_info.zig | 6 ++++++ 4 files changed, 14 insertions(+) (limited to 'src/ir.cpp') diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index 3d103d6d06..911a0eb15c 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -261,6 +261,7 @@ pub const TypeInfo = union(enum) { name: []const u8, field_type: type, default_value: anytype, + is_comptime: bool, }; /// This data structure is used by the Zig language code generation and diff --git a/lib/std/meta/trailer_flags.zig b/lib/std/meta/trailer_flags.zig index 19f6ee2e80..c8c1323686 100644 --- a/lib/std/meta/trailer_flags.zig +++ b/lib/std/meta/trailer_flags.zig @@ -46,6 +46,7 @@ pub fn TrailerFlags(comptime Fields: type) type { ??struct_field.field_type, @as(?struct_field.field_type, null), ), + .is_comptime = false, }; } break :blk @Type(.{ diff --git a/src/ir.cpp b/src/ir.cpp index 692dd392e1..124c1feb06 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -25683,6 +25683,10 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy } set_optional_payload(inner_fields[2], struct_field->init_val); + inner_fields[3]->special = ConstValSpecialStatic; + inner_fields[3]->type = ira->codegen->builtin_types.entry_bool; + inner_fields[3]->data.x_bool = struct_field->is_comptime; + ZigValue *name = create_const_str_lit(ira->codegen, struct_field->name)->data.x_ptr.data.ref.pointee; init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(struct_field->name), true); @@ -26291,6 +26295,8 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI buf_ptr(&field->type_entry->name), buf_ptr(&field->type_entry->name))); return ira->codegen->invalid_inst_gen->value->type; } + if ((err = get_const_field_bool(ira, source_instr->source_node, field_value, "is_comptime", 3, &field->is_comptime))) + return ira->codegen->invalid_inst_gen->value->type; } return entry; diff --git a/test/stage1/behavior/type_info.zig b/test/stage1/behavior/type_info.zig index d0da8815f7..409993a741 100644 --- a/test/stage1/behavior/type_info.zig +++ b/test/stage1/behavior/type_info.zig @@ -418,3 +418,9 @@ test "Struct.is_tuple" { expect(@typeInfo(@TypeOf(.{0})).Struct.is_tuple); expect(!@typeInfo(@TypeOf(.{ .a = 0 })).Struct.is_tuple); } + +test "StructField.is_comptime" { + const info = @typeInfo(struct { x: u8 = 3, comptime y: u32 = 5 }).Struct; + expect(!info.fields[0].is_comptime); + expect(info.fields[1].is_comptime); +} -- cgit v1.2.3