authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-08 18:38:50+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-08 19:09:25-05:00
log8ac711596d3fd698bf3ee84ec2514c8c84103680
tree69710057d7062305504955cf0c11780e66141abf
parent8f646daed6d73fad4adf2b677b1b47f8b23f2d6c

stage1: Validate the specified cc for lazy fn types

Closes #7337

4 files changed, 31 insertions(+), 3 deletions(-)

src/stage1/analyze.cpp+5-3
...@@ -1880,7 +1880,7 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {...@@ -1880,7 +1880,7 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {
1880}1880}
18811881
1882// Sync this with get_llvm_cc in codegen.cpp1882// Sync this with get_llvm_cc in codegen.cpp
1883static Error emit_error_unless_callconv_allowed_for_target(CodeGen *g, AstNode *source_node, CallingConvention cc) {1883Error emit_error_unless_callconv_allowed_for_target(CodeGen *g, AstNode *source_node, CallingConvention cc) {
1884 Error ret = ErrorNone;1884 Error ret = ErrorNone;
1885 const char *allowed_platforms = nullptr;1885 const char *allowed_platforms = nullptr;
1886 switch (cc) {1886 switch (cc) {
...@@ -2065,8 +2065,10 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc...@@ -2065,8 +2065,10 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
2065 fn_entry->align_bytes = fn_type_id.alignment;2065 fn_entry->align_bytes = fn_type_id.alignment;
2066 }2066 }
20672067
2068 if ((err = emit_error_unless_callconv_allowed_for_target(g, proto_node->data.fn_proto.callconv_expr, cc)))2068 if (proto_node->data.fn_proto.callconv_expr != nullptr) {
2069 return g->builtin_types.entry_invalid;2069 if ((err = emit_error_unless_callconv_allowed_for_target(g, proto_node->data.fn_proto.callconv_expr, cc)))
2070 return g->builtin_types.entry_invalid;
2071 }
20702072
2071 if (fn_proto->return_anytype_token != nullptr) {2073 if (fn_proto->return_anytype_token != nullptr) {
2072 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {2074 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
src/stage1/analyze.hpp+1
...@@ -46,6 +46,7 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c...@@ -46,6 +46,7 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c
46ZigType *get_test_fn_type(CodeGen *g);46ZigType *get_test_fn_type(CodeGen *g);
47ZigType *get_any_frame_type(CodeGen *g, ZigType *result_type);47ZigType *get_any_frame_type(CodeGen *g, ZigType *result_type);
48bool handle_is_ptr(CodeGen *g, ZigType *type_entry);48bool handle_is_ptr(CodeGen *g, ZigType *type_entry);
49Error emit_error_unless_callconv_allowed_for_target(CodeGen *g, AstNode *source_node, CallingConvention cc);
4950
50bool type_has_bits(CodeGen *g, ZigType *type_entry);51bool type_has_bits(CodeGen *g, ZigType *type_entry);
51Error type_has_bits2(CodeGen *g, ZigType *type_entry, bool *result);52Error type_has_bits2(CodeGen *g, ZigType *type_entry, bool *result);
src/stage1/ir.cpp+5
...@@ -32796,6 +32796,11 @@ static ZigType *ir_resolve_lazy_fn_type(IrAnalyze *ira, AstNode *source_node, La...@@ -32796,6 +32796,11 @@ static ZigType *ir_resolve_lazy_fn_type(IrAnalyze *ira, AstNode *source_node, La
32796 FnTypeId fn_type_id = {0};32796 FnTypeId fn_type_id = {0};
32797 init_fn_type_id(&fn_type_id, proto_node, lazy_fn_type->cc, proto_node->data.fn_proto.params.length);32797 init_fn_type_id(&fn_type_id, proto_node, lazy_fn_type->cc, proto_node->data.fn_proto.params.length);
3279832798
32799 if (proto_node->data.fn_proto.callconv_expr != nullptr) {
32800 if ((err = emit_error_unless_callconv_allowed_for_target(ira->codegen, proto_node->data.fn_proto.callconv_expr, lazy_fn_type->cc)))
32801 return nullptr;
32802 }
32803
32799 for (; fn_type_id.next_param_index < fn_type_id.param_count; fn_type_id.next_param_index += 1) {32804 for (; fn_type_id.next_param_index < fn_type_id.param_count; fn_type_id.next_param_index += 1) {
32800 AstNode *param_node = proto_node->data.fn_proto.params.at(fn_type_id.next_param_index);32805 AstNode *param_node = proto_node->data.fn_proto.params.at(fn_type_id.next_param_index);
32801 assert(param_node->type == NodeTypeParamDecl);32806 assert(param_node->type == NodeTypeParamDecl);
test/compile_errors.zig+20
...@@ -94,6 +94,26 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -94,6 +94,26 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
94 };94 };
95 break :x tc;95 break :x tc;
96 });96 });
97 cases.addCase(x: {
98 var tc = cases.create("callconv(.Stdcall, .Fastcall, .Thiscall) on unsupported platform",
99 \\const F1 = fn () callconv(.Stdcall) void;
100 \\const F2 = fn () callconv(.Fastcall) void;
101 \\const F3 = fn () callconv(.Thiscall) void;
102 \\export fn entry1() void { var a: F1 = undefined; }
103 \\export fn entry2() void { var a: F2 = undefined; }
104 \\export fn entry3() void { var a: F3 = undefined; }
105 , &[_][]const u8{
106 "tmp.zig:1:27: error: callconv 'Stdcall' is only available on x86, not x86_64",
107 "tmp.zig:2:27: error: callconv 'Fastcall' is only available on x86, not x86_64",
108 "tmp.zig:3:27: error: callconv 'Thiscall' is only available on x86, not x86_64",
109 });
110 tc.target = std.zig.CrossTarget{
111 .cpu_arch = .x86_64,
112 .os_tag = .linux,
113 .abi = .none,
114 };
115 break :x tc;
116 });
97117
98 cases.addCase(x: {118 cases.addCase(x: {
99 var tc = cases.create("callconv(.Stdcall, .Fastcall, .Thiscall) on unsupported platform",119 var tc = cases.create("callconv(.Stdcall, .Fastcall, .Thiscall) on unsupported platform",