authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-03 12:51:59+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-05 17:26:29+03:00
logc0350cf87eaae64ca81e17aef8872e8e55767437
treea65fb350db2a802c678d739cfd6f38c7fe132264
parent3234e8de3a50575195fab625f818a6e5fe141c7b

Sema: avoid passing undefined as reason to `failWithNeededComptime`

Closes #13046

2 files changed, 39 insertions(+), 25 deletions(-)

src/Sema.zig+25-25
......@@ -2469,7 +2469,7 @@ fn createAnonymousDeclTypeNamed(
24692469 const arg = sema.inst_map.get(zir_inst).?;
24702470 // The comptime call code in analyzeCall already did this, so we're
24712471 // just repeating it here and it's guaranteed to work.
2472 const arg_val = sema.resolveConstMaybeUndefVal(block, .unneeded, arg, undefined) catch unreachable;
2472 const arg_val = sema.resolveConstMaybeUndefVal(block, .unneeded, arg, "") catch unreachable;
24732473
24742474 if (arg_i != 0) try buf.appendSlice(",");
24752475 try buf.writer().print("{}", .{arg_val.fmtValue(sema.typeOf(arg), sema.mod)});
......@@ -5631,7 +5631,7 @@ fn zirCall(
56315631 var bound_arg_src: ?LazySrcLoc = null;
56325632 if (func_type.tag() == .bound_fn) {
56335633 bound_arg_src = func_src;
5634 const bound_func = try sema.resolveValue(block, .unneeded, func, undefined);
5634 const bound_func = try sema.resolveValue(block, .unneeded, func, "");
56355635 const bound_data = &bound_func.cast(Value.Payload.BoundFn).?.data;
56365636 func = bound_data.func_inst;
56375637 resolved_args = try sema.arena.alloc(Air.Inst.Ref, args_len + 1);
......@@ -6213,7 +6213,7 @@ fn analyzeCall(
62136213 }
62146214
62156215 if (should_memoize and is_comptime_call) {
6216 const result_val = try sema.resolveConstMaybeUndefVal(block, .unneeded, result, undefined);
6216 const result_val = try sema.resolveConstMaybeUndefVal(block, .unneeded, result, "");
62176217
62186218 // TODO: check whether any external comptime memory was mutated by the
62196219 // comptime function call. If so, then do not memoize the call here.
......@@ -6724,12 +6724,12 @@ fn instantiateGenericCall(
67246724 const child_arg = try child_sema.addConstant(sema.typeOf(arg), arg_val);
67256725 child_sema.inst_map.putAssumeCapacityNoClobber(inst, child_arg);
67266726 } else {
6727 return sema.failWithNeededComptime(block, .unneeded, undefined);
6727 return sema.failWithNeededComptime(block, .unneeded, "");
67286728 }
67296729 } else if (is_anytype) {
67306730 const arg_ty = sema.typeOf(arg);
67316731 if (try sema.typeRequiresComptime(arg_ty)) {
6732 const arg_val = try sema.resolveConstValue(block, .unneeded, arg, undefined);
6732 const arg_val = try sema.resolveConstValue(block, .unneeded, arg, "");
67336733 const child_arg = try child_sema.addConstant(arg_ty, arg_val);
67346734 child_sema.inst_map.putAssumeCapacityNoClobber(inst, child_arg);
67356735 } else {
......@@ -6751,7 +6751,7 @@ fn instantiateGenericCall(
67516751 }
67526752 return err;
67536753 };
6754 const new_func_val = child_sema.resolveConstValue(&child_block, .unneeded, new_func_inst, undefined) catch unreachable;
6754 const new_func_val = child_sema.resolveConstValue(&child_block, .unneeded, new_func_inst, "") catch unreachable;
67556755 const new_func = new_func_val.castTag(.function).?.data;
67566756 errdefer new_func.deinit(gpa);
67576757 assert(new_func == new_module_func);
......@@ -9115,7 +9115,7 @@ fn zirSwitchCapture(
91159115 const union_obj = operand_ty.cast(Type.Payload.Union).?.data;
91169116 const first_item = try sema.resolveInst(items[0]);
91179117 // Previous switch validation ensured this will succeed
9118 const first_item_val = sema.resolveConstValue(block, .unneeded, first_item, undefined) catch unreachable;
9118 const first_item_val = sema.resolveConstValue(block, .unneeded, first_item, "") catch unreachable;
91199119
91209120 const first_field_index = @intCast(u32, operand_ty.unionTagFieldIndex(first_item_val, sema.mod).?);
91219121 const first_field = union_obj.fields.values()[first_field_index];
......@@ -9123,7 +9123,7 @@ fn zirSwitchCapture(
91239123 for (items[1..]) |item, i| {
91249124 const item_ref = try sema.resolveInst(item);
91259125 // Previous switch validation ensured this will succeed
9126 const item_val = sema.resolveConstValue(block, .unneeded, item_ref, undefined) catch unreachable;
9126 const item_val = sema.resolveConstValue(block, .unneeded, item_ref, "") catch unreachable;
91279127
91289128 const field_index = operand_ty.unionTagFieldIndex(item_val, sema.mod).?;
91299129 const field = union_obj.fields.values()[field_index];
......@@ -9184,7 +9184,7 @@ fn zirSwitchCapture(
91849184 for (items) |item| {
91859185 const item_ref = try sema.resolveInst(item);
91869186 // Previous switch validation ensured this will succeed
9187 const item_val = sema.resolveConstValue(block, .unneeded, item_ref, undefined) catch unreachable;
9187 const item_val = sema.resolveConstValue(block, .unneeded, item_ref, "") catch unreachable;
91889188 names.putAssumeCapacityNoClobber(
91899189 item_val.getError().?,
91909190 {},
......@@ -9198,7 +9198,7 @@ fn zirSwitchCapture(
91989198 } else {
91999199 const item_ref = try sema.resolveInst(items[0]);
92009200 // Previous switch validation ensured this will succeed
9201 const item_val = sema.resolveConstValue(block, .unneeded, item_ref, undefined) catch unreachable;
9201 const item_val = sema.resolveConstValue(block, .unneeded, item_ref, "") catch unreachable;
92029202
92039203 const item_ty = try Type.Tag.error_set_single.create(sema.arena, item_val.getError().?);
92049204 return sema.bitCast(block, item_ty, operand, operand_src);
......@@ -9943,7 +9943,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
99439943
99449944 const item = try sema.resolveInst(item_ref);
99459945 // Validation above ensured these will succeed.
9946 const item_val = sema.resolveConstValue(&child_block, .unneeded, item, undefined) catch unreachable;
9946 const item_val = sema.resolveConstValue(&child_block, .unneeded, item, "") catch unreachable;
99479947 if (operand_val.eql(item_val, operand_ty, sema.mod)) {
99489948 if (err_set) try sema.maybeErrorUnwrapComptime(&child_block, body, operand);
99499949 return sema.resolveBlockBody(block, src, &child_block, body, inst, merges);
......@@ -9966,7 +9966,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
99669966 for (items) |item_ref| {
99679967 const item = try sema.resolveInst(item_ref);
99689968 // Validation above ensured these will succeed.
9969 const item_val = sema.resolveConstValue(&child_block, .unneeded, item, undefined) catch unreachable;
9969 const item_val = sema.resolveConstValue(&child_block, .unneeded, item, "") catch unreachable;
99709970 if (operand_val.eql(item_val, operand_ty, sema.mod)) {
99719971 if (err_set) try sema.maybeErrorUnwrapComptime(&child_block, body, operand);
99729972 return sema.resolveBlockBody(block, src, &child_block, body, inst, merges);
......@@ -9981,8 +9981,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
99819981 extra_index += 1;
99829982
99839983 // Validation above ensured these will succeed.
9984 const first_tv = sema.resolveInstConst(&child_block, .unneeded, item_first, undefined) catch unreachable;
9985 const last_tv = sema.resolveInstConst(&child_block, .unneeded, item_last, undefined) catch unreachable;
9984 const first_tv = sema.resolveInstConst(&child_block, .unneeded, item_first, "") catch unreachable;
9985 const last_tv = sema.resolveInstConst(&child_block, .unneeded, item_last, "") catch unreachable;
99869986 if ((try sema.compare(block, src, operand_val, .gte, first_tv.val, operand_ty)) and
99879987 (try sema.compare(block, src, operand_val, .lte, last_tv.val, operand_ty)))
99889988 {
......@@ -10048,7 +10048,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1004810048 // `item` is already guaranteed to be constant known.
1004910049
1005010050 const analyze_body = if (union_originally) blk: {
10051 const item_val = sema.resolveConstValue(block, .unneeded, item, undefined) catch unreachable;
10051 const item_val = sema.resolveConstValue(block, .unneeded, item, "") catch unreachable;
1005210052 const field_ty = maybe_union_ty.unionFieldType(item_val, sema.mod);
1005310053 break :blk field_ty.zigTypeTag() != .NoReturn;
1005410054 } else true;
......@@ -10199,7 +10199,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1019910199 const analyze_body = if (union_originally)
1020010200 for (items) |item_ref| {
1020110201 const item = try sema.resolveInst(item_ref);
10202 const item_val = sema.resolveConstValue(block, .unneeded, item, undefined) catch unreachable;
10202 const item_val = sema.resolveConstValue(block, .unneeded, item, "") catch unreachable;
1020310203 const field_ty = maybe_union_ty.unionFieldType(item_val, sema.mod);
1020410204 if (field_ty.zigTypeTag() != .NoReturn) break true;
1020510205 } else false
......@@ -10587,7 +10587,7 @@ fn resolveSwitchItemVal(
1058710587 // Constructing a LazySrcLoc is costly because we only have the switch AST node.
1058810588 // Only if we know for sure we need to report a compile error do we resolve the
1058910589 // full source locations.
10590 if (sema.resolveConstValue(block, .unneeded, item, undefined)) |val| {
10590 if (sema.resolveConstValue(block, .unneeded, item, "")) |val| {
1059110591 return TypedValue{ .ty = item_ty, .val = val };
1059210592 } else |err| switch (err) {
1059310593 error.NeededSourceLocation => {
......@@ -17094,7 +17094,7 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1709417094 try sema.resolveTypeLayout(block, operand_src, operand_ty);
1709517095 const enum_ty = switch (operand_ty.zigTypeTag()) {
1709617096 .EnumLiteral => {
17097 const val = try sema.resolveConstValue(block, .unneeded, operand, undefined);
17097 const val = try sema.resolveConstValue(block, .unneeded, operand, "");
1709817098 const bytes = val.castTag(.enum_literal).?.data;
1709917099 return sema.addStrLit(block, bytes);
1710017100 },
......@@ -20069,7 +20069,7 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2006920069 var bound_arg_src: ?LazySrcLoc = null;
2007020070 if (sema.typeOf(func).tag() == .bound_fn) {
2007120071 bound_arg_src = func_src;
20072 const bound_func = try sema.resolveValue(block, .unneeded, func, undefined);
20072 const bound_func = try sema.resolveValue(block, .unneeded, func, "");
2007320073 const bound_data = &bound_func.cast(Value.Payload.BoundFn).?.data;
2007420074 func = bound_data.func_inst;
2007520075 resolved_args = try sema.arena.alloc(Air.Inst.Ref, args_ty.structFieldCount() + 1);
......@@ -22020,7 +22020,7 @@ fn fieldPtr(
2202022020 }
2202122021 },
2202222022 .Type => {
22023 _ = try sema.resolveConstValue(block, .unneeded, object_ptr, undefined);
22023 _ = try sema.resolveConstValue(block, .unneeded, object_ptr, "");
2202422024 const result = try sema.analyzeLoad(block, src, object_ptr, object_ptr_src);
2202522025 const inner = if (is_pointer_to)
2202622026 try sema.analyzeLoad(block, src, result, object_ptr_src)
......@@ -23348,7 +23348,7 @@ fn coerceExtra(
2334823348
2334923349 // Function body to function pointer.
2335023350 if (inst_ty.zigTypeTag() == .Fn) {
23351 const fn_val = try sema.resolveConstValue(block, .unneeded, inst, undefined);
23351 const fn_val = try sema.resolveConstValue(block, .unneeded, inst, "");
2335223352 const fn_decl = fn_val.pointerDecl().?;
2335323353 const inst_as_ptr = try sema.analyzeDeclRef(fn_decl);
2335423354 return sema.coerce(block, dest_ty, inst_as_ptr, inst_src);
......@@ -23650,7 +23650,7 @@ fn coerceExtra(
2365023650 },
2365123651 .Float, .ComptimeFloat => switch (inst_ty.zigTypeTag()) {
2365223652 .ComptimeFloat => {
23653 const val = try sema.resolveConstValue(block, .unneeded, inst, undefined);
23653 const val = try sema.resolveConstValue(block, .unneeded, inst, "");
2365423654 const result_val = try val.floatCast(sema.arena, dest_ty, target);
2365523655 return try sema.addConstant(dest_ty, result_val);
2365623656 },
......@@ -23708,7 +23708,7 @@ fn coerceExtra(
2370823708 .Enum => switch (inst_ty.zigTypeTag()) {
2370923709 .EnumLiteral => {
2371023710 // enum literal to enum
23711 const val = try sema.resolveConstValue(block, .unneeded, inst, undefined);
23711 const val = try sema.resolveConstValue(block, .unneeded, inst, "");
2371223712 const bytes = val.castTag(.enum_literal).?.data;
2371323713 const field_index = dest_ty.enumFieldIndex(bytes) orelse {
2371423714 const msg = msg: {
......@@ -24778,7 +24778,7 @@ fn coerceVarArgParam(
2477824778 .{},
2477924779 ),
2478024780 .Fn => blk: {
24781 const fn_val = try sema.resolveConstValue(block, .unneeded, inst, undefined);
24781 const fn_val = try sema.resolveConstValue(block, .unneeded, inst, "");
2478224782 const fn_decl = fn_val.pointerDecl().?;
2478324783 break :blk try sema.analyzeDeclRef(fn_decl);
2478424784 },
......@@ -27202,7 +27202,7 @@ fn analyzeSlice(
2720227202 if (!end_is_len) {
2720327203 break :e try sema.coerce(block, Type.usize, uncasted_end_opt, end_src);
2720427204 }
27205 return sema.fail(block, end_src, "slice of pointer must include end value", .{});
27205 return sema.fail(block, src, "slice of pointer must include end value", .{});
2720627206 };
2720727207
2720827208 const sentinel = s: {
test/cases/compile_errors/stage1/obj/runtime_value_in_switch_prong.zig created+14
......@@ -0,0 +1,14 @@
1pub export fn entry() void {
2 var byte: u8 = 1;
3 switch (byte) {
4 byte => {},
5 else => {},
6 }
7}
8
9// error
10// backend=stage2
11// target=native
12//
13// :4:9: error: unable to resolve comptime value
14// :4:9: note: switch prong values must be comptime known