| ... | ... | @@ -4854,11 +4854,11 @@ fn validateUnionInit( |
| 4854 | 4854 | } |
| 4855 | 4855 | block.instructions.shrinkRetainingCapacity(block_index); |
| 4856 | 4856 | |
| 4857 | | const union_val = try pt.intern(.{ .un = .{ |
| 4857 | const union_val = try pt.internUnion(.{ |
| 4858 | 4858 | .ty = union_ty.toIntern(), |
| 4859 | 4859 | .tag = tag_val.toIntern(), |
| 4860 | 4860 | .val = val.toIntern(), |
| 4861 | | } }); |
| 4861 | }); |
| 4862 | 4862 | const union_init = Air.internedToRef(union_val); |
| 4863 | 4863 | try sema.storePtr2(block, init_src, union_ptr, init_src, union_init, init_src, .store); |
| 4864 | 4864 | return; |
| ... | ... | @@ -5830,8 +5830,6 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 5830 | 5830 | const src = block.nodeOffset(inst_data.src_node); |
| 5831 | 5831 | const msg_inst = try sema.resolveInst(inst_data.operand); |
| 5832 | 5832 | |
| 5833 | | // `panicWithMsg` would perform this coercion for us, but we can get a better |
| 5834 | | // source location if we do it here. |
| 5835 | 5833 | const coerced_msg = try sema.coerce(block, Type.slice_const_u8, msg_inst, block.builtinCallArgSrc(inst_data.src_node, 0)); |
| 5836 | 5834 | |
| 5837 | 5835 | if (block.is_comptime) { |
| ... | ... | @@ -5844,7 +5842,7 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 5844 | 5842 | sema.branch_hint = .cold; |
| 5845 | 5843 | } |
| 5846 | 5844 | |
| 5847 | | try sema.panicWithMsg(block, src, coerced_msg, .@"@panic"); |
| 5845 | try callPanic(sema, block, src, .explicit_call, coerced_msg, .@"@panic"); |
| 5848 | 5846 | } |
| 5849 | 5847 | |
| 5850 | 5848 | fn zirTrap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void { |
| ... | ... | @@ -7325,6 +7323,31 @@ fn callBuiltin( |
| 7325 | 7323 | ); |
| 7326 | 7324 | } |
| 7327 | 7325 | |
| 7326 | const PanicCauseTag = @typeInfo(std.builtin.PanicCause).@"union".tag_type.?; |
| 7327 | |
| 7328 | fn callPanic( |
| 7329 | sema: *Sema, |
| 7330 | block: *Block, |
| 7331 | call_src: LazySrcLoc, |
| 7332 | tag: PanicCauseTag, |
| 7333 | payload: Air.Inst.Ref, |
| 7334 | call_operation: CallOperation, |
| 7335 | ) !void { |
| 7336 | const pt = sema.pt; |
| 7337 | if (!pt.zcu.backendSupportsFeature(.panic_fn)) { |
| 7338 | _ = try block.addNoOp(.trap); |
| 7339 | return; |
| 7340 | } |
| 7341 | const panic_cause_ty = try pt.getBuiltinType("PanicCause"); |
| 7342 | const panic_cause = try block.addUnionInit(panic_cause_ty, @intFromEnum(tag), payload); |
| 7343 | const panic_fn = try pt.getBuiltin("panic"); |
| 7344 | const err_return_trace = try sema.getErrorReturnTrace(block); |
| 7345 | const opt_usize_ty = try pt.optionalType(.usize_type); |
| 7346 | const null_usize = try pt.nullValue(opt_usize_ty); |
| 7347 | const args: [3]Air.Inst.Ref = .{ panic_cause, err_return_trace, Air.internedToRef(null_usize) }; |
| 7348 | try sema.callBuiltin(block, call_src, panic_fn, .auto, &args, call_operation); |
| 7349 | } |
| 7350 | |
| 7328 | 7351 | const CallOperation = enum { |
| 7329 | 7352 | call, |
| 7330 | 7353 | @"@call", |
| ... | ... | @@ -9327,7 +9350,7 @@ fn analyzeErrUnionPayload( |
| 9327 | 9350 | if (safety_check and block.wantSafety() and |
| 9328 | 9351 | !err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) |
| 9329 | 9352 | { |
| 9330 | | try sema.panicUnwrapError(block, src, operand, .unwrap_errunion_err, .is_non_err); |
| 9353 | try sema.addSafetyCheckUnwrapError(block, src, operand, .unwrap_errunion_err, .is_non_err); |
| 9331 | 9354 | } |
| 9332 | 9355 | |
| 9333 | 9356 | return block.addTyOp(.unwrap_errunion_payload, payload_ty, operand); |
| ... | ... | @@ -9411,7 +9434,7 @@ fn analyzeErrUnionPayloadPtr( |
| 9411 | 9434 | if (safety_check and block.wantSafety() and |
| 9412 | 9435 | !err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) |
| 9413 | 9436 | { |
| 9414 | | try sema.panicUnwrapError(block, src, operand, .unwrap_errunion_err_ptr, .is_non_err_ptr); |
| 9437 | try sema.addSafetyCheckUnwrapError(block, src, operand, .unwrap_errunion_err_ptr, .is_non_err_ptr); |
| 9415 | 9438 | } |
| 9416 | 9439 | |
| 9417 | 9440 | if (initializing) { |
| ... | ... | @@ -14190,7 +14213,6 @@ fn maybeErrorUnwrap( |
| 14190 | 14213 | ) !bool { |
| 14191 | 14214 | const pt = sema.pt; |
| 14192 | 14215 | const zcu = pt.zcu; |
| 14193 | | if (!zcu.backendSupportsFeature(.panic_unwrap_error)) return false; |
| 14194 | 14216 | |
| 14195 | 14217 | const tags = sema.code.instructions.items(.tag); |
| 14196 | 14218 | for (body) |inst| { |
| ... | ... | @@ -14223,25 +14245,13 @@ fn maybeErrorUnwrap( |
| 14223 | 14245 | .as_node => try sema.zirAsNode(block, inst), |
| 14224 | 14246 | .field_val => try sema.zirFieldVal(block, inst), |
| 14225 | 14247 | .@"unreachable" => { |
| 14226 | | if (!zcu.comp.formatted_panics) { |
| 14227 | | try sema.safetyPanic(block, operand_src, .unwrap_error); |
| 14228 | | return true; |
| 14229 | | } |
| 14230 | | |
| 14231 | | const panic_fn = try pt.getBuiltin("panicUnwrapError"); |
| 14232 | | const err_return_trace = try sema.getErrorReturnTrace(block); |
| 14233 | | const args: [2]Air.Inst.Ref = .{ err_return_trace, operand }; |
| 14234 | | try sema.callBuiltin(block, operand_src, panic_fn, .auto, &args, .@"safety check"); |
| 14248 | try callPanic(sema, block, operand_src, .unwrap_error, operand, .@"safety check"); |
| 14235 | 14249 | return true; |
| 14236 | 14250 | }, |
| 14237 | 14251 | .panic => { |
| 14238 | 14252 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 14239 | 14253 | const msg_inst = try sema.resolveInst(inst_data.operand); |
| 14240 | | |
| 14241 | | const panic_fn = try pt.getBuiltin("panic"); |
| 14242 | | const err_return_trace = try sema.getErrorReturnTrace(block); |
| 14243 | | const args: [3]Air.Inst.Ref = .{ msg_inst, err_return_trace, .null_value }; |
| 14244 | | try sema.callBuiltin(block, operand_src, panic_fn, .auto, &args, .@"safety check"); |
| 14254 | try callPanic(sema, block, operand_src, .explicit_call, msg_inst, .@"@panic"); |
| 14245 | 14255 | return true; |
| 14246 | 14256 | }, |
| 14247 | 14257 | else => unreachable, |
| ... | ... | @@ -17388,9 +17398,6 @@ fn analyzeArithmetic( |
| 17388 | 17398 | |
| 17389 | 17399 | if (block.wantSafety() and want_safety and scalar_tag == .int) { |
| 17390 | 17400 | if (zcu.backendSupportsFeature(.safety_checked_instructions)) { |
| 17391 | | if (air_tag != air_tag_safe) { |
| 17392 | | _ = try sema.preparePanicId(block, src, .integer_overflow); |
| 17393 | | } |
| 17394 | 17401 | return block.addBinOp(air_tag_safe, casted_lhs, casted_rhs); |
| 17395 | 17402 | } else { |
| 17396 | 17403 | const maybe_op_ov: ?Air.Inst.Tag = switch (air_tag) { |
| ... | ... | @@ -18319,29 +18326,14 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18319 | 18326 | .undefined, |
| 18320 | 18327 | .null, |
| 18321 | 18328 | .enum_literal, |
| 18322 | | => |type_info_tag| return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 18329 | => |type_info_tag| return Air.internedToRef((try pt.internUnion(.{ |
| 18323 | 18330 | .ty = type_info_ty.toIntern(), |
| 18324 | 18331 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(type_info_tag))).toIntern(), |
| 18325 | 18332 | .val = .void_value, |
| 18326 | | } }))), |
| 18333 | }))), |
| 18327 | 18334 | .@"fn" => { |
| 18328 | | const fn_info_nav = try sema.namespaceLookup( |
| 18329 | | block, |
| 18330 | | src, |
| 18331 | | type_info_ty.getNamespaceIndex(zcu), |
| 18332 | | try ip.getOrPutString(gpa, pt.tid, "Fn", .no_embedded_nulls), |
| 18333 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18334 | | try sema.ensureNavResolved(src, fn_info_nav); |
| 18335 | | const fn_info_ty = Type.fromInterned(ip.getNav(fn_info_nav).status.resolved.val); |
| 18336 | | |
| 18337 | | const param_info_nav = try sema.namespaceLookup( |
| 18338 | | block, |
| 18339 | | src, |
| 18340 | | fn_info_ty.getNamespaceIndex(zcu), |
| 18341 | | try ip.getOrPutString(gpa, pt.tid, "Param", .no_embedded_nulls), |
| 18342 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18343 | | try sema.ensureNavResolved(src, param_info_nav); |
| 18344 | | const param_info_ty = Type.fromInterned(ip.getNav(param_info_nav).status.resolved.val); |
| 18335 | const fn_info_ty = try getInnerType(sema, block, src, type_info_ty, "Fn"); |
| 18336 | const param_info_ty = try getInnerType(sema, block, src, fn_info_ty, "Param"); |
| 18345 | 18337 | |
| 18346 | 18338 | const func_ty_info = zcu.typeToFunc(ty).?; |
| 18347 | 18339 | const param_vals = try sema.arena.alloc(InternPool.Index, func_ty_info.param_types.len); |
| ... | ... | @@ -18425,25 +18417,17 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18425 | 18417 | // args: []const Fn.Param, |
| 18426 | 18418 | args_val, |
| 18427 | 18419 | }; |
| 18428 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 18420 | return Air.internedToRef((try pt.internUnion(.{ |
| 18429 | 18421 | .ty = type_info_ty.toIntern(), |
| 18430 | 18422 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.@"fn"))).toIntern(), |
| 18431 | 18423 | .val = try pt.intern(.{ .aggregate = .{ |
| 18432 | 18424 | .ty = fn_info_ty.toIntern(), |
| 18433 | 18425 | .storage = .{ .elems = &field_values }, |
| 18434 | 18426 | } }), |
| 18435 | | } }))); |
| 18427 | }))); |
| 18436 | 18428 | }, |
| 18437 | 18429 | .int => { |
| 18438 | | const int_info_nav = try sema.namespaceLookup( |
| 18439 | | block, |
| 18440 | | src, |
| 18441 | | type_info_ty.getNamespaceIndex(zcu), |
| 18442 | | try ip.getOrPutString(gpa, pt.tid, "Int", .no_embedded_nulls), |
| 18443 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18444 | | try sema.ensureNavResolved(src, int_info_nav); |
| 18445 | | const int_info_ty = Type.fromInterned(ip.getNav(int_info_nav).status.resolved.val); |
| 18446 | | |
| 18430 | const int_info_ty = try getInnerType(sema, block, src, type_info_ty, "Int"); |
| 18447 | 18431 | const signedness_ty = try pt.getBuiltinType("Signedness"); |
| 18448 | 18432 | const info = ty.intInfo(zcu); |
| 18449 | 18433 | const field_values = .{ |
| ... | ... | @@ -18452,37 +18436,30 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18452 | 18436 | // bits: u16, |
| 18453 | 18437 | (try pt.intValue(Type.u16, info.bits)).toIntern(), |
| 18454 | 18438 | }; |
| 18455 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 18439 | return Air.internedToRef((try pt.internUnion(.{ |
| 18456 | 18440 | .ty = type_info_ty.toIntern(), |
| 18457 | 18441 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.int))).toIntern(), |
| 18458 | 18442 | .val = try pt.intern(.{ .aggregate = .{ |
| 18459 | 18443 | .ty = int_info_ty.toIntern(), |
| 18460 | 18444 | .storage = .{ .elems = &field_values }, |
| 18461 | 18445 | } }), |
| 18462 | | } }))); |
| 18446 | }))); |
| 18463 | 18447 | }, |
| 18464 | 18448 | .float => { |
| 18465 | | const float_info_nav = try sema.namespaceLookup( |
| 18466 | | block, |
| 18467 | | src, |
| 18468 | | type_info_ty.getNamespaceIndex(zcu), |
| 18469 | | try ip.getOrPutString(gpa, pt.tid, "Float", .no_embedded_nulls), |
| 18470 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18471 | | try sema.ensureNavResolved(src, float_info_nav); |
| 18472 | | const float_info_ty = Type.fromInterned(ip.getNav(float_info_nav).status.resolved.val); |
| 18449 | const float_info_ty = try getInnerType(sema, block, src, type_info_ty, "Float"); |
| 18473 | 18450 | |
| 18474 | 18451 | const field_vals = .{ |
| 18475 | 18452 | // bits: u16, |
| 18476 | 18453 | (try pt.intValue(Type.u16, ty.bitSize(zcu))).toIntern(), |
| 18477 | 18454 | }; |
| 18478 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 18455 | return Air.internedToRef((try pt.internUnion(.{ |
| 18479 | 18456 | .ty = type_info_ty.toIntern(), |
| 18480 | 18457 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.float))).toIntern(), |
| 18481 | 18458 | .val = try pt.intern(.{ .aggregate = .{ |
| 18482 | 18459 | .ty = float_info_ty.toIntern(), |
| 18483 | 18460 | .storage = .{ .elems = &field_vals }, |
| 18484 | 18461 | } }), |
| 18485 | | } }))); |
| 18462 | }))); |
| 18486 | 18463 | }, |
| 18487 | 18464 | .pointer => { |
| 18488 | 18465 | const info = ty.ptrInfo(zcu); |
| ... | ... | @@ -18492,26 +18469,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18492 | 18469 | try Type.fromInterned(info.child).lazyAbiAlignment(pt); |
| 18493 | 18470 | |
| 18494 | 18471 | const addrspace_ty = try pt.getBuiltinType("AddressSpace"); |
| 18495 | | const pointer_ty = t: { |
| 18496 | | const nav = try sema.namespaceLookup( |
| 18497 | | block, |
| 18498 | | src, |
| 18499 | | (try pt.getBuiltinType("Type")).getNamespaceIndex(zcu), |
| 18500 | | try ip.getOrPutString(gpa, pt.tid, "Pointer", .no_embedded_nulls), |
| 18501 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18502 | | try sema.ensureNavResolved(src, nav); |
| 18503 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 18504 | | }; |
| 18505 | | const ptr_size_ty = t: { |
| 18506 | | const nav = try sema.namespaceLookup( |
| 18507 | | block, |
| 18508 | | src, |
| 18509 | | pointer_ty.getNamespaceIndex(zcu), |
| 18510 | | try ip.getOrPutString(gpa, pt.tid, "Size", .no_embedded_nulls), |
| 18511 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18512 | | try sema.ensureNavResolved(src, nav); |
| 18513 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 18514 | | }; |
| 18472 | const pointer_ty = try getInnerType(sema, block, src, type_info_ty, "Pointer"); |
| 18473 | const ptr_size_ty = try getInnerType(sema, block, src, pointer_ty, "Size"); |
| 18515 | 18474 | |
| 18516 | 18475 | const field_values = .{ |
| 18517 | 18476 | // size: Size, |
| ... | ... | @@ -18534,26 +18493,17 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18534 | 18493 | else => Value.fromInterned(info.sentinel), |
| 18535 | 18494 | })).toIntern(), |
| 18536 | 18495 | }; |
| 18537 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 18496 | return Air.internedToRef((try pt.internUnion(.{ |
| 18538 | 18497 | .ty = type_info_ty.toIntern(), |
| 18539 | 18498 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.pointer))).toIntern(), |
| 18540 | 18499 | .val = try pt.intern(.{ .aggregate = .{ |
| 18541 | 18500 | .ty = pointer_ty.toIntern(), |
| 18542 | 18501 | .storage = .{ .elems = &field_values }, |
| 18543 | 18502 | } }), |
| 18544 | | } }))); |
| 18503 | }))); |
| 18545 | 18504 | }, |
| 18546 | 18505 | .array => { |
| 18547 | | const array_field_ty = t: { |
| 18548 | | const nav = try sema.namespaceLookup( |
| 18549 | | block, |
| 18550 | | src, |
| 18551 | | type_info_ty.getNamespaceIndex(zcu), |
| 18552 | | try ip.getOrPutString(gpa, pt.tid, "Array", .no_embedded_nulls), |
| 18553 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18554 | | try sema.ensureNavResolved(src, nav); |
| 18555 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 18556 | | }; |
| 18506 | const array_field_ty = try getInnerType(sema, block, src, type_info_ty, "Array"); |
| 18557 | 18507 | |
| 18558 | 18508 | const info = ty.arrayInfo(zcu); |
| 18559 | 18509 | const field_values = .{ |
| ... | ... | @@ -18564,26 +18514,17 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18564 | 18514 | // sentinel: ?*const anyopaque, |
| 18565 | 18515 | (try sema.optRefValue(info.sentinel)).toIntern(), |
| 18566 | 18516 | }; |
| 18567 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 18517 | return Air.internedToRef((try pt.internUnion(.{ |
| 18568 | 18518 | .ty = type_info_ty.toIntern(), |
| 18569 | 18519 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.array))).toIntern(), |
| 18570 | 18520 | .val = try pt.intern(.{ .aggregate = .{ |
| 18571 | 18521 | .ty = array_field_ty.toIntern(), |
| 18572 | 18522 | .storage = .{ .elems = &field_values }, |
| 18573 | 18523 | } }), |
| 18574 | | } }))); |
| 18524 | }))); |
| 18575 | 18525 | }, |
| 18576 | 18526 | .vector => { |
| 18577 | | const vector_field_ty = t: { |
| 18578 | | const nav = try sema.namespaceLookup( |
| 18579 | | block, |
| 18580 | | src, |
| 18581 | | type_info_ty.getNamespaceIndex(zcu), |
| 18582 | | try ip.getOrPutString(gpa, pt.tid, "Vector", .no_embedded_nulls), |
| 18583 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18584 | | try sema.ensureNavResolved(src, nav); |
| 18585 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 18586 | | }; |
| 18527 | const vector_field_ty = try getInnerType(sema, block, src, type_info_ty, "Vector"); |
| 18587 | 18528 | |
| 18588 | 18529 | const info = ty.arrayInfo(zcu); |
| 18589 | 18530 | const field_values = .{ |
| ... | ... | @@ -18592,52 +18533,34 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18592 | 18533 | // child: type, |
| 18593 | 18534 | info.elem_type.toIntern(), |
| 18594 | 18535 | }; |
| 18595 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 18536 | return Air.internedToRef((try pt.internUnion(.{ |
| 18596 | 18537 | .ty = type_info_ty.toIntern(), |
| 18597 | 18538 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.vector))).toIntern(), |
| 18598 | 18539 | .val = try pt.intern(.{ .aggregate = .{ |
| 18599 | 18540 | .ty = vector_field_ty.toIntern(), |
| 18600 | 18541 | .storage = .{ .elems = &field_values }, |
| 18601 | 18542 | } }), |
| 18602 | | } }))); |
| 18543 | }))); |
| 18603 | 18544 | }, |
| 18604 | 18545 | .optional => { |
| 18605 | | const optional_field_ty = t: { |
| 18606 | | const nav = try sema.namespaceLookup( |
| 18607 | | block, |
| 18608 | | src, |
| 18609 | | type_info_ty.getNamespaceIndex(zcu), |
| 18610 | | try ip.getOrPutString(gpa, pt.tid, "Optional", .no_embedded_nulls), |
| 18611 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18612 | | try sema.ensureNavResolved(src, nav); |
| 18613 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 18614 | | }; |
| 18546 | const optional_field_ty = try getInnerType(sema, block, src, type_info_ty, "Optional"); |
| 18615 | 18547 | |
| 18616 | 18548 | const field_values = .{ |
| 18617 | 18549 | // child: type, |
| 18618 | 18550 | ty.optionalChild(zcu).toIntern(), |
| 18619 | 18551 | }; |
| 18620 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 18552 | return Air.internedToRef((try pt.internUnion(.{ |
| 18621 | 18553 | .ty = type_info_ty.toIntern(), |
| 18622 | 18554 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.optional))).toIntern(), |
| 18623 | 18555 | .val = try pt.intern(.{ .aggregate = .{ |
| 18624 | 18556 | .ty = optional_field_ty.toIntern(), |
| 18625 | 18557 | .storage = .{ .elems = &field_values }, |
| 18626 | 18558 | } }), |
| 18627 | | } }))); |
| 18559 | }))); |
| 18628 | 18560 | }, |
| 18629 | 18561 | .error_set => { |
| 18630 | 18562 | // Get the Error type |
| 18631 | | const error_field_ty = t: { |
| 18632 | | const nav = try sema.namespaceLookup( |
| 18633 | | block, |
| 18634 | | src, |
| 18635 | | type_info_ty.getNamespaceIndex(zcu), |
| 18636 | | try ip.getOrPutString(gpa, pt.tid, "Error", .no_embedded_nulls), |
| 18637 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18638 | | try sema.ensureNavResolved(src, nav); |
| 18639 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 18640 | | }; |
| 18563 | const error_field_ty = try getInnerType(sema, block, src, type_info_ty, "Error"); |
| 18641 | 18564 | |
| 18642 | 18565 | // Build our list of Error values |
| 18643 | 18566 | // Optional value is only null if anyerror |
| ... | ... | @@ -18726,23 +18649,14 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18726 | 18649 | } }); |
| 18727 | 18650 | |
| 18728 | 18651 | // Construct Type{ .error_set = errors_val } |
| 18729 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 18652 | return Air.internedToRef((try pt.internUnion(.{ |
| 18730 | 18653 | .ty = type_info_ty.toIntern(), |
| 18731 | 18654 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.error_set))).toIntern(), |
| 18732 | 18655 | .val = errors_val, |
| 18733 | | } }))); |
| 18656 | }))); |
| 18734 | 18657 | }, |
| 18735 | 18658 | .error_union => { |
| 18736 | | const error_union_field_ty = t: { |
| 18737 | | const nav = try sema.namespaceLookup( |
| 18738 | | block, |
| 18739 | | src, |
| 18740 | | type_info_ty.getNamespaceIndex(zcu), |
| 18741 | | try ip.getOrPutString(gpa, pt.tid, "ErrorUnion", .no_embedded_nulls), |
| 18742 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18743 | | try sema.ensureNavResolved(src, nav); |
| 18744 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 18745 | | }; |
| 18659 | const error_union_field_ty = try getInnerType(sema, block, src, type_info_ty, "ErrorUnion"); |
| 18746 | 18660 | |
| 18747 | 18661 | const field_values = .{ |
| 18748 | 18662 | // error_set: type, |
| ... | ... | @@ -18750,28 +18664,19 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18750 | 18664 | // payload: type, |
| 18751 | 18665 | ty.errorUnionPayload(zcu).toIntern(), |
| 18752 | 18666 | }; |
| 18753 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 18667 | return Air.internedToRef((try pt.internUnion(.{ |
| 18754 | 18668 | .ty = type_info_ty.toIntern(), |
| 18755 | 18669 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.error_union))).toIntern(), |
| 18756 | 18670 | .val = try pt.intern(.{ .aggregate = .{ |
| 18757 | 18671 | .ty = error_union_field_ty.toIntern(), |
| 18758 | 18672 | .storage = .{ .elems = &field_values }, |
| 18759 | 18673 | } }), |
| 18760 | | } }))); |
| 18674 | }))); |
| 18761 | 18675 | }, |
| 18762 | 18676 | .@"enum" => { |
| 18763 | 18677 | const is_exhaustive = Value.makeBool(ip.loadEnumType(ty.toIntern()).tag_mode != .nonexhaustive); |
| 18764 | 18678 | |
| 18765 | | const enum_field_ty = t: { |
| 18766 | | const nav = try sema.namespaceLookup( |
| 18767 | | block, |
| 18768 | | src, |
| 18769 | | type_info_ty.getNamespaceIndex(zcu), |
| 18770 | | try ip.getOrPutString(gpa, pt.tid, "EnumField", .no_embedded_nulls), |
| 18771 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18772 | | try sema.ensureNavResolved(src, nav); |
| 18773 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 18774 | | }; |
| 18679 | const enum_field_ty = try getInnerType(sema, block, src, type_info_ty, "EnumField"); |
| 18775 | 18680 | |
| 18776 | 18681 | const enum_field_vals = try sema.arena.alloc(InternPool.Index, ip.loadEnumType(ty.toIntern()).names.len); |
| 18777 | 18682 | for (enum_field_vals, 0..) |*field_val, tag_index| { |
| ... | ... | @@ -18858,16 +18763,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18858 | 18763 | |
| 18859 | 18764 | const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ip.loadEnumType(ty.toIntern()).namespace.toOptional()); |
| 18860 | 18765 | |
| 18861 | | const type_enum_ty = t: { |
| 18862 | | const nav = try sema.namespaceLookup( |
| 18863 | | block, |
| 18864 | | src, |
| 18865 | | type_info_ty.getNamespaceIndex(zcu), |
| 18866 | | try ip.getOrPutString(gpa, pt.tid, "Enum", .no_embedded_nulls), |
| 18867 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18868 | | try sema.ensureNavResolved(src, nav); |
| 18869 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 18870 | | }; |
| 18766 | const type_enum_ty = try getInnerType(sema, block, src, type_info_ty, "Enum"); |
| 18871 | 18767 | |
| 18872 | 18768 | const field_values = .{ |
| 18873 | 18769 | // tag_type: type, |
| ... | ... | @@ -18879,37 +18775,18 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18879 | 18775 | // is_exhaustive: bool, |
| 18880 | 18776 | is_exhaustive.toIntern(), |
| 18881 | 18777 | }; |
| 18882 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 18778 | return Air.internedToRef((try pt.internUnion(.{ |
| 18883 | 18779 | .ty = type_info_ty.toIntern(), |
| 18884 | 18780 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.@"enum"))).toIntern(), |
| 18885 | 18781 | .val = try pt.intern(.{ .aggregate = .{ |
| 18886 | 18782 | .ty = type_enum_ty.toIntern(), |
| 18887 | 18783 | .storage = .{ .elems = &field_values }, |
| 18888 | 18784 | } }), |
| 18889 | | } }))); |
| 18785 | }))); |
| 18890 | 18786 | }, |
| 18891 | 18787 | .@"union" => { |
| 18892 | | const type_union_ty = t: { |
| 18893 | | const nav = try sema.namespaceLookup( |
| 18894 | | block, |
| 18895 | | src, |
| 18896 | | type_info_ty.getNamespaceIndex(zcu), |
| 18897 | | try ip.getOrPutString(gpa, pt.tid, "Union", .no_embedded_nulls), |
| 18898 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18899 | | try sema.ensureNavResolved(src, nav); |
| 18900 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 18901 | | }; |
| 18902 | | |
| 18903 | | const union_field_ty = t: { |
| 18904 | | const nav = try sema.namespaceLookup( |
| 18905 | | block, |
| 18906 | | src, |
| 18907 | | type_info_ty.getNamespaceIndex(zcu), |
| 18908 | | try ip.getOrPutString(gpa, pt.tid, "UnionField", .no_embedded_nulls), |
| 18909 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 18910 | | try sema.ensureNavResolved(src, nav); |
| 18911 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 18912 | | }; |
| 18788 | const type_union_ty = try getInnerType(sema, block, src, type_info_ty, "Union"); |
| 18789 | const union_field_ty = try getInnerType(sema, block, src, type_info_ty, "UnionField"); |
| 18913 | 18790 | |
| 18914 | 18791 | try ty.resolveLayout(pt); // Getting alignment requires type layout |
| 18915 | 18792 | const union_obj = zcu.typeToUnion(ty).?; |
| ... | ... | @@ -19004,16 +18881,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 19004 | 18881 | .val = if (ty.unionTagType(zcu)) |tag_ty| tag_ty.toIntern() else .none, |
| 19005 | 18882 | } }); |
| 19006 | 18883 | |
| 19007 | | const container_layout_ty = t: { |
| 19008 | | const nav = try sema.namespaceLookup( |
| 19009 | | block, |
| 19010 | | src, |
| 19011 | | (try pt.getBuiltinType("Type")).getNamespaceIndex(zcu), |
| 19012 | | try ip.getOrPutString(gpa, pt.tid, "ContainerLayout", .no_embedded_nulls), |
| 19013 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 19014 | | try sema.ensureNavResolved(src, nav); |
| 19015 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 19016 | | }; |
| 18884 | const container_layout_ty = try getBuiltinInnerType(sema, block, src, "Type", "ContainerLayout"); |
| 19017 | 18885 | |
| 19018 | 18886 | const field_values = .{ |
| 19019 | 18887 | // layout: ContainerLayout, |
| ... | ... | @@ -19026,37 +18894,18 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 19026 | 18894 | // decls: []const Declaration, |
| 19027 | 18895 | decls_val, |
| 19028 | 18896 | }; |
| 19029 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 18897 | return Air.internedToRef((try pt.internUnion(.{ |
| 19030 | 18898 | .ty = type_info_ty.toIntern(), |
| 19031 | 18899 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.@"union"))).toIntern(), |
| 19032 | 18900 | .val = try pt.intern(.{ .aggregate = .{ |
| 19033 | 18901 | .ty = type_union_ty.toIntern(), |
| 19034 | 18902 | .storage = .{ .elems = &field_values }, |
| 19035 | 18903 | } }), |
| 19036 | | } }))); |
| 18904 | }))); |
| 19037 | 18905 | }, |
| 19038 | 18906 | .@"struct" => { |
| 19039 | | const type_struct_ty = t: { |
| 19040 | | const nav = try sema.namespaceLookup( |
| 19041 | | block, |
| 19042 | | src, |
| 19043 | | type_info_ty.getNamespaceIndex(zcu), |
| 19044 | | try ip.getOrPutString(gpa, pt.tid, "Struct", .no_embedded_nulls), |
| 19045 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 19046 | | try sema.ensureNavResolved(src, nav); |
| 19047 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 19048 | | }; |
| 19049 | | |
| 19050 | | const struct_field_ty = t: { |
| 19051 | | const nav = try sema.namespaceLookup( |
| 19052 | | block, |
| 19053 | | src, |
| 19054 | | type_info_ty.getNamespaceIndex(zcu), |
| 19055 | | try ip.getOrPutString(gpa, pt.tid, "StructField", .no_embedded_nulls), |
| 19056 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 19057 | | try sema.ensureNavResolved(src, nav); |
| 19058 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 19059 | | }; |
| 18907 | const type_struct_ty = try getInnerType(sema, block, src, type_info_ty, "Struct"); |
| 18908 | const struct_field_ty = try getInnerType(sema, block, src, type_info_ty, "StructField"); |
| 19060 | 18909 | |
| 19061 | 18910 | try ty.resolveLayout(pt); // Getting alignment requires type layout |
| 19062 | 18911 | |
| ... | ... | @@ -19233,16 +19082,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 19233 | 19082 | } else .none, |
| 19234 | 19083 | } }); |
| 19235 | 19084 | |
| 19236 | | const container_layout_ty = t: { |
| 19237 | | const nav = try sema.namespaceLookup( |
| 19238 | | block, |
| 19239 | | src, |
| 19240 | | (try pt.getBuiltinType("Type")).getNamespaceIndex(zcu), |
| 19241 | | try ip.getOrPutString(gpa, pt.tid, "ContainerLayout", .no_embedded_nulls), |
| 19242 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 19243 | | try sema.ensureNavResolved(src, nav); |
| 19244 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 19245 | | }; |
| 19085 | const container_layout_ty = try getInnerType(sema, block, src, type_info_ty, "ContainerLayout"); |
| 19246 | 19086 | |
| 19247 | 19087 | const layout = ty.containerLayout(zcu); |
| 19248 | 19088 | |
| ... | ... | @@ -19258,26 +19098,17 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 19258 | 19098 | // is_tuple: bool, |
| 19259 | 19099 | Value.makeBool(ty.isTuple(zcu)).toIntern(), |
| 19260 | 19100 | }; |
| 19261 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 19101 | return Air.internedToRef((try pt.internUnion(.{ |
| 19262 | 19102 | .ty = type_info_ty.toIntern(), |
| 19263 | 19103 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.@"struct"))).toIntern(), |
| 19264 | 19104 | .val = try pt.intern(.{ .aggregate = .{ |
| 19265 | 19105 | .ty = type_struct_ty.toIntern(), |
| 19266 | 19106 | .storage = .{ .elems = &field_values }, |
| 19267 | 19107 | } }), |
| 19268 | | } }))); |
| 19108 | }))); |
| 19269 | 19109 | }, |
| 19270 | 19110 | .@"opaque" => { |
| 19271 | | const type_opaque_ty = t: { |
| 19272 | | const nav = try sema.namespaceLookup( |
| 19273 | | block, |
| 19274 | | src, |
| 19275 | | type_info_ty.getNamespaceIndex(zcu), |
| 19276 | | try ip.getOrPutString(gpa, pt.tid, "Opaque", .no_embedded_nulls), |
| 19277 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 19278 | | try sema.ensureNavResolved(src, nav); |
| 19279 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 19280 | | }; |
| 19111 | const type_opaque_ty = try getInnerType(sema, block, src, type_info_ty, "Opaque"); |
| 19281 | 19112 | |
| 19282 | 19113 | try ty.resolveFields(pt); |
| 19283 | 19114 | const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ty.getNamespace(zcu)); |
| ... | ... | @@ -19286,14 +19117,14 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 19286 | 19117 | // decls: []const Declaration, |
| 19287 | 19118 | decls_val, |
| 19288 | 19119 | }; |
| 19289 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 19120 | return Air.internedToRef((try pt.internUnion(.{ |
| 19290 | 19121 | .ty = type_info_ty.toIntern(), |
| 19291 | 19122 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.@"opaque"))).toIntern(), |
| 19292 | 19123 | .val = try pt.intern(.{ .aggregate = .{ |
| 19293 | 19124 | .ty = type_opaque_ty.toIntern(), |
| 19294 | 19125 | .storage = .{ .elems = &field_values }, |
| 19295 | 19126 | } }), |
| 19296 | | } }))); |
| 19127 | }))); |
| 19297 | 19128 | }, |
| 19298 | 19129 | .frame => return sema.failWithUseOfAsync(block, src), |
| 19299 | 19130 | .@"anyframe" => return sema.failWithUseOfAsync(block, src), |
| ... | ... | @@ -19309,19 +19140,9 @@ fn typeInfoDecls( |
| 19309 | 19140 | ) CompileError!InternPool.Index { |
| 19310 | 19141 | const pt = sema.pt; |
| 19311 | 19142 | const zcu = pt.zcu; |
| 19312 | | const ip = &zcu.intern_pool; |
| 19313 | 19143 | const gpa = sema.gpa; |
| 19314 | 19144 | |
| 19315 | | const declaration_ty = t: { |
| 19316 | | const nav = try sema.namespaceLookup( |
| 19317 | | block, |
| 19318 | | src, |
| 19319 | | type_info_ty.getNamespaceIndex(zcu), |
| 19320 | | try ip.getOrPutString(gpa, pt.tid, "Declaration", .no_embedded_nulls), |
| 19321 | | ) orelse @panic("std.builtin.Type is corrupt"); |
| 19322 | | try sema.ensureNavResolved(src, nav); |
| 19323 | | break :t Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 19324 | | }; |
| 19145 | const declaration_ty = try getInnerType(sema, block, src, type_info_ty, "Declaration"); |
| 19325 | 19146 | |
| 19326 | 19147 | var decl_vals = std.ArrayList(InternPool.Index).init(gpa); |
| 19327 | 19148 | defer decl_vals.deinit(); |
| ... | ... | @@ -20809,11 +20630,11 @@ fn unionInit( |
| 20809 | 20630 | if (try sema.resolveValue(init)) |init_val| { |
| 20810 | 20631 | const tag_ty = union_ty.unionTagTypeHypothetical(zcu); |
| 20811 | 20632 | const tag_val = try pt.enumValueFieldIndex(tag_ty, field_index); |
| 20812 | | return Air.internedToRef((try pt.intern(.{ .un = .{ |
| 20633 | return Air.internedToRef((try pt.internUnion(.{ |
| 20813 | 20634 | .ty = union_ty.toIntern(), |
| 20814 | 20635 | .tag = tag_val.toIntern(), |
| 20815 | 20636 | .val = init_val.toIntern(), |
| 20816 | | } }))); |
| 20637 | }))); |
| 20817 | 20638 | } |
| 20818 | 20639 | |
| 20819 | 20640 | try sema.requireRuntimeBlock(block, init_src, null); |
| ... | ... | @@ -20949,11 +20770,11 @@ fn zirStructInit( |
| 20949 | 20770 | const init_inst = try sema.coerce(block, field_ty, uncoerced_init_inst, field_src); |
| 20950 | 20771 | |
| 20951 | 20772 | if (try sema.resolveValue(init_inst)) |val| { |
| 20952 | | const struct_val = Value.fromInterned(try pt.intern(.{ .un = .{ |
| 20773 | const struct_val = Value.fromInterned(try pt.internUnion(.{ |
| 20953 | 20774 | .ty = resolved_ty.toIntern(), |
| 20954 | 20775 | .tag = tag_val.toIntern(), |
| 20955 | 20776 | .val = val.toIntern(), |
| 20956 | | } })); |
| 20777 | })); |
| 20957 | 20778 | const final_val_inst = try sema.coerce(block, result_ty, Air.internedToRef(struct_val.toIntern()), src); |
| 20958 | 20779 | const final_val = (try sema.resolveValue(final_val_inst)).?; |
| 20959 | 20780 | return sema.addConstantMaybeRef(final_val.toIntern(), is_ref); |
| ... | ... | @@ -21869,11 +21690,20 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 21869 | 21690 | const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| 21870 | 21691 | const src = block.nodeOffset(inst_data.src_node); |
| 21871 | 21692 | const operand = try sema.resolveInst(inst_data.operand); |
| 21693 | return analyzeTagName(sema, block, src, operand_src, operand); |
| 21694 | } |
| 21695 | |
| 21696 | fn analyzeTagName( |
| 21697 | sema: *Sema, |
| 21698 | block: *Block, |
| 21699 | src: LazySrcLoc, |
| 21700 | operand_src: LazySrcLoc, |
| 21701 | operand: Air.Inst.Ref, |
| 21702 | ) CompileError!Air.Inst.Ref { |
| 21872 | 21703 | const operand_ty = sema.typeOf(operand); |
| 21873 | 21704 | const pt = sema.pt; |
| 21874 | 21705 | const zcu = pt.zcu; |
| 21875 | 21706 | const ip = &zcu.intern_pool; |
| 21876 | | |
| 21877 | 21707 | try operand_ty.resolveLayout(pt); |
| 21878 | 21708 | const enum_ty = switch (operand_ty.zigTypeTag(zcu)) { |
| 21879 | 21709 | .enum_literal => { |
| ... | ... | @@ -27840,7 +27670,7 @@ fn explainWhyTypeIsNotPacked( |
| 27840 | 27670 | } |
| 27841 | 27671 | } |
| 27842 | 27672 | |
| 27843 | | fn prepareSimplePanic(sema: *Sema, block: *Block, src: LazySrcLoc) !void { |
| 27673 | fn preparePanic(sema: *Sema, block: *Block, src: LazySrcLoc) !void { |
| 27844 | 27674 | const pt = sema.pt; |
| 27845 | 27675 | const zcu = pt.zcu; |
| 27846 | 27676 | |
| ... | ... | @@ -27871,33 +27701,12 @@ fn prepareSimplePanic(sema: *Sema, block: *Block, src: LazySrcLoc) !void { |
| 27871 | 27701 | .val = .none, |
| 27872 | 27702 | } }); |
| 27873 | 27703 | } |
| 27874 | | } |
| 27875 | | |
| 27876 | | /// Backends depend on panic decls being available when lowering safety-checked |
| 27877 | | /// instructions. This function ensures the panic function will be available to |
| 27878 | | /// be called during that time. |
| 27879 | | fn preparePanicId(sema: *Sema, block: *Block, src: LazySrcLoc, panic_id: Zcu.PanicId) !InternPool.Nav.Index { |
| 27880 | | const pt = sema.pt; |
| 27881 | | const zcu = pt.zcu; |
| 27882 | | const gpa = sema.gpa; |
| 27883 | | if (zcu.panic_messages[@intFromEnum(panic_id)].unwrap()) |x| return x; |
| 27884 | | |
| 27885 | | try sema.prepareSimplePanic(block, src); |
| 27886 | 27704 | |
| 27887 | | const panic_messages_ty = try pt.getBuiltinType("panic_messages"); |
| 27888 | | const msg_nav_index = (sema.namespaceLookup( |
| 27889 | | block, |
| 27890 | | LazySrcLoc.unneeded, |
| 27891 | | panic_messages_ty.getNamespaceIndex(zcu), |
| 27892 | | try zcu.intern_pool.getOrPutString(gpa, pt.tid, @tagName(panic_id), .no_embedded_nulls), |
| 27893 | | ) catch |err| switch (err) { |
| 27894 | | error.AnalysisFail => @panic("std.builtin.panic_messages is corrupt"), |
| 27895 | | error.GenericPoison, error.ComptimeReturn, error.ComptimeBreak => unreachable, |
| 27896 | | error.OutOfMemory => |e| return e, |
| 27897 | | }).?; |
| 27898 | | try sema.ensureNavResolved(src, msg_nav_index); |
| 27899 | | zcu.panic_messages[@intFromEnum(panic_id)] = msg_nav_index.toOptional(); |
| 27900 | | return msg_nav_index; |
| 27705 | if (zcu.panic_cause_type == .none) { |
| 27706 | const panic_cause_ty = try pt.getBuiltinType("PanicCause"); |
| 27707 | try panic_cause_ty.resolveFields(pt); |
| 27708 | zcu.panic_cause_type = panic_cause_ty.toIntern(); |
| 27709 | } |
| 27901 | 27710 | } |
| 27902 | 27711 | |
| 27903 | 27712 | fn addSafetyCheck( |
| ... | ... | @@ -27905,7 +27714,7 @@ fn addSafetyCheck( |
| 27905 | 27714 | parent_block: *Block, |
| 27906 | 27715 | src: LazySrcLoc, |
| 27907 | 27716 | ok: Air.Inst.Ref, |
| 27908 | | panic_id: Zcu.PanicId, |
| 27717 | panic_cause_tag: PanicCauseTag, |
| 27909 | 27718 | ) !void { |
| 27910 | 27719 | const gpa = sema.gpa; |
| 27911 | 27720 | assert(!parent_block.is_comptime); |
| ... | ... | @@ -27923,7 +27732,7 @@ fn addSafetyCheck( |
| 27923 | 27732 | |
| 27924 | 27733 | defer fail_block.instructions.deinit(gpa); |
| 27925 | 27734 | |
| 27926 | | try sema.safetyPanic(&fail_block, src, panic_id); |
| 27735 | try sema.safetyPanic(&fail_block, src, panic_cause_tag); |
| 27927 | 27736 | try sema.addSafetyCheckExtra(parent_block, ok, &fail_block); |
| 27928 | 27737 | } |
| 27929 | 27738 | |
| ... | ... | @@ -27992,30 +27801,7 @@ fn addSafetyCheckExtra( |
| 27992 | 27801 | parent_block.instructions.appendAssumeCapacity(block_inst); |
| 27993 | 27802 | } |
| 27994 | 27803 | |
| 27995 | | fn panicWithMsg(sema: *Sema, block: *Block, src: LazySrcLoc, msg_inst: Air.Inst.Ref, operation: CallOperation) !void { |
| 27996 | | const pt = sema.pt; |
| 27997 | | const zcu = pt.zcu; |
| 27998 | | |
| 27999 | | if (!zcu.backendSupportsFeature(.panic_fn)) { |
| 28000 | | _ = try block.addNoOp(.trap); |
| 28001 | | return; |
| 28002 | | } |
| 28003 | | |
| 28004 | | try sema.prepareSimplePanic(block, src); |
| 28005 | | |
| 28006 | | const panic_func = zcu.funcInfo(zcu.panic_func_index); |
| 28007 | | const panic_fn = try sema.analyzeNavVal(block, src, panic_func.owner_nav); |
| 28008 | | const null_stack_trace = Air.internedToRef(zcu.null_stack_trace); |
| 28009 | | |
| 28010 | | const opt_usize_ty = try pt.optionalType(.usize_type); |
| 28011 | | const null_ret_addr = Air.internedToRef((try pt.intern(.{ .opt = .{ |
| 28012 | | .ty = opt_usize_ty.toIntern(), |
| 28013 | | .val = .none, |
| 28014 | | } }))); |
| 28015 | | try sema.callBuiltin(block, src, panic_fn, .auto, &.{ msg_inst, null_stack_trace, null_ret_addr }, operation); |
| 28016 | | } |
| 28017 | | |
| 28018 | | fn panicUnwrapError( |
| 27804 | fn addSafetyCheckUnwrapError( |
| 28019 | 27805 | sema: *Sema, |
| 28020 | 27806 | parent_block: *Block, |
| 28021 | 27807 | src: LazySrcLoc, |
| ... | ... | @@ -28023,12 +27809,8 @@ fn panicUnwrapError( |
| 28023 | 27809 | unwrap_err_tag: Air.Inst.Tag, |
| 28024 | 27810 | is_non_err_tag: Air.Inst.Tag, |
| 28025 | 27811 | ) !void { |
| 28026 | | const pt = sema.pt; |
| 28027 | 27812 | assert(!parent_block.is_comptime); |
| 28028 | 27813 | const ok = try parent_block.addUnOp(is_non_err_tag, operand); |
| 28029 | | if (!pt.zcu.comp.formatted_panics) { |
| 28030 | | return sema.addSafetyCheck(parent_block, src, ok, .unwrap_error); |
| 28031 | | } |
| 28032 | 27814 | const gpa = sema.gpa; |
| 28033 | 27815 | |
| 28034 | 27816 | var fail_block: Block = .{ |
| ... | ... | @@ -28044,21 +27826,13 @@ fn panicUnwrapError( |
| 28044 | 27826 | |
| 28045 | 27827 | defer fail_block.instructions.deinit(gpa); |
| 28046 | 27828 | |
| 28047 | | { |
| 28048 | | if (!pt.zcu.backendSupportsFeature(.panic_unwrap_error)) { |
| 28049 | | _ = try fail_block.addNoOp(.trap); |
| 28050 | | } else { |
| 28051 | | const panic_fn = try sema.pt.getBuiltin("panicUnwrapError"); |
| 28052 | | const err = try fail_block.addTyOp(unwrap_err_tag, Type.anyerror, operand); |
| 28053 | | const err_return_trace = try sema.getErrorReturnTrace(&fail_block); |
| 28054 | | const args: [2]Air.Inst.Ref = .{ err_return_trace, err }; |
| 28055 | | try sema.callBuiltin(&fail_block, src, panic_fn, .auto, &args, .@"safety check"); |
| 28056 | | } |
| 28057 | | } |
| 27829 | const err = try fail_block.addTyOp(unwrap_err_tag, Type.anyerror, operand); |
| 27830 | try callPanic(sema, &fail_block, src, .unwrap_error, err, .@"safety check"); |
| 27831 | |
| 28058 | 27832 | try sema.addSafetyCheckExtra(parent_block, ok, &fail_block); |
| 28059 | 27833 | } |
| 28060 | 27834 | |
| 28061 | | fn panicIndexOutOfBounds( |
| 27835 | fn addSafetyCheckIndexOob( |
| 28062 | 27836 | sema: *Sema, |
| 28063 | 27837 | parent_block: *Block, |
| 28064 | 27838 | src: LazySrcLoc, |
| ... | ... | @@ -28067,14 +27841,71 @@ fn panicIndexOutOfBounds( |
| 28067 | 27841 | cmp_op: Air.Inst.Tag, |
| 28068 | 27842 | ) !void { |
| 28069 | 27843 | assert(!parent_block.is_comptime); |
| 27844 | const gpa = sema.gpa; |
| 28070 | 27845 | const ok = try parent_block.addBinOp(cmp_op, index, len); |
| 28071 | | if (!sema.pt.zcu.comp.formatted_panics) { |
| 28072 | | return sema.addSafetyCheck(parent_block, src, ok, .index_out_of_bounds); |
| 27846 | |
| 27847 | var fail_block: Block = .{ |
| 27848 | .parent = parent_block, |
| 27849 | .sema = sema, |
| 27850 | .namespace = parent_block.namespace, |
| 27851 | .instructions = .{}, |
| 27852 | .inlining = parent_block.inlining, |
| 27853 | .is_comptime = false, |
| 27854 | .src_base_inst = parent_block.src_base_inst, |
| 27855 | .type_name_ctx = parent_block.type_name_ctx, |
| 27856 | }; |
| 27857 | |
| 27858 | defer fail_block.instructions.deinit(gpa); |
| 27859 | |
| 27860 | const oob_ty = try getBuiltinInnerType(sema, &fail_block, src, "PanicCause", "IndexOutOfBounds"); |
| 27861 | comptime { |
| 27862 | const fields = @typeInfo(std.builtin.PanicCause.IndexOutOfBounds).@"struct".fields; |
| 27863 | assert(std.mem.eql(u8, fields[0].name, "index")); |
| 27864 | assert(std.mem.eql(u8, fields[1].name, "len")); |
| 27865 | assert(fields.len == 2); |
| 28073 | 27866 | } |
| 28074 | | try sema.safetyCheckFormatted(parent_block, src, ok, "panicOutOfBounds", &.{ index, len }); |
| 27867 | const panic_cause_payload = try fail_block.addAggregateInit(oob_ty, &.{ index, len }); |
| 27868 | try callPanic(sema, &fail_block, src, .index_out_of_bounds, panic_cause_payload, .@"safety check"); |
| 27869 | try sema.addSafetyCheckExtra(parent_block, ok, &fail_block); |
| 28075 | 27870 | } |
| 28076 | 27871 | |
| 28077 | | fn panicInactiveUnionField( |
| 27872 | fn addSafetyCheckStartGreaterThanEnd( |
| 27873 | sema: *Sema, |
| 27874 | parent_block: *Block, |
| 27875 | src: LazySrcLoc, |
| 27876 | start: Air.Inst.Ref, |
| 27877 | end: Air.Inst.Ref, |
| 27878 | ) !void { |
| 27879 | assert(!parent_block.is_comptime); |
| 27880 | const gpa = sema.gpa; |
| 27881 | const ok = try parent_block.addBinOp(.cmp_lte, start, end); |
| 27882 | |
| 27883 | var fail_block: Block = .{ |
| 27884 | .parent = parent_block, |
| 27885 | .sema = sema, |
| 27886 | .namespace = parent_block.namespace, |
| 27887 | .instructions = .{}, |
| 27888 | .inlining = parent_block.inlining, |
| 27889 | .is_comptime = false, |
| 27890 | .src_base_inst = parent_block.src_base_inst, |
| 27891 | .type_name_ctx = parent_block.type_name_ctx, |
| 27892 | }; |
| 27893 | |
| 27894 | defer fail_block.instructions.deinit(gpa); |
| 27895 | |
| 27896 | const oob_ty = try getBuiltinInnerType(sema, &fail_block, src, "PanicCause", "StartIndexGreaterThanEnd"); |
| 27897 | comptime { |
| 27898 | const fields = @typeInfo(std.builtin.PanicCause.StartIndexGreaterThanEnd).@"struct".fields; |
| 27899 | assert(std.mem.eql(u8, fields[0].name, "start")); |
| 27900 | assert(std.mem.eql(u8, fields[1].name, "end")); |
| 27901 | assert(fields.len == 2); |
| 27902 | } |
| 27903 | const panic_cause_payload = try fail_block.addAggregateInit(oob_ty, &.{ start, end }); |
| 27904 | try callPanic(sema, &fail_block, src, .start_index_greater_than_end, panic_cause_payload, .@"safety check"); |
| 27905 | try sema.addSafetyCheckExtra(parent_block, ok, &fail_block); |
| 27906 | } |
| 27907 | |
| 27908 | fn addSafetyCheckInactiveUnionField( |
| 28078 | 27909 | sema: *Sema, |
| 28079 | 27910 | parent_block: *Block, |
| 28080 | 27911 | src: LazySrcLoc, |
| ... | ... | @@ -28082,14 +27913,39 @@ fn panicInactiveUnionField( |
| 28082 | 27913 | wanted_tag: Air.Inst.Ref, |
| 28083 | 27914 | ) !void { |
| 28084 | 27915 | assert(!parent_block.is_comptime); |
| 27916 | const gpa = sema.gpa; |
| 28085 | 27917 | const ok = try parent_block.addBinOp(.cmp_eq, active_tag, wanted_tag); |
| 28086 | | if (!sema.pt.zcu.comp.formatted_panics) { |
| 28087 | | return sema.addSafetyCheck(parent_block, src, ok, .inactive_union_field); |
| 28088 | | } |
| 28089 | | try sema.safetyCheckFormatted(parent_block, src, ok, "panicInactiveUnionField", &.{ active_tag, wanted_tag }); |
| 27918 | |
| 27919 | var fail_block: Block = .{ |
| 27920 | .parent = parent_block, |
| 27921 | .sema = sema, |
| 27922 | .namespace = parent_block.namespace, |
| 27923 | .instructions = .{}, |
| 27924 | .inlining = parent_block.inlining, |
| 27925 | .is_comptime = false, |
| 27926 | .src_base_inst = parent_block.src_base_inst, |
| 27927 | .type_name_ctx = parent_block.type_name_ctx, |
| 27928 | }; |
| 27929 | |
| 27930 | defer fail_block.instructions.deinit(gpa); |
| 27931 | |
| 27932 | const payload_ty = try getBuiltinInnerType(sema, &fail_block, src, "PanicCause", "InactiveUnionField"); |
| 27933 | comptime { |
| 27934 | const fields = @typeInfo(std.builtin.PanicCause.InactiveUnionField).@"struct".fields; |
| 27935 | assert(std.mem.eql(u8, fields[0].name, "active")); |
| 27936 | assert(std.mem.eql(u8, fields[1].name, "accessed")); |
| 27937 | assert(fields.len == 2); |
| 27938 | } |
| 27939 | // TODO: before merging the branch, check how many safety checks end up being emitted |
| 27940 | // for union field accesses and avoid extraneous ones. |
| 27941 | const active_str = try analyzeTagName(sema, &fail_block, src, src, active_tag); |
| 27942 | const accessed_str = try analyzeTagName(sema, &fail_block, src, src, wanted_tag); |
| 27943 | const panic_cause_payload = try fail_block.addAggregateInit(payload_ty, &.{ active_str, accessed_str }); |
| 27944 | try callPanic(sema, &fail_block, src, .inactive_union_field, panic_cause_payload, .@"safety check"); |
| 27945 | try sema.addSafetyCheckExtra(parent_block, ok, &fail_block); |
| 28090 | 27946 | } |
| 28091 | 27947 | |
| 28092 | | fn panicSentinelMismatch( |
| 27948 | fn addSafetyCheckSentinelMismatch( |
| 28093 | 27949 | sema: *Sema, |
| 28094 | 27950 | parent_block: *Block, |
| 28095 | 27951 | src: LazySrcLoc, |
| ... | ... | @@ -28099,6 +27955,7 @@ fn panicSentinelMismatch( |
| 28099 | 27955 | sentinel_index: Air.Inst.Ref, |
| 28100 | 27956 | ) !void { |
| 28101 | 27957 | assert(!parent_block.is_comptime); |
| 27958 | const gpa = sema.gpa; |
| 28102 | 27959 | const pt = sema.pt; |
| 28103 | 27960 | const zcu = pt.zcu; |
| 28104 | 27961 | const expected_sentinel_val = maybe_sentinel orelse return; |
| ... | ... | @@ -28114,8 +27971,7 @@ fn panicSentinelMismatch( |
| 28114 | 27971 | }; |
| 28115 | 27972 | |
| 28116 | 27973 | const ok = if (sentinel_ty.zigTypeTag(zcu) == .vector) ok: { |
| 28117 | | const eql = |
| 28118 | | try parent_block.addCmpVector(expected_sentinel, actual_sentinel, .eq); |
| 27974 | const eql = try parent_block.addCmpVector(expected_sentinel, actual_sentinel, .eq); |
| 28119 | 27975 | break :ok try parent_block.addInst(.{ |
| 28120 | 27976 | .tag = .reduce, |
| 28121 | 27977 | .data = .{ .reduce = .{ |
| ... | ... | @@ -28128,25 +27984,6 @@ fn panicSentinelMismatch( |
| 28128 | 27984 | break :ok try parent_block.addBinOp(.cmp_eq, expected_sentinel, actual_sentinel); |
| 28129 | 27985 | }; |
| 28130 | 27986 | |
| 28131 | | if (!pt.zcu.comp.formatted_panics) { |
| 28132 | | return sema.addSafetyCheck(parent_block, src, ok, .sentinel_mismatch); |
| 28133 | | } |
| 28134 | | try sema.safetyCheckFormatted(parent_block, src, ok, "panicSentinelMismatch", &.{ expected_sentinel, actual_sentinel }); |
| 28135 | | } |
| 28136 | | |
| 28137 | | fn safetyCheckFormatted( |
| 28138 | | sema: *Sema, |
| 28139 | | parent_block: *Block, |
| 28140 | | src: LazySrcLoc, |
| 28141 | | ok: Air.Inst.Ref, |
| 28142 | | func: []const u8, |
| 28143 | | args: []const Air.Inst.Ref, |
| 28144 | | ) CompileError!void { |
| 28145 | | const pt = sema.pt; |
| 28146 | | const zcu = pt.zcu; |
| 28147 | | assert(zcu.comp.formatted_panics); |
| 28148 | | const gpa = sema.gpa; |
| 28149 | | |
| 28150 | 27987 | var fail_block: Block = .{ |
| 28151 | 27988 | .parent = parent_block, |
| 28152 | 27989 | .sema = sema, |
| ... | ... | @@ -28160,20 +27997,29 @@ fn safetyCheckFormatted( |
| 28160 | 27997 | |
| 28161 | 27998 | defer fail_block.instructions.deinit(gpa); |
| 28162 | 27999 | |
| 28163 | | if (!zcu.backendSupportsFeature(.safety_check_formatted)) { |
| 28164 | | _ = try fail_block.addNoOp(.trap); |
| 28000 | // A different PanicCause tag must be used depending on what payload type it can be fit into. |
| 28001 | // If it cannot fit into any, the "other" tag can be used, which does not try to carry the |
| 28002 | // sentinel value data. |
| 28003 | |
| 28004 | if (sentinel_ty.isUnsignedInt(zcu) and sentinel_ty.intInfo(zcu).bits <= Type.usize.intInfo(zcu).bits) { |
| 28005 | const mm_ty = try getBuiltinInnerType(sema, &fail_block, src, "PanicCause", "SentinelMismatchUsize"); |
| 28006 | comptime { |
| 28007 | const fields = @typeInfo(std.builtin.PanicCause.SentinelMismatchUsize).@"struct".fields; |
| 28008 | assert(std.mem.eql(u8, fields[0].name, "expected")); |
| 28009 | assert(std.mem.eql(u8, fields[1].name, "found")); |
| 28010 | assert(fields.len == 2); |
| 28011 | } |
| 28012 | const panic_cause_payload = &fail_block.addAggregateInit(mm_ty, &.{ expected_sentinel, actual_sentinel }); |
| 28013 | try callPanic(sema, &fail_block, src, .sentinel_mismatch_usize, panic_cause_payload, .@"safety check"); |
| 28165 | 28014 | } else { |
| 28166 | | const panic_fn = try pt.getBuiltin(func); |
| 28167 | | try sema.callBuiltin(&fail_block, src, panic_fn, .auto, args, .@"safety check"); |
| 28015 | try callPanic(sema, &fail_block, src, .sentinel_mismatch_other, .void_value, .@"safety check"); |
| 28168 | 28016 | } |
| 28169 | 28017 | try sema.addSafetyCheckExtra(parent_block, ok, &fail_block); |
| 28170 | 28018 | } |
| 28171 | 28019 | |
| 28172 | 28020 | /// This does not set `sema.branch_hint`. |
| 28173 | | fn safetyPanic(sema: *Sema, block: *Block, src: LazySrcLoc, panic_id: Zcu.PanicId) CompileError!void { |
| 28174 | | const msg_nav_index = try sema.preparePanicId(block, src, panic_id); |
| 28175 | | const msg_inst = try sema.analyzeNavVal(block, src, msg_nav_index); |
| 28176 | | try sema.panicWithMsg(block, src, msg_inst, .@"safety check"); |
| 28021 | fn safetyPanic(sema: *Sema, block: *Block, src: LazySrcLoc, panic_cause_tag: PanicCauseTag) CompileError!void { |
| 28022 | try callPanic(sema, block, src, panic_cause_tag, .void_value, .@"safety check"); |
| 28177 | 28023 | } |
| 28178 | 28024 | |
| 28179 | 28025 | fn emitBackwardBranch(sema: *Sema, block: *Block, src: LazySrcLoc) !void { |
| ... | ... | @@ -29229,7 +29075,7 @@ fn unionFieldPtr( |
| 29229 | 29075 | // TODO would it be better if get_union_tag supported pointers to unions? |
| 29230 | 29076 | const union_val = try block.addTyOp(.load, union_ty, union_ptr); |
| 29231 | 29077 | const active_tag = try block.addTyOp(.get_union_tag, Type.fromInterned(union_obj.enum_tag_ty), union_val); |
| 29232 | | try sema.panicInactiveUnionField(block, src, active_tag, wanted_tag); |
| 29078 | try sema.addSafetyCheckInactiveUnionField(block, src, active_tag, wanted_tag); |
| 29233 | 29079 | } |
| 29234 | 29080 | if (field_ty.zigTypeTag(zcu) == .noreturn) { |
| 29235 | 29081 | _ = try block.addNoOp(.unreach); |
| ... | ... | @@ -29304,7 +29150,7 @@ fn unionFieldVal( |
| 29304 | 29150 | const wanted_tag_val = try pt.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), enum_field_index); |
| 29305 | 29151 | const wanted_tag = Air.internedToRef(wanted_tag_val.toIntern()); |
| 29306 | 29152 | const active_tag = try block.addTyOp(.get_union_tag, Type.fromInterned(union_obj.enum_tag_ty), union_byval); |
| 29307 | | try sema.panicInactiveUnionField(block, src, active_tag, wanted_tag); |
| 29153 | try sema.addSafetyCheckInactiveUnionField(block, src, active_tag, wanted_tag); |
| 29308 | 29154 | } |
| 29309 | 29155 | if (field_ty.zigTypeTag(zcu) == .noreturn) { |
| 29310 | 29156 | _ = try block.addNoOp(.unreach); |
| ... | ... | @@ -29668,11 +29514,11 @@ fn elemValArray( |
| 29668 | 29514 | |
| 29669 | 29515 | const runtime_src = if (maybe_undef_array_val != null) elem_index_src else array_src; |
| 29670 | 29516 | if (oob_safety and block.wantSafety()) { |
| 29671 | | // Runtime check is only needed if unable to comptime check |
| 29517 | // Runtime check is only needed if unable to comptime check. |
| 29672 | 29518 | if (maybe_index_val == null) { |
| 29673 | 29519 | const len_inst = try pt.intRef(Type.usize, array_len); |
| 29674 | 29520 | const cmp_op: Air.Inst.Tag = if (array_sent != null) .cmp_lte else .cmp_lt; |
| 29675 | | try sema.panicIndexOutOfBounds(block, src, elem_index, len_inst, cmp_op); |
| 29521 | try sema.addSafetyCheckIndexOob(block, src, elem_index, len_inst, cmp_op); |
| 29676 | 29522 | } |
| 29677 | 29523 | } |
| 29678 | 29524 | |
| ... | ... | @@ -29740,7 +29586,7 @@ fn elemPtrArray( |
| 29740 | 29586 | if (oob_safety and block.wantSafety() and offset == null) { |
| 29741 | 29587 | const len_inst = try pt.intRef(Type.usize, array_len); |
| 29742 | 29588 | const cmp_op: Air.Inst.Tag = if (array_sent) .cmp_lte else .cmp_lt; |
| 29743 | | try sema.panicIndexOutOfBounds(block, src, elem_index, len_inst, cmp_op); |
| 29589 | try sema.addSafetyCheckIndexOob(block, src, elem_index, len_inst, cmp_op); |
| 29744 | 29590 | } |
| 29745 | 29591 | |
| 29746 | 29592 | return block.addPtrElemPtr(array_ptr, elem_index, elem_ptr_ty); |
| ... | ... | @@ -29799,7 +29645,7 @@ fn elemValSlice( |
| 29799 | 29645 | else |
| 29800 | 29646 | try block.addTyOp(.slice_len, Type.usize, slice); |
| 29801 | 29647 | const cmp_op: Air.Inst.Tag = if (slice_sent) .cmp_lte else .cmp_lt; |
| 29802 | | try sema.panicIndexOutOfBounds(block, src, elem_index, len_inst, cmp_op); |
| 29648 | try sema.addSafetyCheckIndexOob(block, src, elem_index, len_inst, cmp_op); |
| 29803 | 29649 | } |
| 29804 | 29650 | return block.addBinOp(.slice_elem_val, slice, elem_index); |
| 29805 | 29651 | } |
| ... | ... | @@ -29859,7 +29705,7 @@ fn elemPtrSlice( |
| 29859 | 29705 | break :len try block.addTyOp(.slice_len, Type.usize, slice); |
| 29860 | 29706 | }; |
| 29861 | 29707 | const cmp_op: Air.Inst.Tag = if (slice_sent) .cmp_lte else .cmp_lt; |
| 29862 | | try sema.panicIndexOutOfBounds(block, src, elem_index, len_inst, cmp_op); |
| 29708 | try sema.addSafetyCheckIndexOob(block, src, elem_index, len_inst, cmp_op); |
| 29863 | 29709 | } |
| 29864 | 29710 | return block.addSliceElemPtr(slice, elem_index, elem_ptr_ty); |
| 29865 | 29711 | } |
| ... | ... | @@ -33666,12 +33512,7 @@ fn analyzeSlice( |
| 33666 | 33512 | // requirement: start <= end |
| 33667 | 33513 | assert(!block.is_comptime); |
| 33668 | 33514 | try sema.requireRuntimeBlock(block, src, runtime_src.?); |
| 33669 | | const ok = try block.addBinOp(.cmp_lte, start, end); |
| 33670 | | if (!pt.zcu.comp.formatted_panics) { |
| 33671 | | try sema.addSafetyCheck(block, src, ok, .start_index_greater_than_end); |
| 33672 | | } else { |
| 33673 | | try sema.safetyCheckFormatted(block, src, ok, "panicStartGreaterThanEnd", &.{ start, end }); |
| 33674 | | } |
| 33515 | try sema.addSafetyCheckStartGreaterThanEnd(block, src, start, end); |
| 33675 | 33516 | } |
| 33676 | 33517 | const new_len = if (by_length) |
| 33677 | 33518 | try sema.coerce(block, Type.usize, uncasted_end_opt, end_src) |
| ... | ... | @@ -33726,11 +33567,11 @@ fn analyzeSlice( |
| 33726 | 33567 | else |
| 33727 | 33568 | end; |
| 33728 | 33569 | |
| 33729 | | try sema.panicIndexOutOfBounds(block, src, actual_end, actual_len, .cmp_lte); |
| 33570 | try sema.addSafetyCheckIndexOob(block, src, actual_end, actual_len, .cmp_lte); |
| 33730 | 33571 | } |
| 33731 | 33572 | |
| 33732 | 33573 | // requirement: result[new_len] == slice_sentinel |
| 33733 | | try sema.panicSentinelMismatch(block, src, slice_sentinel, elem_ty, result, new_len); |
| 33574 | try sema.addSafetyCheckSentinelMismatch(block, src, slice_sentinel, elem_ty, result, new_len); |
| 33734 | 33575 | } |
| 33735 | 33576 | return result; |
| 33736 | 33577 | }; |
| ... | ... | @@ -33789,11 +33630,11 @@ fn analyzeSlice( |
| 33789 | 33630 | try sema.analyzeArithmetic(block, .add, end, .one, src, end_src, end_src, true) |
| 33790 | 33631 | else |
| 33791 | 33632 | end; |
| 33792 | | try sema.panicIndexOutOfBounds(block, src, actual_end, len_inst, .cmp_lte); |
| 33633 | try sema.addSafetyCheckIndexOob(block, src, actual_end, len_inst, .cmp_lte); |
| 33793 | 33634 | } |
| 33794 | 33635 | |
| 33795 | 33636 | // requirement: start <= end |
| 33796 | | try sema.panicIndexOutOfBounds(block, src, start, end, .cmp_lte); |
| 33637 | try sema.addSafetyCheckIndexOob(block, src, start, end, .cmp_lte); |
| 33797 | 33638 | } |
| 33798 | 33639 | const result = try block.addInst(.{ |
| 33799 | 33640 | .tag = .slice, |
| ... | ... | @@ -33807,7 +33648,7 @@ fn analyzeSlice( |
| 33807 | 33648 | }); |
| 33808 | 33649 | if (block.wantSafety()) { |
| 33809 | 33650 | // requirement: result[new_len] == slice_sentinel |
| 33810 | | try sema.panicSentinelMismatch(block, src, slice_sentinel, elem_ty, result, new_len); |
| 33651 | try sema.addSafetyCheckSentinelMismatch(block, src, slice_sentinel, elem_ty, result, new_len); |
| 33811 | 33652 | } |
| 33812 | 33653 | return result; |
| 33813 | 33654 | } |
| ... | ... | @@ -37688,11 +37529,11 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { |
| 37688 | 37529 | const only_field_ty = Type.fromInterned(union_obj.field_types.get(ip)[0]); |
| 37689 | 37530 | const val_val = (try sema.typeHasOnePossibleValue(only_field_ty)) orelse |
| 37690 | 37531 | return null; |
| 37691 | | const only = try pt.intern(.{ .un = .{ |
| 37532 | const only = try pt.internUnion(.{ |
| 37692 | 37533 | .ty = ty.toIntern(), |
| 37693 | 37534 | .tag = tag_val.toIntern(), |
| 37694 | 37535 | .val = val_val.toIntern(), |
| 37695 | | } }); |
| 37536 | }); |
| 37696 | 37537 | return Value.fromInterned(only); |
| 37697 | 37538 | }, |
| 37698 | 37539 | |
| ... | ... | @@ -38849,7 +38690,7 @@ fn analyzeUnreachable(sema: *Sema, block: *Block, src: LazySrcLoc, safety_check: |
| 38849 | 38690 | sema.branch_hint = .cold; |
| 38850 | 38691 | } |
| 38851 | 38692 | |
| 38852 | | try sema.safetyPanic(block, src, .unreach); |
| 38693 | try sema.safetyPanic(block, src, .reached_unreachable); |
| 38853 | 38694 | } else { |
| 38854 | 38695 | _ = try block.addNoOp(.unreach); |
| 38855 | 38696 | } |
| ... | ... | @@ -39123,3 +38964,36 @@ const loadComptimePtr = @import("Sema/comptime_ptr_access.zig").loadComptimePtr; |
| 39123 | 38964 | const ComptimeLoadResult = @import("Sema/comptime_ptr_access.zig").ComptimeLoadResult; |
| 39124 | 38965 | const storeComptimePtr = @import("Sema/comptime_ptr_access.zig").storeComptimePtr; |
| 39125 | 38966 | const ComptimeStoreResult = @import("Sema/comptime_ptr_access.zig").ComptimeStoreResult; |
| 38967 | |
| 38968 | /// Convenience function that looks 2 levels deep into `std.builtin`. |
| 38969 | fn getBuiltinInnerType( |
| 38970 | sema: *Sema, |
| 38971 | block: *Block, |
| 38972 | src: LazySrcLoc, |
| 38973 | outer_name: []const u8, |
| 38974 | inner_name: []const u8, |
| 38975 | ) !Type { |
| 38976 | const outer_ty = try sema.pt.getBuiltinType(outer_name); |
| 38977 | return getInnerType(sema, block, src, outer_ty, inner_name); |
| 38978 | } |
| 38979 | |
| 38980 | fn getInnerType( |
| 38981 | sema: *Sema, |
| 38982 | block: *Block, |
| 38983 | src: LazySrcLoc, |
| 38984 | outer_ty: Type, |
| 38985 | inner_name: []const u8, |
| 38986 | ) !Type { |
| 38987 | const pt = sema.pt; |
| 38988 | const zcu = pt.zcu; |
| 38989 | const ip = &zcu.intern_pool; |
| 38990 | const gpa = sema.gpa; |
| 38991 | const nav = try sema.namespaceLookup( |
| 38992 | block, |
| 38993 | src, |
| 38994 | outer_ty.getNamespaceIndex(zcu), |
| 38995 | try ip.getOrPutString(gpa, pt.tid, inner_name, .no_embedded_nulls), |
| 38996 | ) orelse return sema.fail(block, src, "std.builtin missing {s}", .{inner_name}); |
| 38997 | try sema.ensureNavResolved(src, nav); |
| 38998 | return Type.fromInterned(ip.getNav(nav).status.resolved.val); |
| 38999 | } |