authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-21 13:07:27-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-21 13:55:29-07:00
logfc6e111b76764ae00e2c868ad46f39235837e239
treebde27d8b548e01d17a3fac8cfb61abbc396f68ba
parentb9469345761bbac5dcb95f2ba662d4ddd7fc567e

Sema: improve compile error for bad function alignment

* Integrate more declaratively with src/target.zig * Only trigger the check when a function body is found, do not trigger for function types.

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

src/Sema.zig+6-3
...@@ -18010,6 +18010,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -18010,6 +18010,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
18010 const section_src: LazySrcLoc = .{ .node_offset_fn_type_section = inst_data.src_node };18010 const section_src: LazySrcLoc = .{ .node_offset_fn_type_section = inst_data.src_node };
18011 const cc_src: LazySrcLoc = .{ .node_offset_fn_type_cc = inst_data.src_node };18011 const cc_src: LazySrcLoc = .{ .node_offset_fn_type_cc = inst_data.src_node };
18012 const ret_src: LazySrcLoc = .{ .node_offset_fn_type_ret_ty = inst_data.src_node };18012 const ret_src: LazySrcLoc = .{ .node_offset_fn_type_ret_ty = inst_data.src_node };
18013 const has_body = extra.data.body_len != 0;
1801318014
18014 var extra_index: usize = extra.end;18015 var extra_index: usize = extra.end;
1801518016
...@@ -18019,8 +18020,11 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -18019,8 +18020,11 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
18019 break :blk lib_name;18020 break :blk lib_name;
18020 } else null;18021 } else null;
1802118022
18022 if ((extra.data.bits.has_align_body or extra.data.bits.has_align_ref) and sema.mod.getTarget().cpu.arch.isWasm()) {18023 if (has_body and
18023 return sema.fail(block, align_src, "'align' is not allowed on functions in wasm", .{});18024 (extra.data.bits.has_align_body or extra.data.bits.has_align_ref) and
18025 !target_util.supportsFunctionAlignment(target))
18026 {
18027 return sema.fail(block, align_src, "target does not support function alignment", .{});
18024 }18028 }
1802518029
18026 const @"align": ?u32 = if (extra.data.bits.has_align_body) blk: {18030 const @"align": ?u32 = if (extra.data.bits.has_align_body) blk: {
...@@ -18162,7 +18166,6 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -18162,7 +18166,6 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
18162 } else 0;18166 } else 0;
1816318167
18164 var src_locs: Zir.Inst.Func.SrcLocs = undefined;18168 var src_locs: Zir.Inst.Func.SrcLocs = undefined;
18165 const has_body = extra.data.body_len != 0;
18166 if (has_body) {18169 if (has_body) {
18167 extra_index += extra.data.body_len;18170 extra_index += extra.data.body_len;
18168 src_locs = sema.code.extraData(Zir.Inst.Func.SrcLocs, extra_index).data;18171 src_locs = sema.code.extraData(Zir.Inst.Func.SrcLocs, extra_index).data;
src/target.zig+8
...@@ -744,6 +744,7 @@ pub fn llvmMachineAbi(target: std.Target) ?[:0]const u8 {...@@ -744,6 +744,7 @@ pub fn llvmMachineAbi(target: std.Target) ?[:0]const u8 {
744 }744 }
745}745}
746746
747/// This function returns 1 if function alignment is not observable or settable.
747pub fn defaultFunctionAlignment(target: std.Target) u32 {748pub fn defaultFunctionAlignment(target: std.Target) u32 {
748 return switch (target.cpu.arch) {749 return switch (target.cpu.arch) {
749 .arm, .armeb => 4,750 .arm, .armeb => 4,
...@@ -753,3 +754,10 @@ pub fn defaultFunctionAlignment(target: std.Target) u32 {...@@ -753,3 +754,10 @@ pub fn defaultFunctionAlignment(target: std.Target) u32 {
753 else => 1,754 else => 1,
754 };755 };
755}756}
757
758pub fn supportsFunctionAlignment(target: std.Target) bool {
759 return switch (target.cpu.arch) {
760 .wasm32, .wasm64 => false,
761 else => true,
762 };
763}
test/cases/compile_errors/align_n_expr_function_pointers_is_a_compile_error.zig+1-1
...@@ -6,4 +6,4 @@ export fn foo() align(1) void {...@@ -6,4 +6,4 @@ export fn foo() align(1) void {
6// backend=stage26// backend=stage2
7// target=wasm32-freestanding-none7// target=wasm32-freestanding-none
8//8//
9// :1:23: error: 'align' is not allowed on functions in wasm
\ No newline at end of file
9// :1:23: error: target does not support function alignment