authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-07 14:01:54-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-07 14:01:54-07:00
log8e07b0c4b97817b89b0cd59c8e558348cfb5005b
tree606d08f1970cc81dda71604e7756295a40d72fd5
parente0b92004926103b88efda5c6d57f36f39a42a1f8
parent0c78ece1c95164f4a321f5705b20896415336d02

Merge remote-tracking branch 'origin/master' into llvm14


95 files changed, 961 insertions(+), 750 deletions(-)

lib/std/log.zig+5-5
......@@ -156,11 +156,11 @@ pub fn defaultLog(
156156 comptime format: []const u8,
157157 args: anytype,
158158) void {
159 if (builtin.os.tag == .freestanding) {
160 // On freestanding one must provide a log function; we do not have
161 // any I/O configured.
162 return;
163 }
159 if (builtin.os.tag == .freestanding)
160 @compileError(
161 \\freestanding targets do not have I/O configured;
162 \\please provide at least an empty `log` function declaration
163 );
164164
165165 const level_txt = comptime message_level.asText();
166166 const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
src/AstGen.zig+42-12
......@@ -812,7 +812,7 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr
812812
813813 .deref => {
814814 const lhs = try expr(gz, scope, .none, node_datas[node].lhs);
815 _ = try gz.addUnTok(.validate_deref, lhs, main_tokens[node]);
815 _ = try gz.addUnNode(.validate_deref, lhs, node);
816816 switch (rl) {
817817 .ref => return lhs,
818818 else => {
......@@ -1320,7 +1320,10 @@ fn arrayInitExpr(
13201320 const len_inst = try gz.addInt(array_init.ast.elements.len);
13211321 const elem_type = try typeExpr(gz, scope, array_type.ast.elem_type);
13221322 if (array_type.ast.sentinel == 0) {
1323 const array_type_inst = try gz.addBin(.array_type, len_inst, elem_type);
1323 const array_type_inst = try gz.addPlNode(.array_type, array_init.ast.type_expr, Zir.Inst.Bin{
1324 .lhs = len_inst,
1325 .rhs = elem_type,
1326 });
13241327 break :inst .{
13251328 .array = array_type_inst,
13261329 .elem = elem_type,
......@@ -1553,7 +1556,10 @@ fn structInitExpr(
15531556 if (is_inferred_array_len) {
15541557 const elem_type = try typeExpr(gz, scope, array_type.ast.elem_type);
15551558 const array_type_inst = if (array_type.ast.sentinel == 0) blk: {
1556 break :blk try gz.addBin(.array_type, .zero_usize, elem_type);
1559 break :blk try gz.addPlNode(.array_type, struct_init.ast.type_expr, Zir.Inst.Bin{
1560 .lhs = .zero_usize,
1561 .rhs = elem_type,
1562 });
15571563 } else blk: {
15581564 const sentinel = try comptimeExpr(gz, scope, .{ .ty = elem_type }, array_type.ast.sentinel);
15591565 break :blk try gz.addPlNode(
......@@ -2332,8 +2338,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
23322338 .error_union_type,
23332339 .bit_not,
23342340 .error_value,
2335 .error_to_int,
2336 .int_to_error,
23372341 .slice_start,
23382342 .slice_end,
23392343 .slice_sentinel,
......@@ -2420,7 +2424,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
24202424 .splat,
24212425 .reduce,
24222426 .shuffle,
2423 .select,
24242427 .atomic_load,
24252428 .atomic_rmw,
24262429 .mul_add,
......@@ -2467,6 +2470,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
24672470 .repeat,
24682471 .repeat_inline,
24692472 .panic,
2473 .panic_comptime,
24702474 => {
24712475 noreturn_src_node = statement;
24722476 break :b true;
......@@ -3100,6 +3104,10 @@ fn ptrType(
31003104 node: Ast.Node.Index,
31013105 ptr_info: Ast.full.PtrType,
31023106) InnerError!Zir.Inst.Ref {
3107 if (ptr_info.size == .C and ptr_info.allowzero_token != null) {
3108 return gz.astgen.failTok(ptr_info.allowzero_token.?, "C pointers always allow address zero", .{});
3109 }
3110
31033111 const elem_type = try typeExpr(gz, scope, ptr_info.ast.child_type);
31043112
31053113 const simple = ptr_info.ast.align_node == 0 and
......@@ -3205,7 +3213,10 @@ fn arrayType(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) !Z
32053213 const len = try expr(gz, scope, .{ .coerced_ty = .usize_type }, len_node);
32063214 const elem_type = try typeExpr(gz, scope, node_datas[node].rhs);
32073215
3208 const result = try gz.addBin(.array_type, len, elem_type);
3216 const result = try gz.addPlNode(.array_type, node, Zir.Inst.Bin{
3217 .lhs = len,
3218 .rhs = elem_type,
3219 });
32093220 return rvalue(gz, rl, result, node);
32103221}
32113222
......@@ -4997,10 +5008,14 @@ fn orelseCatchExpr(
49975008 var err_val_scope: Scope.LocalVal = undefined;
49985009 const else_sub_scope = blk: {
49995010 const payload = payload_token orelse break :blk &else_scope.base;
5000 if (mem.eql(u8, tree.tokenSlice(payload), "_")) {
5011 const err_str = tree.tokenSlice(payload);
5012 if (mem.eql(u8, err_str, "_")) {
50015013 return astgen.failTok(payload, "discard of error capture; omit it instead", .{});
50025014 }
50035015 const err_name = try astgen.identAsString(payload);
5016
5017 try astgen.detectLocalShadowing(scope, err_name, payload, err_str);
5018
50045019 err_val_scope = .{
50055020 .parent = &else_scope.base,
50065021 .gen_zir = &else_scope,
......@@ -7355,15 +7370,13 @@ fn builtinCall(
73557370 .align_of => return simpleUnOpType(gz, scope, rl, node, params[0], .align_of),
73567371
73577372 .ptr_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .ptr_to_int),
7358 .error_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .error_to_int),
7359 .int_to_error => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .u16_type }, params[0], .int_to_error),
73607373 .compile_error => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .compile_error),
73617374 .set_eval_branch_quota => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .u32_type }, params[0], .set_eval_branch_quota),
73627375 .enum_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .enum_to_int),
73637376 .bool_to_int => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .bool_to_int),
73647377 .embed_file => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .embed_file),
73657378 .error_name => return simpleUnOp(gz, scope, rl, node, .{ .ty = .anyerror_type }, params[0], .error_name),
7366 .panic => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .panic),
7379 .panic => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], if (gz.force_comptime) .panic_comptime else .panic),
73677380 .set_cold => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_cold),
73687381 .set_runtime_safety => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_runtime_safety),
73697382 .sqrt => return simpleUnOp(gz, scope, rl, node, .none, params[0], .sqrt),
......@@ -7396,6 +7409,22 @@ fn builtinCall(
73967409 .truncate => return typeCast(gz, scope, rl, node, params[0], params[1], .truncate),
73977410 // zig fmt: on
73987411
7412 .error_to_int => {
7413 const operand = try expr(gz, scope, .none, params[0]);
7414 const result = try gz.addExtendedPayload(.error_to_int, Zir.Inst.UnNode{
7415 .node = gz.nodeIndexToRelative(node),
7416 .operand = operand,
7417 });
7418 return rvalue(gz, rl, result, node);
7419 },
7420 .int_to_error => {
7421 const operand = try expr(gz, scope, .{ .coerced_ty = .u16_type }, params[0]);
7422 const result = try gz.addExtendedPayload(.int_to_error, Zir.Inst.UnNode{
7423 .node = gz.nodeIndexToRelative(node),
7424 .operand = operand,
7425 });
7426 return rvalue(gz, rl, result, node);
7427 },
73997428 .align_cast => {
74007429 const dest_align = try comptimeExpr(gz, scope, align_rl, params[0]);
74017430 const rhs = try expr(gz, scope, .none, params[1]);
......@@ -7635,7 +7664,8 @@ fn builtinCall(
76357664 return rvalue(gz, rl, result, node);
76367665 },
76377666 .select => {
7638 const result = try gz.addPlNode(.select, node, Zir.Inst.Select{
7667 const result = try gz.addExtendedPayload(.select, Zir.Inst.Select{
7668 .node = gz.nodeIndexToRelative(node),
76397669 .elem_type = try typeExpr(gz, scope, params[0]),
76407670 .pred = try expr(gz, scope, .none, params[1]),
76417671 .a = try expr(gz, scope, .none, params[2]),
src/Module.zig+49-2
......@@ -2171,7 +2171,11 @@ pub const SrcLoc = struct {
21712171 const token_starts = tree.tokens.items(.start);
21722172 return token_starts[tok_index];
21732173 },
2174 .node_offset_slice_sentinel => |node_off| {
2174 .node_offset_slice_ptr,
2175 .node_offset_slice_start,
2176 .node_offset_slice_end,
2177 .node_offset_slice_sentinel,
2178 => |node_off| {
21752179 const tree = try src_loc.file_scope.getTree(gpa);
21762180 const node_tags = tree.nodes.items(.tag);
21772181 const node = src_loc.declRelativeToNodeIndex(node_off);
......@@ -2182,7 +2186,15 @@ pub const SrcLoc = struct {
21822186 else => unreachable,
21832187 };
21842188 const main_tokens = tree.nodes.items(.main_token);
2185 const tok_index = main_tokens[full.ast.sentinel];
2189 const tok_index = main_tokens[
2190 switch (src_loc.lazy) {
2191 .node_offset_slice_ptr => full.ast.sliced,
2192 .node_offset_slice_start => full.ast.start,
2193 .node_offset_slice_end => full.ast.end,
2194 .node_offset_slice_sentinel => full.ast.sentinel,
2195 else => unreachable,
2196 }
2197 ];
21862198 const token_starts = tree.tokens.items(.start);
21872199 return token_starts[tok_index];
21882200 },
......@@ -2501,6 +2513,16 @@ pub const SrcLoc = struct {
25012513 const token_starts = tree.tokens.items(.start);
25022514 return token_starts[tok_index];
25032515 },
2516 .node_offset_un_op => |node_off| {
2517 const tree = try src_loc.file_scope.getTree(gpa);
2518 const node_datas = tree.nodes.items(.data);
2519 const node = src_loc.declRelativeToNodeIndex(node_off);
2520
2521 const main_tokens = tree.nodes.items(.main_token);
2522 const tok_index = main_tokens[node_datas[node].lhs];
2523 const token_starts = tree.tokens.items(.start);
2524 return token_starts[tok_index];
2525 },
25042526 }
25052527 }
25062528
......@@ -2614,6 +2636,24 @@ pub const LazySrcLoc = union(enum) {
26142636 /// to the index expression.
26152637 /// The Decl is determined contextually.
26162638 node_offset_array_access_index: i32,
2639 /// The source location points to the LHS of a slice expression
2640 /// expression, found by taking this AST node index offset from the containing
2641 /// Decl AST node, which points to a slice AST node. Next, navigate
2642 /// to the sentinel expression.
2643 /// The Decl is determined contextually.
2644 node_offset_slice_ptr: i32,
2645 /// The source location points to start expression of a slice expression
2646 /// expression, found by taking this AST node index offset from the containing
2647 /// Decl AST node, which points to a slice AST node. Next, navigate
2648 /// to the sentinel expression.
2649 /// The Decl is determined contextually.
2650 node_offset_slice_start: i32,
2651 /// The source location points to the end expression of a slice
2652 /// expression, found by taking this AST node index offset from the containing
2653 /// Decl AST node, which points to a slice AST node. Next, navigate
2654 /// to the sentinel expression.
2655 /// The Decl is determined contextually.
2656 node_offset_slice_end: i32,
26172657 /// The source location points to the sentinel expression of a slice
26182658 /// expression, found by taking this AST node index offset from the containing
26192659 /// Decl AST node, which points to a slice AST node. Next, navigate
......@@ -2728,6 +2768,9 @@ pub const LazySrcLoc = union(enum) {
27282768 /// to the elem expression.
27292769 /// The Decl is determined contextually.
27302770 node_offset_array_type_elem: i32,
2771 /// The source location points to the operand of an unary expression.
2772 /// The Decl is determined contextually.
2773 node_offset_un_op: i32,
27312774
27322775 pub const nodeOffset = if (TracedOffset.want_tracing) nodeOffsetDebug else nodeOffsetRelease;
27332776
......@@ -2768,6 +2811,9 @@ pub const LazySrcLoc = union(enum) {
27682811 .node_offset_builtin_call_arg4,
27692812 .node_offset_builtin_call_arg5,
27702813 .node_offset_array_access_index,
2814 .node_offset_slice_ptr,
2815 .node_offset_slice_start,
2816 .node_offset_slice_end,
27712817 .node_offset_slice_sentinel,
27722818 .node_offset_call_func,
27732819 .node_offset_field_name,
......@@ -2788,6 +2834,7 @@ pub const LazySrcLoc = union(enum) {
27882834 .node_offset_array_type_len,
27892835 .node_offset_array_type_sentinel,
27902836 .node_offset_array_type_elem,
2837 .node_offset_un_op,
27912838 => .{
27922839 .file_scope = decl.getFileScope(),
27932840 .parent_decl_node = decl.src_node,
src/Sema.zig+189-115
......@@ -739,8 +739,6 @@ fn analyzeBodyInner(
739739 .err_union_payload_unsafe_ptr => try sema.zirErrUnionPayloadPtr(block, inst, false),
740740 .error_union_type => try sema.zirErrorUnionType(block, inst),
741741 .error_value => try sema.zirErrorValue(block, inst),
742 .error_to_int => try sema.zirErrorToInt(block, inst),
743 .int_to_error => try sema.zirIntToError(block, inst),
744742 .field_ptr => try sema.zirFieldPtr(block, inst),
745743 .field_ptr_named => try sema.zirFieldPtrNamed(block, inst),
746744 .field_val => try sema.zirFieldVal(block, inst),
......@@ -835,7 +833,6 @@ fn analyzeBodyInner(
835833 .splat => try sema.zirSplat(block, inst),
836834 .reduce => try sema.zirReduce(block, inst),
837835 .shuffle => try sema.zirShuffle(block, inst),
838 .select => try sema.zirSelect(block, inst),
839836 .atomic_load => try sema.zirAtomicLoad(block, inst),
840837 .atomic_rmw => try sema.zirAtomicRmw(block, inst),
841838 .mul_add => try sema.zirMulAdd(block, inst),
......@@ -906,7 +903,8 @@ fn analyzeBodyInner(
906903 .ret_load => break sema.zirRetLoad(block, inst),
907904 .ret_err_value => break sema.zirRetErrValue(block, inst),
908905 .@"unreachable" => break sema.zirUnreachable(block, inst),
909 .panic => break sema.zirPanic(block, inst),
906 .panic => break sema.zirPanic(block, inst, false),
907 .panic_comptime => break sema.zirPanic(block, inst, true),
910908 // zig fmt: on
911909
912910 .extended => ext: {
......@@ -942,6 +940,9 @@ fn analyzeBodyInner(
942940 .field_call_bind_named => try sema.zirFieldCallBindNamed(block, extended),
943941 .err_set_cast => try sema.zirErrSetCast( block, extended),
944942 .await_nosuspend => try sema.zirAwaitNosuspend( block, extended),
943 .select => try sema.zirSelect( block, extended),
944 .error_to_int => try sema.zirErrorToInt( block, extended),
945 .int_to_error => try sema.zirIntToError( block, extended),
945946 // zig fmt: on
946947 .fence => {
947948 try sema.zirFence(block, extended);
......@@ -1685,7 +1686,9 @@ fn resolveMaybeUndefValAllowVariables(
16851686 switch (air_tags[i]) {
16861687 .constant => {
16871688 const ty_pl = sema.air_instructions.items(.data)[i].ty_pl;
1688 return sema.air_values.items[ty_pl.payload];
1689 const val = sema.air_values.items[ty_pl.payload];
1690 if (val.tag() == .runtime_int) return null;
1691 return val;
16891692 },
16901693 .const_ty => {
16911694 return try sema.air_instructions.items(.data)[i].ty.toValue(sema.arena);
......@@ -1717,9 +1720,17 @@ fn failWithExpectedOptionalType(sema: *Sema, block: *Block, src: LazySrcLoc, opt
17171720}
17181721
17191722fn failWithArrayInitNotSupported(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError {
1720 return sema.fail(block, src, "type '{}' does not support array initialization syntax", .{
1721 ty.fmt(sema.mod),
1722 });
1723 const msg = msg: {
1724 const msg = try sema.errMsg(block, src, "type '{}' does not support array initialization syntax", .{
1725 ty.fmt(sema.mod),
1726 });
1727 errdefer msg.destroy(sema.gpa);
1728 if (ty.isSlice()) {
1729 try sema.errNote(block, src, msg, "inferred array length is specified with an underscore: '[_]{}'", .{ty.elemType2().fmt(sema.mod)});
1730 }
1731 break :msg msg;
1732 };
1733 return sema.failWithOwnedErrorMsg(block, msg);
17231734}
17241735
17251736fn failWithStructInitNotSupported(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError {
......@@ -2804,7 +2815,7 @@ fn zirAllocExtended(
28042815) CompileError!Air.Inst.Ref {
28052816 const extra = sema.code.extraData(Zir.Inst.AllocExtended, extended.operand);
28062817 const src = LazySrcLoc.nodeOffset(extra.data.src_node);
2807 const ty_src = src; // TODO better source location
2818 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = extra.data.src_node };
28082819 const align_src = src; // TODO better source location
28092820 const small = @bitCast(Zir.Inst.AllocExtended.Small, extended.small);
28102821
......@@ -3856,9 +3867,9 @@ fn zirValidateArrayInit(
38563867}
38573868
38583869fn zirValidateDeref(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
3859 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
3870 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
38603871 const src = inst_data.src();
3861 const operand_src: LazySrcLoc = .{ .token_offset = inst_data.src_tok + 1 };
3872 const operand_src: LazySrcLoc = .{ .node_offset_un_op = inst_data.src_node };
38623873 const operand = try sema.resolveInst(inst_data.operand);
38633874 const operand_ty = sema.typeOf(operand);
38643875
......@@ -3870,10 +3881,26 @@ fn zirValidateDeref(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
38703881 .Slice => return sema.fail(block, src, "index syntax required for slice type '{}'", .{operand_ty.fmt(sema.mod)}),
38713882 }
38723883
3884 const elem_ty = operand_ty.elemType2();
38733885 if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |val| {
38743886 if (val.isUndef()) {
38753887 return sema.fail(block, src, "cannot dereference undefined value", .{});
38763888 }
3889 } else if (!(try sema.validateRunTimeType(block, src, elem_ty, false))) {
3890 const msg = msg: {
3891 const msg = try sema.errMsg(
3892 block,
3893 src,
3894 "values of type '{}' must be comptime known, but operand value is runtime known",
3895 .{elem_ty.fmt(sema.mod)},
3896 );
3897 errdefer msg.destroy(sema.gpa);
3898
3899 const src_decl = sema.mod.declPtr(block.src_decl);
3900 try sema.explainWhyTypeIsComptime(block, src, msg, src.toSrcLoc(src_decl), elem_ty);
3901 break :msg msg;
3902 };
3903 return sema.failWithOwnedErrorMsg(block, msg);
38773904 }
38783905}
38793906
......@@ -4308,11 +4335,15 @@ fn zirCompileLog(
43084335 return Air.Inst.Ref.void_value;
43094336}
43104337
4311fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
4338fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index, force_comptime: bool) CompileError!Zir.Inst.Index {
43124339 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
4313 const src: LazySrcLoc = inst_data.src();
4340 const src = inst_data.src();
43144341 const msg_inst = try sema.resolveInst(inst_data.operand);
43154342
4343 if (block.is_comptime or force_comptime) {
4344 return sema.fail(block, src, "encountered @panic at comptime", .{});
4345 }
4346 try sema.requireRuntimeBlock(block, src);
43164347 return sema.panicWithMsg(block, src, msg_inst);
43174348}
43184349
......@@ -4796,14 +4827,16 @@ fn zirSetAlignStack(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Inst
47964827 alignment,
47974828 });
47984829 }
4799 const func = sema.owner_func orelse
4830 const func = sema.func orelse
48004831 return sema.fail(block, src, "@setAlignStack outside function body", .{});
48014832
48024833 const fn_owner_decl = sema.mod.declPtr(func.owner_decl);
48034834 switch (fn_owner_decl.ty.fnCallingConvention()) {
48044835 .Naked => return sema.fail(block, src, "@setAlignStack in naked function", .{}),
48054836 .Inline => return sema.fail(block, src, "@setAlignStack in inline function", .{}),
4806 else => {},
4837 else => if (block.inlining != null) {
4838 return sema.fail(block, src, "@setAlignStack in inline call", .{});
4839 },
48074840 }
48084841
48094842 const gop = try sema.mod.align_stack_fns.getOrPut(sema.mod.gpa, func);
......@@ -6161,11 +6194,12 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
61616194 const tracy = trace(@src());
61626195 defer tracy.end();
61636196
6164 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
6165 const len_src = sema.src; // TODO better source location
6166 const elem_src = sema.src; // TODO better source location
6167 const len = try sema.resolveInt(block, len_src, bin_inst.lhs, Type.usize);
6168 const elem_type = try sema.resolveType(block, elem_src, bin_inst.rhs);
6197 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
6198 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
6199 const len_src: LazySrcLoc = .{ .node_offset_array_type_len = inst_data.src_node };
6200 const elem_src: LazySrcLoc = .{ .node_offset_array_type_elem = inst_data.src_node };
6201 const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize);
6202 const elem_type = try sema.resolveType(block, elem_src, extra.rhs);
61696203 const array_ty = try Type.array(sema.arena, len, null, elem_type, sema.mod);
61706204
61716205 return sema.addType(array_ty);
......@@ -6240,18 +6274,18 @@ fn zirErrorValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
62406274 );
62416275}
62426276
6243fn zirErrorToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6277fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
62446278 const tracy = trace(@src());
62456279 defer tracy.end();
62466280
6247 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
6248 const src = inst_data.src();
6249 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
6250 const op = try sema.resolveInst(inst_data.operand);
6251 const op_coerced = try sema.coerce(block, Type.anyerror, op, operand_src);
6281 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
6282 const src = LazySrcLoc.nodeOffset(extra.node);
6283 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
6284 const uncasted_operand = try sema.resolveInst(extra.operand);
6285 const operand = try sema.coerce(block, Type.anyerror, uncasted_operand, operand_src);
62526286 const result_ty = Type.u16;
62536287
6254 if (try sema.resolveMaybeUndefVal(block, src, op_coerced)) |val| {
6288 if (try sema.resolveMaybeUndefVal(block, src, operand)) |val| {
62556289 if (val.isUndef()) {
62566290 return sema.addConstUndef(result_ty);
62576291 }
......@@ -6273,7 +6307,7 @@ fn zirErrorToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
62736307 }
62746308 }
62756309
6276 const op_ty = sema.typeOf(op);
6310 const op_ty = sema.typeOf(uncasted_operand);
62776311 try sema.resolveInferredErrorSetTy(block, src, op_ty);
62786312 if (!op_ty.isAnyError()) {
62796313 const names = op_ty.errorSetNames();
......@@ -6285,17 +6319,17 @@ fn zirErrorToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
62856319 }
62866320
62876321 try sema.requireRuntimeBlock(block, src);
6288 return block.addBitCast(result_ty, op_coerced);
6322 return block.addBitCast(result_ty, operand);
62896323}
62906324
6291fn zirIntToError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6325fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
62926326 const tracy = trace(@src());
62936327 defer tracy.end();
62946328
6295 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
6296 const src = inst_data.src();
6297 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
6298 const uncasted_operand = try sema.resolveInst(inst_data.operand);
6329 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
6330 const src = LazySrcLoc.nodeOffset(extra.node);
6331 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
6332 const uncasted_operand = try sema.resolveInst(extra.operand);
62996333 const operand = try sema.coerce(block, Type.u16, uncasted_operand, operand_src);
63006334 const target = sema.mod.getTarget();
63016335
......@@ -7432,6 +7466,9 @@ fn analyzeAs(
74327466 const dest_ty = try sema.resolveType(block, src, zir_dest_type);
74337467 const operand = try sema.resolveInst(zir_operand);
74347468 if (dest_ty.tag() == .var_args_param) return operand;
7469 if (dest_ty.zigTypeTag() == .NoReturn) {
7470 return sema.fail(block, src, "cannot cast to noreturn", .{});
7471 }
74357472 return sema.coerce(block, dest_ty, operand, src);
74367473}
74377474
......@@ -8099,9 +8136,11 @@ fn zirSwitchCond(
80998136 const union_ty = try sema.resolveTypeFields(block, operand_src, operand_ty);
81008137 const enum_ty = union_ty.unionTagType() orelse {
81018138 const msg = msg: {
8102 const msg = try sema.errMsg(block, src, "switch on untagged union", .{});
8139 const msg = try sema.errMsg(block, src, "switch on union with no attached enum", .{});
81038140 errdefer msg.destroy(sema.gpa);
8104 try sema.addDeclaredHereNote(msg, union_ty);
8141 if (union_ty.declSrcLocOrNull(sema.mod)) |union_src| {
8142 try sema.mod.errNoteNonLazy(union_src, msg, "consider 'union(enum)' here", .{});
8143 }
81058144 break :msg msg;
81068145 };
81078146 return sema.failWithOwnedErrorMsg(block, msg);
......@@ -8357,7 +8396,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
83578396 return sema.fail(
83588397 block,
83598398 src,
8360 "switch must handle all possibilities",
8399 "else prong required when switching on type 'anyerror'",
83618400 .{},
83628401 );
83638402 }
......@@ -8592,7 +8631,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
85928631 if (true_count + false_count == 2) {
85938632 return sema.fail(
85948633 block,
8595 src,
8634 special_prong_src,
85968635 "unreachable else prong; all cases already handled",
85978636 .{},
85988637 );
......@@ -9720,7 +9759,7 @@ fn zirBitNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
97209759
97219760 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
97229761 const src = inst_data.src();
9723 const operand_src = src; // TODO put this on the operand, not the '~'
9762 const operand_src: LazySrcLoc = .{ .node_offset_un_op = inst_data.src_node };
97249763
97259764 const operand = try sema.resolveInst(inst_data.operand);
97269765 const operand_type = sema.typeOf(operand);
......@@ -10219,7 +10258,7 @@ fn zirNegate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1021910258 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
1022010259 const src = inst_data.src();
1022110260 const lhs_src = src;
10222 const rhs_src = src; // TODO better source location
10261 const rhs_src: LazySrcLoc = .{ .node_offset_un_op = inst_data.src_node };
1022310262
1022410263 const rhs = try sema.resolveInst(inst_data.operand);
1022510264 const rhs_ty = sema.typeOf(rhs);
......@@ -10255,7 +10294,7 @@ fn zirNegateWrap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
1025510294 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
1025610295 const src = inst_data.src();
1025710296 const lhs_src = src;
10258 const rhs_src = src; // TODO better source location
10297 const rhs_src: LazySrcLoc = .{ .node_offset_un_op = inst_data.src_node };
1025910298
1026010299 const rhs = try sema.resolveInst(inst_data.operand);
1026110300 const rhs_ty = sema.typeOf(rhs);
......@@ -10532,18 +10571,17 @@ fn analyzeArithmetic(
1053210571 if (lhs_zig_ty_tag == .Pointer) switch (lhs_ty.ptrSize()) {
1053310572 .One, .Slice => {},
1053410573 .Many, .C => {
10535 const op_src = src; // TODO better source location
1053610574 const air_tag: Air.Inst.Tag = switch (zir_tag) {
1053710575 .add => .ptr_add,
1053810576 .sub => .ptr_sub,
1053910577 else => return sema.fail(
1054010578 block,
10541 op_src,
10579 src,
1054210580 "invalid pointer arithmetic operand: '{s}''",
1054310581 .{@tagName(zir_tag)},
1054410582 ),
1054510583 };
10546 return analyzePtrArithmetic(sema, block, op_src, lhs, rhs, air_tag, lhs_src, rhs_src);
10584 return analyzePtrArithmetic(sema, block, src, lhs, rhs, air_tag, lhs_src, rhs_src);
1054710585 },
1054810586 };
1054910587
......@@ -11941,7 +11979,6 @@ fn runtimeBoolCmp(
1194111979
1194211980fn zirSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1194311981 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
11944 const src = inst_data.src();
1194511982 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
1194611983 const ty = try sema.resolveType(block, operand_src, inst_data.operand);
1194711984 switch (ty.zigTypeTag()) {
......@@ -11951,7 +11988,7 @@ fn zirSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1195111988 .Null,
1195211989 .BoundFn,
1195311990 .Opaque,
11954 => return sema.fail(block, src, "no size available for type '{}'", .{ty.fmt(sema.mod)}),
11991 => return sema.fail(block, operand_src, "no size available for type '{}'", .{ty.fmt(sema.mod)}),
1195511992
1195611993 .Type,
1195711994 .EnumLiteral,
......@@ -12077,7 +12114,7 @@ fn zirBuiltinSrc(
1207712114 const tracy = trace(@src());
1207812115 defer tracy.end();
1207912116
12080 const src = LazySrcLoc.nodeOffset(@bitCast(i32, extended.operand));
12117 const src = sema.src; // TODO better source location
1208112118 const extra = sema.code.extraData(Zir.Inst.LineColumn, extended.operand).data;
1208212119 const func = sema.func orelse return sema.fail(block, src, "@src outside function", .{});
1208312120 const fn_owner_decl = sema.mod.declPtr(func.owner_decl);
......@@ -12116,9 +12153,8 @@ fn zirBuiltinSrc(
1211612153 field_values[0] = file_name_val;
1211712154 // fn_name: [:0]const u8,
1211812155 field_values[1] = func_name_val;
12119 // TODO these should be runtime only!
1212012156 // line: u32
12121 field_values[2] = try Value.Tag.int_u64.create(sema.arena, extra.line + 1);
12157 field_values[2] = try Value.Tag.runtime_int.create(sema.arena, extra.line + 1);
1212212158 // column: u32,
1212312159 field_values[3] = try Value.Tag.int_u64.create(sema.arena, extra.column + 1);
1212412160
......@@ -13123,7 +13159,7 @@ fn zirBoolNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1312313159
1312413160 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
1312513161 const src = inst_data.src();
13126 const operand_src = src; // TODO put this on the operand, not the `!`
13162 const operand_src: LazySrcLoc = .{ .node_offset_un_op = inst_data.src_node };
1312713163 const uncasted_operand = try sema.resolveInst(inst_data.operand);
1312813164
1312913165 const operand = try sema.coerce(block, Type.bool, uncasted_operand, operand_src);
......@@ -13667,7 +13703,8 @@ fn zirPtrTypeSimple(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
1366713703 defer tracy.end();
1366813704
1366913705 const inst_data = sema.code.instructions.items(.data)[inst].ptr_type_simple;
13670 const elem_type = try sema.resolveType(block, .unneeded, inst_data.elem_type);
13706 const elem_ty_src = sema.src; // TODO better source location
13707 const elem_type = try sema.resolveType(block, elem_ty_src, inst_data.elem_type);
1367113708 const ty = try Type.ptr(sema.arena, sema.mod, .{
1367213709 .pointee_type = elem_type,
1367313710 .@"addrspace" = .generic,
......@@ -13676,6 +13713,7 @@ fn zirPtrTypeSimple(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
1367613713 .@"volatile" = inst_data.is_volatile,
1367713714 .size = inst_data.size,
1367813715 });
13716 try sema.validatePtrTy(block, elem_ty_src, ty);
1367913717 return sema.addType(ty);
1368013718}
1368113719
......@@ -13683,9 +13721,12 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1368313721 const tracy = trace(@src());
1368413722 defer tracy.end();
1368513723
13686 // TODO better source location
13687 const src: LazySrcLoc = sema.src;
13688 const elem_ty_src: LazySrcLoc = .unneeded;
13724 const src: LazySrcLoc = sema.src; // TODO better source location
13725 const elem_ty_src: LazySrcLoc = sema.src; // TODO better source location
13726 const sentinel_src: LazySrcLoc = sema.src; // TODO better source location
13727 const addrspace_src: LazySrcLoc = sema.src; // TODO better source location
13728 const bitoffset_src: LazySrcLoc = sema.src; // TODO better source location
13729 const hostsize_src: LazySrcLoc = sema.src; // TODO better source location
1368913730 const inst_data = sema.code.instructions.items(.data)[inst].ptr_type;
1369013731 const extra = sema.code.extraData(Zir.Inst.PtrType, inst_data.payload_index);
1369113732 const unresolved_elem_ty = try sema.resolveType(block, elem_ty_src, extra.data.elem_type);
......@@ -13696,7 +13737,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1369613737 const sentinel = if (inst_data.flags.has_sentinel) blk: {
1369713738 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
1369813739 extra_i += 1;
13699 break :blk (try sema.resolveInstConst(block, .unneeded, ref)).val;
13740 break :blk (try sema.resolveInstConst(block, sentinel_src, ref)).val;
1370013741 } else null;
1370113742
1370213743 const abi_align: u32 = if (inst_data.flags.has_align) blk: {
......@@ -13718,20 +13759,20 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1371813759 const address_space = if (inst_data.flags.has_addrspace) blk: {
1371913760 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
1372013761 extra_i += 1;
13721 break :blk try sema.analyzeAddrspace(block, .unneeded, ref, .pointer);
13762 break :blk try sema.analyzeAddrspace(block, addrspace_src, ref, .pointer);
1372213763 } else .generic;
1372313764
1372413765 const bit_offset = if (inst_data.flags.has_bit_range) blk: {
1372513766 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
1372613767 extra_i += 1;
13727 const bit_offset = try sema.resolveInt(block, .unneeded, ref, Type.u16);
13768 const bit_offset = try sema.resolveInt(block, bitoffset_src, ref, Type.u16);
1372813769 break :blk @intCast(u16, bit_offset);
1372913770 } else 0;
1373013771
1373113772 const host_size: u16 = if (inst_data.flags.has_bit_range) blk: {
1373213773 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
1373313774 extra_i += 1;
13734 const host_size = try sema.resolveInt(block, .unneeded, ref, Type.u16);
13775 const host_size = try sema.resolveInt(block, hostsize_src, ref, Type.u16);
1373513776 break :blk @intCast(u16, host_size);
1373613777 } else 0;
1373713778
......@@ -13758,9 +13799,25 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1375813799 .@"volatile" = inst_data.flags.is_volatile,
1375913800 .size = inst_data.size,
1376013801 });
13802 try sema.validatePtrTy(block, elem_ty_src, ty);
1376113803 return sema.addType(ty);
1376213804}
1376313805
13806fn validatePtrTy(sema: *Sema, block: *Block, elem_src: LazySrcLoc, ty: Type) CompileError!void {
13807 const ptr_info = ty.ptrInfo().data;
13808 const pointee_tag = ptr_info.pointee_type.zigTypeTag();
13809 if (pointee_tag == .NoReturn) {
13810 return sema.fail(block, elem_src, "pointer to noreturn not allowed", .{});
13811 } else if (ptr_info.size == .Many and pointee_tag == .Opaque) {
13812 return sema.fail(block, elem_src, "unknown-length pointer to opaque not allowed", .{});
13813 } else if (ptr_info.size == .C) {
13814 // TODO check extern type
13815 if (pointee_tag == .Opaque) {
13816 return sema.fail(block, elem_src, "C pointers cannot point to opaque types", .{});
13817 }
13818 }
13819}
13820
1376413821fn zirStructInitEmpty(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1376513822 const tracy = trace(@src());
1376613823 defer tracy.end();
......@@ -14559,14 +14616,12 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1455914616 },
1456014617 .Enum => operand_ty,
1456114618 .Union => operand_ty.unionTagType() orelse {
14562 const decl_index = operand_ty.getOwnerDecl();
14563 const decl = mod.declPtr(decl_index);
1456414619 const msg = msg: {
14565 const msg = try sema.errMsg(block, src, "union '{s}' is untagged", .{
14566 decl.name,
14620 const msg = try sema.errMsg(block, src, "union '{}' is untagged", .{
14621 operand_ty.fmt(sema.mod),
1456714622 });
1456814623 errdefer msg.destroy(sema.gpa);
14569 try mod.errNoteNonLazy(decl.srcLoc(), msg, "declared here", .{});
14624 try sema.addDeclaredHereNote(msg, operand_ty);
1457014625 break :msg msg;
1457114626 };
1457214627 return sema.failWithOwnedErrorMsg(block, msg);
......@@ -16703,14 +16758,13 @@ fn analyzeShuffle(
1670316758 });
1670416759}
1670516760
16706fn zirSelect(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
16707 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
16708 const extra = sema.code.extraData(Zir.Inst.Select, inst_data.payload_index).data;
16761fn zirSelect(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
16762 const extra = sema.code.extraData(Zir.Inst.Select, extended.operand).data;
1670916763
16710 const elem_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
16711 const pred_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
16712 const a_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = inst_data.src_node };
16713 const b_src: LazySrcLoc = .{ .node_offset_builtin_call_arg3 = inst_data.src_node };
16764 const elem_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
16765 const pred_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = extra.node };
16766 const a_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = extra.node };
16767 const b_src: LazySrcLoc = .{ .node_offset_builtin_call_arg3 = extra.node };
1671416768
1671516769 const elem_ty = try sema.resolveType(block, elem_ty_src, extra.elem_type);
1671616770 try sema.checkVectorElemType(block, elem_ty_src, elem_ty);
......@@ -17417,7 +17471,7 @@ fn zirVarExtended(
1741717471 const extra = sema.code.extraData(Zir.Inst.ExtendedVar, extended.operand);
1741817472 const src = sema.src;
1741917473 const ty_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at type
17420 const mut_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at mut token
17474 const name_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at the name token
1742117475 const init_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at init expr
1742217476 const small = @bitCast(Zir.Inst.ExtendedVar.Small, extended.small);
1742317477
......@@ -17461,7 +17515,7 @@ fn zirVarExtended(
1746117515 return sema.failWithNeededComptime(block, init_src);
1746217516 } else Value.initTag(.unreachable_value);
1746317517
17464 try sema.validateVarType(block, mut_src, var_ty, small.is_extern);
17518 try sema.validateVarType(block, name_src, var_ty, small.is_extern);
1746517519
1746617520 const new_var = try sema.gpa.create(Module.Var);
1746717521 errdefer sema.gpa.destroy(new_var);
......@@ -17958,6 +18012,9 @@ fn validateVarType(
1795818012
1795918013 const src_decl = mod.declPtr(block.src_decl);
1796018014 try sema.explainWhyTypeIsComptime(block, src, msg, src.toSrcLoc(src_decl), var_ty);
18015 if (var_ty.zigTypeTag() == .ComptimeInt or var_ty.zigTypeTag() == .ComptimeFloat) {
18016 try sema.errNote(block, src, msg, "to modify this variable at runtime, it must be given an explicit fixed-size number type", .{});
18017 }
1796118018
1796218019 break :msg msg;
1796318020 };
......@@ -19445,6 +19502,34 @@ fn elemVal(
1944519502 }
1944619503}
1944719504
19505fn validateRuntimeElemAccess(
19506 sema: *Sema,
19507 block: *Block,
19508 elem_index_src: LazySrcLoc,
19509 elem_ty: Type,
19510 parent_ty: Type,
19511 parent_src: LazySrcLoc,
19512) CompileError!void {
19513 const valid_rt = try sema.validateRunTimeType(block, elem_index_src, elem_ty, false);
19514 if (!valid_rt) {
19515 const msg = msg: {
19516 const msg = try sema.errMsg(
19517 block,
19518 elem_index_src,
19519 "values of type '{}' must be comptime known, but index value is runtime known",
19520 .{parent_ty.fmt(sema.mod)},
19521 );
19522 errdefer msg.destroy(sema.gpa);
19523
19524 const src_decl = sema.mod.declPtr(block.src_decl);
19525 try sema.explainWhyTypeIsComptime(block, elem_index_src, msg, parent_src.toSrcLoc(src_decl), parent_ty);
19526
19527 break :msg msg;
19528 };
19529 return sema.failWithOwnedErrorMsg(block, msg);
19530 }
19531}
19532
1944819533fn tupleFieldPtr(
1944919534 sema: *Sema,
1945019535 block: *Block,
......@@ -19485,6 +19570,8 @@ fn tupleFieldPtr(
1948519570 );
1948619571 }
1948719572
19573 try sema.validateRuntimeElemAccess(block, field_index_src, field_ty, tuple_ty, tuple_ptr_src);
19574
1948819575 try sema.requireRuntimeBlock(block, tuple_ptr_src);
1948919576 return block.addStructFieldPtr(tuple_ptr, field_index, ptr_field_ty);
1949019577}
......@@ -19523,6 +19610,8 @@ fn tupleField(
1952319610 return sema.addConstant(field_ty, field_values[field_index]);
1952419611 }
1952519612
19613 try sema.validateRuntimeElemAccess(block, field_index_src, field_ty, tuple_ty, tuple_src);
19614
1952619615 try sema.requireRuntimeBlock(block, tuple_src);
1952719616 return block.addStructFieldVal(tuple, field_index, field_ty);
1952819617}
......@@ -19573,24 +19662,7 @@ fn elemValArray(
1957319662 }
1957419663 }
1957519664
19576 const valid_rt = try sema.validateRunTimeType(block, elem_index_src, elem_ty, false);
19577 if (!valid_rt) {
19578 const msg = msg: {
19579 const msg = try sema.errMsg(
19580 block,
19581 elem_index_src,
19582 "values of type '{}' must be comptime known, but index value is runtime known",
19583 .{array_ty.fmt(sema.mod)},
19584 );
19585 errdefer msg.destroy(sema.gpa);
19586
19587 const src_decl = sema.mod.declPtr(block.src_decl);
19588 try sema.explainWhyTypeIsComptime(block, elem_index_src, msg, array_src.toSrcLoc(src_decl), array_ty);
19589
19590 break :msg msg;
19591 };
19592 return sema.failWithOwnedErrorMsg(block, msg);
19593 }
19665 try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ty, array_ty, array_src);
1959419666
1959519667 const runtime_src = if (maybe_undef_array_val != null) elem_index_src else array_src;
1959619668 try sema.requireRuntimeBlock(block, runtime_src);
......@@ -19648,23 +19720,8 @@ fn elemPtrArray(
1964819720 }
1964919721 }
1965019722
19651 const valid_rt = try sema.validateRunTimeType(block, elem_index_src, array_ty.elemType2(), false);
19652 if (!valid_rt and !init) {
19653 const msg = msg: {
19654 const msg = try sema.errMsg(
19655 block,
19656 elem_index_src,
19657 "values of type '{}' must be comptime known, but index value is runtime known",
19658 .{array_ty.fmt(sema.mod)},
19659 );
19660 errdefer msg.destroy(sema.gpa);
19661
19662 const src_decl = sema.mod.declPtr(block.src_decl);
19663 try sema.explainWhyTypeIsComptime(block, elem_index_src, msg, array_ptr_src.toSrcLoc(src_decl), array_ty);
19664
19665 break :msg msg;
19666 };
19667 return sema.failWithOwnedErrorMsg(block, msg);
19723 if (!init) {
19724 try sema.validateRuntimeElemAccess(block, elem_index_src, array_ty.elemType2(), array_ty, array_ptr_src);
1966819725 }
1966919726
1967019727 const runtime_src = if (maybe_undef_array_ptr_val != null) elem_index_src else array_ptr_src;
......@@ -19720,6 +19777,8 @@ fn elemValSlice(
1972019777 }
1972119778 }
1972219779
19780 try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ty, slice_ty, slice_src);
19781
1972319782 try sema.requireRuntimeBlock(block, runtime_src);
1972419783 if (block.wantSafety()) {
1972519784 const len_inst = if (maybe_slice_val) |slice_val|
......@@ -19773,6 +19832,8 @@ fn elemPtrSlice(
1977319832 }
1977419833 }
1977519834
19835 try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ptr_ty, slice_ty, slice_src);
19836
1977619837 const runtime_src = if (maybe_undef_slice_val != null) elem_index_src else slice_src;
1977719838 try sema.requireRuntimeBlock(block, runtime_src);
1977819839 if (block.wantSafety()) {
......@@ -20042,6 +20103,15 @@ fn coerce(
2004220103 });
2004320104 return sema.addConstant(dest_ty, slice_val);
2004420105 }
20106
20107 if (inst_ty.zigTypeTag() == .Array) {
20108 return sema.fail(
20109 block,
20110 inst_src,
20111 "array literal requires address-of operator (&) to coerce to slice type '{}'",
20112 .{dest_ty.fmt(sema.mod)},
20113 );
20114 }
2004520115 },
2004620116 .Many => p: {
2004720117 if (!inst_ty.isSlice()) break :p;
......@@ -22368,7 +22438,7 @@ fn analyzeLoad(
2236822438 }
2236922439
2237022440 if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| {
22371 if (try sema.pointerDeref(block, ptr_src, ptr_val, ptr_ty)) |elem_val| {
22441 if (try sema.pointerDeref(block, src, ptr_val, ptr_ty)) |elem_val| {
2237222442 return sema.addConstant(elem_ty, elem_val);
2237322443 }
2237422444 if (block.is_typeof) {
......@@ -22543,9 +22613,9 @@ fn analyzeSlice(
2254322613 sentinel_opt: Air.Inst.Ref,
2254422614 sentinel_src: LazySrcLoc,
2254522615) CompileError!Air.Inst.Ref {
22546 const ptr_src = src; // TODO better source location
22547 const start_src = src; // TODO better source location
22548 const end_src = src; // TODO better source location
22616 const ptr_src: LazySrcLoc = .{ .node_offset_slice_ptr = src.node_offset.x };
22617 const start_src: LazySrcLoc = .{ .node_offset_slice_start = src.node_offset.x };
22618 const end_src: LazySrcLoc = .{ .node_offset_slice_end = src.node_offset.x };
2254922619 // Slice expressions can operate on a variable whose type is an array. This requires
2255022620 // the slice operand to be a pointer. In the case of a non-array, it will be a double pointer.
2255122621 const ptr_ptr_ty = sema.typeOf(ptr_ptr);
......@@ -22575,7 +22645,7 @@ fn analyzeSlice(
2257522645 array_ty = double_child_ty;
2257622646 elem_ty = double_child_ty.childType();
2257722647 } else {
22578 return sema.fail(block, ptr_src, "slice of single-item pointer", .{});
22648 return sema.fail(block, src, "slice of single-item pointer", .{});
2257922649 }
2258022650 },
2258122651 .Many, .C => {
......@@ -22588,7 +22658,7 @@ fn analyzeSlice(
2258822658 if (ptr_ptr_child_ty.ptrSize() == .C) {
2258922659 if (try sema.resolveDefinedValue(block, ptr_src, ptr_or_slice)) |ptr_val| {
2259022660 if (ptr_val.isNull()) {
22591 return sema.fail(block, ptr_src, "slice of null pointer", .{});
22661 return sema.fail(block, src, "slice of null pointer", .{});
2259222662 }
2259322663 }
2259422664 }
......@@ -22601,7 +22671,7 @@ fn analyzeSlice(
2260122671 elem_ty = ptr_ptr_child_ty.childType();
2260222672 },
2260322673 },
22604 else => return sema.fail(block, ptr_src, "slice of non-array type '{}'", .{ptr_ptr_child_ty.fmt(mod)}),
22674 else => return sema.fail(block, src, "slice of non-array type '{}'", .{ptr_ptr_child_ty.fmt(mod)}),
2260522675 }
2260622676
2260722677 const ptr = if (slice_ty.isSlice())
......@@ -22774,7 +22844,7 @@ fn analyzeSlice(
2277422844 return sema.addConstUndef(return_ty);
2277522845 }
2277622846
22777 return sema.fail(block, ptr_src, "non-zero length slice of undefined pointer", .{});
22847 return sema.fail(block, src, "non-zero length slice of undefined pointer", .{});
2277822848 }
2277922849
2278022850 const return_ty = try Type.ptr(sema.arena, mod, .{
......@@ -24434,6 +24504,10 @@ fn semaUnionFields(block: *Block, mod: *Module, union_obj: *Module.Union) Compil
2443424504 } else {
2443524505 // The provided type is the enum tag type.
2443624506 union_obj.tag_ty = try provided_ty.copy(decl_arena_allocator);
24507 if (union_obj.tag_ty.zigTypeTag() != .Enum) {
24508 const tag_ty_src = src; // TODO better source location
24509 return sema.fail(block, tag_ty_src, "expected enum tag type, found '{}'", .{union_obj.tag_ty.fmt(sema.mod)});
24510 }
2443724511 // The fields of the union must match the enum exactly.
2443824512 // Store a copy of the enum field names so we can check for
2443924513 // missing or extraneous fields later.
src/TypedValue.zig+1
......@@ -495,5 +495,6 @@ pub fn print(
495495 },
496496 .generic_poison_type => return writer.writeAll("(generic poison type)"),
497497 .generic_poison => return writer.writeAll("(generic poison)"),
498 .runtime_int => return writer.writeAll("[runtime value]"),
498499 };
499500}
src/Zir.zig+21-22
......@@ -212,7 +212,7 @@ pub const Inst = struct {
212212 /// Uses the `pl_node` union field. Payload is `Bin`.
213213 array_mul,
214214 /// `[N]T` syntax. No source location provided.
215 /// Uses the `bin` union field. lhs is length, rhs is element type.
215 /// Uses the `pl_node` union field. Payload is `Bin`. lhs is length, rhs is element type.
216216 array_type,
217217 /// `[N:S]T` syntax. Source location is the array type expression node.
218218 /// Uses the `pl_node` union field. Payload is `ArrayTypeSentinel`.
......@@ -244,7 +244,7 @@ pub const Inst = struct {
244244 /// Uses the pl_node field with payload `Bin`.
245245 bitcast,
246246 /// Bitwise NOT. `~`
247 /// Uses `un_node`.
247 /// Uses `un_tok`.
248248 bit_not,
249249 /// Bitwise OR. `|`
250250 bit_or,
......@@ -260,7 +260,7 @@ pub const Inst = struct {
260260 /// Uses the `pl_node` union field. Payload is `Block`.
261261 suspend_block,
262262 /// Boolean NOT. See also `bit_not`.
263 /// Uses the `un_node` field.
263 /// Uses the `un_tok` field.
264264 bool_not,
265265 /// Short-circuiting boolean `and`. `lhs` is a boolean `Ref` and the other operand
266266 /// is a block, which is evaluated if `lhs` is `true`.
......@@ -729,7 +729,7 @@ pub const Inst = struct {
729729 /// resulting array initialization value is within a comptime scope.
730730 validate_array_init_comptime,
731731 /// Check that operand type supports the dereference operand (.*).
732 /// Uses the `un_tok` field.
732 /// Uses the `un_node` field.
733733 validate_deref,
734734 /// A struct literal with a specified type, with no fields.
735735 /// Uses the `un_node` field.
......@@ -778,10 +778,6 @@ pub const Inst = struct {
778778 /// Implement builtin `@ptrToInt`. Uses `un_node`.
779779 /// Convert a pointer to a `usize` integer.
780780 ptr_to_int,
781 /// Implement builtin `@errToInt`. Uses `un_node`.
782 error_to_int,
783 /// Implement builtin `@intToError`. Uses `un_node`.
784 int_to_error,
785781 /// Emit an error message and fail compilation.
786782 /// Uses the `un_node` field.
787783 compile_error,
......@@ -802,6 +798,8 @@ pub const Inst = struct {
802798 error_name,
803799 /// Implement builtin `@panic`. Uses `un_node`.
804800 panic,
801 /// Same as `panic` but forces comptime.
802 panic_comptime,
805803 /// Implement builtin `@setCold`. Uses `un_node`.
806804 set_cold,
807805 /// Implement builtin `@setRuntimeSafety`. Uses `un_node`.
......@@ -916,9 +914,6 @@ pub const Inst = struct {
916914 /// Implements the `@shuffle` builtin.
917915 /// Uses the `pl_node` union field with payload `Shuffle`.
918916 shuffle,
919 /// Implements the `@select` builtin.
920 /// Uses the `pl_node` union field with payload `Select`.
921 select,
922917 /// Implements the `@atomicLoad` builtin.
923918 /// Uses the `pl_node` union field with payload `AtomicLoad`.
924919 atomic_load,
......@@ -1125,8 +1120,6 @@ pub const Inst = struct {
11251120 .err_union_payload_unsafe_ptr,
11261121 .err_union_code,
11271122 .err_union_code_ptr,
1128 .error_to_int,
1129 .int_to_error,
11301123 .ptr_type,
11311124 .ptr_type_simple,
11321125 .ensure_err_payload_void,
......@@ -1230,7 +1223,6 @@ pub const Inst = struct {
12301223 .splat,
12311224 .reduce,
12321225 .shuffle,
1233 .select,
12341226 .atomic_load,
12351227 .atomic_rmw,
12361228 .atomic_store,
......@@ -1270,6 +1262,7 @@ pub const Inst = struct {
12701262 .repeat,
12711263 .repeat_inline,
12721264 .panic,
1265 .panic_comptime,
12731266 => true,
12741267 };
12751268 }
......@@ -1423,8 +1416,6 @@ pub const Inst = struct {
14231416 .err_union_payload_unsafe_ptr,
14241417 .err_union_code,
14251418 .err_union_code_ptr,
1426 .error_to_int,
1427 .int_to_error,
14281419 .ptr_type,
14291420 .ptr_type_simple,
14301421 .enum_literal,
......@@ -1516,7 +1507,6 @@ pub const Inst = struct {
15161507 .splat,
15171508 .reduce,
15181509 .shuffle,
1519 .select,
15201510 .atomic_load,
15211511 .atomic_rmw,
15221512 .mul_add,
......@@ -1546,6 +1536,7 @@ pub const Inst = struct {
15461536 .repeat,
15471537 .repeat_inline,
15481538 .panic,
1539 .panic_comptime,
15491540 .@"try",
15501541 .try_ptr,
15511542 //.try_inline,
......@@ -1580,7 +1571,7 @@ pub const Inst = struct {
15801571 .param_anytype_comptime = .str_tok,
15811572 .array_cat = .pl_node,
15821573 .array_mul = .pl_node,
1583 .array_type = .bin,
1574 .array_type = .pl_node,
15841575 .array_type_sentinel = .pl_node,
15851576 .vector_type = .pl_node,
15861577 .elem_type_index = .bin,
......@@ -1713,7 +1704,7 @@ pub const Inst = struct {
17131704 .validate_struct_init_comptime = .pl_node,
17141705 .validate_array_init = .pl_node,
17151706 .validate_array_init_comptime = .pl_node,
1716 .validate_deref = .un_tok,
1707 .validate_deref = .un_node,
17171708 .struct_init_empty = .un_node,
17181709 .field_type = .pl_node,
17191710 .field_type_ref = .pl_node,
......@@ -1731,8 +1722,6 @@ pub const Inst = struct {
17311722 .bit_size_of = .un_node,
17321723
17331724 .ptr_to_int = .un_node,
1734 .error_to_int = .un_node,
1735 .int_to_error = .un_node,
17361725 .compile_error = .un_node,
17371726 .set_eval_branch_quota = .un_node,
17381727 .enum_to_int = .un_node,
......@@ -1741,6 +1730,7 @@ pub const Inst = struct {
17411730 .embed_file = .un_node,
17421731 .error_name = .un_node,
17431732 .panic = .un_node,
1733 .panic_comptime = .un_node,
17441734 .set_cold = .un_node,
17451735 .set_runtime_safety = .un_node,
17461736 .sqrt = .un_node,
......@@ -1802,7 +1792,6 @@ pub const Inst = struct {
18021792 .splat = .pl_node,
18031793 .reduce = .pl_node,
18041794 .shuffle = .pl_node,
1805 .select = .pl_node,
18061795 .atomic_load = .pl_node,
18071796 .atomic_rmw = .pl_node,
18081797 .atomic_store = .pl_node,
......@@ -1971,6 +1960,15 @@ pub const Inst = struct {
19711960 await_nosuspend,
19721961 /// `operand` is `src_node: i32`.
19731962 breakpoint,
1963 /// Implements the `@select` builtin.
1964 /// operand` is payload index to `Select`.
1965 select,
1966 /// Implement builtin `@errToInt`.
1967 /// `operand` is payload index to `UnNode`.
1968 error_to_int,
1969 /// Implement builtin `@intToError`.
1970 /// `operand` is payload index to `UnNode`.
1971 int_to_error,
19741972
19751973 pub const InstData = struct {
19761974 opcode: Extended,
......@@ -3448,6 +3446,7 @@ pub const Inst = struct {
34483446 };
34493447
34503448 pub const Select = struct {
3449 node: i32,
34513450 elem_type: Ref,
34523451 pred: Ref,
34533452 a: Ref,
src/print_zir.zig+9-8
......@@ -142,7 +142,6 @@ const Writer = struct {
142142 const tag = tags[inst];
143143 try stream.print("= {s}(", .{@tagName(tags[inst])});
144144 switch (tag) {
145 .array_type,
146145 .as,
147146 .store,
148147 .store_to_block_ptr,
......@@ -189,8 +188,6 @@ const Writer = struct {
189188 .typeof_log2_int_type,
190189 .log2_int_type,
191190 .ptr_to_int,
192 .error_to_int,
193 .int_to_error,
194191 .compile_error,
195192 .set_eval_branch_quota,
196193 .enum_to_int,
......@@ -199,6 +196,7 @@ const Writer = struct {
199196 .embed_file,
200197 .error_name,
201198 .panic,
199 .panic_comptime,
202200 .set_cold,
203201 .set_runtime_safety,
204202 .sqrt,
......@@ -284,7 +282,6 @@ const Writer = struct {
284282 .memcpy => try self.writeMemcpy(stream, inst),
285283 .memset => try self.writeMemset(stream, inst),
286284 .shuffle => try self.writeShuffle(stream, inst),
287 .select => try self.writeSelect(stream, inst),
288285 .mul_add => try self.writeMulAdd(stream, inst),
289286 .field_parent_ptr => try self.writeFieldParentPtr(stream, inst),
290287 .builtin_call => try self.writeBuiltinCall(stream, inst),
......@@ -356,6 +353,7 @@ const Writer = struct {
356353 .elem_ptr,
357354 .elem_val,
358355 .coerce_result_ptr,
356 .array_type,
359357 => try self.writePlNodeBin(stream, inst),
360358
361359 .elem_ptr_imm => try self.writeElemPtrImm(stream, inst),
......@@ -478,6 +476,8 @@ const Writer = struct {
478476 .compile_log => try self.writeNodeMultiOp(stream, extended),
479477 .typeof_peer => try self.writeTypeofPeer(stream, extended),
480478
479 .select => try self.writeSelect(stream, extended),
480
481481 .add_with_overflow,
482482 .sub_with_overflow,
483483 .mul_with_overflow,
......@@ -496,6 +496,8 @@ const Writer = struct {
496496 .set_float_mode,
497497 .set_align_stack,
498498 .wasm_memory_size,
499 .error_to_int,
500 .int_to_error,
499501 => {
500502 const inst_data = self.code.extraData(Zir.Inst.UnNode, extended.operand).data;
501503 const src = LazySrcLoc.nodeOffset(inst_data.node);
......@@ -772,9 +774,8 @@ const Writer = struct {
772774 try self.writeSrc(stream, inst_data.src());
773775 }
774776
775 fn writeSelect(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
776 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
777 const extra = self.code.extraData(Zir.Inst.Select, inst_data.payload_index).data;
777 fn writeSelect(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
778 const extra = self.code.extraData(Zir.Inst.Select, extended.operand).data;
778779 try self.writeInstRef(stream, extra.elem_type);
779780 try stream.writeAll(", ");
780781 try self.writeInstRef(stream, extra.pred);
......@@ -783,7 +784,7 @@ const Writer = struct {
783784 try stream.writeAll(", ");
784785 try self.writeInstRef(stream, extra.b);
785786 try stream.writeAll(") ");
786 try self.writeSrc(stream, inst_data.src());
787 try self.writeSrc(stream, LazySrcLoc.nodeOffset(extra.node));
787788 }
788789
789790 fn writeMulAdd(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
src/value.zig+8
......@@ -111,6 +111,7 @@ pub const Value = extern union {
111111 int_i64,
112112 int_big_positive,
113113 int_big_negative,
114 runtime_int,
114115 function,
115116 extern_fn,
116117 variable,
......@@ -304,6 +305,7 @@ pub const Value = extern union {
304305 .int_type => Payload.IntType,
305306 .int_u64 => Payload.U64,
306307 .int_i64 => Payload.I64,
308 .runtime_int => Payload.U64,
307309 .function => Payload.Function,
308310 .variable => Payload.Variable,
309311 .decl_ref_mut => Payload.DeclRefMut,
......@@ -483,6 +485,7 @@ pub const Value = extern union {
483485 },
484486 .int_type => return self.copyPayloadShallow(arena, Payload.IntType),
485487 .int_u64 => return self.copyPayloadShallow(arena, Payload.U64),
488 .runtime_int => return self.copyPayloadShallow(arena, Payload.U64),
486489 .int_i64 => return self.copyPayloadShallow(arena, Payload.I64),
487490 .int_big_positive, .int_big_negative => {
488491 const old_payload = self.cast(Payload.BigInt).?;
......@@ -762,6 +765,7 @@ pub const Value = extern union {
762765 .int_i64 => return std.fmt.formatIntValue(val.castTag(.int_i64).?.data, "", options, out_stream),
763766 .int_big_positive => return out_stream.print("{}", .{val.castTag(.int_big_positive).?.asBigInt()}),
764767 .int_big_negative => return out_stream.print("{}", .{val.castTag(.int_big_negative).?.asBigInt()}),
768 .runtime_int => return out_stream.writeAll("[runtime value]"),
765769 .function => return out_stream.print("(function decl={d})", .{val.castTag(.function).?.data.owner_decl}),
766770 .extern_fn => return out_stream.writeAll("(extern function)"),
767771 .variable => return out_stream.writeAll("(variable)"),
......@@ -1077,6 +1081,8 @@ pub const Value = extern union {
10771081 .int_big_positive => return val.castTag(.int_big_positive).?.asBigInt(),
10781082 .int_big_negative => return val.castTag(.int_big_negative).?.asBigInt(),
10791083
1084 .runtime_int => return BigIntMutable.init(&space.limbs, val.castTag(.runtime_int).?.data).toConst(),
1085
10801086 .undef => unreachable,
10811087
10821088 .lazy_align => {
......@@ -1132,6 +1138,8 @@ pub const Value = extern union {
11321138 .int_big_positive => return val.castTag(.int_big_positive).?.asBigInt().to(u64) catch null,
11331139 .int_big_negative => return val.castTag(.int_big_negative).?.asBigInt().to(u64) catch null,
11341140
1141 .runtime_int => return val.castTag(.runtime_int).?.data,
1142
11351143 .undef => unreachable,
11361144
11371145 .lazy_align => {
test/cases/compile_errors/array_access_of_type.zig created+10
......@@ -0,0 +1,10 @@
1export fn foo() void {
2 var b: u8[40] = undefined;
3 _ = b;
4}
5
6// error
7// backend=stage2
8// target=native
9//
10// :2:14: error: element access of non-indexable type 'type'
test/cases/compile_errors/invalid_array_elem_ty.zig+1-1
......@@ -9,4 +9,4 @@ pub export fn entry() void {
99// target=native
1010// backend=stage2
1111//
12// :4:1: error: expected type 'type', found 'fn() type'
12// :5:12: error: expected type 'type', found 'fn() type'
test/cases/compile_errors/method_call_with_first_arg_type_primitive.zig created+21
......@@ -0,0 +1,21 @@
1const Foo = struct {
2 x: i32,
3
4 fn init(x: i32) Foo {
5 return Foo {
6 .x = x,
7 };
8 }
9};
10
11export fn f() void {
12 const derp = Foo.init(3);
13
14 derp.init();
15}
16
17// error
18// backend=stage2
19// target=native
20//
21// :14:9: error: type 'tmp.Foo' has no field or member function named 'init'
test/cases/compile_errors/method_call_with_first_arg_type_wrong_container.zig created+30
......@@ -0,0 +1,30 @@
1pub const List = struct {
2 len: usize,
3 allocator: *Allocator,
4
5 pub fn init(allocator: *Allocator) List {
6 return List {
7 .len = 0,
8 .allocator = allocator,
9 };
10 }
11};
12
13pub var global_allocator = Allocator {
14 .field = 1234,
15};
16
17pub const Allocator = struct {
18 field: i32,
19};
20
21export fn foo() void {
22 var x = List.init(&global_allocator);
23 x.init();
24}
25
26// error
27// backend=llvm
28// target=native
29//
30// :23:6: error: type 'tmp.List' has no field or member function named 'init'
test/cases/compile_errors/missing_const_in_slice_with_nested_array_type.zig created+18
......@@ -0,0 +1,18 @@
1const Geo3DTex2D = struct { vertices: [][2]f32 };
2pub fn getGeo3DTex2D() Geo3DTex2D {
3 return Geo3DTex2D{
4 .vertices = [_][2]f32{
5 [_]f32{ -0.5, -0.5},
6 },
7 };
8}
9export fn entry() void {
10 var geo_data = getGeo3DTex2D();
11 _ = geo_data;
12}
13
14// error
15// backend=llvm
16// target=native
17//
18// :4:30: error: array literal requires address-of operator (&) to coerce to slice type '[][2]f32'
test/cases/compile_errors/mul_overflow_in_function_evaluation.zig created+14
......@@ -0,0 +1,14 @@
1const y = mul(300, 6000);
2fn mul(a: u16, b: u16) u16 {
3 return a * b;
4}
5
6export fn entry() usize { return @sizeOf(@TypeOf(&y)); }
7
8// error
9// backend=stage2
10// target=native
11//
12// :3:14: error: overflow of integer type 'u16' with value '1800000'
13// :1:14: note: called from here
14
test/cases/compile_errors/negation_overflow_in_function_evaluation.zig created+13
......@@ -0,0 +1,13 @@
1const y = neg(-128);
2fn neg(x: i8) i8 {
3 return -x;
4}
5
6export fn entry() usize { return @sizeOf(@TypeOf(&y)); }
7
8// error
9// backend=stage2
10// target=native
11//
12// :3:12: error: overflow of integer type 'i8' with value '128'
13// :1:14: note: called from here
test/cases/compile_errors/no_else_prong_on_switch_on_global_error_set.zig created+14
......@@ -0,0 +1,14 @@
1export fn entry() void {
2 foo(error.A);
3}
4fn foo(a: anyerror) void {
5 switch (a) {
6 error.A => {},
7 }
8}
9
10// error
11// backend=stage2
12// target=native
13//
14// :5:5: error: else prong required when switching on type 'anyerror'
test/cases/compile_errors/non-const_variables_of_things_that_require_const_variables.zig created+44
......@@ -0,0 +1,44 @@
1export fn entry1() void {
2 var m2 = &2;
3 _ = m2;
4}
5export fn entry2() void {
6 var a = undefined;
7 _ = a;
8}
9export fn entry3() void {
10 var b = 1;
11 _ = b;
12}
13export fn entry4() void {
14 var c = 1.0;
15 _ = c;
16}
17export fn entry5() void {
18 var d = null;
19 _ = d;
20}
21export fn entry6(opaque_: *Opaque) void {
22 var e = opaque_.*;
23 _ = e;
24}
25export fn entry7() void {
26 var f = i32;
27 _ = f;
28}
29const Opaque = opaque {};
30
31// error
32// backend=stage2
33// target=native
34//
35// :2:8: error: variable of type '*const comptime_int' must be const or comptime
36// :6:8: error: variable of type '@TypeOf(undefined)' must be const or comptime
37// :10:8: error: variable of type 'comptime_int' must be const or comptime
38// :10:8: note: to modify this variable at runtime, it must be given an explicit fixed-size number type
39// :14:8: error: variable of type 'comptime_float' must be const or comptime
40// :14:8: note: to modify this variable at runtime, it must be given an explicit fixed-size number type
41// :18:8: error: variable of type '@TypeOf(null)' must be const or comptime
42// :22:19: error: values of type 'tmp.Opaque' must be comptime known, but operand value is runtime known
43// :26:8: error: variable of type 'type' must be const or comptime
44// :26:8: note: types are not available at runtime
test/cases/compile_errors/non-inline_for_loop_on_a_type_that_requires_comptime.zig created+16
......@@ -0,0 +1,16 @@
1const Foo = struct {
2 name: []const u8,
3 T: type,
4};
5export fn entry() void {
6 const xx: [2]Foo = .{ .{ .name = "", .T = u8 }, .{ .name = "", .T = u8 } };
7 for (xx) |f| { _ = f;}
8}
9
10// error
11// backend=stage2
12// target=native
13//
14// :7:10: error: values of type '[2]tmp.Foo' must be comptime known, but index value is runtime known
15// :3:8: note: struct requires comptime because of this field
16// :3:8: note: types are not available at runtime
test/cases/compile_errors/non_constant_expression_in_array_size.zig created+14
......@@ -0,0 +1,14 @@
1const Foo = struct {
2 y: [get()]u8,
3};
4var global_var: usize = 1;
5fn get() usize { return global_var; }
6
7export fn entry() usize { return @offsetOf(Foo, "y"); }
8
9// error
10// backend=stage2
11// target=native
12//
13// :5:25: error: unable to resolve comptime value
14// :2:15: note: called from here
test/cases/compile_errors/non_error_sets_used_in_merge_error_sets_operator.zig created+15
......@@ -0,0 +1,15 @@
1export fn foo() void {
2 const Errors = u8 || u16;
3 _ = Errors;
4}
5export fn bar() void {
6 const Errors = error{} || u16;
7 _ = Errors;
8}
9
10// error
11// backend=stage2
12// target=native
13//
14// :2:20: error: expected error set type, found 'u8'
15// :6:31: error: expected error set type, found 'u16'
test/cases/compile_errors/panic_called_at_compile_time.zig created+11
......@@ -0,0 +1,11 @@
1export fn entry() void {
2 comptime {
3 @panic("aoeu",);
4 }
5}
6
7// error
8// backend=stage2
9// target=native
10//
11// :3:9: error: encountered @panic at comptime
test/cases/compile_errors/range_operator_in_switch_used_on_error_set.zig created+20
......@@ -0,0 +1,20 @@
1export fn entry() void {
2 foo(452) catch |err| switch (err) {
3 error.Foo ... error.Bar => {},
4 else => {},
5 };
6}
7fn foo(x: i32) !void {
8 switch (x) {
9 0 ... 10 => return error.Foo,
10 11 ... 20 => return error.Bar,
11 else => {},
12 }
13}
14
15// error
16// backend=llvm
17// target=native
18//
19// :2:34: error: ranges not allowed when switching on type '@typeInfo(@typeInfo(@TypeOf(tmp.foo)).Fn.return_type.?).ErrorUnion.error_set'
20// :3:19: note: range here
test/cases/compile_errors/reading_past_end_of_pointer_casted_array.zig created+13
......@@ -0,0 +1,13 @@
1comptime {
2 const array: [4]u8 = "aoeu".*;
3 const sub_array = array[1..];
4 const int_ptr = @ptrCast(*const u24, sub_array);
5 const deref = int_ptr.*;
6 _ = deref;
7}
8
9// error
10// backend=stage2
11// target=native
12//
13// :5:26: error: dereference of '*const u24' exceeds bounds of containing decl of type '[4]u8'
test/cases/compile_errors/runtime_index_into_comptime_type_slice.zig created+19
......@@ -0,0 +1,19 @@
1const Struct = struct {
2 a: u32,
3};
4fn getIndex() usize {
5 return 2;
6}
7export fn entry() void {
8 const index = getIndex();
9 const field = @typeInfo(Struct).Struct.fields[index];
10 _ = field;
11}
12
13// error
14// backend=stage2
15// target=native
16//
17// :9:51: error: values of type '[]const builtin.Type.StructField' must be comptime known, but index value is runtime known
18// :?:21: note: struct requires comptime because of this field
19// :?:21: note: types are not available at runtime
test/cases/compile_errors/setAlignStack_in_inline_function.zig created+23
......@@ -0,0 +1,23 @@
1export fn entry() void {
2 foo();
3}
4fn foo() callconv(.Inline) void {
5 @setAlignStack(16);
6}
7
8export fn entry1() void {
9 comptime bar();
10}
11fn bar() void {
12 @setAlignStack(16);
13}
14
15
16// error
17// backend=stage2
18// target=native
19//
20// :5:5: error: @setAlignStack in inline function
21// :2:8: note: called from here
22// :12:5: error: @setAlignStack in inline call
23// :9:17: note: called from here
test/cases/compile_errors/signed_integer_remainder_division.zig created+9
......@@ -0,0 +1,9 @@
1export fn foo(a: i32, b: i32) i32 {
2 return a % b;
3}
4
5// error
6// backend=stage2
7// target=native
8//
9// :2:12: error: remainder division with 'i32' and 'i32': signed integers and floats must use @rem or @mod
test/cases/compile_errors/sizeOf_bad_type.zig created+9
......@@ -0,0 +1,9 @@
1export fn entry() usize {
2 return @sizeOf(@TypeOf(null));
3}
4
5// error
6// backend=stage2
7// target=native
8//
9// :2:20: error: no size available for type '@TypeOf(null)'
test/cases/compile_errors/slice_cannot_have_its_bytes_reinterpreted.zig created+11
......@@ -0,0 +1,11 @@
1export fn foo() void {
2 const bytes = [1]u8{ 0xfa } ** 16;
3 var value = @ptrCast(*const []const u8, &bytes).*;
4 _ = value;
5}
6
7// error
8// backend=stage2
9// target=native
10//
11// :3:52: error: comptime dereference requires '[]const u8' to have a well-defined layout, but it does not.
test/cases/compile_errors/slice_passed_as_array_init_type.zig created+11
......@@ -0,0 +1,11 @@
1export fn entry() void {
2 const x = []u8{};
3 _ = x;
4}
5
6// error
7// backend=stage2
8// target=native
9//
10// :2:19: error: type '[]u8' does not support array initialization syntax
11// :2:19: note: inferred array length is specified with an underscore: '[_]u8'
test/cases/compile_errors/slice_passed_as_array_init_type_with_elems.zig created+11
......@@ -0,0 +1,11 @@
1export fn entry() void {
2 const x = []u8{1, 2};
3 _ = x;
4}
5
6// error
7// backend=stage2
8// target=native
9//
10// :2:19: error: type '[]u8' does not support array initialization syntax
11// :2:19: note: inferred array length is specified with an underscore: '[_]u8'
test/cases/compile_errors/src_fields_runtime.zig created+14
......@@ -0,0 +1,14 @@
1pub export fn entry1() void {
2 const s = @src();
3 comptime var a: []const u8 = s.file;
4 comptime var b: []const u8 = s.fn_name;
5 comptime var c: u32 = s.column;
6 comptime var d: u32 = s.line;
7 _ = a; _ = b; _ = c; _ = d;
8}
9
10// error
11// backend=stage2
12// target=native
13//
14// :6:28: error: cannot store runtime value in compile time variable
test/cases/compile_errors/stage1/non_compile_time_array_concatenation.zig created+11
......@@ -0,0 +1,11 @@
1fn f() []u8 {
2 return s ++ "foo";
3}
4var s: [10]u8 = undefined;
5export fn entry() usize { return @sizeOf(@TypeOf(f)); }
6
7// error
8// backend=stage1
9// target=native
10//
11// tmp.zig:2:12: error: unable to evaluate constant expression
test/cases/compile_errors/stage1/obj/array_access_of_type.zig deleted-10
......@@ -1,10 +0,0 @@
1export fn foo() void {
2 var b: u8[40] = undefined;
3 _ = b;
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:2:14: error: array access of non-array type 'type'
test/cases/compile_errors/stage1/obj/method_call_with_first_arg_type_primitive.zig deleted-21
......@@ -1,21 +0,0 @@
1const Foo = struct {
2 x: i32,
3
4 fn init(x: i32) Foo {
5 return Foo {
6 .x = x,
7 };
8 }
9};
10
11export fn f() void {
12 const derp = Foo.init(3);
13
14 derp.init();
15}
16
17// error
18// backend=stage1
19// target=native
20//
21// tmp.zig:14:5: error: expected type 'i32', found 'Foo'
test/cases/compile_errors/stage1/obj/method_call_with_first_arg_type_wrong_container.zig deleted-30
......@@ -1,30 +0,0 @@
1pub const List = struct {
2 len: usize,
3 allocator: *Allocator,
4
5 pub fn init(allocator: *Allocator) List {
6 return List {
7 .len = 0,
8 .allocator = allocator,
9 };
10 }
11};
12
13pub var global_allocator = Allocator {
14 .field = 1234,
15};
16
17pub const Allocator = struct {
18 field: i32,
19};
20
21export fn foo() void {
22 var x = List.init(&global_allocator);
23 x.init();
24}
25
26// error
27// backend=stage1
28// target=native
29//
30// tmp.zig:23:5: error: expected type '*Allocator', found '*List'
test/cases/compile_errors/stage1/obj/missing_const_in_slice_with_nested_array_type.zig deleted-18
......@@ -1,18 +0,0 @@
1const Geo3DTex2D = struct { vertices: [][2]f32 };
2pub fn getGeo3DTex2D() Geo3DTex2D {
3 return Geo3DTex2D{
4 .vertices = [_][2]f32{
5 [_]f32{ -0.5, -0.5},
6 },
7 };
8}
9export fn entry() void {
10 var geo_data = getGeo3DTex2D();
11 _ = geo_data;
12}
13
14// error
15// backend=stage1
16// target=native
17//
18// tmp.zig:4:30: error: array literal requires address-of operator (&) to coerce to slice type '[][2]f32'
test/cases/compile_errors/stage1/obj/mul_overflow_in_function_evaluation.zig deleted-12
......@@ -1,12 +0,0 @@
1const y = mul(300, 6000);
2fn mul(a: u16, b: u16) u16 {
3 return a * b;
4}
5
6export fn entry() usize { return @sizeOf(@TypeOf(y)); }
7
8// error
9// backend=stage1
10// target=native
11//
12// tmp.zig:3:14: error: operation caused overflow
test/cases/compile_errors/stage1/obj/negation_overflow_in_function_evaluation.zig deleted-12
......@@ -1,12 +0,0 @@
1const y = neg(-128);
2fn neg(x: i8) i8 {
3 return -x;
4}
5
6export fn entry() usize { return @sizeOf(@TypeOf(y)); }
7
8// error
9// backend=stage1
10// target=native
11//
12// tmp.zig:3:12: error: negation caused overflow
test/cases/compile_errors/stage1/obj/no_else_prong_on_switch_on_global_error_set.zig deleted-14
......@@ -1,14 +0,0 @@
1export fn entry() void {
2 foo(error.A);
3}
4fn foo(a: anyerror) void {
5 switch (a) {
6 error.A => {},
7 }
8}
9
10// error
11// backend=stage1
12// target=native
13//
14// tmp.zig:5:5: error: else prong required when switching on type 'anyerror'
test/cases/compile_errors/stage1/obj/non-const_variables_of_things_that_require_const_variables.zig deleted-51
......@@ -1,51 +0,0 @@
1export fn entry1() void {
2 var m2 = &2;
3 _ = m2;
4}
5export fn entry2() void {
6 var a = undefined;
7 _ = a;
8}
9export fn entry3() void {
10 var b = 1;
11 _ = b;
12}
13export fn entry4() void {
14 var c = 1.0;
15 _ = c;
16}
17export fn entry5() void {
18 var d = null;
19 _ = d;
20}
21export fn entry6(opaque_: *Opaque) void {
22 var e = opaque_.*;
23 _ = e;
24}
25export fn entry7() void {
26 var f = i32;
27 _ = f;
28}
29export fn entry8() void {
30 var h = (Foo {}).bar;
31 _ = h;
32}
33const Opaque = opaque {};
34const Foo = struct {
35 fn bar(self: *const Foo) void {_ = self;}
36};
37
38// error
39// backend=stage1
40// target=native
41//
42// tmp.zig:2:4: error: variable of type '*const comptime_int' must be const or comptime
43// tmp.zig:6:4: error: variable of type '@Type(.Undefined)' must be const or comptime
44// tmp.zig:10:4: error: variable of type 'comptime_int' must be const or comptime
45// tmp.zig:10:4: note: to modify this variable at runtime, it must be given an explicit fixed-size number type
46// tmp.zig:14:4: error: variable of type 'comptime_float' must be const or comptime
47// tmp.zig:14:4: note: to modify this variable at runtime, it must be given an explicit fixed-size number type
48// tmp.zig:18:4: error: variable of type '@Type(.Null)' must be const or comptime
49// tmp.zig:22:4: error: variable of type 'Opaque' not allowed
50// tmp.zig:26:4: error: variable of type 'type' must be const or comptime
51// tmp.zig:30:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime
test/cases/compile_errors/stage1/obj/non-inline_for_loop_on_a_type_that_requires_comptime.zig deleted-14
......@@ -1,14 +0,0 @@
1const Foo = struct {
2 name: []const u8,
3 T: type,
4};
5export fn entry() void {
6 const xx: [2]Foo = undefined;
7 for (xx) |f| { _ = f;}
8}
9
10// error
11// backend=stage1
12// target=native
13//
14// tmp.zig:7:5: error: values of type 'Foo' must be comptime known, but index value is runtime known
test/cases/compile_errors/stage1/obj/non_compile_time_array_concatenation.zig deleted-11
......@@ -1,11 +0,0 @@
1fn f() []u8 {
2 return s ++ "foo";
3}
4var s: [10]u8 = undefined;
5export fn entry() usize { return @sizeOf(@TypeOf(f)); }
6
7// error
8// backend=stage1
9// target=native
10//
11// tmp.zig:2:12: error: unable to evaluate constant expression
test/cases/compile_errors/stage1/obj/non_constant_expression_in_array_size.zig deleted-14
......@@ -1,14 +0,0 @@
1const Foo = struct {
2 y: [get()]u8,
3};
4var global_var: usize = 1;
5fn get() usize { return global_var; }
6
7export fn entry() usize { return @sizeOf(@TypeOf(Foo)); }
8
9// error
10// backend=stage1
11// target=native
12//
13// tmp.zig:5:25: error: cannot store runtime value in compile time variable
14// tmp.zig:2:12: note: called from here
test/cases/compile_errors/stage1/obj/non_error_sets_used_in_merge_error_sets_operator.zig deleted-17
......@@ -1,17 +0,0 @@
1export fn foo() void {
2 const Errors = u8 || u16;
3 _ = Errors;
4}
5export fn bar() void {
6 const Errors = error{} || u16;
7 _ = Errors;
8}
9
10// error
11// backend=stage1
12// target=native
13//
14// tmp.zig:2:20: error: expected error set type, found type 'u8'
15// tmp.zig:2:23: note: `||` merges error sets; `or` performs boolean OR
16// tmp.zig:6:31: error: expected error set type, found type 'u16'
17// tmp.zig:6:28: note: `||` merges error sets; `or` performs boolean OR
test/cases/compile_errors/stage1/obj/panic_called_at_compile_time.zig deleted-11
......@@ -1,11 +0,0 @@
1export fn entry() void {
2 comptime {
3 @panic("aoeu",);
4 }
5}
6
7// error
8// backend=stage1
9// target=native
10//
11// tmp.zig:3:9: error: encountered @panic at compile-time
test/cases/compile_errors/stage1/obj/ptrCast_a_0_bit_type_to_a_non-_0_bit_type.zig deleted-13
......@@ -1,13 +0,0 @@
1export fn entry() bool {
2 var x: u0 = 0;
3 const p = @ptrCast(?*u0, &x);
4 return p == null;
5}
6
7// error
8// backend=stage1
9// target=native
10//
11// tmp.zig:3:15: error: '*u0' and '?*u0' do not have the same in-memory representation
12// tmp.zig:3:31: note: '*u0' has no in-memory bits
13// tmp.zig:3:24: note: '?*u0' has in-memory bits
test/cases/compile_errors/stage1/obj/ptrToInt_on_void.zig deleted-9
......@@ -1,9 +0,0 @@
1export fn entry() bool {
2 return @ptrToInt(&{}) == @ptrToInt(&{});
3}
4
5// error
6// backend=stage1
7// target=native
8//
9// tmp.zig:2:23: error: pointer to size 0 type has no address
test/cases/compile_errors/stage1/obj/range_operator_in_switch_used_on_error_set.zig deleted-19
......@@ -1,19 +0,0 @@
1export fn entry() void {
2 try foo(452) catch |err| switch (err) {
3 error.A ... error.B => {},
4 else => {},
5 };
6}
7fn foo(x: i32) !void {
8 switch (x) {
9 0 ... 10 => return error.Foo,
10 11 ... 20 => return error.Bar,
11 else => {},
12 }
13}
14
15// error
16// backend=stage1
17// target=native
18//
19// tmp.zig:3:17: error: operator not allowed for errors
test/cases/compile_errors/stage1/obj/reading_past_end_of_pointer_casted_array.zig deleted-13
......@@ -1,13 +0,0 @@
1comptime {
2 const array: [4]u8 = "aoeu".*;
3 const sub_array = array[1..];
4 const int_ptr = @ptrCast(*const u24, sub_array);
5 const deref = int_ptr.*;
6 _ = deref;
7}
8
9// error
10// backend=stage1
11// target=native
12//
13// tmp.zig:5:26: error: attempt to read 4 bytes from [4]u8 at index 1 which is 3 bytes
test/cases/compile_errors/stage1/obj/recursive_inferred_error_set.zig deleted-12
......@@ -1,12 +0,0 @@
1export fn entry() void {
2 foo() catch unreachable;
3}
4fn foo() !void {
5 try foo();
6}
7
8// error
9// backend=stage1
10// target=native
11//
12// tmp.zig:5:5: error: cannot resolve inferred error set '@typeInfo(@typeInfo(@TypeOf(foo)).Fn.return_type.?).ErrorUnion.error_set': function 'foo' not fully analyzed yet
test/cases/compile_errors/stage1/obj/runtime_index_into_comptime_type_slice.zig deleted-17
......@@ -1,17 +0,0 @@
1const Struct = struct {
2 a: u32,
3};
4fn getIndex() usize {
5 return 2;
6}
7export fn entry() void {
8 const index = getIndex();
9 const field = @typeInfo(Struct).Struct.fields[index];
10 _ = field;
11}
12
13// error
14// backend=stage1
15// target=native
16//
17// tmp.zig:9:51: error: values of type 'std.builtin.Type.StructField' must be comptime known, but index value is runtime known
test/cases/compile_errors/stage1/obj/setAlignStack_in_inline_function.zig deleted-12
......@@ -1,12 +0,0 @@
1export fn entry() void {
2 foo();
3}
4fn foo() callconv(.Inline) void {
5 @setAlignStack(16);
6}
7
8// error
9// backend=stage1
10// target=native
11//
12// tmp.zig:5:5: error: @setAlignStack in inline function
test/cases/compile_errors/stage1/obj/signed_integer_remainder_division.zig deleted-9
......@@ -1,9 +0,0 @@
1export fn foo(a: i32, b: i32) i32 {
2 return a % b;
3}
4
5// error
6// backend=stage1
7// target=native
8//
9// tmp.zig:2:14: error: remainder division with 'i32' and 'i32': signed integers and floats must use @rem or @mod
test/cases/compile_errors/stage1/obj/sizeOf_bad_type.zig deleted-9
......@@ -1,9 +0,0 @@
1export fn entry() usize {
2 return @sizeOf(@TypeOf(null));
3}
4
5// error
6// backend=stage1
7// target=native
8//
9// tmp.zig:2:20: error: no size available for type '@Type(.Null)'
test/cases/compile_errors/stage1/obj/slice_cannot_have_its_bytes_reinterpreted.zig deleted-11
......@@ -1,11 +0,0 @@
1export fn foo() void {
2 const bytes = [1]u8{ 0xfa } ** 16;
3 var value = @ptrCast(*const []const u8, &bytes).*;
4 _ = value;
5}
6
7// error
8// backend=stage1
9// target=native
10//
11// :3:52: error: slice '[]const u8' cannot have its bytes reinterpreted
test/cases/compile_errors/stage1/obj/slice_passed_as_array_init_type.zig deleted-10
......@@ -1,10 +0,0 @@
1export fn entry() void {
2 const x = []u8{};
3 _ = x;
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:2:15: error: array literal requires address-of operator (&) to coerce to slice type '[]u8'
test/cases/compile_errors/stage1/obj/slice_passed_as_array_init_type_with_elems.zig deleted-10
......@@ -1,10 +0,0 @@
1export fn entry() void {
2 const x = []u8{1, 2};
3 _ = x;
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:2:15: error: array literal requires address-of operator (&) to coerce to slice type '[]u8'
test/cases/compile_errors/stage1/obj/slicing_of_global_undefined_pointer.zig deleted-10
......@@ -1,10 +0,0 @@
1var buf: *[1]u8 = undefined;
2export fn entry() void {
3 _ = buf[0..1];
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:3:12: error: non-zero length slice of undefined pointer
test/cases/compile_errors/stage1/obj/sub_overflow_in_function_evaluation.zig deleted-12
......@@ -1,12 +0,0 @@
1const y = sub(10, 20);
2fn sub(a: u16, b: u16) u16 {
3 return a - b;
4}
5
6export fn entry() usize { return @sizeOf(@TypeOf(y)); }
7
8// error
9// backend=stage1
10// target=native
11//
12// tmp.zig:3:14: error: operation caused overflow
test/cases/compile_errors/stage1/obj/switch_expression-duplicate_or_overlapping_integer_value.zig deleted-16
......@@ -1,16 +0,0 @@
1fn foo(x: u8) u8 {
2 return switch (x) {
3 0 ... 100 => @as(u8, 0),
4 101 ... 200 => 1,
5 201, 203 ... 207 => 2,
6 206 ... 255 => 3,
7 };
8}
9export fn entry() usize { return @sizeOf(@TypeOf(foo)); }
10
11// error
12// backend=stage1
13// target=native
14//
15// tmp.zig:6:9: error: duplicate switch value
16// tmp.zig:5:14: note: previous value here
test/cases/compile_errors/stage1/obj/switch_expression-switch_on_pointer_type_with_no_else.zig deleted-13
......@@ -1,13 +0,0 @@
1fn foo(x: *u8) void {
2 switch (x) {
3 &y => {},
4 }
5}
6const y: u8 = 100;
7export fn entry() usize { return @sizeOf(@TypeOf(foo)); }
8
9// error
10// backend=stage1
11// target=native
12//
13// tmp.zig:2:5: error: else prong required when switching on type '*u8'
test/cases/compile_errors/stage1/obj/switch_expression-unreachable_else_prong_bool.zig deleted-14
......@@ -1,14 +0,0 @@
1fn foo(x: bool) void {
2 switch (x) {
3 true => {},
4 false => {},
5 else => {},
6 }
7}
8export fn entry() usize { return @sizeOf(@TypeOf(foo)); }
9
10// error
11// backend=stage1
12// target=native
13//
14// tmp.zig:5:9: error: unreachable else prong, all cases already handled
test/cases/compile_errors/stage1/obj/switch_on_union_with_no_attached_enum.zig deleted-22
......@@ -1,22 +0,0 @@
1const Payload = union {
2 A: i32,
3 B: f64,
4 C: bool,
5};
6export fn entry() void {
7 const a = Payload { .A = 1234 };
8 foo(a);
9}
10fn foo(a: *const Payload) void {
11 switch (a.*) {
12 Payload.A => {},
13 else => unreachable,
14 }
15}
16
17// error
18// backend=stage1
19// target=native
20//
21// tmp.zig:11:14: error: switch on union which has no attached enum
22// tmp.zig:1:17: note: consider 'union(enum)' here
test/cases/compile_errors/stage1/obj/switch_with_invalid_expression_parameter.zig deleted-17
......@@ -1,17 +0,0 @@
1export fn entry() void {
2 Test(i32);
3}
4fn Test(comptime T: type) void {
5 const x = switch (T) {
6 []u8 => |x| x,
7 i32 => |x| x,
8 else => unreachable,
9 };
10 _ = x;
11}
12
13// error
14// backend=stage1
15// target=native
16//
17// tmp.zig:7:17: error: switch on type 'type' provides no expression parameter
test/cases/compile_errors/stage1/obj/switch_with_overlapping_case_ranges.zig deleted-13
......@@ -1,13 +0,0 @@
1export fn entry() void {
2 var q: u8 = 0;
3 switch (q) {
4 1...2 => {},
5 0...255 => {},
6 }
7}
8
9// error
10// backend=stage1
11// target=native
12//
13// tmp.zig:5:9: error: duplicate switch value
test/cases/compile_errors/stage1/obj/tagName_used_on_union_with_no_associated_enum_tag.zig deleted-16
......@@ -1,16 +0,0 @@
1const FloatInt = extern union {
2 Float: f32,
3 Int: i32,
4};
5export fn entry() void {
6 var fi = FloatInt{.Float = 123.45};
7 var tagName = @tagName(fi);
8 _ = tagName;
9}
10
11// error
12// backend=stage1
13// target=native
14//
15// tmp.zig:7:19: error: union has no associated enum
16// tmp.zig:1:18: note: declared here
test/cases/compile_errors/stage1/obj/type_variables_must_be_constant.zig deleted-10
......@@ -1,10 +0,0 @@
1var foo = u8;
2export fn entry() foo {
3 return 1;
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// :1:1: error: variable of type 'type' must be constant
test/cases/compile_errors/stage1/obj/unreachable_executed_at_comptime.zig deleted-16
......@@ -1,16 +0,0 @@
1fn foo(comptime x: i32) i32 {
2 comptime {
3 if (x >= 0) return -x;
4 unreachable;
5 }
6}
7export fn entry() void {
8 _ = foo(-42);
9}
10
11// error
12// backend=stage1
13// target=native
14//
15// tmp.zig:4:9: error: reached unreachable code
16// tmp.zig:8:12: note: called from here
test/cases/compile_errors/stage1/obj/use_of_comptime-known_undefined_function_value.zig deleted-13
......@@ -1,13 +0,0 @@
1const Cmd = struct {
2 exec: fn () void,
3};
4export fn entry() void {
5 const command = Cmd{ .exec = undefined };
6 command.exec();
7}
8
9// error
10// backend=stage1
11// target=native
12//
13// tmp.zig:6:12: error: use of undefined value here causes undefined behavior
test/cases/compile_errors/stage1/obj/wasmMemoryGrow_is_a_compile_error_in_non-Wasm_targets.zig deleted-10
......@@ -1,10 +0,0 @@
1export fn foo() void {
2 _ = @wasmMemoryGrow(0, 1);
3 return;
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:2:9: error: @wasmMemoryGrow is a wasm32 feature only
test/cases/compile_errors/stage1/obj/wasmMemorySize_is_a_compile_error_in_non-Wasm_targets.zig deleted-10
......@@ -1,10 +0,0 @@
1export fn foo() void {
2 _ = @wasmMemorySize(0);
3 return;
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:2:9: error: @wasmMemorySize is a wasm32 feature only
test/cases/compile_errors/stage1/obj/wrong_type_for_reify_type.zig deleted-9
......@@ -1,9 +0,0 @@
1export fn entry() void {
2 _ = @Type(0);
3}
4
5// error
6// backend=stage1
7// target=native
8//
9// tmp.zig:2:15: error: expected type 'std.builtin.Type', found 'comptime_int'
test/cases/compile_errors/stage1/ptrCast_a_0_bit_type_to_a_non-_0_bit_type.zig created+13
......@@ -0,0 +1,13 @@
1export fn entry() bool {
2 var x: u0 = 0;
3 const p = @ptrCast(?*u0, &x);
4 return p == null;
5}
6
7// error
8// backend=stage1
9// target=native
10//
11// tmp.zig:3:15: error: '*u0' and '?*u0' do not have the same in-memory representation
12// tmp.zig:3:31: note: '*u0' has no in-memory bits
13// tmp.zig:3:24: note: '?*u0' has in-memory bits
test/cases/compile_errors/stage1/ptrToInt_on_void.zig created+9
......@@ -0,0 +1,9 @@
1export fn entry() bool {
2 return @ptrToInt(&{}) == @ptrToInt(&{});
3}
4
5// error
6// backend=stage1
7// target=native
8//
9// tmp.zig:2:23: error: pointer to size 0 type has no address
test/cases/compile_errors/stage1/recursive_inferred_error_set.zig created+12
......@@ -0,0 +1,12 @@
1export fn entry() void {
2 foo() catch unreachable;
3}
4fn foo() !void {
5 try foo();
6}
7
8// error
9// backend=stage1
10// target=native
11//
12// tmp.zig:5:5: error: cannot resolve inferred error set '@typeInfo(@typeInfo(@TypeOf(foo)).Fn.return_type.?).ErrorUnion.error_set': function 'foo' not fully analyzed yet
test/cases/compile_errors/stage1/slicing_of_global_undefined_pointer.zig created+10
......@@ -0,0 +1,10 @@
1var buf: *[1]u8 = undefined;
2export fn entry() void {
3 _ = buf[0..1];
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:3:12: error: non-zero length slice of undefined pointer
test/cases/compile_errors/stage1/switch_with_invalid_expression_parameter.zig created+17
......@@ -0,0 +1,17 @@
1export fn entry() void {
2 Test(i32);
3}
4fn Test(comptime T: type) void {
5 const x = switch (T) {
6 []u8 => |x| x,
7 i32 => |x| x,
8 else => unreachable,
9 };
10 _ = x;
11}
12
13// error
14// backend=stage1
15// target=native
16//
17// tmp.zig:7:17: error: switch on type 'type' provides no expression parameter
test/cases/compile_errors/stage1/use_of_comptime-known_undefined_function_value.zig created+13
......@@ -0,0 +1,13 @@
1const Cmd = struct {
2 exec: fn () void,
3};
4export fn entry() void {
5 const command = Cmd{ .exec = undefined };
6 command.exec();
7}
8
9// error
10// backend=stage1
11// target=native
12//
13// tmp.zig:6:12: error: use of undefined value here causes undefined behavior
test/cases/compile_errors/stage2/out_of_bounds_index.zig+4-4
......@@ -23,7 +23,7 @@ comptime {
2323// error
2424// target=native
2525//
26// :4:26: error: end index 6 out of bounds for slice of length 4 +1 (sentinel)
27// :9:22: error: end index 6 out of bounds for array of length 4 +1 (sentinel)
28// :14:22: error: end index 5 out of bounds for array of length 4
29// :19:22: error: start index 3 is larger than end index 2
26// :4:30: error: end index 6 out of bounds for slice of length 4 +1 (sentinel)
27// :9:26: error: end index 6 out of bounds for array of length 4 +1 (sentinel)
28// :14:26: error: end index 5 out of bounds for array of length 4
29// :19:23: error: start index 3 is larger than end index 2
test/cases/compile_errors/sub_overflow_in_function_evaluation.zig created+13
......@@ -0,0 +1,13 @@
1const y = sub(10, 20);
2fn sub(a: u16, b: u16) u16 {
3 return a - b;
4}
5
6export fn entry() usize { return @sizeOf(@TypeOf(&y)); }
7
8// error
9// backend=stage2
10// target=native
11//
12// :3:14: error: overflow of integer type 'u16' with value '-10'
13// :1:14: note: called from here
test/cases/compile_errors/switch_expression-duplicate_or_overlapping_integer_value.zig created+16
......@@ -0,0 +1,16 @@
1fn foo(x: u8) u8 {
2 return switch (x) {
3 0 ... 100 => @as(u8, 0),
4 101 ... 200 => 1,
5 201, 203 ... 207 => 2,
6 206 ... 255 => 3,
7 };
8}
9export fn entry() usize { return @sizeOf(@TypeOf(&foo)); }
10
11// error
12// backend=stage2
13// target=native
14//
15// :6:13: error: duplicate switch value
16// :5:18: note: previous value here
test/cases/compile_errors/switch_expression-switch_on_pointer_type_with_no_else.zig created+13
......@@ -0,0 +1,13 @@
1fn foo(x: *u8) void {
2 switch (x) {
3 &y => {},
4 }
5}
6var y: u8 = 100;
7export fn entry() usize { return @sizeOf(@TypeOf(&foo)); }
8
9// error
10// backend=stage2
11// target=native
12//
13// :2:5: error: else prong required when switching on type '*u8'
test/cases/compile_errors/switch_expression-unreachable_else_prong_bool.zig created+14
......@@ -0,0 +1,14 @@
1fn foo(x: bool) void {
2 switch (x) {
3 true => {},
4 false => {},
5 else => {},
6 }
7}
8export fn entry() usize { return @sizeOf(@TypeOf(&foo)); }
9
10// error
11// backend=stage2
12// target=native
13//
14// :5:14: error: unreachable else prong; all cases already handled
test/cases/compile_errors/switch_on_union_with_no_attached_enum.zig created+22
......@@ -0,0 +1,22 @@
1const Payload = union {
2 A: i32,
3 B: f64,
4 C: bool,
5};
6export fn entry() void {
7 const a = Payload { .A = 1234 };
8 foo(&a);
9}
10fn foo(a: *const Payload) void {
11 switch (a.*) {
12 Payload.A => {},
13 else => unreachable,
14 }
15}
16
17// error
18// backend=stage2
19// target=native
20//
21// :11:14: error: switch on union with no attached enum
22// :1:17: note: consider 'union(enum)' here
test/cases/compile_errors/switch_with_overlapping_case_ranges.zig created+14
......@@ -0,0 +1,14 @@
1export fn entry() void {
2 var q: u8 = 0;
3 switch (q) {
4 1...2 => {},
5 0...255 => {},
6 }
7}
8
9// error
10// backend=stage2
11// target=native
12//
13// :5:10: error: duplicate switch value
14// :4:10: note: previous value here
test/cases/compile_errors/tagName_used_on_union_with_no_associated_enum_tag.zig created+16
......@@ -0,0 +1,16 @@
1const FloatInt = extern union {
2 Float: f32,
3 Int: i32,
4};
5export fn entry() void {
6 var fi = FloatInt{.Float = 123.45};
7 var tagName = @tagName(fi);
8 _ = tagName;
9}
10
11// error
12// backend=stage2
13// target=native
14//
15// :7:19: error: union 'tmp.FloatInt' is untagged
16// :1:25: note: union declared here
test/cases/compile_errors/type_variables_must_be_constant.zig created+11
......@@ -0,0 +1,11 @@
1var foo = u8;
2export fn entry() foo {
3 return 1;
4}
5
6// error
7// backend=stage2
8// target=native
9//
10// :1:1: error: variable of type 'type' must be const or comptime
11// :1:1: note: types are not available at runtime
test/cases/compile_errors/unreachable_executed_at_comptime.zig created+16
......@@ -0,0 +1,16 @@
1fn foo(comptime x: i32) i32 {
2 comptime {
3 if (x >= 0) return -x;
4 unreachable;
5 }
6}
7export fn entry() void {
8 _ = comptime foo(-42);
9}
10
11// error
12// backend=stage2
13// target=native
14//
15// :4:9: error: reached unreachable code
16// :8:21: note: called from here
test/cases/compile_errors/unreachable_variable.zig+1-1
......@@ -7,4 +7,4 @@ export fn f() void {
77// backend=stage2
88// target=native
99//
10// :2:25: error: expected type 'noreturn', found 'void'
10// :2:25: error: cannot cast to noreturn
test/cases/compile_errors/use_invalid_number_literal_as_array_index.zig+1
......@@ -9,3 +9,4 @@ export fn entry() void {
99// target=native
1010//
1111// :1:1: error: variable of type 'comptime_int' must be const or comptime
12// :1:1: note: to modify this variable at runtime, it must be given an explicit fixed-size number type
test/cases/compile_errors/wasmMemoryGrow_is_a_compile_error_in_non-Wasm_targets.zig created+10
......@@ -0,0 +1,10 @@
1export fn foo() void {
2 _ = @wasmMemoryGrow(0, 1);
3 return;
4}
5
6// error
7// backend=stage2
8// target=x86_64-native
9//
10// :2:9: error: builtin @wasmMemoryGrow is available when targeting WebAssembly; targeted CPU architecture is x86_64
test/cases/compile_errors/wasmMemorySize_is_a_compile_error_in_non-Wasm_targets.zig created+10
......@@ -0,0 +1,10 @@
1export fn foo() void {
2 _ = @wasmMemorySize(0);
3 return;
4}
5
6// error
7// backend=stage2
8// target=x86_64-native
9//
10// :2:9: error: builtin @wasmMemorySize is available when targeting WebAssembly; targeted CPU architecture is x86_64
test/cases/compile_errors/wrong_type_for_reify_type.zig created+9
......@@ -0,0 +1,9 @@
1export fn entry() void {
2 _ = @Type(0);
3}
4
5// error
6// backend=stage2
7// target=native
8//
9// :2:15: error: expected type 'builtin.Type', found 'comptime_int'
test/cases/variable_shadowing.10.zig created+9
......@@ -0,0 +1,9 @@
1fn foo() !void {
2 var i: anyerror!usize = 1;
3 _ = i catch |i| return i;
4}
5
6// error
7//
8// :3:18: error: redeclaration of local variable 'i'
9// :2:9: note: previous declaration here
test/standalone/issue_7030/main.zig+12
......@@ -1,5 +1,17 @@
11const std = @import("std");
22
3pub fn log(
4 comptime message_level: std.log.Level,
5 comptime scope: @Type(.EnumLiteral),
6 comptime format: []const u8,
7 args: anytype,
8) void {
9 _ = message_level;
10 _ = scope;
11 _ = format;
12 _ = args;
13}
14
315pub fn main() anyerror!void {
416 std.log.info("All your codebase are belong to us.", .{});
517}