From d5621504b0e2160fa44991fc4180165584d72819 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Fri, 17 Dec 2021 18:50:38 +0100 Subject: [PATCH] stage2: save and restore parameters when resolving inline bodies This caused zirParam instructions of parent blocks to be present in inline analyzed blocks, and so function prototypes declared in the inline blocks would also gain and add to the parameters in the parent block. Only block and block_inline are affected in this commit, as prototypes and declarations are always generated in block_inline. This might need to be resolved in a more general way at some point. --- src/Sema.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Sema.zig b/src/Sema.zig index bdbdea42e7..174e038db4 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -940,6 +940,15 @@ pub fn analyzeBody( const inst_data = datas[inst].pl_node; const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index); const inline_body = sema.code.extra[extra.end..][0..extra.data.body_len]; + // If this block contains a function prototype, we need to reset the + // current list of parameters and restore it later. + // Note: this probably needs to be resolved in a more general manner. + const prev_params = block.params; + block.params = .{}; + defer { + block.params.deinit(sema.gpa); + block.params = prev_params; + } const break_inst = try sema.analyzeBody(block, inline_body); const break_data = datas[break_inst].@"break"; if (inst == break_data.block_inst) { @@ -953,6 +962,15 @@ pub fn analyzeBody( const inst_data = datas[inst].pl_node; const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index); const inline_body = sema.code.extra[extra.end..][0..extra.data.body_len]; + // If this block contains a function prototype, we need to reset the + // current list of parameters and restore it later. + // Note: this probably needs to be resolved in a more general manner. + const prev_params = block.params; + block.params = .{}; + defer { + block.params.deinit(sema.gpa); + block.params = prev_params; + } const break_inst = try sema.analyzeBody(block, inline_body); const break_data = datas[break_inst].@"break"; if (inst == break_data.block_inst) {