diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-01-03 12:57:01 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-03 12:57:01 -0800 |
| commit | a9337bef2d54201ab304bb3428479339ecaacbac (patch) | |
| tree | e253679e591e86d84a29139b7d7987eab3c3718f /test | |
| parent | 9a56228c2b90d94d01bb76784c77fdec5710cf0a (diff) | |
| parent | 047d6d996e540eaa50ec24b0751d147bfe2cacdb (diff) | |
| download | zig-a9337bef2d54201ab304bb3428479339ecaacbac.tar.gz zig-a9337bef2d54201ab304bb3428479339ecaacbac.zip | |
Merge pull request #18431 from jacobly0/cbe-extern
cbe: fix non-msvc externs and exports
Diffstat (limited to 'test')
| -rw-r--r-- | test/behavior.zig | 4 | ||||
| -rw-r--r-- | test/behavior/bugs/12680.zig | 22 | ||||
| -rw-r--r-- | test/behavior/bugs/12680_other_file.zig | 8 | ||||
| -rw-r--r-- | test/behavior/export_c_keywords.zig | 44 | ||||
| -rw-r--r-- | test/behavior/import_c_keywords.zig | 92 |
5 files changed, 137 insertions, 33 deletions
diff --git a/test/behavior.zig b/test/behavior.zig index 287a9d7ea8..5c9d714165 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -110,7 +110,6 @@ test { _ = @import("behavior/bugs/12551.zig"); _ = @import("behavior/bugs/12571.zig"); _ = @import("behavior/bugs/12644.zig"); - _ = @import("behavior/bugs/12680.zig"); _ = @import("behavior/bugs/12723.zig"); _ = @import("behavior/bugs/12776.zig"); _ = @import("behavior/bugs/12786.zig"); @@ -175,6 +174,7 @@ test { _ = @import("behavior/hasfield.zig"); _ = @import("behavior/if.zig"); _ = @import("behavior/import.zig"); + _ = @import("behavior/import_c_keywords.zig"); _ = @import("behavior/incomplete_struct_param_tld.zig"); _ = @import("behavior/inline_switch.zig"); _ = @import("behavior/int128.zig"); @@ -251,9 +251,7 @@ test { } if (builtin.zig_backend != .stage2_arm and - builtin.zig_backend != .stage2_x86_64 and builtin.zig_backend != .stage2_aarch64 and - builtin.zig_backend != .stage2_c and builtin.zig_backend != .stage2_spirv64) { _ = @import("behavior/export_keyword.zig"); diff --git a/test/behavior/bugs/12680.zig b/test/behavior/bugs/12680.zig deleted file mode 100644 index e75cf6ec3b..0000000000 --- a/test/behavior/bugs/12680.zig +++ /dev/null @@ -1,22 +0,0 @@ -const std = @import("std"); -const expectEqual = std.testing.expectEqual; -const other_file = @import("12680_other_file.zig"); -const builtin = @import("builtin"); - -extern fn test_func() callconv(.C) usize; - -test "export a function twice" { - if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf) return error.SkipZigTest; - - if (builtin.os.tag == .macos and builtin.zig_backend == .stage2_c) { - // TODO: test.c: error: aliases are not supported on darwin - return error.SkipZigTest; - } - - // If it exports the function correctly, `test_func` and `testFunc` will points to the same address. - try expectEqual(test_func(), other_file.testFunc()); -} diff --git a/test/behavior/bugs/12680_other_file.zig b/test/behavior/bugs/12680_other_file.zig deleted file mode 100644 index 5bcc9e1c16..0000000000 --- a/test/behavior/bugs/12680_other_file.zig +++ /dev/null @@ -1,8 +0,0 @@ -// export this function twice -pub export fn testFunc() callconv(.C) usize { - return @intFromPtr(&testFunc); -} - -comptime { - @export(testFunc, .{ .name = "test_func", .linkage = .Strong }); -} diff --git a/test/behavior/export_c_keywords.zig b/test/behavior/export_c_keywords.zig new file mode 100644 index 0000000000..a41a9e75d1 --- /dev/null +++ b/test/behavior/export_c_keywords.zig @@ -0,0 +1,44 @@ +pub const Id = enum(c_int) { + c_keyword_variable = 12, + non_c_keyword_variable = 34, + c_keyword_constant = 56, + non_c_keyword_constant = 78, + c_keyword_function = 910, + non_c_keyword_function = 1112, +}; + +export var int: Id = .c_keyword_variable; + +export var some_non_c_keyword_variable: Id = .non_c_keyword_variable; + +export const @"if": Id = .c_keyword_constant; + +export const some_non_c_keyword_constant: Id = .non_c_keyword_constant; + +export fn float() Id { + return .c_keyword_function; +} + +export fn some_non_c_keyword_function() Id { + return .non_c_keyword_function; +} + +comptime { + @export(int, .{ .name = "long" }); + @export(int, .{ .name = "an_alias_of_int" }); + + @export(some_non_c_keyword_variable, .{ .name = "void" }); + @export(some_non_c_keyword_variable, .{ .name = "an_alias_of_some_non_c_keyword_variable" }); + + @export(@"if", .{ .name = "else" }); + @export(@"if", .{ .name = "an_alias_of_if" }); + + @export(some_non_c_keyword_constant, .{ .name = "switch" }); + @export(some_non_c_keyword_constant, .{ .name = "an_alias_of_some_non_c_keyword_constant" }); + + @export(float, .{ .name = "double" }); + @export(float, .{ .name = "an_alias_of_float" }); + + @export(some_non_c_keyword_function, .{ .name = "break" }); + @export(some_non_c_keyword_function, .{ .name = "an_alias_of_some_non_c_keyword_function" }); +} diff --git a/test/behavior/import_c_keywords.zig b/test/behavior/import_c_keywords.zig new file mode 100644 index 0000000000..bb179bdf00 --- /dev/null +++ b/test/behavior/import_c_keywords.zig @@ -0,0 +1,92 @@ +const builtin = @import("builtin"); +const Id = @import("export_c_keywords.zig").Id; +const std = @import("std"); + +extern var int: Id; +extern var long: Id; +extern var an_alias_of_int: Id; + +extern var some_non_c_keyword_variable: Id; +extern var @"void": Id; +extern var an_alias_of_some_non_c_keyword_variable: Id; + +extern const @"if": Id; +extern const @"else": Id; +extern const an_alias_of_if: Id; + +extern const some_non_c_keyword_constant: Id; +extern const @"switch": Id; +extern const an_alias_of_some_non_c_keyword_constant: Id; + +extern fn float() Id; +extern fn double() Id; +extern fn an_alias_of_float() Id; + +extern fn some_non_c_keyword_function() Id; +extern fn @"break"() Id; +extern fn an_alias_of_some_non_c_keyword_function() Id; + +test "import c keywords" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf) return error.SkipZigTest; + + try std.testing.expect(int == .c_keyword_variable); + try std.testing.expect(long == .c_keyword_variable); + try std.testing.expect(an_alias_of_int == .c_keyword_variable); + + try std.testing.expect(some_non_c_keyword_variable == .non_c_keyword_variable); + try std.testing.expect(@"void" == .non_c_keyword_variable); + try std.testing.expect(an_alias_of_some_non_c_keyword_variable == .non_c_keyword_variable); + + try std.testing.expect(@"if" == .c_keyword_constant); + try std.testing.expect(@"else" == .c_keyword_constant); + try std.testing.expect(an_alias_of_if == .c_keyword_constant); + + try std.testing.expect(some_non_c_keyword_constant == .non_c_keyword_constant); + try std.testing.expect(@"switch" == .non_c_keyword_constant); + try std.testing.expect(an_alias_of_some_non_c_keyword_constant == .non_c_keyword_constant); + + try std.testing.expect(float() == .c_keyword_function); + try std.testing.expect(double() == .c_keyword_function); + try std.testing.expect(an_alias_of_float() == .c_keyword_function); + + try std.testing.expect(some_non_c_keyword_function() == .non_c_keyword_function); + try std.testing.expect(@"break"() == .non_c_keyword_function); + try std.testing.expect(an_alias_of_some_non_c_keyword_function() == .non_c_keyword_function); + + var ptr_id: *const Id = &long; + try std.testing.expect(ptr_id == &int); + ptr_id = &an_alias_of_int; + try std.testing.expect(ptr_id == &int); + + ptr_id = &@"void"; + try std.testing.expect(ptr_id == &some_non_c_keyword_variable); + ptr_id = &an_alias_of_some_non_c_keyword_variable; + try std.testing.expect(ptr_id == &some_non_c_keyword_variable); + + ptr_id = &@"else"; + try std.testing.expect(ptr_id == &@"if"); + ptr_id = &an_alias_of_if; + try std.testing.expect(ptr_id == &@"if"); + + ptr_id = &@"switch"; + try std.testing.expect(ptr_id == &some_non_c_keyword_constant); + ptr_id = &an_alias_of_some_non_c_keyword_constant; + try std.testing.expect(ptr_id == &some_non_c_keyword_constant); + + if (builtin.target.ofmt != .coff and builtin.target.os.tag != .windows) { + var ptr_fn: *const fn () callconv(.C) Id = &double; + try std.testing.expect(ptr_fn == &float); + ptr_fn = &an_alias_of_float; + try std.testing.expect(ptr_fn == &float); + + ptr_fn = &@"break"; + try std.testing.expect(ptr_fn == &some_non_c_keyword_function); + ptr_fn = &an_alias_of_some_non_c_keyword_function; + try std.testing.expect(ptr_fn == &some_non_c_keyword_function); + } +} |
