stage2: add union compile error tests

This commit is contained in:
John Schmidt 2022-03-26 19:34:55 +01:00
parent e4d427f12e
commit fd1ce329b3
4 changed files with 75 additions and 0 deletions

View File

@ -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

View 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

View 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

View File

@ -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