mirror of
https://github.com/ziglang/zig.git
synced 2026-02-21 16:54:52 +00:00
stage2: add union compile error tests
This commit is contained in:
parent
e4d427f12e
commit
fd1ce329b3
@ -0,0 +1,14 @@
|
||||
const U = union {
|
||||
a: void,
|
||||
b: u64,
|
||||
};
|
||||
comptime {
|
||||
var u: U = .{.a = {}};
|
||||
const v = u.b;
|
||||
_ = v;
|
||||
}
|
||||
|
||||
// access of inactive union field
|
||||
//
|
||||
// :7:16: error: access of union field 'b' while field 'a' is active
|
||||
// :1:11: note: union declared here
|
||||
20
test/compile_errors/stage2/union_enum_field_missing.zig
Normal file
20
test/compile_errors/stage2/union_enum_field_missing.zig
Normal file
@ -0,0 +1,20 @@
|
||||
const E = enum {
|
||||
a,
|
||||
b,
|
||||
c,
|
||||
};
|
||||
|
||||
const U = union(E) {
|
||||
a: i32,
|
||||
b: f64,
|
||||
};
|
||||
|
||||
export fn entry() usize {
|
||||
return @sizeOf(U);
|
||||
}
|
||||
|
||||
// enum field missing in union
|
||||
//
|
||||
// :7:1: error: enum field(s) missing in union
|
||||
// :4:5: note: field 'c' missing, declared here
|
||||
// :1:11: note: enum declared here
|
||||
19
test/compile_errors/stage2/union_extra_field.zig
Normal file
19
test/compile_errors/stage2/union_extra_field.zig
Normal file
@ -0,0 +1,19 @@
|
||||
const E = enum {
|
||||
a,
|
||||
b,
|
||||
c,
|
||||
};
|
||||
const U = union(E) {
|
||||
a: i32,
|
||||
b: f64,
|
||||
c: f64,
|
||||
d: f64,
|
||||
};
|
||||
export fn entry() usize {
|
||||
return @sizeOf(U);
|
||||
}
|
||||
|
||||
// union extra field
|
||||
//
|
||||
// :6:1: error: enum 'tmp.E' hs no field named 'd'
|
||||
// :1:11: note: enum declared here
|
||||
@ -0,0 +1,22 @@
|
||||
const E = enum {
|
||||
a,
|
||||
b,
|
||||
};
|
||||
const U = union(E) {
|
||||
a: u32,
|
||||
b: u64,
|
||||
};
|
||||
fn foo() E {
|
||||
return E.b;
|
||||
}
|
||||
export fn doTheTest() u64 {
|
||||
var u: U = foo();
|
||||
return u.b;
|
||||
}
|
||||
|
||||
// runtime coercion from enum to union
|
||||
//
|
||||
// :13:19: error: runtime coercion from enum 'tmp.E' to union 'tmp.U' which has non-void fields
|
||||
// :6:5: note: field 'a' has type 'u32'
|
||||
// :7:5: note: field 'b' has type 'u64'
|
||||
// :5:11: note: union declared here
|
||||
Loading…
x
Reference in New Issue
Block a user