Skip to content

Commit 0c78ece

Browse files
authored
Merge pull request #12016 from Vexu/stage2-compile-errors
Stage2 compile error improvements
2 parents 6f17be0 + 5007f72 commit 0c78ece

File tree

71 files changed

+660
-474
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+660
-474
lines changed

src/AstGen.zig

+37-11
Original file line numberDiff line numberDiff line change
@@ -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

@@ -7359,15 +7370,13 @@ fn builtinCall(
73597370
.align_of => return simpleUnOpType(gz, scope, rl, node, params[0], .align_of),
73607371

73617372
.ptr_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .ptr_to_int),
7362-
.error_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .error_to_int),
7363-
.int_to_error => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .u16_type }, params[0], .int_to_error),
73647373
.compile_error => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .compile_error),
73657374
.set_eval_branch_quota => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .u32_type }, params[0], .set_eval_branch_quota),
73667375
.enum_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .enum_to_int),
73677376
.bool_to_int => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .bool_to_int),
73687377
.embed_file => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .embed_file),
73697378
.error_name => return simpleUnOp(gz, scope, rl, node, .{ .ty = .anyerror_type }, params[0], .error_name),
7370-
.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),
73717380
.set_cold => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_cold),
73727381
.set_runtime_safety => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_runtime_safety),
73737382
.sqrt => return simpleUnOp(gz, scope, rl, node, .none, params[0], .sqrt),
@@ -7400,6 +7409,22 @@ fn builtinCall(
74007409
.truncate => return typeCast(gz, scope, rl, node, params[0], params[1], .truncate),
74017410
// zig fmt: on
74027411

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+
},
74037428
.align_cast => {
74047429
const dest_align = try comptimeExpr(gz, scope, align_rl, params[0]);
74057430
const rhs = try expr(gz, scope, .none, params[1]);
@@ -7639,7 +7664,8 @@ fn builtinCall(
76397664
return rvalue(gz, rl, result, node);
76407665
},
76417666
.select => {
7642-
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),
76437669
.elem_type = try typeExpr(gz, scope, params[0]),
76447670
.pred = try expr(gz, scope, .none, params[1]),
76457671
.a = try expr(gz, scope, .none, params[2]),

src/Module.zig

+49-2
Original file line numberDiff line numberDiff line change
@@ -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,

0 commit comments

Comments
 (0)