Skip to content

Stage2 compile error improvements #12016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 7, 2022
48 changes: 37 additions & 11 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr

.deref => {
const lhs = try expr(gz, scope, .none, node_datas[node].lhs);
_ = try gz.addUnTok(.validate_deref, lhs, main_tokens[node]);
_ = try gz.addUnNode(.validate_deref, lhs, node);
switch (rl) {
.ref => return lhs,
else => {
Expand Down Expand Up @@ -1320,7 +1320,10 @@ fn arrayInitExpr(
const len_inst = try gz.addInt(array_init.ast.elements.len);
const elem_type = try typeExpr(gz, scope, array_type.ast.elem_type);
if (array_type.ast.sentinel == 0) {
const array_type_inst = try gz.addBin(.array_type, len_inst, elem_type);
const array_type_inst = try gz.addPlNode(.array_type, array_init.ast.type_expr, Zir.Inst.Bin{
.lhs = len_inst,
.rhs = elem_type,
});
break :inst .{
.array = array_type_inst,
.elem = elem_type,
Expand Down Expand Up @@ -1553,7 +1556,10 @@ fn structInitExpr(
if (is_inferred_array_len) {
const elem_type = try typeExpr(gz, scope, array_type.ast.elem_type);
const array_type_inst = if (array_type.ast.sentinel == 0) blk: {
break :blk try gz.addBin(.array_type, .zero_usize, elem_type);
break :blk try gz.addPlNode(.array_type, struct_init.ast.type_expr, Zir.Inst.Bin{
.lhs = .zero_usize,
.rhs = elem_type,
});
} else blk: {
const sentinel = try comptimeExpr(gz, scope, .{ .ty = elem_type }, array_type.ast.sentinel);
break :blk try gz.addPlNode(
Expand Down Expand Up @@ -2332,8 +2338,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
.error_union_type,
.bit_not,
.error_value,
.error_to_int,
.int_to_error,
.slice_start,
.slice_end,
.slice_sentinel,
Expand Down Expand Up @@ -2420,7 +2424,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
.splat,
.reduce,
.shuffle,
.select,
.atomic_load,
.atomic_rmw,
.mul_add,
Expand Down Expand Up @@ -2467,6 +2470,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
.repeat,
.repeat_inline,
.panic,
.panic_comptime,
=> {
noreturn_src_node = statement;
break :b true;
Expand Down Expand Up @@ -3100,6 +3104,10 @@ fn ptrType(
node: Ast.Node.Index,
ptr_info: Ast.full.PtrType,
) InnerError!Zir.Inst.Ref {
if (ptr_info.size == .C and ptr_info.allowzero_token != null) {
return gz.astgen.failTok(ptr_info.allowzero_token.?, "C pointers always allow address zero", .{});
}

const elem_type = try typeExpr(gz, scope, ptr_info.ast.child_type);

const simple = ptr_info.ast.align_node == 0 and
Expand Down Expand Up @@ -3205,7 +3213,10 @@ fn arrayType(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) !Z
const len = try expr(gz, scope, .{ .coerced_ty = .usize_type }, len_node);
const elem_type = try typeExpr(gz, scope, node_datas[node].rhs);

const result = try gz.addBin(.array_type, len, elem_type);
const result = try gz.addPlNode(.array_type, node, Zir.Inst.Bin{
.lhs = len,
.rhs = elem_type,
});
return rvalue(gz, rl, result, node);
}

Expand Down Expand Up @@ -7355,15 +7366,13 @@ fn builtinCall(
.align_of => return simpleUnOpType(gz, scope, rl, node, params[0], .align_of),

.ptr_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .ptr_to_int),
.error_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .error_to_int),
.int_to_error => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .u16_type }, params[0], .int_to_error),
.compile_error => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .compile_error),
.set_eval_branch_quota => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .u32_type }, params[0], .set_eval_branch_quota),
.enum_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .enum_to_int),
.bool_to_int => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .bool_to_int),
.embed_file => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .embed_file),
.error_name => return simpleUnOp(gz, scope, rl, node, .{ .ty = .anyerror_type }, params[0], .error_name),
.panic => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .panic),
.panic => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], if (gz.force_comptime) .panic_comptime else .panic),
.set_cold => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_cold),
.set_runtime_safety => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_runtime_safety),
.sqrt => return simpleUnOp(gz, scope, rl, node, .none, params[0], .sqrt),
Expand Down Expand Up @@ -7396,6 +7405,22 @@ fn builtinCall(
.truncate => return typeCast(gz, scope, rl, node, params[0], params[1], .truncate),
// zig fmt: on

.error_to_int => {
const operand = try expr(gz, scope, .none, params[0]);
const result = try gz.addExtendedPayload(.error_to_int, Zir.Inst.UnNode{
.node = gz.nodeIndexToRelative(node),
.operand = operand,
});
return rvalue(gz, rl, result, node);
},
.int_to_error => {
const operand = try expr(gz, scope, .{ .coerced_ty = .u16_type }, params[0]);
const result = try gz.addExtendedPayload(.int_to_error, Zir.Inst.UnNode{
.node = gz.nodeIndexToRelative(node),
.operand = operand,
});
return rvalue(gz, rl, result, node);
},
.align_cast => {
const dest_align = try comptimeExpr(gz, scope, align_rl, params[0]);
const rhs = try expr(gz, scope, .none, params[1]);
Expand Down Expand Up @@ -7635,7 +7660,8 @@ fn builtinCall(
return rvalue(gz, rl, result, node);
},
.select => {
const result = try gz.addPlNode(.select, node, Zir.Inst.Select{
const result = try gz.addExtendedPayload(.select, Zir.Inst.Select{
.node = gz.nodeIndexToRelative(node),
.elem_type = try typeExpr(gz, scope, params[0]),
.pred = try expr(gz, scope, .none, params[1]),
.a = try expr(gz, scope, .none, params[2]),
Expand Down
51 changes: 49 additions & 2 deletions src/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2171,7 +2171,11 @@ pub const SrcLoc = struct {
const token_starts = tree.tokens.items(.start);
return token_starts[tok_index];
},
.node_offset_slice_sentinel => |node_off| {
.node_offset_slice_ptr,
.node_offset_slice_start,
.node_offset_slice_end,
.node_offset_slice_sentinel,
=> |node_off| {
const tree = try src_loc.file_scope.getTree(gpa);
const node_tags = tree.nodes.items(.tag);
const node = src_loc.declRelativeToNodeIndex(node_off);
Expand All @@ -2182,7 +2186,15 @@ pub const SrcLoc = struct {
else => unreachable,
};
const main_tokens = tree.nodes.items(.main_token);
const tok_index = main_tokens[full.ast.sentinel];
const tok_index = main_tokens[
switch (src_loc.lazy) {
.node_offset_slice_ptr => full.ast.sliced,
.node_offset_slice_start => full.ast.start,
.node_offset_slice_end => full.ast.end,
.node_offset_slice_sentinel => full.ast.sentinel,
else => unreachable,
}
];
const token_starts = tree.tokens.items(.start);
return token_starts[tok_index];
},
Expand Down Expand Up @@ -2501,6 +2513,16 @@ pub const SrcLoc = struct {
const token_starts = tree.tokens.items(.start);
return token_starts[tok_index];
},
.node_offset_un_op => |node_off| {
const tree = try src_loc.file_scope.getTree(gpa);
const node_datas = tree.nodes.items(.data);
const node = src_loc.declRelativeToNodeIndex(node_off);

const main_tokens = tree.nodes.items(.main_token);
const tok_index = main_tokens[node_datas[node].lhs];
const token_starts = tree.tokens.items(.start);
return token_starts[tok_index];
},
}
}

Expand Down Expand Up @@ -2614,6 +2636,24 @@ pub const LazySrcLoc = union(enum) {
/// to the index expression.
/// The Decl is determined contextually.
node_offset_array_access_index: i32,
/// The source location points to the LHS of a slice expression
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a slice AST node. Next, navigate
/// to the sentinel expression.
/// The Decl is determined contextually.
node_offset_slice_ptr: i32,
/// The source location points to start expression of a slice expression
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a slice AST node. Next, navigate
/// to the sentinel expression.
/// The Decl is determined contextually.
node_offset_slice_start: i32,
/// The source location points to the end expression of a slice
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a slice AST node. Next, navigate
/// to the sentinel expression.
/// The Decl is determined contextually.
node_offset_slice_end: i32,
/// The source location points to the sentinel expression of a slice
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a slice AST node. Next, navigate
Expand Down Expand Up @@ -2728,6 +2768,9 @@ pub const LazySrcLoc = union(enum) {
/// to the elem expression.
/// The Decl is determined contextually.
node_offset_array_type_elem: i32,
/// The source location points to the operand of an unary expression.
/// The Decl is determined contextually.
node_offset_un_op: i32,

pub const nodeOffset = if (TracedOffset.want_tracing) nodeOffsetDebug else nodeOffsetRelease;

Expand Down Expand Up @@ -2768,6 +2811,9 @@ pub const LazySrcLoc = union(enum) {
.node_offset_builtin_call_arg4,
.node_offset_builtin_call_arg5,
.node_offset_array_access_index,
.node_offset_slice_ptr,
.node_offset_slice_start,
.node_offset_slice_end,
.node_offset_slice_sentinel,
.node_offset_call_func,
.node_offset_field_name,
Expand All @@ -2788,6 +2834,7 @@ pub const LazySrcLoc = union(enum) {
.node_offset_array_type_len,
.node_offset_array_type_sentinel,
.node_offset_array_type_elem,
.node_offset_un_op,
=> .{
.file_scope = decl.getFileScope(),
.parent_decl_node = decl.src_node,
Expand Down
Loading