Skip to content

Commit 503b22b

Browse files
committed
Sema: catch error sets in atomic operations
also fix the struct test
1 parent b27c2a9 commit 503b22b

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/Zcu.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3958,7 +3958,12 @@ pub fn atomicPtrAlignment(
39583958
}
39593959
return .none;
39603960
}
3961-
if (ty.isAbiInt(zcu)) {
3961+
if (switch (ty.zigTypeTag(zcu)) {
3962+
.int, .@"enum" => true,
3963+
.@"struct" => ty.containerLayout(zcu) == .@"packed",
3964+
else => false,
3965+
}) {
3966+
assert(ty.isAbiInt(zcu));
39623967
const bit_count = ty.intInfo(zcu).bits;
39633968
if (bit_count > max_atomic_bits) {
39643969
diags.* = .{

test/cases/compile_errors/atomics_with_invalid_type.zig

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,27 @@ export fn float() void {
55

66
const NormalStruct = struct { x: u32 };
77
export fn normalStruct() void {
8-
var x: NormalStruct = 0;
8+
var x: NormalStruct = .{ .x = 0 };
99
_ = @cmpxchgWeak(NormalStruct, &x, .{ .x = 1 }, .{ .x = 2 }, .seq_cst, .seq_cst);
1010
}
1111

12+
export fn anyError() void {
13+
var x: anyerror = error.A;
14+
_ = @cmpxchgWeak(anyerror, &x, error.A, error.B, .seq_cst, .seq_cst);
15+
}
16+
17+
const ErrorSet = error{ A, B };
18+
export fn errorSet() void {
19+
var x: ErrorSet = error.A;
20+
_ = @cmpxchgWeak(ErrorSet, &x, error.A, error.B, .seq_cst, .seq_cst);
21+
}
22+
1223
// error
1324
// backend=stage2
1425
// target=native
1526
//
1627
// :3:22: error: expected bool, integer, enum, packed struct, or pointer type; found 'f32'
17-
// :8:27: error: expected type 'tmp.NormalStruct', found 'comptime_int'
28+
// :9:22: error: expected bool, integer, float, enum, packed struct, or pointer type; found 'tmp.NormalStruct'
1829
// :6:22: note: struct declared here
30+
// :14:22: error: expected bool, integer, float, enum, packed struct, or pointer type; found 'anyerror'
31+
// :20:22: error: expected bool, integer, float, enum, packed struct, or pointer type; found 'error{A,B}'

0 commit comments

Comments
 (0)