authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-30 15:21:50+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-30 12:22:07-07:00
logd3b4b2edf140c002da4c9c1396c26e0f66835eb0
tree84fe3e9442f391eb19c79e1a51dc11c385884740
parent01d19a8d3cdd52ad50f45bfb8666b56ccf8d3a22

Sema: shift of comptime int with runtime value

Closes #12290

8 files changed, 167 insertions(+), 59 deletions(-)

src/AstGen.zig+22-11
......@@ -226,6 +226,8 @@ pub const ResultLoc = union(enum) {
226226 ref,
227227 /// The expression will be coerced into this type, but it will be evaluated as an rvalue.
228228 ty: Zir.Inst.Ref,
229 /// Same as `ty` but for shift operands.
230 ty_shift_operand: Zir.Inst.Ref,
229231 /// Same as `ty` but it is guaranteed that Sema will additionally perform the coercion,
230232 /// so no `as` instruction needs to be emitted.
231233 coerced_ty: Zir.Inst.Ref,
......@@ -259,7 +261,7 @@ pub const ResultLoc = union(enum) {
259261 fn strategy(rl: ResultLoc, block_scope: *GenZir) Strategy {
260262 switch (rl) {
261263 // In this branch there will not be any store_to_block_ptr instructions.
262 .none, .ty, .coerced_ty, .ref => return .{
264 .none, .ty, .ty_shift_operand, .coerced_ty, .ref => return .{
263265 .tag = .break_operand,
264266 .elide_store_to_block_ptr_instructions = false,
265267 },
......@@ -302,6 +304,14 @@ pub const ResultLoc = union(enum) {
302304 else => rl,
303305 };
304306 }
307
308 fn zirTag(rl: ResultLoc) Zir.Inst.Tag {
309 return switch (rl) {
310 .ty => .as_node,
311 .ty_shift_operand => .as_shift_operand,
312 else => unreachable,
313 };
314 }
305315};
306316
307317pub const align_rl: ResultLoc = .{ .ty = .u29_type };
......@@ -1385,7 +1395,7 @@ fn arrayInitExpr(
13851395 const tag: Zir.Inst.Tag = if (types.array != .none) .array_init else .array_init_anon;
13861396 return arrayInitExprInner(gz, scope, node, array_init.ast.elements, types.array, types.elem, tag);
13871397 },
1388 .ty, .coerced_ty => {
1398 .ty, .ty_shift_operand, .coerced_ty => {
13891399 const tag: Zir.Inst.Tag = if (types.array != .none) .array_init else .array_init_anon;
13901400 const result = try arrayInitExprInner(gz, scope, node, array_init.ast.elements, types.array, types.elem, tag);
13911401 return rvalue(gz, rl, result, node);
......@@ -1631,7 +1641,7 @@ fn structInitExpr(
16311641 return structInitExprRlNone(gz, scope, node, struct_init, .none, .struct_init_anon);
16321642 }
16331643 },
1634 .ty, .coerced_ty => |ty_inst| {
1644 .ty, .ty_shift_operand, .coerced_ty => |ty_inst| {
16351645 if (struct_init.ast.type_expr == 0) {
16361646 const result = try structInitExprRlNone(gz, scope, node, struct_init, ty_inst, .struct_init_anon);
16371647 return rvalue(gz, rl, result, node);
......@@ -2327,6 +2337,7 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
23272337 .anyframe_type,
23282338 .as,
23292339 .as_node,
2340 .as_shift_operand,
23302341 .bit_and,
23312342 .bitcast,
23322343 .bit_or,
......@@ -2497,7 +2508,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
24972508 .field_parent_ptr,
24982509 .maximum,
24992510 .minimum,
2500 .builtin_async_call,
25012511 .c_import,
25022512 .@"resume",
25032513 .@"await",
......@@ -7278,7 +7288,7 @@ fn as(
72787288) InnerError!Zir.Inst.Ref {
72797289 const dest_type = try typeExpr(gz, scope, lhs);
72807290 switch (rl) {
7281 .none, .discard, .ref, .ty, .coerced_ty => {
7291 .none, .discard, .ref, .ty, .ty_shift_operand, .coerced_ty => {
72827292 const result = try reachableExpr(gz, scope, .{ .ty = dest_type }, rhs, node);
72837293 return rvalue(gz, rl, result, node);
72847294 },
......@@ -7959,7 +7969,8 @@ fn builtinCall(
79597969 return rvalue(gz, rl, result, node);
79607970 },
79617971 .async_call => {
7962 const result = try gz.addPlNode(.builtin_async_call, node, Zir.Inst.AsyncCall{
7972 const result = try gz.addExtendedPayload(.builtin_async_call, Zir.Inst.AsyncCall{
7973 .node = gz.nodeIndexToRelative(node),
79637974 .frame_buffer = try expr(gz, scope, .none, params[0]),
79647975 .result_ptr = try expr(gz, scope, .none, params[1]),
79657976 .fn_ptr = try expr(gz, scope, .none, params[2]),
......@@ -8178,7 +8189,7 @@ fn shiftOp(
81788189) InnerError!Zir.Inst.Ref {
81798190 const lhs = try expr(gz, scope, .none, lhs_node);
81808191 const log2_int_type = try gz.addUnNode(.typeof_log2_int_type, lhs, lhs_node);
8181 const rhs = try expr(gz, scope, .{ .ty = log2_int_type }, rhs_node);
8192 const rhs = try expr(gz, scope, .{ .ty_shift_operand = log2_int_type }, rhs_node);
81828193 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
81838194 .lhs = lhs,
81848195 .rhs = rhs,
......@@ -9409,7 +9420,7 @@ fn rvalue(
94099420 }
94109421 return indexToRef(gop.value_ptr.*);
94119422 },
9412 .ty => |ty_inst| {
9423 .ty, .ty_shift_operand => |ty_inst| {
94139424 // Quickly eliminate some common, unnecessary type coercion.
94149425 const as_ty = @as(u64, @enumToInt(Zir.Inst.Ref.type_type)) << 32;
94159426 const as_comptime_int = @as(u64, @enumToInt(Zir.Inst.Ref.comptime_int_type)) << 32;
......@@ -9470,7 +9481,7 @@ fn rvalue(
94709481 => return result, // type of result is already correct
94719482
94729483 // Need an explicit type coercion instruction.
9473 else => return gz.addPlNode(.as_node, src_node, Zir.Inst.As{
9484 else => return gz.addPlNode(rl.zirTag(), src_node, Zir.Inst.As{
94749485 .dest_type = ty_inst,
94759486 .operand = result,
94769487 }),
......@@ -10350,7 +10361,7 @@ const GenZir = struct {
1035010361 // we emit ZIR for the block break instructions to have the result values,
1035110362 // and then rvalue() on that to pass the value to the result location.
1035210363 switch (parent_rl) {
10353 .ty, .coerced_ty => |ty_inst| {
10364 .ty, .ty_shift_operand, .coerced_ty => |ty_inst| {
1035410365 gz.rl_ty_inst = ty_inst;
1035510366 gz.break_result_loc = parent_rl;
1035610367 },
......@@ -11506,7 +11517,7 @@ const GenZir = struct {
1150611517 fn addRet(gz: *GenZir, rl: ResultLoc, operand: Zir.Inst.Ref, node: Ast.Node.Index) !void {
1150711518 switch (rl) {
1150811519 .ptr => |ret_ptr| _ = try gz.addUnNode(.ret_load, ret_ptr, node),
11509 .ty => _ = try gz.addUnNode(.ret_node, operand, node),
11520 .ty, .ty_shift_operand => _ = try gz.addUnNode(.ret_node, operand, node),
1151011521 else => unreachable,
1151111522 }
1151211523 }
src/Autodoc.zig+1-1
......@@ -1888,7 +1888,7 @@ fn walkInstruction(
18881888 .expr = .{ .typeInfo = operand_index },
18891889 };
18901890 },
1891 .as_node => {
1891 .as_node, .as_shift_operand => {
18921892 const pl_node = data[inst_index].pl_node;
18931893 const extra = file.zir.extraData(Zir.Inst.As, pl_node.payload_index);
18941894 const dest_type_walk = try self.walkRef(
src/Sema.zig+76-26
......@@ -712,6 +712,7 @@ fn analyzeBodyInner(
712712 .vector_type => try sema.zirVectorType(block, inst),
713713 .as => try sema.zirAs(block, inst),
714714 .as_node => try sema.zirAsNode(block, inst),
715 .as_shift_operand => try sema.zirAsShiftOperand(block, inst),
715716 .bit_and => try sema.zirBitwise(block, inst, .bit_and),
716717 .bit_not => try sema.zirBitNot(block, inst),
717718 .bit_or => try sema.zirBitwise(block, inst, .bit_or),
......@@ -848,7 +849,6 @@ fn analyzeBodyInner(
848849 .mul_add => try sema.zirMulAdd(block, inst),
849850 .builtin_call => try sema.zirBuiltinCall(block, inst),
850851 .field_parent_ptr => try sema.zirFieldParentPtr(block, inst),
851 .builtin_async_call => try sema.zirBuiltinAsyncCall(block, inst),
852852 .@"resume" => try sema.zirResume(block, inst),
853853 .@"await" => try sema.zirAwait(block, inst),
854854 .array_base_ptr => try sema.zirArrayBasePtr(block, inst),
......@@ -956,6 +956,7 @@ fn analyzeBodyInner(
956956 .error_to_int => try sema.zirErrorToInt( block, extended),
957957 .int_to_error => try sema.zirIntToError( block, extended),
958958 .reify => try sema.zirReify( block, extended, inst),
959 .builtin_async_call => try sema.zirBuiltinAsyncCall( block, extended),
959960 // zig fmt: on
960961 .fence => {
961962 try sema.zirFence(block, extended);
......@@ -8257,7 +8258,7 @@ fn zirAs(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst
82578258 defer tracy.end();
82588259
82598260 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
8260 return sema.analyzeAs(block, sema.src, bin_inst.lhs, bin_inst.rhs);
8261 return sema.analyzeAs(block, sema.src, bin_inst.lhs, bin_inst.rhs, false);
82618262}
82628263
82638264fn zirAsNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -8267,7 +8268,17 @@ fn zirAsNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
82678268 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
82688269 const src = inst_data.src();
82698270 const extra = sema.code.extraData(Zir.Inst.As, inst_data.payload_index).data;
8270 return sema.analyzeAs(block, src, extra.dest_type, extra.operand);
8271 return sema.analyzeAs(block, src, extra.dest_type, extra.operand, false);
8272}
8273
8274fn zirAsShiftOperand(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8275 const tracy = trace(@src());
8276 defer tracy.end();
8277
8278 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8279 const src = inst_data.src();
8280 const extra = sema.code.extraData(Zir.Inst.As, inst_data.payload_index).data;
8281 return sema.analyzeAs(block, src, extra.dest_type, extra.operand, true);
82718282}
82728283
82738284fn analyzeAs(
......@@ -8276,6 +8287,7 @@ fn analyzeAs(
82768287 src: LazySrcLoc,
82778288 zir_dest_type: Zir.Inst.Ref,
82788289 zir_operand: Zir.Inst.Ref,
8290 no_cast_to_comptime_int: bool,
82798291) CompileError!Air.Inst.Ref {
82808292 const is_ret = if (Zir.refToIndex(zir_dest_type)) |ptr_index|
82818293 sema.code.instructions.items(.tag)[ptr_index] == .ret_type
......@@ -8287,7 +8299,7 @@ fn analyzeAs(
82878299 if (dest_ty.zigTypeTag() == .NoReturn) {
82888300 return sema.fail(block, src, "cannot cast to noreturn", .{});
82898301 }
8290 return sema.coerceExtra(block, dest_ty, operand, src, true, is_ret) catch |err| switch (err) {
8302 return sema.coerceExtra(block, dest_ty, operand, src, .{ .is_ret = is_ret, .no_cast_to_comptime_int = no_cast_to_comptime_int }) catch |err| switch (err) {
82918303 error.NotCoercible => unreachable,
82928304 else => |e| return e,
82938305 };
......@@ -10491,7 +10503,12 @@ fn zirShl(
1049110503
1049210504 const runtime_src = if (maybe_lhs_val) |lhs_val| rs: {
1049310505 if (lhs_val.isUndef()) return sema.addConstUndef(lhs_ty);
10494 const rhs_val = maybe_rhs_val orelse break :rs rhs_src;
10506 const rhs_val = maybe_rhs_val orelse {
10507 if (scalar_ty.zigTypeTag() == .ComptimeInt) {
10508 return sema.fail(block, src, "LHS of shift must be a fixed-width integer type, or RHS must be a comptime known", .{});
10509 }
10510 break :rs rhs_src;
10511 };
1049510512
1049610513 const val = switch (air_tag) {
1049710514 .shl_exact => val: {
......@@ -10615,7 +10632,10 @@ fn zirShr(
1061510632 const target = sema.mod.getTarget();
1061610633 const scalar_ty = lhs_ty.scalarType();
1061710634
10618 const runtime_src = if (try sema.resolveMaybeUndefVal(block, rhs_src, rhs)) |rhs_val| rs: {
10635 const maybe_lhs_val = try sema.resolveMaybeUndefVal(block, lhs_src, lhs);
10636 const maybe_rhs_val = try sema.resolveMaybeUndefVal(block, rhs_src, rhs);
10637
10638 const runtime_src = if (maybe_rhs_val) |rhs_val| rs: {
1061910639 if (rhs_val.isUndef()) {
1062010640 return sema.addConstUndef(lhs_ty);
1062110641 }
......@@ -10647,7 +10667,7 @@ fn zirShr(
1064710667 });
1064810668 }
1064910669 }
10650 if (try sema.resolveMaybeUndefVal(block, lhs_src, lhs)) |lhs_val| {
10670 if (maybe_lhs_val) |lhs_val| {
1065110671 if (lhs_val.isUndef()) {
1065210672 return sema.addConstUndef(lhs_ty);
1065310673 }
......@@ -10665,6 +10685,10 @@ fn zirShr(
1066510685 }
1066610686 } else rhs_src;
1066710687
10688 if (maybe_rhs_val == null and scalar_ty.zigTypeTag() == .ComptimeInt) {
10689 return sema.fail(block, src, "LHS of shift must be a fixed-width integer type, or RHS must be a comptime known", .{});
10690 }
10691
1066810692 try sema.requireRuntimeBlock(block, src, runtime_src);
1066910693 const result = try block.addBinOp(air_tag, lhs, rhs);
1067010694 if (block.wantSafety()) {
......@@ -15385,7 +15409,7 @@ fn analyzeRet(
1538515409 if (sema.fn_ret_ty.zigTypeTag() == .ErrorUnion) {
1538615410 try sema.addToInferredErrorSet(uncasted_operand);
1538715411 }
15388 const operand = sema.coerceExtra(block, sema.fn_ret_ty, uncasted_operand, src, true, true) catch |err| switch (err) {
15412 const operand = sema.coerceExtra(block, sema.fn_ret_ty, uncasted_operand, src, .{ .is_ret = true }) catch |err| switch (err) {
1538915413 error.NotCoercible => unreachable,
1539015414 else => |e| return e,
1539115415 };
......@@ -19652,9 +19676,9 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
1965219676 });
1965319677}
1965419678
19655fn zirBuiltinAsyncCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
19656 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
19657 const src = inst_data.src();
19679fn zirBuiltinAsyncCall(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
19680 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
19681 const src = LazySrcLoc.nodeOffset(extra.node);
1965819682 return sema.failWithUseOfAsync(block, src);
1965919683}
1966019684
......@@ -22542,7 +22566,7 @@ fn coerce(
2254222566 inst: Air.Inst.Ref,
2254322567 inst_src: LazySrcLoc,
2254422568) CompileError!Air.Inst.Ref {
22545 return sema.coerceExtra(block, dest_ty_unresolved, inst, inst_src, true, false) catch |err| switch (err) {
22569 return sema.coerceExtra(block, dest_ty_unresolved, inst, inst_src, .{}) catch |err| switch (err) {
2254622570 error.NotCoercible => unreachable,
2254722571 else => |e| return e,
2254822572 };
......@@ -22554,14 +22578,22 @@ const CoersionError = CompileError || error{
2255422578 NotCoercible,
2255522579};
2255622580
22581const CoerceOpts = struct {
22582 /// Should coerceExtra emit error messages.
22583 report_err: bool = true,
22584 /// Ignored if `report_err == false`.
22585 is_ret: bool = false,
22586 /// Should coercion to comptime_int ermit an error message.
22587 no_cast_to_comptime_int: bool = false,
22588};
22589
2255722590fn coerceExtra(
2255822591 sema: *Sema,
2255922592 block: *Block,
2256022593 dest_ty_unresolved: Type,
2256122594 inst: Air.Inst.Ref,
2256222595 inst_src: LazySrcLoc,
22563 report_err: bool,
22564 is_ret: bool,
22596 opts: CoerceOpts,
2256522597) CoersionError!Air.Inst.Ref {
2256622598 switch (dest_ty_unresolved.tag()) {
2256722599 .var_args_param => return sema.coerceVarArgParam(block, inst, inst_src),
......@@ -22613,7 +22645,7 @@ fn coerceExtra(
2261322645
2261422646 // T to ?T
2261522647 const child_type = try dest_ty.optionalChildAlloc(sema.arena);
22616 const intermediate = sema.coerceExtra(block, child_type, inst, inst_src, false, is_ret) catch |err| switch (err) {
22648 const intermediate = sema.coerceExtra(block, child_type, inst, inst_src, .{ .report_err = false }) catch |err| switch (err) {
2261722649 error.NotCoercible => {
2261822650 if (in_memory_result == .no_match) {
2261922651 // Try to give more useful notes
......@@ -22729,7 +22761,7 @@ fn coerceExtra(
2272922761 return sema.addConstant(dest_ty, Value.@"null");
2273022762 },
2273122763 .ComptimeInt => {
22732 const addr = sema.coerceExtra(block, Type.usize, inst, inst_src, false, is_ret) catch |err| switch (err) {
22764 const addr = sema.coerceExtra(block, Type.usize, inst, inst_src, .{ .report_err = false }) catch |err| switch (err) {
2273322765 error.NotCoercible => break :pointer,
2273422766 else => |e| return e,
2273522767 };
......@@ -22740,7 +22772,7 @@ fn coerceExtra(
2274022772 .signed => Type.isize,
2274122773 .unsigned => Type.usize,
2274222774 };
22743 const addr = sema.coerceExtra(block, ptr_size_ty, inst, inst_src, false, is_ret) catch |err| switch (err) {
22775 const addr = sema.coerceExtra(block, ptr_size_ty, inst, inst_src, .{ .report_err = false }) catch |err| switch (err) {
2274422776 error.NotCoercible => {
2274522777 // Try to give more useful notes
2274622778 in_memory_result = try sema.coerceInMemoryAllowed(block, ptr_size_ty, inst_ty, false, target, dest_ty_src, inst_src);
......@@ -22866,7 +22898,13 @@ fn coerceExtra(
2286622898 },
2286722899 .Int, .ComptimeInt => switch (inst_ty.zigTypeTag()) {
2286822900 .Float, .ComptimeFloat => float: {
22869 const val = (try sema.resolveDefinedValue(block, inst_src, inst)) orelse break :float;
22901 const val = (try sema.resolveDefinedValue(block, inst_src, inst)) orelse {
22902 if (dest_ty.zigTypeTag() == .ComptimeInt) {
22903 if (!opts.report_err) return error.NotCoercible;
22904 return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_int' must be comptime known");
22905 }
22906 break :float;
22907 };
2287022908
2287122909 if (val.floatHasFraction()) {
2287222910 return sema.fail(
......@@ -22883,11 +22921,16 @@ fn coerceExtra(
2288322921 if (try sema.resolveDefinedValue(block, inst_src, inst)) |val| {
2288422922 // comptime known integer to other number
2288522923 if (!(try sema.intFitsInType(block, inst_src, val, dest_ty, null))) {
22886 if (!report_err) return error.NotCoercible;
22924 if (!opts.report_err) return error.NotCoercible;
2288722925 return sema.fail(block, inst_src, "type '{}' cannot represent integer value '{}'", .{ dest_ty.fmt(sema.mod), val.fmtValue(inst_ty, sema.mod) });
2288822926 }
2288922927 return try sema.addConstant(dest_ty, val);
2289022928 }
22929 if (dest_ty.zigTypeTag() == .ComptimeInt) {
22930 if (!opts.report_err) return error.NotCoercible;
22931 if (opts.no_cast_to_comptime_int) return inst;
22932 return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_int' must be comptime known");
22933 }
2289122934
2289222935 // integer widening
2289322936 const dst_info = dest_ty.intInfo(target);
......@@ -22924,6 +22967,7 @@ fn coerceExtra(
2292422967 }
2292522968 return try sema.addConstant(dest_ty, result_val);
2292622969 } else if (dest_ty.zigTypeTag() == .ComptimeFloat) {
22970 if (!opts.report_err) return error.NotCoercible;
2292722971 return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_float' must be comptime known");
2292822972 }
2292922973
......@@ -22936,7 +22980,13 @@ fn coerceExtra(
2293622980 }
2293722981 },
2293822982 .Int, .ComptimeInt => int: {
22939 const val = (try sema.resolveDefinedValue(block, inst_src, inst)) orelse break :int;
22983 const val = (try sema.resolveDefinedValue(block, inst_src, inst)) orelse {
22984 if (dest_ty.zigTypeTag() == .ComptimeFloat) {
22985 if (!opts.report_err) return error.NotCoercible;
22986 return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_float' must be comptime known");
22987 }
22988 break :int;
22989 };
2294022990 const result_val = try val.intToFloat(sema.arena, inst_ty, dest_ty, target);
2294122991 // TODO implement this compile error
2294222992 //const int_again_val = try result_val.floatToInt(sema.arena, inst_ty);
......@@ -23088,9 +23138,9 @@ fn coerceExtra(
2308823138 return sema.addConstUndef(dest_ty);
2308923139 }
2309023140
23091 if (!report_err) return error.NotCoercible;
23141 if (!opts.report_err) return error.NotCoercible;
2309223142
23093 if (is_ret and dest_ty.zigTypeTag() == .NoReturn) {
23143 if (opts.is_ret and dest_ty.zigTypeTag() == .NoReturn) {
2309423144 const msg = msg: {
2309523145 const msg = try sema.errMsg(block, inst_src, "function declared 'noreturn' returns", .{});
2309623146 errdefer msg.destroy(sema.gpa);
......@@ -23127,7 +23177,7 @@ fn coerceExtra(
2312723177 try in_memory_result.report(sema, block, inst_src, msg);
2312823178
2312923179 // Add notes about function return type
23130 if (is_ret and sema.mod.test_functions.get(sema.func.?.owner_decl) == null) {
23180 if (opts.is_ret and sema.mod.test_functions.get(sema.func.?.owner_decl) == null) {
2313123181 const ret_ty_src: LazySrcLoc = .{ .node_offset_fn_type_ret_ty = 0 };
2313223182 const src_decl = sema.mod.declPtr(sema.func.?.owner_decl);
2313323183 if (inst_ty.isError() and !dest_ty.isError()) {
......@@ -24088,7 +24138,7 @@ fn storePtr2(
2408824138 // https://github.com/ziglang/zig/issues/11154
2408924139 if (sema.obtainBitCastedVectorPtr(ptr)) |vector_ptr| {
2409024140 const vector_ty = sema.typeOf(vector_ptr).childType();
24091 const vector = sema.coerceExtra(block, vector_ty, uncasted_operand, operand_src, true, is_ret) catch |err| switch (err) {
24141 const vector = sema.coerceExtra(block, vector_ty, uncasted_operand, operand_src, .{ .is_ret = is_ret }) catch |err| switch (err) {
2409224142 error.NotCoercible => unreachable,
2409324143 else => |e| return e,
2409424144 };
......@@ -24096,7 +24146,7 @@ fn storePtr2(
2409624146 return;
2409724147 }
2409824148
24099 const operand = sema.coerceExtra(block, elem_ty, uncasted_operand, operand_src, true, is_ret) catch |err| switch (err) {
24149 const operand = sema.coerceExtra(block, elem_ty, uncasted_operand, operand_src, .{ .is_ret = is_ret }) catch |err| switch (err) {
2410024150 error.NotCoercible => unreachable,
2410124151 else => |e| return e,
2410224152 };
......@@ -26831,7 +26881,7 @@ fn wrapErrorUnionPayload(
2683126881 inst_src: LazySrcLoc,
2683226882) !Air.Inst.Ref {
2683326883 const dest_payload_ty = dest_ty.errorUnionPayload();
26834 const coerced = try sema.coerceExtra(block, dest_payload_ty, inst, inst_src, false, false);
26884 const coerced = try sema.coerceExtra(block, dest_payload_ty, inst, inst_src, .{ .report_err = false });
2683526885 if (try sema.resolveMaybeUndefVal(block, inst_src, coerced)) |val| {
2683626886 return sema.addConstant(dest_ty, try Value.Tag.eu_payload.create(sema.arena, val));
2683726887 }
src/Zir.zig+9-6
......@@ -242,6 +242,8 @@ pub const Inst = struct {
242242 /// Type coercion to the function's return type.
243243 /// Uses the `pl_node` field. Payload is `As`. AST node could be many things.
244244 as_node,
245 /// Same as `as_node` but ignores runtime to comptime int error.
246 as_shift_operand,
245247 /// Bitwise AND. `&`
246248 bit_and,
247249 /// Reinterpret the memory representation of a value as a different type.
......@@ -942,9 +944,6 @@ pub const Inst = struct {
942944 /// Implements the `@maximum` builtin.
943945 /// Uses the `pl_node` union field with payload `Bin`
944946 maximum,
945 /// Implements the `@asyncCall` builtin.
946 /// Uses the `pl_node` union field with payload `AsyncCall`.
947 builtin_async_call,
948947 /// Implements the `@cImport` builtin.
949948 /// Uses the `pl_node` union field with payload `Block`.
950949 c_import,
......@@ -1029,6 +1028,7 @@ pub const Inst = struct {
10291028 .anyframe_type,
10301029 .as,
10311030 .as_node,
1031 .as_shift_operand,
10321032 .bit_and,
10331033 .bitcast,
10341034 .bit_or,
......@@ -1231,7 +1231,6 @@ pub const Inst = struct {
12311231 .memcpy,
12321232 .memset,
12331233 .minimum,
1234 .builtin_async_call,
12351234 .c_import,
12361235 .@"resume",
12371236 .@"await",
......@@ -1339,6 +1338,7 @@ pub const Inst = struct {
13391338 .anyframe_type,
13401339 .as,
13411340 .as_node,
1341 .as_shift_operand,
13421342 .bit_and,
13431343 .bitcast,
13441344 .bit_or,
......@@ -1513,7 +1513,6 @@ pub const Inst = struct {
15131513 .field_parent_ptr,
15141514 .maximum,
15151515 .minimum,
1516 .builtin_async_call,
15171516 .c_import,
15181517 .@"resume",
15191518 .@"await",
......@@ -1577,6 +1576,7 @@ pub const Inst = struct {
15771576 .anyframe_type = .un_node,
15781577 .as = .bin,
15791578 .as_node = .pl_node,
1579 .as_shift_operand = .pl_node,
15801580 .bit_and = .pl_node,
15811581 .bitcast = .pl_node,
15821582 .bit_not = .un_node,
......@@ -1801,7 +1801,6 @@ pub const Inst = struct {
18011801 .memcpy = .pl_node,
18021802 .memset = .pl_node,
18031803 .minimum = .pl_node,
1804 .builtin_async_call = .pl_node,
18051804 .c_import = .pl_node,
18061805
18071806 .alloc = .un_node,
......@@ -1972,6 +1971,9 @@ pub const Inst = struct {
19721971 /// `operand` is payload index to `UnNode`.
19731972 /// `small` contains `NameStrategy
19741973 reify,
1974 /// Implements the `@asyncCall` builtin.
1975 /// `operand` is payload index to `AsyncCall`.
1976 builtin_async_call,
19751977
19761978 pub const InstData = struct {
19771979 opcode: Extended,
......@@ -3454,6 +3456,7 @@ pub const Inst = struct {
34543456 };
34553457
34563458 pub const AsyncCall = struct {
3459 node: i32,
34573460 frame_buffer: Ref,
34583461 result_ptr: Ref,
34593462 fn_ptr: Ref,
src/print_zir.zig+5-6
......@@ -283,7 +283,6 @@ const Writer = struct {
283283 .mul_add => try self.writeMulAdd(stream, inst),
284284 .field_parent_ptr => try self.writeFieldParentPtr(stream, inst),
285285 .builtin_call => try self.writeBuiltinCall(stream, inst),
286 .builtin_async_call => try self.writeBuiltinAsyncCall(stream, inst),
287286
288287 .struct_init_anon,
289288 .struct_init_anon_ref,
......@@ -397,7 +396,7 @@ const Writer = struct {
397396 .field_val_named,
398397 => try self.writePlNodeFieldNamed(stream, inst),
399398
400 .as_node => try self.writeAs(stream, inst),
399 .as_node, .as_shift_operand => try self.writeAs(stream, inst),
401400
402401 .repeat,
403402 .repeat_inline,
......@@ -531,6 +530,7 @@ const Writer = struct {
531530 try stream.writeAll(") ");
532531 try self.writeSrc(stream, src);
533532 },
533 .builtin_async_call => try self.writeBuiltinAsyncCall(stream, extended),
534534 }
535535 }
536536
......@@ -814,9 +814,8 @@ const Writer = struct {
814814 try self.writeSrc(stream, inst_data.src());
815815 }
816816
817 fn writeBuiltinAsyncCall(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
818 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
819 const extra = self.code.extraData(Zir.Inst.AsyncCall, inst_data.payload_index).data;
817 fn writeBuiltinAsyncCall(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
818 const extra = self.code.extraData(Zir.Inst.AsyncCall, extended.operand).data;
820819 try self.writeInstRef(stream, extra.frame_buffer);
821820 try stream.writeAll(", ");
822821 try self.writeInstRef(stream, extra.result_ptr);
......@@ -825,7 +824,7 @@ const Writer = struct {
825824 try stream.writeAll(", ");
826825 try self.writeInstRef(stream, extra.args);
827826 try stream.writeAll(") ");
828 try self.writeSrc(stream, inst_data.src());
827 try self.writeSrc(stream, LazySrcLoc.nodeOffset(extra.node));
829828 }
830829
831830 fn writeParam(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
test/cases/compile_errors/runtime_to_comptime_num.zig created+31
......@@ -0,0 +1,31 @@
1pub export fn entry() void {
2 var a: u32 = 0;
3 _ = @as(comptime_int, a);
4}
5pub export fn entry2() void{
6 var a: u32 = 0;
7 _ = @as(comptime_float, a);
8}
9pub export fn entry3() void{
10 comptime var aa: comptime_float = 0.0;
11 var a: f32 = 4;
12 aa = a;
13}
14pub export fn entry4() void{
15 comptime var aa: comptime_int = 0.0;
16 var a: f32 = 4;
17 aa = a;
18}
19
20// error
21// backend=stage2
22// target=native
23//
24// :3:27: error: unable to resolve comptime value
25// :3:27: note: value being casted to 'comptime_int' must be comptime known
26// :7:29: error: unable to resolve comptime value
27// :7:29: note: value being casted to 'comptime_float' must be comptime known
28// :12:10: error: unable to resolve comptime value
29// :12:10: note: value being casted to 'comptime_float' must be comptime known
30// :17:10: error: unable to resolve comptime value
31// :17:10: note: value being casted to 'comptime_int' must be comptime known
test/cases/compile_errors/shifting_without_int_type_or_comptime_known.zig created+23
......@@ -0,0 +1,23 @@
1export fn entry(x: u8) u8 {
2 return 0x11 << x;
3}
4export fn entry1(x: u8) u8 {
5 return 0x11 >> x;
6}
7export fn entry2() void {
8 var x: u5 = 1;
9 _ = @shlExact(12345, x);
10}
11export fn entry3() void {
12 var x: u5 = 1;
13 _ = @shrExact(12345, x);
14}
15
16// error
17// backend=stage2
18// target=native
19//
20// :2:17: error: LHS of shift must be a fixed-width integer type, or RHS must be a comptime known
21// :5:17: error: LHS of shift must be a fixed-width integer type, or RHS must be a comptime known
22// :9:9: error: LHS of shift must be a fixed-width integer type, or RHS must be a comptime known
23// :13:9: error: LHS of shift must be a fixed-width integer type, or RHS must be a comptime known
test/cases/compile_errors/stage1/obj/shifting_without_int_type_or_comptime_known.zig deleted-9
......@@ -1,9 +0,0 @@
1export fn entry(x: u8) u8 {
2 return 0x11 << x;
3}
4
5// error
6// backend=stage1
7// target=native
8//
9// tmp.zig:2:17: error: LHS of shift must be a fixed-width integer type, or RHS must be compile-time known