From 16c555cf3eb5d483e4eea4b529cba2bb1812a2f8 Mon Sep 17 00:00:00 2001 From: yuzhe <53376445+bkmashiro@users.noreply.github.com> Date: Mon, 6 Apr 2026 19:10:18 +0100 Subject: [PATCH] burn(bug): Fix unused retSlot variable in LIR lowering [9R14TP] --- src/__tests__/lir/lower.test.ts | 32 ++++++++++++++++++++++++++++++++ src/lir/lower.ts | 1 - 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/__tests__/lir/lower.test.ts b/src/__tests__/lir/lower.test.ts index 7ed8ede7..560d99ab 100644 --- a/src/__tests__/lir/lower.test.ts +++ b/src/__tests__/lir/lower.test.ts @@ -519,6 +519,38 @@ describe('MIR→LIR lowering — return', () => { const main = lir.functions.find(f => f.name === 'main')! expect(main.instructions.find(i => i.kind === 'return_value')).toBeUndefined() }) + + test('return with constant → materialises const slot, not $ret slot', () => { + // Regression: retSlot ($ret) was declared but unused; return_value must + // carry the *source* slot, not the destination. + const mod = mkModule([ + mkFn('main', [ + mkBlock('entry', [], { kind: 'return', value: c(7) }), + ]), + ]) + const lir = lowerToLIR(mod) + const main = lir.functions.find(f => f.name === 'main')! + const ret = main.instructions.find(i => i.kind === 'return_value') as any + expect(ret).toBeDefined() + // slot must be the materialised const slot, not $ret + expect(ret.slot).toEqual({ player: '$__const_7', obj: OBJ }) + expect(ret.slot.player).not.toBe('$ret') + }) + + test('return_value slot is never the $ret player (regression guard)', () => { + // Ensures the removed retSlot was not accidentally used as the instruction slot. + const mod = mkModule([ + mkFn('main', [ + mkBlock('entry', [ + { kind: 'const', dst: 't0', value: 99 }, + ], { kind: 'return', value: t('t0') }), + ]), + ]) + const lir = lowerToLIR(mod) + const main = lir.functions.find(f => f.name === 'main')! + const ret = main.instructions.find(i => i.kind === 'return_value') as any + expect(ret.slot.player).not.toBe('$ret') + }) }) // --------------------------------------------------------------------------- diff --git a/src/lir/lower.ts b/src/lir/lower.ts index d7ce6ef1..4a6f6b77 100644 --- a/src/lir/lower.ts +++ b/src/lir/lower.ts @@ -636,7 +636,6 @@ function lowerTerminatorInner( switch (term.kind) { case 'return': { if (term.value) { - const retSlot: Slot = { player: '$ret', obj: ctx.objective } const srcSlot = operandToSlot(term.value, ctx, instrs) instrs.push({ kind: 'return_value', slot: srcSlot }) }