zig/test/behavior.zig

139 lines
5.7 KiB
Zig
Raw Normal View History

const builtin = @import("builtin");
test {
stage2: type system treats fn ptr and body separately This commit updates stage2 to enforce the property that the syntax `fn()void` is a function *body* not a *pointer*. To get a pointer, the syntax `*const fn()void` is required. ZIR puts function alignment into the func instruction rather than the decl because this way it makes it into function types. LLVM backend respects function alignments. Struct and Union have methods `fieldSrcLoc` to help look up source locations of their fields. These trigger full loading, tokenization, and parsing of source files, so should only be called once it is confirmed that an error message needs to be printed. There are some nice new error hints for explaining why a type is required to be comptime, particularly for structs that contain function body types. `Type.requiresComptime` is now moved into Sema because it can fail and might need to trigger field type resolution. Comptime pointer loading takes into account types that do not have a well-defined memory layout and does not try to compute a byte offset for them. `fn()void` syntax no longer secretly makes a pointer. You get a function body type, which requires comptime. However a pointer to a function body can be runtime known (obviously). Compile errors that report "expected pointer, found ..." are factored out into convenience functions `checkPtrOperand` and `checkPtrType` and have a note about function pointers. Implemented `Value.hash` for functions, enum literals, and undefined values. stage1 is not updated to this (yet?), so some workarounds and disabled tests are needed to keep everything working. Should we update stage1 to these new type semantics? Yes probably because I don't want to add too much conditional compilation logic in the std lib for the different backends.
2022-01-21 07:49:58 +00:00
_ = @import("behavior/align.zig");
_ = @import("behavior/alignof.zig");
stage2: type system treats fn ptr and body separately This commit updates stage2 to enforce the property that the syntax `fn()void` is a function *body* not a *pointer*. To get a pointer, the syntax `*const fn()void` is required. ZIR puts function alignment into the func instruction rather than the decl because this way it makes it into function types. LLVM backend respects function alignments. Struct and Union have methods `fieldSrcLoc` to help look up source locations of their fields. These trigger full loading, tokenization, and parsing of source files, so should only be called once it is confirmed that an error message needs to be printed. There are some nice new error hints for explaining why a type is required to be comptime, particularly for structs that contain function body types. `Type.requiresComptime` is now moved into Sema because it can fail and might need to trigger field type resolution. Comptime pointer loading takes into account types that do not have a well-defined memory layout and does not try to compute a byte offset for them. `fn()void` syntax no longer secretly makes a pointer. You get a function body type, which requires comptime. However a pointer to a function body can be runtime known (obviously). Compile errors that report "expected pointer, found ..." are factored out into convenience functions `checkPtrOperand` and `checkPtrType` and have a note about function pointers. Implemented `Value.hash` for functions, enum literals, and undefined values. stage1 is not updated to this (yet?), so some workarounds and disabled tests are needed to keep everything working. Should we update stage1 to these new type semantics? Yes probably because I don't want to add too much conditional compilation logic in the std lib for the different backends.
2022-01-21 07:49:58 +00:00
_ = @import("behavior/array.zig");
_ = @import("behavior/async_fn.zig");
_ = @import("behavior/atomics.zig");
_ = @import("behavior/await_struct.zig");
_ = @import("behavior/basic.zig");
_ = @import("behavior/bit_shifting.zig");
2022-02-22 12:52:40 +00:00
_ = @import("behavior/bitcast.zig");
_ = @import("behavior/bitreverse.zig");
stage2: type system treats fn ptr and body separately This commit updates stage2 to enforce the property that the syntax `fn()void` is a function *body* not a *pointer*. To get a pointer, the syntax `*const fn()void` is required. ZIR puts function alignment into the func instruction rather than the decl because this way it makes it into function types. LLVM backend respects function alignments. Struct and Union have methods `fieldSrcLoc` to help look up source locations of their fields. These trigger full loading, tokenization, and parsing of source files, so should only be called once it is confirmed that an error message needs to be printed. There are some nice new error hints for explaining why a type is required to be comptime, particularly for structs that contain function body types. `Type.requiresComptime` is now moved into Sema because it can fail and might need to trigger field type resolution. Comptime pointer loading takes into account types that do not have a well-defined memory layout and does not try to compute a byte offset for them. `fn()void` syntax no longer secretly makes a pointer. You get a function body type, which requires comptime. However a pointer to a function body can be runtime known (obviously). Compile errors that report "expected pointer, found ..." are factored out into convenience functions `checkPtrOperand` and `checkPtrType` and have a note about function pointers. Implemented `Value.hash` for functions, enum literals, and undefined values. stage1 is not updated to this (yet?), so some workarounds and disabled tests are needed to keep everything working. Should we update stage1 to these new type semantics? Yes probably because I don't want to add too much conditional compilation logic in the std lib for the different backends.
2022-01-21 07:49:58 +00:00
_ = @import("behavior/bool.zig");
_ = @import("behavior/builtin_functions_returning_void_or_noreturn.zig");
_ = @import("behavior/byteswap.zig");
_ = @import("behavior/byval_arg_var.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/call.zig");
_ = @import("behavior/call_tail.zig");
stage2: type system treats fn ptr and body separately This commit updates stage2 to enforce the property that the syntax `fn()void` is a function *body* not a *pointer*. To get a pointer, the syntax `*const fn()void` is required. ZIR puts function alignment into the func instruction rather than the decl because this way it makes it into function types. LLVM backend respects function alignments. Struct and Union have methods `fieldSrcLoc` to help look up source locations of their fields. These trigger full loading, tokenization, and parsing of source files, so should only be called once it is confirmed that an error message needs to be printed. There are some nice new error hints for explaining why a type is required to be comptime, particularly for structs that contain function body types. `Type.requiresComptime` is now moved into Sema because it can fail and might need to trigger field type resolution. Comptime pointer loading takes into account types that do not have a well-defined memory layout and does not try to compute a byte offset for them. `fn()void` syntax no longer secretly makes a pointer. You get a function body type, which requires comptime. However a pointer to a function body can be runtime known (obviously). Compile errors that report "expected pointer, found ..." are factored out into convenience functions `checkPtrOperand` and `checkPtrType` and have a note about function pointers. Implemented `Value.hash` for functions, enum literals, and undefined values. stage1 is not updated to this (yet?), so some workarounds and disabled tests are needed to keep everything working. Should we update stage1 to these new type semantics? Yes probably because I don't want to add too much conditional compilation logic in the std lib for the different backends.
2022-01-21 07:49:58 +00:00
_ = @import("behavior/cast.zig");
_ = @import("behavior/cast_int.zig");
stage2: type system treats fn ptr and body separately This commit updates stage2 to enforce the property that the syntax `fn()void` is a function *body* not a *pointer*. To get a pointer, the syntax `*const fn()void` is required. ZIR puts function alignment into the func instruction rather than the decl because this way it makes it into function types. LLVM backend respects function alignments. Struct and Union have methods `fieldSrcLoc` to help look up source locations of their fields. These trigger full loading, tokenization, and parsing of source files, so should only be called once it is confirmed that an error message needs to be printed. There are some nice new error hints for explaining why a type is required to be comptime, particularly for structs that contain function body types. `Type.requiresComptime` is now moved into Sema because it can fail and might need to trigger field type resolution. Comptime pointer loading takes into account types that do not have a well-defined memory layout and does not try to compute a byte offset for them. `fn()void` syntax no longer secretly makes a pointer. You get a function body type, which requires comptime. However a pointer to a function body can be runtime known (obviously). Compile errors that report "expected pointer, found ..." are factored out into convenience functions `checkPtrOperand` and `checkPtrType` and have a note about function pointers. Implemented `Value.hash` for functions, enum literals, and undefined values. stage1 is not updated to this (yet?), so some workarounds and disabled tests are needed to keep everything working. Should we update stage1 to these new type semantics? Yes probably because I don't want to add too much conditional compilation logic in the std lib for the different backends.
2022-01-21 07:49:58 +00:00
_ = @import("behavior/comptime_memory.zig");
_ = @import("behavior/const_slice_child.zig");
_ = @import("behavior/decl_literals.zig");
_ = @import("behavior/decltest.zig");
_ = @import("behavior/duplicated_test_names.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/defer.zig");
compiler: implement destructuring syntax This change implements the following syntax into the compiler: ```zig const x: u32, var y, foo.bar = .{ 1, 2, 3 }; ``` A destructure expression may only appear within a block (i.e. not at comtainer scope). The LHS consists of a sequence of comma-separated var decls and/or lvalue expressions. The RHS is a normal expression. A new result location type, `destructure`, is used, which contains result pointers for each component of the destructure. This means that when the RHS is a more complicated expression, peer type resolution is not used: each result value is individually destructured and written to the result pointers. RLS is always used for destructure expressions, meaning every `const` on the LHS of such an expression creates a true stack allocation. Aside from anonymous array literals, Sema is capable of destructuring the following types: * Tuples * Arrays * Vectors A destructure may be prefixed with the `comptime` keyword, in which case the entire destructure is evaluated at comptime: this means all `var`s in the LHS are `comptime var`s, every lvalue expression is evaluated at comptime, and the RHS is evaluated at comptime. If every LHS is a `const`, this is not allowed: as with single declarations, the user should instead mark the RHS as `comptime`. There are a few subtleties in the grammar changes here. For one thing, if every LHS is an lvalue expression (rather than a var decl), a destructure is considered an expression. This makes, for instance, `if (cond) x, y = .{ 1, 2 };` valid Zig code. A destructure is allowed in almost every context where a standard assignment expression is permitted. The exception is `switch` prongs, which cannot be destructures as the comma is ambiguous with the end of the prong. A follow-up commit will begin utilizing this syntax in the Zig compiler. Resolves: #498
2023-08-31 14:30:58 +01:00
_ = @import("behavior/destructure.zig");
_ = @import("behavior/empty_union.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/enum.zig");
_ = @import("behavior/error.zig");
_ = @import("behavior/eval.zig");
_ = @import("behavior/export_builtin.zig");
_ = @import("behavior/export_self_referential_type_info.zig");
_ = @import("behavior/extern.zig");
_ = @import("behavior/field_parent_ptr.zig");
_ = @import("behavior/floatop.zig");
_ = @import("behavior/fn.zig");
_ = @import("behavior/fn_delegation.zig");
_ = @import("behavior/fn_in_struct_in_comptime.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/for.zig");
_ = @import("behavior/generics.zig");
_ = @import("behavior/globals.zig");
_ = @import("behavior/hasdecl.zig");
_ = @import("behavior/hasfield.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/if.zig");
_ = @import("behavior/import.zig");
_ = @import("behavior/import_c_keywords.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/incomplete_struct_param_tld.zig");
_ = @import("behavior/inline_switch.zig");
_ = @import("behavior/int128.zig");
_ = @import("behavior/int_comparison_elision.zig");
_ = @import("behavior/ptrfromint.zig");
_ = @import("behavior/ir_block_deps.zig");
2022-12-13 21:55:59 +00:00
_ = @import("behavior/lower_strlit_to_vector.zig");
_ = @import("behavior/math.zig");
_ = @import("behavior/maximum_minimum.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/member_func.zig");
_ = @import("behavior/memcpy.zig");
_ = @import("behavior/memset.zig");
_ = @import("behavior/merge_error_sets.zig");
_ = @import("behavior/muladd.zig");
_ = @import("behavior/multiple_externs_with_conflicting_types.zig");
_ = @import("behavior/namespace_depends_on_compile_var.zig");
_ = @import("behavior/nan.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/null.zig");
_ = @import("behavior/optional.zig");
_ = @import("behavior/packed-struct.zig");
_ = @import("behavior/packed_struct_explicit_backing_int.zig");
_ = @import("behavior/packed-union.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/pointers.zig");
_ = @import("behavior/popcount.zig");
_ = @import("behavior/prefetch.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/ptrcast.zig");
_ = @import("behavior/pub_enum.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/ref_var_in_if_after_if_2nd_switch_prong.zig");
_ = @import("behavior/reflection.zig");
_ = @import("behavior/return_address.zig");
_ = @import("behavior/saturating_arithmetic.zig");
_ = @import("behavior/select.zig");
_ = @import("behavior/shuffle.zig");
_ = @import("behavior/sizeof_and_typeof.zig");
_ = @import("behavior/slice.zig");
_ = @import("behavior/slice_sentinel_comptime.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/src.zig");
_ = @import("behavior/string_literals.zig");
_ = @import("behavior/struct.zig");
_ = @import("behavior/struct_contains_null_ptr_itself.zig");
_ = @import("behavior/struct_contains_slice_of_itself.zig");
_ = @import("behavior/switch.zig");
_ = @import("behavior/switch_loop.zig");
_ = @import("behavior/switch_prong_err_enum.zig");
_ = @import("behavior/switch_prong_implicit_cast.zig");
_ = @import("behavior/switch_on_captured_error.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/this.zig");
_ = @import("behavior/threadlocal.zig");
_ = @import("behavior/truncate.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/try.zig");
_ = @import("behavior/tuple.zig");
_ = @import("behavior/tuple_declarations.zig");
_ = @import("behavior/type.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/type_info.zig");
_ = @import("behavior/type_info_mul_linksection_addrspace_decls.zig");
_ = @import("behavior/typename.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/undefined.zig");
_ = @import("behavior/underscore.zig");
_ = @import("behavior/union.zig");
_ = @import("behavior/union_with_members.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/usingnamespace.zig");
_ = @import("behavior/var_args.zig");
_ = @import("behavior/vector.zig");
2022-02-22 13:15:09 +00:00
_ = @import("behavior/void.zig");
_ = @import("behavior/while.zig");
_ = @import("behavior/widening.zig");
2023-07-18 00:52:14 +01:00
_ = @import("behavior/abs.zig");
if (builtin.cpu.arch == .wasm32) {
2022-03-03 18:16:30 +00:00
_ = @import("behavior/wasm.zig");
}
if (builtin.os.tag != .wasi) {
_ = @import("behavior/asm.zig");
}
2022-02-22 13:15:09 +00:00
if (builtin.zig_backend != .stage2_arm and
builtin.zig_backend != .stage2_aarch64 and
2023-05-11 06:53:34 +01:00
builtin.zig_backend != .stage2_spirv64)
2022-02-22 13:15:09 +00:00
{
_ = @import("behavior/export_keyword.zig");
}
}
// This bug only repros in the root file
test "deference @embedFile() of a file full of zero bytes" {
const contents = @embedFile("behavior/zero.bin").*;
try @import("std").testing.expect(contents.len == 456);
for (contents) |byte| try @import("std").testing.expect(byte == 0);
}