diff --git a/doc/langref.html.in b/doc/langref.html.in index e87decdf4a..643c948b68 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2103,8 +2103,9 @@ or less than {#syntax#}1 << 29{#endsyntax#}.

- In Zig, a pointer type has an alignment value. If the value is equal to the - alignment of the underlying type, it can be omitted from the type: + Pointer types may explicitly specify an alignment in bytes. If it is not + specified, the alignment is assumed to be equal to the alignment of the + underlying type.

{#code|test_variable_alignment.zig#} diff --git a/doc/langref/test_comptime_invalid_error_code.zig b/doc/langref/test_comptime_invalid_error_code.zig index ebc6314764..e4fedf5812 100644 --- a/doc/langref/test_comptime_invalid_error_code.zig +++ b/doc/langref/test_comptime_invalid_error_code.zig @@ -1,8 +1,5 @@ comptime { - const err = error.AnError; - const number = @intFromError(err) + 10; - const invalid_err = @errorFromInt(number); - _ = invalid_err; + _ = @errorFromInt(12345); } -// test_error=integer value '11' represents no error +// test_error=integer value '12345' represents no error diff --git a/doc/langref/test_missized_packed_struct.zig b/doc/langref/test_missized_packed_struct.zig index 791f97cc31..df323244d4 100644 --- a/doc/langref/test_missized_packed_struct.zig +++ b/doc/langref/test_missized_packed_struct.zig @@ -3,4 +3,4 @@ test "missized packed struct" { _ = S{ .a = 4, .b = 2 }; } -// test_error=backing integer type 'u32' has bit size 32 but the struct fields have a total bit size of 24 +// test_error=backing integer bit width does not match total bit width of fields diff --git a/doc/langref/test_variable_alignment.zig b/doc/langref/test_variable_alignment.zig index 01768e29eb..d0a4560fa2 100644 --- a/doc/langref/test_variable_alignment.zig +++ b/doc/langref/test_variable_alignment.zig @@ -1,15 +1,20 @@ const std = @import("std"); const builtin = @import("builtin"); +const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; test "variable alignment" { var x: i32 = 1234; - const align_of_i32 = @alignOf(@TypeOf(x)); + try expectEqual(*i32, @TypeOf(&x)); - try expectEqual(*align(align_of_i32) i32, *i32); - if (builtin.target.cpu.arch == .x86_64) { - try expectEqual(4, @typeInfo(*i32).pointer.alignment); - } + + try expect(@intFromPtr(&x) % @alignOf(i32) == 0); + + // The implicitly-aligned pointer can be coerced to be explicitly-aligned to + // the alignment of the underlying type `i32`: + const ptr: *align(@alignOf(i32)) i32 = &x; + + try expectEqual(1234, ptr.*); } // test