blob: ee58a93124dc64bbe46b6380715b3b3fa95e71b0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
const std = @import("std");
const builtin = @import("builtin");
// baseline (control) struct with array of scalar
const Box0 = struct {
items: [4]Item,
const Item = struct {
num: u32,
};
};
// struct with array of empty struct
const Box1 = struct {
items: [4]Item,
const Item = struct {};
};
// struct with array of zero-size struct
const Box2 = struct {
items: [4]Item,
const Item = struct {
nothing: void,
};
};
fn mutable() !void {
var box0: Box0 = .{ .items = undefined };
try std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == false);
var box1: Box1 = .{ .items = undefined };
try std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == false);
var box2: Box2 = .{ .items = undefined };
try std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == false);
}
fn constant() !void {
const box0: Box0 = .{ .items = undefined };
try std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == true);
const box1: Box1 = .{ .items = undefined };
try std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == true);
const box2: Box2 = .{ .items = undefined };
try std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == true);
}
test "pointer-to-array constness for zero-size elements, var" {
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
try mutable();
try comptime mutable();
}
test "pointer-to-array constness for zero-size elements, const" {
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
try constant();
try comptime constant();
}
|