authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-09-26 20:53:26-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-09-26 20:53:26-07:00
log2e14cbe9cd4a5a17fcdfcfa79869feef718f3076
treebd6a5cbb7737efd844cd2ab7ccdff85b32cbc99a
parent5b1a9fbb45128fdab615d7af5e93fa037e5d7749

Sema: better utility function semantics

better names, return error instead of panicking, better diagnostics, use the standard APIs for resolving values

1 files changed, 71 insertions(+), 81 deletions(-)

src/Sema.zig+71-81
......@@ -14209,10 +14209,10 @@ fn maybeErrorUnwrap(
1420914209 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
1421014210 const msg_inst = try sema.resolveInst(inst_data.operand);
1421114211
14212 const panic_fn = try getBuiltinInnerAsInst(sema, block, operand_src, "Panic", "call");
14212 const panic_fn = try getPanicInnerFn(sema, block, operand_src, "call");
1421314213 const err_return_trace = try sema.getErrorReturnTrace(block);
1421414214 const args: [3]Air.Inst.Ref = .{ msg_inst, err_return_trace, .null_value };
14215 try sema.callBuiltin(block, operand_src, panic_fn, .auto, &args, .@"safety check");
14215 try sema.callBuiltin(block, operand_src, Air.internedToRef(panic_fn), .auto, &args, .@"safety check");
1421614216 return true;
1421714217 },
1421814218 else => unreachable,
......@@ -18293,8 +18293,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1829318293 => |type_info_tag| return unionInitFromEnumTag(sema, block, src, type_info_ty, @intFromEnum(type_info_tag), .void_value),
1829418294
1829518295 .@"fn" => {
18296 const fn_info_ty = try getInnerType(sema, block, src, type_info_ty, "Fn");
18297 const param_info_ty = try getInnerType(sema, block, src, fn_info_ty, "Param");
18296 const fn_info_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "Fn");
18297 const param_info_ty = try getBuiltinInnerType(sema, block, src, fn_info_ty, "Type.Fn", "Param");
1829818298
1829918299 const func_ty_info = zcu.typeToFunc(ty).?;
1830018300 const param_vals = try sema.arena.alloc(InternPool.Index, func_ty_info.param_types.len);
......@@ -18388,7 +18388,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1838818388 })));
1838918389 },
1839018390 .int => {
18391 const int_info_ty = try getInnerType(sema, block, src, type_info_ty, "Int");
18391 const int_info_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "Int");
1839218392 const signedness_ty = try sema.getBuiltinType("Signedness");
1839318393 const info = ty.intInfo(zcu);
1839418394 const field_values = .{
......@@ -18407,7 +18407,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1840718407 })));
1840818408 },
1840918409 .float => {
18410 const float_info_ty = try getInnerType(sema, block, src, type_info_ty, "Float");
18410 const float_info_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "Float");
1841118411
1841218412 const field_vals = .{
1841318413 // bits: u16,
......@@ -18430,8 +18430,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1843018430 try Type.fromInterned(info.child).lazyAbiAlignment(pt);
1843118431
1843218432 const addrspace_ty = try sema.getBuiltinType("AddressSpace");
18433 const pointer_ty = try getInnerType(sema, block, src, type_info_ty, "Pointer");
18434 const ptr_size_ty = try getInnerType(sema, block, src, pointer_ty, "Size");
18433 const pointer_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "Pointer");
18434 const ptr_size_ty = try getBuiltinInnerType(sema, block, src, pointer_ty, "Type.Pointer", "Size");
1843518435
1843618436 const field_values = .{
1843718437 // size: Size,
......@@ -18464,7 +18464,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1846418464 })));
1846518465 },
1846618466 .array => {
18467 const array_field_ty = try getInnerType(sema, block, src, type_info_ty, "Array");
18467 const array_field_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "Array");
1846818468
1846918469 const info = ty.arrayInfo(zcu);
1847018470 const field_values = .{
......@@ -18485,7 +18485,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1848518485 })));
1848618486 },
1848718487 .vector => {
18488 const vector_field_ty = try getInnerType(sema, block, src, type_info_ty, "Vector");
18488 const vector_field_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "Vector");
1848918489
1849018490 const info = ty.arrayInfo(zcu);
1849118491 const field_values = .{
......@@ -18504,7 +18504,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1850418504 })));
1850518505 },
1850618506 .optional => {
18507 const optional_field_ty = try getInnerType(sema, block, src, type_info_ty, "Optional");
18507 const optional_field_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "Optional");
1850818508
1850918509 const field_values = .{
1851018510 // child: type,
......@@ -18521,7 +18521,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1852118521 },
1852218522 .error_set => {
1852318523 // Get the Error type
18524 const error_field_ty = try getInnerType(sema, block, src, type_info_ty, "Error");
18524 const error_field_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "Error");
1852518525
1852618526 // Build our list of Error values
1852718527 // Optional value is only null if anyerror
......@@ -18617,7 +18617,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1861718617 })));
1861818618 },
1861918619 .error_union => {
18620 const error_union_field_ty = try getInnerType(sema, block, src, type_info_ty, "ErrorUnion");
18620 const error_union_field_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "ErrorUnion");
1862118621
1862218622 const field_values = .{
1862318623 // error_set: type,
......@@ -18637,7 +18637,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1863718637 .@"enum" => {
1863818638 const is_exhaustive = Value.makeBool(ip.loadEnumType(ty.toIntern()).tag_mode != .nonexhaustive);
1863918639
18640 const enum_field_ty = try getInnerType(sema, block, src, type_info_ty, "EnumField");
18640 const enum_field_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "EnumField");
1864118641
1864218642 const enum_field_vals = try sema.arena.alloc(InternPool.Index, ip.loadEnumType(ty.toIntern()).names.len);
1864318643 for (enum_field_vals, 0..) |*field_val, tag_index| {
......@@ -18724,7 +18724,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1872418724
1872518725 const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ip.loadEnumType(ty.toIntern()).namespace.toOptional());
1872618726
18727 const type_enum_ty = try getInnerType(sema, block, src, type_info_ty, "Enum");
18727 const type_enum_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "Enum");
1872818728
1872918729 const field_values = .{
1873018730 // tag_type: type,
......@@ -18746,8 +18746,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1874618746 })));
1874718747 },
1874818748 .@"union" => {
18749 const type_union_ty = try getInnerType(sema, block, src, type_info_ty, "Union");
18750 const union_field_ty = try getInnerType(sema, block, src, type_info_ty, "UnionField");
18749 const type_union_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "Union");
18750 const union_field_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "UnionField");
1875118751
1875218752 try ty.resolveLayout(pt); // Getting alignment requires type layout
1875318753 const union_obj = zcu.typeToUnion(ty).?;
......@@ -18842,7 +18842,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1884218842 .val = if (ty.unionTagType(zcu)) |tag_ty| tag_ty.toIntern() else .none,
1884318843 } });
1884418844
18845 const container_layout_ty = try getBuiltinInnerType(sema, block, src, "Type", "ContainerLayout");
18845 const container_layout_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "ContainerLayout");
1884618846
1884718847 const field_values = .{
1884818848 // layout: ContainerLayout,
......@@ -18865,8 +18865,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1886518865 })));
1886618866 },
1886718867 .@"struct" => {
18868 const type_struct_ty = try getInnerType(sema, block, src, type_info_ty, "Struct");
18869 const struct_field_ty = try getInnerType(sema, block, src, type_info_ty, "StructField");
18868 const type_struct_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "Struct");
18869 const struct_field_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "StructField");
1887018870
1887118871 try ty.resolveLayout(pt); // Getting alignment requires type layout
1887218872
......@@ -19043,7 +19043,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1904319043 } else .none,
1904419044 } });
1904519045
19046 const container_layout_ty = try getInnerType(sema, block, src, type_info_ty, "ContainerLayout");
19046 const container_layout_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "ContainerLayout");
1904719047
1904819048 const layout = ty.containerLayout(zcu);
1904919049
......@@ -19069,7 +19069,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1906919069 })));
1907019070 },
1907119071 .@"opaque" => {
19072 const type_opaque_ty = try getInnerType(sema, block, src, type_info_ty, "Opaque");
19072 const type_opaque_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "Opaque");
1907319073
1907419074 try ty.resolveFields(pt);
1907519075 const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ty.getNamespace(zcu));
......@@ -19103,7 +19103,7 @@ fn typeInfoDecls(
1910319103 const zcu = pt.zcu;
1910419104 const gpa = sema.gpa;
1910519105
19106 const declaration_ty = try getInnerType(sema, block, src, type_info_ty, "Declaration");
19106 const declaration_ty = try getBuiltinInnerType(sema, block, src, type_info_ty, "Type", "Declaration");
1910719107
1910819108 var decl_vals = std.ArrayList(InternPool.Index).init(gpa);
1910919109 defer decl_vals.deinit();
......@@ -27639,14 +27639,10 @@ fn prepareSimplePanic(sema: *Sema, block: *Block, src: LazySrcLoc) !void {
2763927639 const zcu = pt.zcu;
2764027640
2764127641 if (zcu.panic_func_index == .none) {
27642 const fn_ref = try sema.getBuiltinInnerAsInst(block, src, "Panic", "call");
27643 const fn_val = try sema.resolveConstValue(block, src, fn_ref, .{
27644 .needed_comptime_reason = "panic handler must be comptime-known",
27645 });
27646 assert(fn_val.typeOf(zcu).zigTypeTag(zcu) == .@"fn");
27647 assert(try fn_val.typeOf(zcu).fnHasRuntimeBitsSema(pt));
27648 try zcu.ensureFuncBodyAnalysisQueued(fn_val.toIntern());
27649 zcu.panic_func_index = fn_val.toIntern();
27642 zcu.panic_func_index = try sema.getPanicInnerFn(block, src, "call");
27643 // Here, function body analysis must be queued up so that backends can
27644 // make calls to this function.
27645 try zcu.ensureFuncBodyAnalysisQueued(zcu.panic_func_index);
2765027646 }
2765127647
2765227648 if (zcu.null_stack_trace == .none) {
......@@ -27678,14 +27674,15 @@ fn preparePanicId(sema: *Sema, block: *Block, src: LazySrcLoc, panic_id: Zcu.Pan
2767827674
2767927675 try sema.prepareSimplePanic(block, src);
2768027676
27681 const panic_messages_ty = try sema.getBuiltinType("panic_messages");
27677 const panic_ty = try sema.getBuiltinType("Panic");
27678 const panic_messages_ty = try sema.getBuiltinInnerType(block, src, panic_ty, "Panic", "messages");
2768227679 const msg_nav_index = (sema.namespaceLookup(
2768327680 block,
2768427681 LazySrcLoc.unneeded,
2768527682 panic_messages_ty.getNamespaceIndex(zcu),
2768627683 try zcu.intern_pool.getOrPutString(gpa, pt.tid, @tagName(panic_id), .no_embedded_nulls),
2768727684 ) catch |err| switch (err) {
27688 error.AnalysisFail => @panic("std.builtin.panic_messages is corrupt"),
27685 error.AnalysisFail => return error.AnalysisFail,
2768927686 error.GenericPoison, error.ComptimeReturn, error.ComptimeBreak => unreachable,
2769027687 error.OutOfMemory => |e| return e,
2769127688 }).?;
......@@ -27846,10 +27843,10 @@ fn safetyPanicUnwrapError(sema: *Sema, block: *Block, src: LazySrcLoc, err: Air.
2784627843 if (!zcu.backendSupportsFeature(.panic_fn)) {
2784727844 _ = try block.addNoOp(.trap);
2784827845 } else {
27849 const panic_fn = try getBuiltinInnerAsInst(sema, block, src, "Panic", "unwrapError");
27846 const panic_fn = try getPanicInnerFn(sema, block, src, "unwrapError");
2785027847 const err_return_trace = try sema.getErrorReturnTrace(block);
2785127848 const args: [2]Air.Inst.Ref = .{ err_return_trace, err };
27852 try sema.callBuiltin(block, src, panic_fn, .auto, &args, .@"safety check");
27849 try sema.callBuiltin(block, src, Air.internedToRef(panic_fn), .auto, &args, .@"safety check");
2785327850 }
2785427851}
2785527852
......@@ -27950,8 +27947,8 @@ fn addSafetyCheckCall(
2795027947 if (!zcu.backendSupportsFeature(.panic_fn)) {
2795127948 _ = try fail_block.addNoOp(.trap);
2795227949 } else {
27953 const panic_fn = try getBuiltinInnerAsInst(sema, &fail_block, src, "Panic", func_name);
27954 try sema.callBuiltin(&fail_block, src, panic_fn, .auto, args, .@"safety check");
27950 const panic_fn = try getPanicInnerFn(sema, &fail_block, src, func_name);
27951 try sema.callBuiltin(&fail_block, src, Air.internedToRef(panic_fn), .auto, args, .@"safety check");
2795527952 }
2795627953
2795727954 try sema.addSafetyCheckExtra(parent_block, ok, &fail_block);
......@@ -38908,61 +38905,62 @@ const ComptimeLoadResult = @import("Sema/comptime_ptr_access.zig").ComptimeLoadR
3890838905const storeComptimePtr = @import("Sema/comptime_ptr_access.zig").storeComptimePtr;
3890938906const ComptimeStoreResult = @import("Sema/comptime_ptr_access.zig").ComptimeStoreResult;
3891038907
38911/// Convenience function that looks 2 levels deep into `std.builtin`.
38912fn getBuiltinInnerAsInst(
38908fn getPanicInnerFn(
3891338909 sema: *Sema,
3891438910 block: *Block,
3891538911 src: LazySrcLoc,
38916 outer_name: []const u8,
3891738912 inner_name: []const u8,
38918) !Air.Inst.Ref {
38919 const outer_ty = try sema.getBuiltinType(outer_name);
38920 const inner_val = try getInnerValue(sema, block, src, outer_ty, inner_name);
38921 return Air.internedToRef(inner_val.toIntern());
38913) !InternPool.Index {
38914 const gpa = sema.gpa;
38915 const pt = sema.pt;
38916 const zcu = pt.zcu;
38917 const ip = &zcu.intern_pool;
38918 const outer_ty = try sema.getBuiltinType("Panic");
38919 const inner_name_ip = try ip.getOrPutString(gpa, pt.tid, inner_name, .no_embedded_nulls);
38920 const opt_fn_ref = try namespaceLookupVal(sema, block, src, outer_ty.getNamespaceIndex(zcu), inner_name_ip);
38921 const fn_ref = opt_fn_ref orelse return sema.fail(block, src, "std.builtin.Panic missing {s}", .{inner_name});
38922 const fn_val = try sema.resolveConstValue(block, src, fn_ref, .{
38923 .needed_comptime_reason = "panic handler must be comptime-known",
38924 });
38925 if (fn_val.typeOf(zcu).zigTypeTag(zcu) != .@"fn") {
38926 return sema.fail(block, src, "std.builtin.Panic.{s} is not a function", .{inner_name});
38927 }
38928 // Better not to queue up function body analysis because the function might be generic, and
38929 // the semantic analysis for the call will already queue if necessary.
38930 return fn_val.toIntern();
3892238931}
3892338932
38924/// Convenience function that looks 2 levels deep into `std.builtin`.
38925fn getBuiltinInnerType(
38926 sema: *Sema,
38927 block: *Block,
38928 src: LazySrcLoc,
38929 outer_name: []const u8,
38930 inner_name: []const u8,
38931) !Type {
38932 const outer_ty = try sema.getBuiltinType(outer_name);
38933 return getInnerType(sema, block, src, outer_ty, inner_name);
38933fn getBuiltinType(sema: *Sema, name: []const u8) SemaError!Type {
38934 const pt = sema.pt;
38935 const ty_inst = try sema.getBuiltin(name);
38936 const ty = Type.fromInterned(ty_inst.toInterned() orelse @panic("std.builtin is corrupt"));
38937 try ty.resolveFully(pt);
38938 return ty;
3893438939}
3893538940
38936fn getInnerType(
38941fn getBuiltinInnerType(
3893738942 sema: *Sema,
3893838943 block: *Block,
3893938944 src: LazySrcLoc,
3894038945 outer_ty: Type,
38946 /// Relative to "std.builtin".
38947 compile_error_parent_name: []const u8,
3894138948 inner_name: []const u8,
3894238949) !Type {
38943 const inner_val = try getInnerValue(sema, block, src, outer_ty, inner_name);
38944 return inner_val.toType();
38945}
38946
38947fn getInnerValue(
38948 sema: *Sema,
38949 block: *Block,
38950 src: LazySrcLoc,
38951 outer_ty: Type,
38952 inner_name: []const u8,
38953) !Value {
3895438950 const pt = sema.pt;
3895538951 const zcu = pt.zcu;
3895638952 const ip = &zcu.intern_pool;
3895738953 const gpa = sema.gpa;
38958 const nav = try sema.namespaceLookup(
38959 block,
38960 src,
38961 outer_ty.getNamespaceIndex(zcu),
38962 try ip.getOrPutString(gpa, pt.tid, inner_name, .no_embedded_nulls),
38963 ) orelse return sema.fail(block, src, "std.builtin missing {s}", .{inner_name});
38954 const inner_name_ip = try ip.getOrPutString(gpa, pt.tid, inner_name, .no_embedded_nulls);
38955 const opt_nav = try sema.namespaceLookup(block, src, outer_ty.getNamespaceIndex(zcu), inner_name_ip);
38956 const nav = opt_nav orelse return sema.fail(block, src, "std.builtin.{s} missing {s}", .{
38957 compile_error_parent_name, inner_name,
38958 });
3896438959 try sema.ensureNavResolved(src, nav);
38965 return Value.fromInterned(ip.getNav(nav).status.resolved.val);
38960 const val = Value.fromInterned(ip.getNav(nav).status.resolved.val);
38961 const ty = val.toType();
38962 try ty.resolveFully(pt);
38963 return ty;
3896638964}
3896738965
3896838966fn getBuiltin(sema: *Sema, name: []const u8) SemaError!Air.Inst.Ref {
......@@ -38973,11 +38971,3 @@ fn getBuiltin(sema: *Sema, name: []const u8) SemaError!Air.Inst.Ref {
3897338971 try pt.ensureCauAnalyzed(ip.getNav(nav).analysis_owner.unwrap().?);
3897438972 return Air.internedToRef(ip.getNav(nav).status.resolved.val);
3897538973}
38976
38977fn getBuiltinType(sema: *Sema, name: []const u8) SemaError!Type {
38978 const pt = sema.pt;
38979 const ty_inst = try sema.getBuiltin(name);
38980 const ty = Type.fromInterned(ty_inst.toInterned() orelse @panic("std.builtin is corrupt"));
38981 try ty.resolveFully(pt);
38982 return ty;
38983}