Skip to content

Commit 7e98b04

Browse files
committed
AstGen: simplify function return type expressions
This check for primitives is already handled by the generic logic that checks if the body ends up being empty. I kept this commit in the git history in case we ever want that nodePrimitive function again in the future, it might be useful.
1 parent 9da3a05 commit 7e98b04

File tree

1 file changed

+7
-35
lines changed

1 file changed

+7
-35
lines changed

src/AstGen.zig

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3562,17 +3562,14 @@ fn fnDecl(
35623562

35633563
var ret_gz = decl_gz.makeSubBlock(params_scope);
35643564
defer ret_gz.unstack();
3565-
const ret_ref: Zir.Inst.Ref = switch (nodePrimitive(tree, fn_proto.ast.return_type)) {
3566-
.none => inst: {
3567-
const inst = try expr(&ret_gz, params_scope, coerced_type_rl, fn_proto.ast.return_type);
3568-
if (ret_gz.instructionsSlice().len == 0) {
3569-
// In this case we will send a len=0 body which can be encoded more efficiently.
3570-
break :inst inst;
3571-
}
3572-
_ = try ret_gz.addBreak(.break_inline, 0, inst);
3565+
const ret_ref: Zir.Inst.Ref = inst: {
3566+
const inst = try expr(&ret_gz, params_scope, coerced_type_rl, fn_proto.ast.return_type);
3567+
if (ret_gz.instructionsSlice().len == 0) {
3568+
// In this case we will send a len=0 body which can be encoded more efficiently.
35733569
break :inst inst;
3574-
},
3575-
else => |p| p,
3570+
}
3571+
_ = try ret_gz.addBreak(.break_inline, 0, inst);
3572+
break :inst inst;
35763573
};
35773574

35783575
const func_inst: Zir.Inst.Ref = if (body_node == 0) func: {
@@ -9025,31 +9022,6 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {
90259022
}
90269023
}
90279024

9028-
fn nodePrimitive(tree: *const Ast, start_node: Ast.Node.Index) Zir.Inst.Ref {
9029-
const node_tags = tree.nodes.items(.tag);
9030-
const node_datas = tree.nodes.items(.data);
9031-
9032-
var node = start_node;
9033-
while (true) {
9034-
switch (node_tags[node]) {
9035-
// Forward the question to the LHS sub-expression.
9036-
.grouped_expression => node = node_datas[node].lhs,
9037-
9038-
.identifier => {
9039-
const main_tokens = tree.nodes.items(.main_token);
9040-
const ident_bytes = tree.tokenSlice(main_tokens[node]);
9041-
if (primitives.get(ident_bytes)) |primitive| {
9042-
return primitive;
9043-
} else {
9044-
return .none;
9045-
}
9046-
},
9047-
9048-
else => return .none,
9049-
}
9050-
}
9051-
}
9052-
90539025
/// Applies `rl` semantics to `result`. Expressions which do not do their own handling of
90549026
/// result locations must call this function on their result.
90559027
/// As an example, if the `ResultLoc` is `ptr`, it will write the result to the pointer.

0 commit comments

Comments
 (0)