zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 29815fe9de50be3b07e39cd02ccfcc801a54c455 (tree)
parent 62120e3d0e382274dec2962f376ceb1e645f1a30
Author: Veikka Tuominen <git@vexu.eu>
Date:   Tue, 12 Jul 2022 14:22:57 +0300

Sema: disallow 'align' on functions on wasm

Diffstat:
Msrc/Sema.zig | 4++++
Atest/cases/compile_errors/align_n_expr_function_pointers_is_a_compile_error.zig | 9+++++++++
Atest/cases/compile_errors/array_access_of_non_array.zig | 15+++++++++++++++
Atest/cases/compile_errors/array_access_with_non_integer_index.zig | 17+++++++++++++++++
Atest/cases/compile_errors/control_reaches_end_of_non-void_function.zig | 9+++++++++
Atest/cases/compile_errors/endless_loop_in_function_evaluation.zig | 15+++++++++++++++
Atest/cases/compile_errors/load_too_many_bytes_from_comptime_reinterpreted_pointer.zig | 13+++++++++++++
Rtest/cases/compile_errors/stage1/obj/aligned_variable_of_zero-bit_type.zig -> test/cases/compile_errors/stage1/aligned_variable_of_zero-bit_type.zig | 0
Rtest/cases/compile_errors/stage1/obj/implicit_casting_undefined_c_pointer_to_zig_pointer.zig -> test/cases/compile_errors/stage1/implicit_casting_undefined_c_pointer_to_zig_pointer.zig | 0
Dtest/cases/compile_errors/stage1/obj/align_n_expr_function_pointers_is_a_compile_error.zig | 9---------
Dtest/cases/compile_errors/stage1/obj/array_access_of_non_array.zig | 15---------------
Dtest/cases/compile_errors/stage1/obj/array_access_with_non_integer_index.zig | 17-----------------
Dtest/cases/compile_errors/stage1/obj/control_reaches_end_of_non-void_function.zig | 8--------
Dtest/cases/compile_errors/stage1/obj/endless_loop_in_function_evaluation.zig | 12------------
Dtest/cases/compile_errors/stage1/obj/load_too_many_bytes_from_comptime_reinterpreted_pointer.zig | 13-------------
Dtest/cases/compile_errors/stage1/obj/use_implicit_casts_to_assign_null_to_non-nullable_pointer.zig | 14--------------
Atest/cases/compile_errors/use_implicit_casts_to_assign_null_to_non-nullable_pointer.zig | 16++++++++++++++++
17 files changed, 98 insertions(+), 88 deletions(-)

