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.
This commit is contained in:
Andrew Kelley
2022-01-21 00:49:58 -07:00
parent 0866fa9d1d
commit b34f994c0b
24 changed files with 858 additions and 356 deletions

View File

@@ -1520,6 +1520,11 @@ pub const Value = extern union {
}
return true;
},
.function => {
const a_payload = a.castTag(.function).?.data;
const b_payload = b.castTag(.function).?.data;
return a_payload == b_payload;
},
else => {},
}
} else if (a_tag == .null_value or b_tag == .null_value) {
@@ -1573,6 +1578,7 @@ pub const Value = extern union {
pub fn hash(val: Value, ty: Type, hasher: *std.hash.Wyhash) void {
const zig_ty_tag = ty.zigTypeTag();
std.hash.autoHash(hasher, zig_ty_tag);
if (val.isUndef()) return;
switch (zig_ty_tag) {
.BoundFn => unreachable, // TODO remove this from the language
@@ -1694,7 +1700,8 @@ pub const Value = extern union {
union_obj.val.hash(active_field_ty, hasher);
},
.Fn => {
@panic("TODO implement hashing function values");
const func = val.castTag(.function).?.data;
return std.hash.autoHash(hasher, func.owner_decl);
},
.Frame => {
@panic("TODO implement hashing frame values");
@@ -1703,7 +1710,8 @@ pub const Value = extern union {
@panic("TODO implement hashing anyframe values");
},
.EnumLiteral => {
@panic("TODO implement hashing enum literal values");
const bytes = val.castTag(.enum_literal).?.data;
hasher.update(bytes);
},
}
}