2020-06-05 12:48:36 +08:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-05-05 02:23:22 +08:00
|
|
|
fn doTest() !void {
|
2020-06-05 12:48:36 +08:00
|
|
|
// var
|
|
|
|
{
|
|
|
|
var box0: Box0 = .{ .items = undefined };
|
2021-05-05 02:23:22 +08:00
|
|
|
try std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == false);
|
2020-06-05 12:48:36 +08:00
|
|
|
|
|
|
|
var box1: Box1 = .{ .items = undefined };
|
2021-05-05 02:23:22 +08:00
|
|
|
try std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == false);
|
2020-06-05 12:48:36 +08:00
|
|
|
|
|
|
|
var box2: Box2 = .{ .items = undefined };
|
2021-05-05 02:23:22 +08:00
|
|
|
try std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == false);
|
2020-06-05 12:48:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// const
|
|
|
|
{
|
|
|
|
const box0: Box0 = .{ .items = undefined };
|
2021-05-05 02:23:22 +08:00
|
|
|
try std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == true);
|
2020-06-05 12:48:36 +08:00
|
|
|
|
|
|
|
const box1: Box1 = .{ .items = undefined };
|
2021-05-05 02:23:22 +08:00
|
|
|
try std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == true);
|
2020-06-05 12:48:36 +08:00
|
|
|
|
|
|
|
const box2: Box2 = .{ .items = undefined };
|
2021-05-05 02:23:22 +08:00
|
|
|
try std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == true);
|
2020-06-05 12:48:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test "pointer-to-array constness for zero-size elements" {
|
2021-05-05 02:23:22 +08:00
|
|
|
try doTest();
|
|
|
|
comptime try doTest();
|
2020-06-05 12:48:36 +08:00
|
|
|
}
|