diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-09-21 14:16:14 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-09-21 14:16:14 -0400 |
| commit | 0ca12ded2ffbbc6f703d66541481bab5d8cbb2e3 (patch) | |
| tree | 5bc73aa60ecc49695fd9bf8bab550ac1e75657be | |
| parent | 44f2ee101f244342ec7e4cd81bb21d9a0c23267b (diff) | |
| parent | 7c5e3e1f8e4671b383865df0150b549e2445d170 (diff) | |
| download | zig-0ca12ded2ffbbc6f703d66541481bab5d8cbb2e3.tar.gz zig-0ca12ded2ffbbc6f703d66541481bab5d8cbb2e3.zip | |
Merge branch 'raulgrell-BitByteOffsetOfs'
| -rw-r--r-- | doc/langref.html.in | 34 | ||||
| -rw-r--r-- | src/all_types.hpp | 15 | ||||
| -rw-r--r-- | src/codegen.cpp | 6 | ||||
| -rw-r--r-- | src/ir.cpp | 118 | ||||
| -rw-r--r-- | src/ir_print.cpp | 19 | ||||
| -rw-r--r-- | std/c/darwin.zig | 26 | ||||
| -rw-r--r-- | test/cases/sizeof_and_typeof.zig | 57 | ||||
| -rw-r--r-- | test/compile_errors.zig | 30 |
8 files changed, 233 insertions, 72 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in index 9bd11b8236..f28c8d6cfa 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -1947,6 +1947,15 @@ test "linked list" { assert(list2.first.?.data == 1234); } {#code_end#} + {#header_open|packed struct#} + <p>{#syntax#}packed{#endsyntax#} structs have guaranteed in-memory layout.</p> + <p>TODO bit fields</p> + <p>TODO alignment</p> + <p>TODO endianness</p> + <p>TODO @bitOffsetOf and @byteOffsetOf</p> + <p>TODO mention how volatile loads and stores of bit packed fields could be more efficient when + done by hand instead of with packed struct</p> + {#header_close#} {#header_open|struct Naming#} <p>Since all structs are anonymous, Zig infers the type name based on a few rules.</p> <ul> @@ -5135,6 +5144,18 @@ fn seq(c: u8) void { Works at compile-time if {#syntax#}value{#endsyntax#} is known at compile time. It's a compile error to bitcast a struct to a scalar type of the same size since structs have undefined layout. However if the struct is packed then it works. </p> {#header_close#} + {#header_open|@bitOffsetOf#} + <pre>{#syntax#}@bitOffsetOf(comptime T: type, comptime field_name: [] const u8) comptime_int{#endsyntax#}</pre> + <p> + Returns the bit offset of a field relative to its containing struct. + </p> + <p> + For non {#link|packed structs|packed struct#}, this will always be divisible by {#syntax#}8{#endsyntax#}. + For packed structs, non-byte-aligned fields will share a byte offset, but they will have different + bit offsets. + </p> + {#see_also|@byteOffsetOf#} + {#header_close#} {#header_open|@breakpoint#} <pre>{#syntax#}@breakpoint(){#endsyntax#}</pre> <p> @@ -5146,6 +5167,13 @@ fn seq(c: u8) void { </p> {#header_close#} + {#header_open|@byteOffsetOf#} + <pre>{#syntax#}@byteOffsetOf(comptime T: type, comptime field_name: [] const u8) comptime_int{#endsyntax#}</pre> + <p> + Returns the byte offset of a field relative to its containing struct. + </p> + {#see_also|@bitOffsetOf#} + {#header_close#} {#header_open|@alignCast#} <pre>{#syntax#}@alignCast(comptime alignment: u29, ptr: var) var{#endsyntax#}</pre> <p> @@ -5868,12 +5896,6 @@ fn add(a: i32, b: i32) i32 { </p> {#see_also|@inlineCall#} {#header_close#} - {#header_open|@offsetOf#} - <pre>{#syntax#}@offsetOf(comptime T: type, comptime field_name: [] const u8) comptime_int{#endsyntax#}</pre> - <p> - This function returns the byte offset of a field relative to its containing struct. - </p> - {#header_close#} {#header_open|@OpaqueType#} <pre>{#syntax#}@OpaqueType() type{#endsyntax#}</pre> <p> diff --git a/src/all_types.hpp b/src/all_types.hpp index 290e4bb0dd..0eaf69ed64 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1409,7 +1409,8 @@ enum BuiltinFnId { BuiltinFnIdTagName, BuiltinFnIdTagType, BuiltinFnIdFieldParentPtr, - BuiltinFnIdOffsetOf, + BuiltinFnIdByteOffsetOf, + BuiltinFnIdBitOffsetOf, BuiltinFnIdInlineCall, BuiltinFnIdNoInlineCall, BuiltinFnIdNewStackCall, @@ -2134,7 +2135,8 @@ enum IrInstructionId { IrInstructionIdTagName, IrInstructionIdTagType, IrInstructionIdFieldParentPtr, - IrInstructionIdOffsetOf, + IrInstructionIdByteOffsetOf, + IrInstructionIdBitOffsetOf, IrInstructionIdTypeInfo, IrInstructionIdTypeId, IrInstructionIdSetEvalBranchQuota, @@ -3037,7 +3039,14 @@ struct IrInstructionFieldParentPtr { TypeStructField *field; }; -struct IrInstructionOffsetOf { +struct IrInstructionByteOffsetOf { + IrInstruction base; + + IrInstruction *type_value; + IrInstruction *field_name; +}; + +struct IrInstructionBitOffsetOf { IrInstruction base; IrInstruction *type_value; diff --git a/src/codegen.cpp b/src/codegen.cpp index 6c4b54261c..d4d57d541c 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -5098,7 +5098,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, case IrInstructionIdTypeName: case IrInstructionIdDeclRef: case IrInstructionIdSwitchVar: - case IrInstructionIdOffsetOf: + case IrInstructionIdByteOffsetOf: + case IrInstructionIdBitOffsetOf: case IrInstructionIdTypeInfo: case IrInstructionIdTypeId: case IrInstructionIdSetEvalBranchQuota: @@ -6671,7 +6672,8 @@ static void define_builtin_fns(CodeGen *g) { create_builtin_fn(g, BuiltinFnIdTagName, "tagName", 1); create_builtin_fn(g, BuiltinFnIdTagType, "TagType", 1); create_builtin_fn(g, BuiltinFnIdFieldParentPtr, "fieldParentPtr", 3); - create_builtin_fn(g, BuiltinFnIdOffsetOf, "offsetOf", 2); + create_builtin_fn(g, BuiltinFnIdByteOffsetOf, "byteOffsetOf", 2); + create_builtin_fn(g, BuiltinFnIdBitOffsetOf, "bitOffsetOf", 2); create_builtin_fn(g, BuiltinFnIdDivExact, "divExact", 2); create_builtin_fn(g, BuiltinFnIdDivTrunc, "divTrunc", 2); create_builtin_fn(g, BuiltinFnIdDivFloor, "divFloor", 2); diff --git a/src/ir.cpp b/src/ir.cpp index 327c4f9398..5e0f79ec13 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -720,8 +720,12 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionFieldParentPtr * return IrInstructionIdFieldParentPtr; } -static constexpr IrInstructionId ir_instruction_id(IrInstructionOffsetOf *) { - return IrInstructionIdOffsetOf; +static constexpr IrInstructionId ir_instruction_id(IrInstructionByteOffsetOf *) { + return IrInstructionIdByteOffsetOf; +} + +static constexpr IrInstructionId ir_instruction_id(IrInstructionBitOffsetOf *) { + return IrInstructionIdBitOffsetOf; } static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeInfo *) { @@ -2628,10 +2632,23 @@ static IrInstruction *ir_build_field_parent_ptr(IrBuilder *irb, Scope *scope, As return &instruction->base; } -static IrInstruction *ir_build_offset_of(IrBuilder *irb, Scope *scope, AstNode *source_node, +static IrInstruction *ir_build_byte_offset_of(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *type_value, IrInstruction *field_name) { - IrInstructionOffsetOf *instruction = ir_build_instruction<IrInstructionOffsetOf>(irb, scope, source_node); + IrInstructionByteOffsetOf *instruction = ir_build_instruction<IrInstructionByteOffsetOf>(irb, scope, source_node); + instruction->type_value = type_value; + instruction->field_name = field_name; + + ir_ref_instruction(type_value, irb->current_basic_block); + ir_ref_instruction(field_name, irb->current_basic_block); + + return &instruction->base; +} + +static IrInstruction *ir_build_bit_offset_of(IrBuilder *irb, Scope *scope, AstNode *source_node, + IrInstruction *type_value, IrInstruction *field_name) +{ + IrInstructionBitOffsetOf *instruction = ir_build_instruction<IrInstructionBitOffsetOf>(irb, scope, source_node); instruction->type_value = type_value; instruction->field_name = field_name; @@ -4688,7 +4705,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo IrInstruction *field_parent_ptr = ir_build_field_parent_ptr(irb, scope, node, arg0_value, arg1_value, arg2_value, nullptr); return ir_lval_wrap(irb, scope, field_parent_ptr, lval); } - case BuiltinFnIdOffsetOf: + case BuiltinFnIdByteOffsetOf: { AstNode *arg0_node = node->data.fn_call_expr.params.at(0); IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope); @@ -4700,7 +4717,22 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - IrInstruction *offset_of = ir_build_offset_of(irb, scope, node, arg0_value, arg1_value); + IrInstruction *offset_of = ir_build_byte_offset_of(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, offset_of, lval); + } + case BuiltinFnIdBitOffsetOf: + { + AstNode *arg0_node = node->data.fn_call_expr.params.at(0); + IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope); + if (arg0_value == irb->codegen->invalid_instruction) + return arg0_value; + + AstNode *arg1_node = node->data.fn_call_expr.params.at(1); + IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope); + if (arg1_value == irb->codegen->invalid_instruction) + return arg1_value; + + IrInstruction *offset_of = ir_build_bit_offset_of(irb, scope, node, arg0_value, arg1_value); return ir_lval_wrap(irb, scope, offset_of, lval); } case BuiltinFnIdInlineCall: @@ -17010,49 +17042,84 @@ static ZigType *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira, return result_type; } -static ZigType *ir_analyze_instruction_offset_of(IrAnalyze *ira, - IrInstructionOffsetOf *instruction) +static TypeStructField *validate_byte_offset(IrAnalyze *ira, + IrInstruction *type_value, + IrInstruction *field_name_value, + size_t *byte_offset) { - Error err; - IrInstruction *type_value = instruction->type_value->other; ZigType *container_type = ir_resolve_type(ira, type_value); if (type_is_invalid(container_type)) - return ira->codegen->builtin_types.entry_invalid; + return nullptr; + Error err; if ((err = ensure_complete_type(ira->codegen, container_type))) - return ira->codegen->builtin_types.entry_invalid; + return nullptr; - IrInstruction *field_name_value = instruction->field_name->other; Buf *field_name = ir_resolve_str(ira, field_name_value); if (!field_name) - return ira->codegen->builtin_types.entry_invalid; + return nullptr; if (container_type->id != ZigTypeIdStruct) { ir_add_error(ira, type_value, buf_sprintf("expected struct type, found '%s'", buf_ptr(&container_type->name))); - return ira->codegen->builtin_types.entry_invalid; + return nullptr; } TypeStructField *field = find_struct_type_field(container_type, field_name); if (field == nullptr) { ir_add_error(ira, field_name_value, buf_sprintf("struct '%s' has no field '%s'", - buf_ptr(&container_type->name), buf_ptr(field_name))); - return ira->codegen->builtin_types.entry_invalid; + buf_ptr(&container_type->name), buf_ptr(field_name))); + return nullptr; } if (!type_has_bits(field->type_entry)) { ir_add_error(ira, field_name_value, - buf_sprintf("zero-bit field '%s' in struct '%s' has no offset", - buf_ptr(field_name), buf_ptr(&container_type->name))); - return ira->codegen->builtin_types.entry_invalid; + buf_sprintf("zero-bit field '%s' in struct '%s' has no offset", + buf_ptr(field_name), buf_ptr(&container_type->name))); + return nullptr; } - size_t byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, container_type->type_ref, field->gen_index); + + *byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, container_type->type_ref, field->gen_index); + return field; +} + +static ZigType *ir_analyze_instruction_byte_offset_of(IrAnalyze *ira, + IrInstructionByteOffsetOf *instruction) +{ + IrInstruction *type_value = instruction->type_value->other; + if (type_is_invalid(type_value->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *field_name_value = instruction->field_name->other; + size_t byte_offset = 0; + if (!validate_byte_offset(ira, type_value, field_name_value, &byte_offset)) + return ira->codegen->builtin_types.entry_invalid; + + ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base); bigint_init_unsigned(&out_val->data.x_bigint, byte_offset); return ira->codegen->builtin_types.entry_num_lit_int; } +static ZigType *ir_analyze_instruction_bit_offset_of(IrAnalyze *ira, + IrInstructionBitOffsetOf *instruction) +{ + IrInstruction *type_value = instruction->type_value->other; + if (type_is_invalid(type_value->value.type)) + return ira->codegen->builtin_types.entry_invalid; + IrInstruction *field_name_value = instruction->field_name->other; + size_t byte_offset = 0; + TypeStructField *field = nullptr; + if (!(field = validate_byte_offset(ira, type_value, field_name_value, &byte_offset))) + return ira->codegen->builtin_types.entry_invalid; + + size_t bit_offset = byte_offset * 8 + field->packed_bits_offset; + ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base); + bigint_init_unsigned(&out_val->data.x_bigint, bit_offset); + return ira->codegen->builtin_types.entry_num_lit_int; +} + static void ensure_field_index(ZigType *type, const char *field_name, size_t index) { Buf *field_name_buf; @@ -21550,8 +21617,10 @@ static ZigType *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *ins return ir_analyze_instruction_enum_tag_name(ira, (IrInstructionTagName *)instruction); case IrInstructionIdFieldParentPtr: return ir_analyze_instruction_field_parent_ptr(ira, (IrInstructionFieldParentPtr *)instruction); - case IrInstructionIdOffsetOf: - return ir_analyze_instruction_offset_of(ira, (IrInstructionOffsetOf *)instruction); + case IrInstructionIdByteOffsetOf: + return ir_analyze_instruction_byte_offset_of(ira, (IrInstructionByteOffsetOf *)instruction); + case IrInstructionIdBitOffsetOf: + return ir_analyze_instruction_bit_offset_of(ira, (IrInstructionBitOffsetOf *)instruction); case IrInstructionIdTypeInfo: return ir_analyze_instruction_type_info(ira, (IrInstructionTypeInfo *) instruction); case IrInstructionIdTypeId: @@ -21835,7 +21904,8 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdTypeName: case IrInstructionIdTagName: case IrInstructionIdFieldParentPtr: - case IrInstructionIdOffsetOf: + case IrInstructionIdByteOffsetOf: + case IrInstructionIdBitOffsetOf: case IrInstructionIdTypeInfo: case IrInstructionIdTypeId: case IrInstructionIdAlignCast: diff --git a/src/ir_print.cpp b/src/ir_print.cpp index d57d554c72..dc756cf492 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -1041,8 +1041,16 @@ static void ir_print_field_parent_ptr(IrPrint *irp, IrInstructionFieldParentPtr fprintf(irp->f, ")"); } -static void ir_print_offset_of(IrPrint *irp, IrInstructionOffsetOf *instruction) { - fprintf(irp->f, "@offset_of("); +static void ir_print_byte_offset_of(IrPrint *irp, IrInstructionByteOffsetOf *instruction) { + fprintf(irp->f, "@byte_offset_of("); + ir_print_other_instruction(irp, instruction->type_value); + fprintf(irp->f, ","); + ir_print_other_instruction(irp, instruction->field_name); + fprintf(irp->f, ")"); +} + +static void ir_print_bit_offset_of(IrPrint *irp, IrInstructionBitOffsetOf *instruction) { + fprintf(irp->f, "@bit_offset_of("); ir_print_other_instruction(irp, instruction->type_value); fprintf(irp->f, ","); ir_print_other_instruction(irp, instruction->field_name); @@ -1649,8 +1657,11 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { case IrInstructionIdFieldParentPtr: ir_print_field_parent_ptr(irp, (IrInstructionFieldParentPtr *)instruction); break; - case IrInstructionIdOffsetOf: - ir_print_offset_of(irp, (IrInstructionOffsetOf *)instruction); + case IrInstructionIdByteOffsetOf: + ir_print_byte_offset_of(irp, (IrInstructionByteOffsetOf *)instruction); + break; + case IrInstructionIdBitOffsetOf: + ir_print_bit_offset_of(irp, (IrInstructionBitOffsetOf *)instruction); break; case IrInstructionIdTypeInfo: ir_print_type_info(irp, (IrInstructionTypeInfo *)instruction); diff --git a/std/c/darwin.zig b/std/c/darwin.zig index 2e238e40eb..86c5e4828e 100644 --- a/std/c/darwin.zig +++ b/std/c/darwin.zig @@ -158,12 +158,12 @@ const std = @import("../index.zig"); const assert = std.debug.assert; comptime { - assert(@offsetOf(Kevent, "ident") == 0); - assert(@offsetOf(Kevent, "filter") == 8); - assert(@offsetOf(Kevent, "flags") == 10); - assert(@offsetOf(Kevent, "fflags") == 12); - assert(@offsetOf(Kevent, "data") == 16); - assert(@offsetOf(Kevent, "udata") == 24); + assert(@byteOffsetOf(Kevent, "ident") == 0); + assert(@byteOffsetOf(Kevent, "filter") == 8); + assert(@byteOffsetOf(Kevent, "flags") == 10); + assert(@byteOffsetOf(Kevent, "fflags") == 12); + assert(@byteOffsetOf(Kevent, "data") == 16); + assert(@byteOffsetOf(Kevent, "udata") == 24); } pub const kevent64_s = extern struct { @@ -180,11 +180,11 @@ pub const kevent64_s = extern struct { // to make sure the struct is laid out the same. These values were // produced from C code using the offsetof macro. comptime { - assert(@offsetOf(kevent64_s, "ident") == 0); - assert(@offsetOf(kevent64_s, "filter") == 8); - assert(@offsetOf(kevent64_s, "flags") == 10); - assert(@offsetOf(kevent64_s, "fflags") == 12); - assert(@offsetOf(kevent64_s, "data") == 16); - assert(@offsetOf(kevent64_s, "udata") == 24); - assert(@offsetOf(kevent64_s, "ext") == 32); + assert(@byteOffsetOf(kevent64_s, "ident") == 0); + assert(@byteOffsetOf(kevent64_s, "filter") == 8); + assert(@byteOffsetOf(kevent64_s, "flags") == 10); + assert(@byteOffsetOf(kevent64_s, "fflags") == 12); + assert(@byteOffsetOf(kevent64_s, "data") == 16); + assert(@byteOffsetOf(kevent64_s, "udata") == 24); + assert(@byteOffsetOf(kevent64_s, "ext") == 32); } diff --git a/test/cases/sizeof_and_typeof.zig b/test/cases/sizeof_and_typeof.zig index ee2061c8cb..11c6b2f6ba 100644 --- a/test/cases/sizeof_and_typeof.zig +++ b/test/cases/sizeof_and_typeof.zig @@ -1,6 +1,7 @@ +const builtin = @import("builtin"); const assert = @import("std").debug.assert; -test "sizeofAndTypeOf" { +test "@sizeOf and @typeOf" { const y: @typeOf(x) = 120; assert(@sizeOf(@typeOf(y)) == 2); } @@ -11,24 +12,58 @@ const A = struct { a: u8, b: u32, c: u8, + d: u3, + e: u5, + f: u16, + g: u16, }; const P = packed struct { a: u8, b: u32, c: u8, + d: u3, + e: u5, + f: u16, + g: u16, }; -test "offsetOf" { +test "@byteOffsetOf" { // Packed structs have fixed memory layout - const p: P = undefined; - assert(@offsetOf(P, "a") == 0); - assert(@offsetOf(@typeOf(p), "b") == 1); - assert(@offsetOf(@typeOf(p), "c") == 5); + assert(@byteOffsetOf(P, "a") == 0); + assert(@byteOffsetOf(P, "b") == 1); + assert(@byteOffsetOf(P, "c") == 5); + assert(@byteOffsetOf(P, "d") == 6); + assert(@byteOffsetOf(P, "e") == 6); + assert(@byteOffsetOf(P, "f") == 7); + assert(@byteOffsetOf(P, "g") == 9); - // Non-packed struct fields can be moved/padded - const a: A = undefined; - assert(@ptrToInt(&a.a) - @ptrToInt(&a) == @offsetOf(A, "a")); - assert(@ptrToInt(&a.b) - @ptrToInt(&a) == @offsetOf(@typeOf(a), "b")); - assert(@ptrToInt(&a.c) - @ptrToInt(&a) == @offsetOf(@typeOf(a), "c")); + // Normal struct fields can be moved/padded + var a: A = undefined; + assert(@ptrToInt(&a.a) - @ptrToInt(&a) == @byteOffsetOf(A, "a")); + assert(@ptrToInt(&a.b) - @ptrToInt(&a) == @byteOffsetOf(A, "b")); + assert(@ptrToInt(&a.c) - @ptrToInt(&a) == @byteOffsetOf(A, "c")); + assert(@ptrToInt(&a.d) - @ptrToInt(&a) == @byteOffsetOf(A, "d")); + assert(@ptrToInt(&a.e) - @ptrToInt(&a) == @byteOffsetOf(A, "e")); + assert(@ptrToInt(&a.f) - @ptrToInt(&a) == @byteOffsetOf(A, "f")); + assert(@ptrToInt(&a.g) - @ptrToInt(&a) == @byteOffsetOf(A, "g")); +} + +test "@bitOffsetOf" { + // Packed structs have fixed memory layout + assert(@bitOffsetOf(P, "a") == 0); + assert(@bitOffsetOf(P, "b") == 8); + assert(@bitOffsetOf(P, "c") == 40); + assert(@bitOffsetOf(P, "d") == 48); + assert(@bitOffsetOf(P, "e") == 51); + assert(@bitOffsetOf(P, "f") == 56); + assert(@bitOffsetOf(P, "g") == 72); + + assert(@byteOffsetOf(A, "a") * 8 == @bitOffsetOf(A, "a")); + assert(@byteOffsetOf(A, "b") * 8 == @bitOffsetOf(A, "b")); + assert(@byteOffsetOf(A, "c") * 8 == @bitOffsetOf(A, "c")); + assert(@byteOffsetOf(A, "d") * 8 == @bitOffsetOf(A, "d")); + assert(@byteOffsetOf(A, "e") * 8 == @bitOffsetOf(A, "e")); + assert(@byteOffsetOf(A, "f") * 8 == @bitOffsetOf(A, "f")); + assert(@byteOffsetOf(A, "g") * 8 == @bitOffsetOf(A, "g")); } diff --git a/test/compile_errors.zig b/test/compile_errors.zig index dfe17e9d82..fae022f3fb 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -3763,25 +3763,25 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { ); cases.add( - "@offsetOf - non struct", + "@byteOffsetOf - non struct", \\const Foo = i32; \\export fn foo() usize { - \\ return @offsetOf(Foo, "a",); + \\ return @byteOffsetOf(Foo, "a",); \\} , - ".tmp_source.zig:3:22: error: expected struct type, found 'i32'", + ".tmp_source.zig:3:26: error: expected struct type, found 'i32'", ); cases.add( - "@offsetOf - bad field name", + "@byteOffsetOf - bad field name", \\const Foo = struct { \\ derp: i32, \\}; \\export fn foo() usize { - \\ return @offsetOf(Foo, "a",); + \\ return @byteOffsetOf(Foo, "a",); \\} , - ".tmp_source.zig:5:27: error: struct 'Foo' has no field 'a'", + ".tmp_source.zig:5:31: error: struct 'Foo' has no field 'a'", ); cases.addExe( @@ -5084,15 +5084,27 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { ); cases.add( - "taking offset of void field in struct", + "taking byte offset of void field in struct", \\const Empty = struct { \\ val: void, \\}; \\export fn foo() void { - \\ const fieldOffset = @offsetOf(Empty, "val",); + \\ const fieldOffset = @byteOffsetOf(Empty, "val",); \\} , - ".tmp_source.zig:5:42: error: zero-bit field 'val' in struct 'Empty' has no offset", + ".tmp_source.zig:5:46: error: zero-bit field 'val' in struct 'Empty' has no offset", + ); + + cases.add( + "taking bit offset of void field in struct", + \\const Empty = struct { + \\ val: void, + \\}; + \\export fn foo() void { + \\ const fieldOffset = @bitOffsetOf(Empty, "val",); + \\} + , + ".tmp_source.zig:5:45: error: zero-bit field 'val' in struct 'Empty' has no offset", ); cases.add( |
