Skip to content

Commit c95eeb1

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 91939c3 commit c95eeb1

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
@@ -3540,17 +3540,14 @@ fn fnDecl(
35403540

35413541
var ret_gz = decl_gz.makeSubBlock(params_scope);
35423542
defer ret_gz.unstack();
3543-
const ret_ref: Zir.Inst.Ref = switch (nodePrimitive(tree, fn_proto.ast.return_type)) {
3544-
.none => inst: {
3545-
const inst = try expr(&ret_gz, params_scope, coerced_type_rl, fn_proto.ast.return_type);
3546-
if (ret_gz.instructionsSlice().len == 0) {
3547-
// In this case we will send a len=0 body which can be encoded more efficiently.
3548-
break :inst inst;
3549-
}
3550-
_ = try ret_gz.addBreak(.break_inline, 0, inst);
3543+
const ret_ref: Zir.Inst.Ref = inst: {
3544+
const inst = try expr(&ret_gz, params_scope, coerced_type_rl, fn_proto.ast.return_type);
3545+
if (ret_gz.instructionsSlice().len == 0) {
3546+
// In this case we will send a len=0 body which can be encoded more efficiently.
35513547
break :inst inst;
3552-
},
3553-
else => |p| p,
3548+
}
3549+
_ = try ret_gz.addBreak(.break_inline, 0, inst);
3550+
break :inst inst;
35543551
};
35553552

35563553
const func_inst: Zir.Inst.Ref = if (body_node == 0) func: {
@@ -9003,31 +9000,6 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {
90039000
}
90049001
}
90059002

9006-
fn nodePrimitive(tree: *const Ast, start_node: Ast.Node.Index) Zir.Inst.Ref {
9007-
const node_tags = tree.nodes.items(.tag);
9008-
const node_datas = tree.nodes.items(.data);
9009-
9010-
var node = start_node;
9011-
while (true) {
9012-
switch (node_tags[node]) {
9013-
// Forward the question to the LHS sub-expression.
9014-
.grouped_expression => node = node_datas[node].lhs,
9015-
9016-
.identifier => {
9017-
const main_tokens = tree.nodes.items(.main_token);
9018-
const ident_bytes = tree.tokenSlice(main_tokens[node]);
9019-
if (primitives.get(ident_bytes)) |primitive| {
9020-
return primitive;
9021-
} else {
9022-
return .none;
9023-
}
9024-
},
9025-
9026-
else => return .none,
9027-
}
9028-
}
9029-
}
9030-
90319003
/// Applies `rl` semantics to `result`. Expressions which do not do their own handling of
90329004
/// result locations must call this function on their result.
90339005
/// As an example, if the `ResultLoc` is `ptr`, it will write the result to the pointer.

0 commit comments

Comments
 (0)