mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-03-08 03:24:46 +01:00
langref: update for language changes
This commit is contained in:
parent
83b0118f74
commit
cfebb91e50
4 changed files with 16 additions and 13 deletions
|
|
@ -2103,8 +2103,9 @@ or
|
|||
less than {#syntax#}1 << 29{#endsyntax#}.
|
||||
</p>
|
||||
<p>
|
||||
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.
|
||||
</p>
|
||||
{#code|test_variable_alignment.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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue