mirror of
https://github.com/ziglang/zig.git
synced 2026-01-03 03:53:20 +00:00
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.
This commit is contained in:
parent
9da3a058d8
commit
7e98b047dd
@ -3562,17 +3562,14 @@ fn fnDecl(
|
||||
|
||||
var ret_gz = decl_gz.makeSubBlock(params_scope);
|
||||
defer ret_gz.unstack();
|
||||
const ret_ref: Zir.Inst.Ref = switch (nodePrimitive(tree, fn_proto.ast.return_type)) {
|
||||
.none => inst: {
|
||||
const inst = try expr(&ret_gz, params_scope, coerced_type_rl, fn_proto.ast.return_type);
|
||||
if (ret_gz.instructionsSlice().len == 0) {
|
||||
// In this case we will send a len=0 body which can be encoded more efficiently.
|
||||
break :inst inst;
|
||||
}
|
||||
_ = try ret_gz.addBreak(.break_inline, 0, inst);
|
||||
const ret_ref: Zir.Inst.Ref = inst: {
|
||||
const inst = try expr(&ret_gz, params_scope, coerced_type_rl, fn_proto.ast.return_type);
|
||||
if (ret_gz.instructionsSlice().len == 0) {
|
||||
// In this case we will send a len=0 body which can be encoded more efficiently.
|
||||
break :inst inst;
|
||||
},
|
||||
else => |p| p,
|
||||
}
|
||||
_ = try ret_gz.addBreak(.break_inline, 0, inst);
|
||||
break :inst inst;
|
||||
};
|
||||
|
||||
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 {
|
||||
}
|
||||
}
|
||||
|
||||
fn nodePrimitive(tree: *const Ast, start_node: Ast.Node.Index) Zir.Inst.Ref {
|
||||
const node_tags = tree.nodes.items(.tag);
|
||||
const node_datas = tree.nodes.items(.data);
|
||||
|
||||
var node = start_node;
|
||||
while (true) {
|
||||
switch (node_tags[node]) {
|
||||
// Forward the question to the LHS sub-expression.
|
||||
.grouped_expression => node = node_datas[node].lhs,
|
||||
|
||||
.identifier => {
|
||||
const main_tokens = tree.nodes.items(.main_token);
|
||||
const ident_bytes = tree.tokenSlice(main_tokens[node]);
|
||||
if (primitives.get(ident_bytes)) |primitive| {
|
||||
return primitive;
|
||||
} else {
|
||||
return .none;
|
||||
}
|
||||
},
|
||||
|
||||
else => return .none,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Applies `rl` semantics to `result`. Expressions which do not do their own handling of
|
||||
/// result locations must call this function on their result.
|
||||
/// As an example, if the `ResultLoc` is `ptr`, it will write the result to the pointer.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user