aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-01-21 00:49:58 -0700
committerAndrew Kelley <andrew@ziglang.org>2022-01-24 21:47:53 -0700
commitb34f994c0ba2d87fce2a3409d6bcfa7a5ebe78ff (patch)
tree436e0ad81e8baddbdcbe5d7148338bcac11a23df /lib/std
parent0866fa9d1d46f3c66a4adcaf1d863e762f874c6c (diff)
downloadzig-b34f994c0ba2d87fce2a3409d6bcfa7a5ebe78ff.tar.gz
zig-b34f994c0ba2d87fce2a3409d6bcfa7a5ebe78ff.zip
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.
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/builtin.zig8
1 files changed, 7 insertions, 1 deletions
diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig
index ba5801e936..3675a90257 100644
--- a/lib/std/builtin.zig
+++ b/lib/std/builtin.zig
@@ -730,10 +730,16 @@ pub const CompilerBackend = enum(u64) {
/// therefore must be kept in sync with the compiler implementation.
pub const TestFn = struct {
name: []const u8,
- func: fn () anyerror!void,
+ func: testFnProto,
async_frame_size: ?usize,
};
+/// stage1 is *wrong*. It is not yet updated to support the new function type semantics.
+const testFnProto = switch (builtin.zig_backend) {
+ .stage1 => fn () anyerror!void, // wrong!
+ else => *const fn () anyerror!void,
+};
+
/// This function type is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const PanicFn = fn ([]const u8, ?*StackTrace) noreturn;