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) {
18801880}
18811881
18821882// 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) {
18841884 Error ret = ErrorNone;
18851885 const char *allowed_platforms = nullptr;
18861886 switch (cc) {
......@@ -2065,8 +2065,10 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
20652065 fn_entry->align_bytes = fn_type_id.alignment;
20662066 }
20672067
2068 if ((err = emit_error_unless_callconv_allowed_for_target(g, proto_node->data.fn_proto.callconv_expr, cc)))
2069 return g->builtin_types.entry_invalid;
2068 if (proto_node->data.fn_proto.callconv_expr != nullptr) {
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
20712073 if (fn_proto->return_anytype_token != nullptr) {
20722074 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
4646ZigType *get_test_fn_type(CodeGen *g);
4747ZigType *get_any_frame_type(CodeGen *g, ZigType *result_type);
4848bool handle_is_ptr(CodeGen *g, ZigType *type_entry);
49Error emit_error_unless_callconv_allowed_for_target(CodeGen *g, AstNode *source_node, CallingConvention cc);
4950
5051bool type_has_bits(CodeGen *g, ZigType *type_entry);
5152Error 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
3279632796 FnTypeId fn_type_id = {0};
3279732797 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
3279932804 for (; fn_type_id.next_param_index < fn_type_id.param_count; fn_type_id.next_param_index += 1) {
3280032805 AstNode *param_node = proto_node->data.fn_proto.params.at(fn_type_id.next_param_index);
3280132806 assert(param_node->type == NodeTypeParamDecl);
test/compile_errors.zig+20
......@@ -94,6 +94,26 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
9494 };
9595 break :x tc;
9696 });
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
98118 cases.addCase(x: {
99119 var tc = cases.create("callconv(.Stdcall, .Fastcall, .Thiscall) on unsupported platform",