Skip to content

Commit

Permalink
Sema: catch non-sized input operands
Browse files Browse the repository at this point in the history
  • Loading branch information
wooster0 committed Jun 27, 2023
1 parent 88284c1 commit 14390aa
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2385,6 +2385,14 @@ pub const SrcLoc = struct {
const node_datas = tree.nodes.items(.data);
return nodeToSpan(tree, node_datas[asm_output].lhs);
},
.asm_input_op => |asm_input_op| {
const tree = try src_loc.file_scope.getTree(gpa);
const node = src_loc.declRelativeToNodeIndex(asm_input_op.asm_node_offset);
const full = tree.fullAsm(node).?;
const asm_input = full.inputs[asm_input_op.input_index];
const node_datas = tree.nodes.items(.data);
return nodeToSpan(tree, node_datas[asm_input].lhs);
},

.node_offset_if_cond => |node_off| {
const tree = try src_loc.file_scope.getTree(gpa);
Expand Down Expand Up @@ -2988,6 +2996,17 @@ pub const LazySrcLoc = union(enum) {
/// to the return type expression.
/// The Decl is determined contextually.
node_offset_asm_ret_ty: i32,
/// The source location points to the operand of an input of an inline assembly
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to inline assembly AST node. Next, navigate
/// to the input operand expression.
/// The Decl is determined contextually.
asm_input_op: struct {
/// Points to the asm AST node.
asm_node_offset: i32,
/// Picks one of the inputs from the asm.
input_index: u32,
},
/// The source location points to the condition expression of an if
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to an if expression AST node. Next, navigate
Expand Down Expand Up @@ -3196,6 +3215,7 @@ pub const LazySrcLoc = union(enum) {
.node_offset_deref_ptr,
.node_offset_asm_source,
.node_offset_asm_ret_ty,
.asm_input_op,
.node_offset_if_cond,
.node_offset_bin_op,
.node_offset_bin_lhs,
Expand Down
9 changes: 9 additions & 0 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15796,6 +15796,15 @@ fn zirAsm(
.ComptimeInt => arg.* = try sema.coerce(block, Type.usize, uncasted_arg, src),
.ComptimeFloat => arg.* = try sema.coerce(block, Type.f64, uncasted_arg, src),
else => {
if (!uncasted_arg_ty.hasRuntimeBits(mod)) {
const input_op_src: LazySrcLoc = .{
.asm_input_op = .{
.asm_node_offset = extra.data.src_node,
.input_index = @intCast(arg_i),
},
};
return sema.fail(block, input_op_src, "input operand of type '{}' is not sized", .{uncasted_arg_ty.fmt(mod)});
}
arg.* = uncasted_arg;
try sema.queueFullTypeResolution(uncasted_arg_ty);
},
Expand Down
30 changes: 30 additions & 0 deletions test/cases/compile_errors/non_sized_asm_input_operand.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export fn entry1() void {
asm volatile (""
:
: [_] "{al}" (u8),
);
}

export fn entry2() void {
asm volatile (""
:
: [_] "{a}" (0),
[_] "{x}" (void),
);
}

export fn entry3() void {
asm volatile (""
:
: [_] "{x}" (enum {}),
[_] "{x}" (u0),
);
}

// error
// backend=stage2
// target=native
//
// :4:23: error: input operand of type 'type' is not sized
// :12:22: error: input operand of type 'type' is not sized
// :19:22: error: input operand of type 'type' is not sized

0 comments on commit 14390aa

Please sign in to comment.