diff --git a/src/Sema.zig b/src/Sema.zig @@ -17713,6 +17713,10 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A break :blk lib_name; } else null; + if ((extra.data.bits.has_align_body or extra.data.bits.has_align_ref) and sema.mod.getTarget().cpu.arch.isWasm()) { + return sema.fail(block, align_src, "'align' is not allowed on functions in wasm", .{}); + } + const @"align": ?u32 = if (extra.data.bits.has_align_body) blk: { const body_len = sema.code.extra[extra_index]; extra_index += 1; diff --git a/test/cases/compile_errors/align_n_expr_function_pointers_is_a_compile_error.zig b/test/cases/compile_errors/align_n_expr_function_pointers_is_a_compile_error.zig @@ -0,0 +1,9 @@ +export fn foo() align(1) void { + return; +} + +// error +// backend=stage2 +// target=wasm32-freestanding-none +// +// :1:8: error: 'align' is not allowed on functions in wasm diff --git a/test/cases/compile_errors/array_access_of_non_array.zig b/test/cases/compile_errors/array_access_of_non_array.zig @@ -0,0 +1,15 @@ +export fn f() void { + var bad : bool = undefined; + bad[0] = bad[0]; +} +export fn g() void { + var bad : bool = undefined; + _ = bad[0]; +} + +// error +// backend=stage2 +// target=native +// +// :3:8: error: element access of non-indexable type 'bool' +// :7:12: error: element access of non-indexable type 'bool' diff --git a/test/cases/compile_errors/array_access_with_non_integer_index.zig b/test/cases/compile_errors/array_access_with_non_integer_index.zig @@ -0,0 +1,17 @@ +export fn f() void { + var array = "aoeu"; + var bad = false; + array[bad] = array[bad]; +} +export fn g() void { + var array = "aoeu"; + var bad = false; + _ = array[bad]; +} + +// error +// backend=stage2 +// target=native +// +// :4:11: error: expected type 'usize', found 'bool' +// :9:15: error: expected type 'usize', found 'bool' diff --git a/test/cases/compile_errors/control_reaches_end_of_non-void_function.zig b/test/cases/compile_errors/control_reaches_end_of_non-void_function.zig @@ -0,0 +1,9 @@ +fn a() i32 {} +export fn entry() void { _ = a(); } + +// error +// backend=stage2 +// target=native +// +// :1:13: error: expected type 'i32', found 'void' +// :1:8: note: function return type declared here diff --git a/test/cases/compile_errors/endless_loop_in_function_evaluation.zig b/test/cases/compile_errors/endless_loop_in_function_evaluation.zig @@ -0,0 +1,15 @@ +const seventh_fib_number = fibonacci(7); +fn fibonacci(x: i32) i32 { + return fibonacci(x - 1) + fibonacci(x - 2); +} + +export fn entry() usize { return @sizeOf(@TypeOf(&seventh_fib_number)); } + +// error +// backend=stage2 +// target=native +// +// :3:21: error: evaluation exceeded 1000 backwards branches +// :3:21: note: use @setEvalBranchQuota() to raise the branch limit from 1000 +// :3:21: note: called from here (999 times) +// :1:37: note: called from here diff --git a/test/cases/compile_errors/load_too_many_bytes_from_comptime_reinterpreted_pointer.zig b/test/cases/compile_errors/load_too_many_bytes_from_comptime_reinterpreted_pointer.zig @@ -0,0 +1,13 @@ +export fn entry() void { + const float: f32 = 5.99999999999994648725e-01; + const float_ptr = &float; + const int_ptr = @ptrCast(*const i64, float_ptr); + const int_val = int_ptr.*; + _ = int_val; +} + +// error +// backend=stage2 +// target=native +// +// :5:28: error: dereference of '*const i64' exceeds bounds of containing decl of type 'f32' diff --git a/test/cases/compile_errors/stage1/obj/aligned_variable_of_zero-bit_type.zig b/test/cases/compile_errors/stage1/aligned_variable_of_zero-bit_type.zig diff --git a/test/cases/compile_errors/stage1/obj/implicit_casting_undefined_c_pointer_to_zig_pointer.zig b/test/cases/compile_errors/stage1/implicit_casting_undefined_c_pointer_to_zig_pointer.zig diff --git a/test/cases/compile_errors/stage1/obj/align_n_expr_function_pointers_is_a_compile_error.zig b/test/cases/compile_errors/stage1/obj/align_n_expr_function_pointers_is_a_compile_error.zig @@ -1,9 +0,0 @@ -export fn foo() align(1) void { - return; -} - -// error -// backend=stage1 -// target=wasm32-freestanding-none -// -// tmp.zig:1:23: error: align(N) expr is not allowed on function prototypes in wasm32/wasm64 diff --git a/test/cases/compile_errors/stage1/obj/array_access_of_non_array.zig b/test/cases/compile_errors/stage1/obj/array_access_of_non_array.zig @@ -1,15 +0,0 @@ -export fn f() void { - var bad : bool = undefined; - bad[0] = bad[0]; -} -export fn g() void { - var bad : bool = undefined; - _ = bad[0]; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:3:8: error: array access of non-array type 'bool' -// tmp.zig:7:12: error: array access of non-array type 'bool' diff --git a/test/cases/compile_errors/stage1/obj/array_access_with_non_integer_index.zig b/test/cases/compile_errors/stage1/obj/array_access_with_non_integer_index.zig @@ -1,17 +0,0 @@ -export fn f() void { - var array = "aoeu"; - var bad = false; - array[bad] = array[bad]; -} -export fn g() void { - var array = "aoeu"; - var bad = false; - _ = array[bad]; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:4:11: error: expected type 'usize', found 'bool' -// tmp.zig:9:15: error: expected type 'usize', found 'bool' diff --git a/test/cases/compile_errors/stage1/obj/control_reaches_end_of_non-void_function.zig b/test/cases/compile_errors/stage1/obj/control_reaches_end_of_non-void_function.zig @@ -1,8 +0,0 @@ -fn a() i32 {} -export fn entry() void { _ = a(); } - -// error -// backend=stage1 -// target=native -// -// tmp.zig:1:12: error: expected type 'i32', found 'void' diff --git a/test/cases/compile_errors/stage1/obj/endless_loop_in_function_evaluation.zig b/test/cases/compile_errors/stage1/obj/endless_loop_in_function_evaluation.zig @@ -1,12 +0,0 @@ -const seventh_fib_number = fibonacci(7); -fn fibonacci(x: i32) i32 { - return fibonacci(x - 1) + fibonacci(x - 2); -} - -export fn entry() usize { return @sizeOf(@TypeOf(seventh_fib_number)); } - -// error -// backend=stage1 -// target=native -// -// tmp.zig:3:21: error: evaluation exceeded 1000 backwards branches diff --git a/test/cases/compile_errors/stage1/obj/load_too_many_bytes_from_comptime_reinterpreted_pointer.zig b/test/cases/compile_errors/stage1/obj/load_too_many_bytes_from_comptime_reinterpreted_pointer.zig @@ -1,13 +0,0 @@ -export fn entry() void { - const float: f32 = 5.99999999999994648725e-01; - const float_ptr = &float; - const int_ptr = @ptrCast(*const i64, float_ptr); - const int_val = int_ptr.*; - _ = int_val; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:5:28: error: attempt to read 8 bytes from pointer to f32 which is 4 bytes diff --git a/test/cases/compile_errors/stage1/obj/use_implicit_casts_to_assign_null_to_non-nullable_pointer.zig b/test/cases/compile_errors/stage1/obj/use_implicit_casts_to_assign_null_to_non-nullable_pointer.zig @@ -1,14 +0,0 @@ -export fn entry() void { - var x: i32 = 1234; - var p: *i32 = &x; - var pp: *?*i32 = &p; - pp.* = null; - var y = p.*; - _ = y; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:4:23: error: expected type '*?*i32', found '**i32' diff --git a/test/cases/compile_errors/use_implicit_casts_to_assign_null_to_non-nullable_pointer.zig b/test/cases/compile_errors/use_implicit_casts_to_assign_null_to_non-nullable_pointer.zig @@ -0,0 +1,16 @@ +export fn entry() void { + var x: i32 = 1234; + var p: *i32 = &x; + var pp: *?*i32 = &p; + pp.* = null; + var y = p.*; + _ = y; +} + +// error +// backend=stage2 +// target=native +// +// :4:22: error: expected type '*?*i32', found '**i32' +// :4:22: note: pointer type child '*i32' cannot cast into pointer type child '?*i32' +// :4:22: note: mutable '*i32' allows illegal null values stored to type '?*i32'