aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-10-08 15:47:45 -0700
committerAndrew Kelley <andrew@ziglang.org>2020-10-08 15:47:45 -0700
commit8b7539bd9554d25599ccff6f0e9ea79aa051946b (patch)
tree9157fafcbe37f904346fd73042be1d1d299d4da2 /test
parentb5a36f676b1fd69f195d9f1eb6e35f3eb2f15946 (diff)
parentb02341d6f58e0b8a87fc2ab589dcfd85e5dc96cd (diff)
downloadzig-8b7539bd9554d25599ccff6f0e9ea79aa051946b.tar.gz
zig-8b7539bd9554d25599ccff6f0e9ea79aa051946b.zip
Merge remote-tracking branch 'origin/master' into llvm11
Conflicts: src/clang.zig Master branch renamed an enum; this branch gave it an explicit tag type and explicitly initialized values. This commit combines the changes together.
Diffstat (limited to 'test')
-rw-r--r--test/compile_errors.zig87
-rw-r--r--test/gen_h.zig2
-rw-r--r--test/stage1/behavior/misc.zig6
-rw-r--r--test/stage1/behavior/type.zig13
-rw-r--r--test/stage1/behavior/type_info.zig17
-rw-r--r--test/stage1/c_abi/cfuncs.c18
-rw-r--r--test/stage1/c_abi/main.zig11
-rw-r--r--test/translate_c.zig14
8 files changed, 93 insertions, 75 deletions
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index 816d569b4d..44ee58f002 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -2,6 +2,19 @@ const tests = @import("tests.zig");
const std = @import("std");
pub fn addCases(cases: *tests.CompileErrorContext) void {
+ cases.add("array in c exported function",
+ \\export fn zig_array(x: [10]u8) void {
+ \\ expect(std.mem.eql(u8, &x, "1234567890"));
+ \\}
+ \\
+ \\export fn zig_return_array() [10]u8 {
+ \\ return "1234567890".*;
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:1:24: error: parameter of type '[10]u8' not allowed in function with calling convention 'C'",
+ "tmp.zig:5:30: error: return type '[10]u8' not allowed in function with calling convention 'C'",
+ });
+
cases.add("@Type for exhaustive enum with undefined tag type",
\\const TypeInfo = @import("builtin").TypeInfo;
\\const Tag = @Type(.{
@@ -125,6 +138,31 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:15:23: error: enum field missing: 'arst'",
"tmp.zig:27:24: note: referenced here",
});
+
+ cases.add("field access of opaque type",
+ \\const MyType = opaque {};
+ \\
+ \\export fn entry() bool {
+ \\ var x: i32 = 1;
+ \\ return bar(@ptrCast(*MyType, &x));
+ \\}
+ \\
+ \\fn bar(x: *MyType) bool {
+ \\ return x.blah;
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:9:13: error: no member named 'blah' in opaque type 'MyType'",
+ });
+
+ cases.add("opaque type with field",
+ \\const Opaque = opaque { foo: i32 };
+ \\export fn entry() void {
+ \\ const foo: ?*Opaque = null;
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:1:25: error: opaque types cannot have fields",
+ });
+
cases.add("@Type(.Fn) with is_generic = true",
\\const Foo = @Type(.{
\\ .Fn = .{
@@ -180,7 +218,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ .layout = .Auto,
\\ .tag_type = null,
\\ .fields = &[_]TypeInfo.UnionField{
- \\ .{ .name = "foo", .field_type = @Type(.Opaque), .alignment = 1 },
+ \\ .{ .name = "foo", .field_type = opaque {}, .alignment = 1 },
\\ },
\\ .decls = &[_]TypeInfo.Declaration{},
\\ },
@@ -2287,7 +2325,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ return error.OutOfMemory;
\\}
, &[_][]const u8{
- "tmp.zig:2:12: error: error is discarded",
+ "tmp.zig:2:12: error: error is discarded. consider using `try`, `catch`, or `if`",
});
cases.add("volatile on global assembly",
@@ -2338,9 +2376,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ return error.Bad;
\\}
, &[_][]const u8{
- "tmp.zig:2:24: error: expression value is ignored",
- "tmp.zig:6:25: error: expression value is ignored",
- "tmp.zig:10:25: error: expression value is ignored",
+ "tmp.zig:2:24: error: error is ignored. consider using `try`, `catch`, or `if`",
+ "tmp.zig:6:25: error: error is ignored. consider using `try`, `catch`, or `if`",
+ "tmp.zig:10:25: error: error is ignored. consider using `try`, `catch`, or `if`",
});
cases.add("empty while loop body",
@@ -2613,7 +2651,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
});
cases.add("directly embedding opaque type in struct and union",
- \\const O = @Type(.Opaque);
+ \\const O = opaque {};
\\const Foo = struct {
\\ o: O,
\\};
@@ -2628,7 +2666,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ var bar: Bar = undefined;
\\}
\\export fn c() void {
- \\ var baz: *@Type(.Opaque) = undefined;
+ \\ var baz: *opaque {} = undefined;
\\ const qux = .{baz.*};
\\}
, &[_][]const u8{
@@ -3592,7 +3630,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
});
cases.add("unknown length pointer to opaque",
- \\export const T = [*]@Type(.Opaque);
+ \\export const T = [*]opaque {};
, &[_][]const u8{
"tmp.zig:1:21: error: unknown-length pointer to opaque",
});
@@ -6236,7 +6274,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\}
\\fn bar() anyerror!i32 { return 0; }
, &[_][]const u8{
- "tmp.zig:2:14: error: expression value is ignored",
+ "tmp.zig:2:14: error: error is ignored. consider using `try`, `catch`, or `if`",
});
cases.add("dereference an array",
@@ -6827,8 +6865,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:2:31: error: index 2 outside array of size 2",
});
- cases.add("wrong pointer coerced to pointer to @Type(.Opaque)",
- \\const Derp = @Type(.Opaque);
+ cases.add("wrong pointer coerced to pointer to opaque {}",
+ \\const Derp = opaque {};
\\extern fn bar(d: *Derp) void;
\\export fn foo() void {
\\ var x = @as(u8, 1);
@@ -6854,8 +6892,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\export fn entry5() void {
\\ var d = null;
\\}
- \\export fn entry6(opaque: *Opaque) void {
- \\ var e = opaque.*;
+ \\export fn entry6(opaque_: *Opaque) void {
+ \\ var e = opaque_.*;
\\}
\\export fn entry7() void {
\\ var f = i32;
@@ -6866,7 +6904,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\export fn entry9() void {
\\ var z: noreturn = return;
\\}
- \\const Opaque = @Type(.Opaque);
+ \\const Opaque = opaque {};
\\const Foo = struct {
\\ fn bar(self: *const Foo) void {}
\\};
@@ -7019,21 +7057,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:37:29: error: cannot store runtime value in compile time variable",
});
- cases.add("field access of opaque type",
- \\const MyType = @Type(.Opaque);
- \\
- \\export fn entry() bool {
- \\ var x: i32 = 1;
- \\ return bar(@ptrCast(*MyType, &x));
- \\}
- \\
- \\fn bar(x: *MyType) bool {
- \\ return x.blah;
- \\}
- , &[_][]const u8{
- "tmp.zig:9:13: error: type '*MyType' does not support field access",
- });
-
cases.add("invalid legacy unicode escape",
\\export fn entry() void {
\\ const a = '\U1234';
@@ -7623,7 +7646,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
});
cases.add("function returning opaque type",
- \\const FooType = @Type(.Opaque);
+ \\const FooType = opaque {};
\\export fn bar() !FooType {
\\ return error.InvalidValue;
\\}
@@ -7641,7 +7664,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
});
cases.add("generic function returning opaque type",
- \\const FooType = @Type(.Opaque);
+ \\const FooType = opaque {};
\\fn generic(comptime T: type) !T {
\\ return undefined;
\\}
@@ -7665,7 +7688,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
});
cases.add("function parameter is opaque",
- \\const FooType = @Type(.Opaque);
+ \\const FooType = opaque {};
\\export fn entry1() void {
\\ const someFuncPtr: fn (FooType) void = undefined;
\\}
diff --git a/test/gen_h.zig b/test/gen_h.zig
index 7a86d914d7..2122558079 100644
--- a/test/gen_h.zig
+++ b/test/gen_h.zig
@@ -74,7 +74,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
});
cases.add("declare opaque type",
- \\const Foo = @Type(.Opaque);
+ \\const Foo = opaque {};
\\
\\export fn entry(foo: ?*Foo) void { }
, &[_][]const u8{
diff --git a/test/stage1/behavior/misc.zig b/test/stage1/behavior/misc.zig
index a71d6f86f3..04087dd4af 100644
--- a/test/stage1/behavior/misc.zig
+++ b/test/stage1/behavior/misc.zig
@@ -438,8 +438,8 @@ export fn writeToVRam() void {
vram[0] = 'X';
}
-const OpaqueA = @Type(.Opaque);
-const OpaqueB = @Type(.Opaque);
+const OpaqueA = opaque {};
+const OpaqueB = opaque {};
test "opaque types" {
expect(*OpaqueA != *OpaqueB);
expect(mem.eql(u8, @typeName(OpaqueA), "OpaqueA"));
@@ -704,7 +704,7 @@ test "auto created variables have correct alignment" {
comptime expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
}
-extern var opaque_extern_var: @Type(.Opaque);
+extern var opaque_extern_var: opaque {};
var var_to_export: u32 = 42;
test "extern variable with non-pointer opaque type" {
@export(var_to_export, .{ .name = "opaque_extern_var" });
diff --git a/test/stage1/behavior/type.zig b/test/stage1/behavior/type.zig
index 60a23ffa94..53a47228db 100644
--- a/test/stage1/behavior/type.zig
+++ b/test/stage1/behavior/type.zig
@@ -190,8 +190,17 @@ test "Type.ErrorUnion" {
}
test "Type.Opaque" {
- testing.expect(@Type(.Opaque) != @Type(.Opaque));
- testing.expect(@typeInfo(@Type(.Opaque)) == .Opaque);
+ const Opaque = @Type(.{
+ .Opaque = .{
+ .decls = &[_]TypeInfo.Declaration{},
+ },
+ });
+ testing.expect(Opaque != opaque {});
+ testing.expectEqualSlices(
+ TypeInfo.Declaration,
+ &[_]TypeInfo.Declaration{},
+ @typeInfo(Opaque).Opaque.decls,
+ );
}
test "Type.Vector" {
diff --git a/test/stage1/behavior/type_info.zig b/test/stage1/behavior/type_info.zig
index 8b413bf031..e663284d0d 100644
--- a/test/stage1/behavior/type_info.zig
+++ b/test/stage1/behavior/type_info.zig
@@ -199,7 +199,7 @@ fn testUnion() void {
expect(typeinfo_info.Union.tag_type.? == TypeId);
expect(typeinfo_info.Union.fields.len == 25);
expect(typeinfo_info.Union.fields[4].field_type == @TypeOf(@typeInfo(u8).Int));
- expect(typeinfo_info.Union.decls.len == 21);
+ expect(typeinfo_info.Union.decls.len == 22);
const TestNoTagUnion = union {
Foo: void,
@@ -265,6 +265,21 @@ const TestStruct = packed struct {
const Self = @This();
};
+test "type info: opaque info" {
+ testOpaque();
+ comptime testOpaque();
+}
+
+fn testOpaque() void {
+ const Foo = opaque {
+ const A = 1;
+ fn b() void {}
+ };
+
+ const foo_info = @typeInfo(Foo);
+ expect(foo_info.Opaque.decls.len == 2);
+}
+
test "type info: function type info" {
// wasm doesn't support align attributes on functions
if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
diff --git a/test/stage1/c_abi/cfuncs.c b/test/stage1/c_abi/cfuncs.c
index 0e7bd2906f..0e8204779a 100644
--- a/test/stage1/c_abi/cfuncs.c
+++ b/test/stage1/c_abi/cfuncs.c
@@ -28,8 +28,6 @@ void zig_ptr(void *);
void zig_bool(bool);
-void zig_array(uint8_t[10]);
-
struct BigStruct {
uint64_t a;
uint64_t b;
@@ -97,9 +95,6 @@ void run_c_tests(void) {
zig_bool(true);
- uint8_t array[10] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
- zig_array(array);
-
{
struct BigStruct s = {1, 2, 3, 4, 5};
zig_big_struct(s);
@@ -190,19 +185,6 @@ void c_five_floats(float a, float b, float c, float d, float e) {
assert_or_panic(e == 5.0);
}
-void c_array(uint8_t x[10]) {
- assert_or_panic(x[0] == '1');
- assert_or_panic(x[1] == '2');
- assert_or_panic(x[2] == '3');
- assert_or_panic(x[3] == '4');
- assert_or_panic(x[4] == '5');
- assert_or_panic(x[5] == '6');
- assert_or_panic(x[6] == '7');
- assert_or_panic(x[7] == '8');
- assert_or_panic(x[8] == '9');
- assert_or_panic(x[9] == '0');
-}
-
void c_big_struct(struct BigStruct x) {
assert_or_panic(x.a == 1);
assert_or_panic(x.b == 2);
diff --git a/test/stage1/c_abi/main.zig b/test/stage1/c_abi/main.zig
index 9090bb481e..18e9858da4 100644
--- a/test/stage1/c_abi/main.zig
+++ b/test/stage1/c_abi/main.zig
@@ -116,17 +116,6 @@ export fn zig_bool(x: bool) void {
expect(x);
}
-extern fn c_array([10]u8) void;
-
-test "C ABI array" {
- var array: [10]u8 = "1234567890".*;
- c_array(array);
-}
-
-export fn zig_array(x: [10]u8) void {
- expect(std.mem.eql(u8, &x, "1234567890"));
-}
-
const BigStruct = extern struct {
a: u64,
b: u64,
diff --git a/test/translate_c.zig b/test/translate_c.zig
index 4182dbaa13..8b9537b45d 100644
--- a/test/translate_c.zig
+++ b/test/translate_c.zig
@@ -137,9 +137,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\struct foo { int x; int y[]; };
\\struct bar { int x; int y[0]; };
, &[_][]const u8{
- \\pub const struct_foo = @Type(.Opaque);
+ \\pub const struct_foo = opaque {};
,
- \\pub const struct_bar = @Type(.Opaque);
+ \\pub const struct_bar = opaque {};
});
cases.add("nested loops without blocks",
@@ -207,7 +207,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\pub const struct_arcan_shmif_page = //
,
\\warning: unsupported type: 'Atomic'
- \\ @Type(.Opaque); //
+ \\ opaque {}; //
,
\\ warning: struct demoted to opaque type - unable to translate type of field abufused
, // TODO should be `addr: *struct_arcan_shmif_page`
@@ -386,8 +386,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ struct opaque_2 *cast = (struct opaque_2 *)opaque;
\\}
, &[_][]const u8{
- \\pub const struct_opaque = @Type(.Opaque);
- \\pub const struct_opaque_2 = @Type(.Opaque);
+ \\pub const struct_opaque = opaque {};
+ \\pub const struct_opaque_2 = opaque {};
\\pub export fn function(arg_opaque_1: ?*struct_opaque) void {
\\ var opaque_1 = arg_opaque_1;
\\ var cast: ?*struct_opaque_2 = @ptrCast(?*struct_opaque_2, opaque_1);
@@ -628,7 +628,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ struct Foo *foo;
\\};
, &[_][]const u8{
- \\pub const struct_Foo = @Type(.Opaque);
+ \\pub const struct_Foo = opaque {};
,
\\pub const struct_Bar = extern struct {
\\ foo: ?*struct_Foo,
@@ -705,7 +705,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\struct Foo;
\\struct Foo *some_func(struct Foo *foo, int x);
, &[_][]const u8{
- \\pub const struct_Foo = @Type(.Opaque);
+ \\pub const struct_Foo = opaque {};
,
\\pub extern fn some_func(foo: ?*struct_Foo, x: c_int) ?*struct_Foo;
,