@@ -1519,3 +1519,51 @@ let ir = first(only(Base.code_ircode(f_with_early_try_catch_exit, (); optimize_u
15191519end
15201520
15211521@test isnothing (f_with_early_try_catch_exit ())
1522+
1523+ # Issue #51144 - UndefRefError during compaction
1524+ let m = Meta. @lower 1 + 1
1525+ @assert Meta. isexpr (m, :thunk )
1526+ src = m. args[1 ]:: CodeInfo
1527+ src. code = Any[
1528+ # block 1 → 2, 3
1529+ #= %1: =# Expr (:(= ), Core. SlotNumber (4 ), Core. Argument (2 )),
1530+ #= %2: =# Expr (:call , :(=== ), Core. SlotNumber (4 ), nothing ),
1531+ #= %3: =# GotoIfNot (Core. SSAValue (1 ), 5 ),
1532+ # block 2
1533+ #= %4: =# ReturnNode (nothing ),
1534+ # block 3 → 4, 5
1535+ #= %5: =# Expr (:(= ), Core. SlotNumber (4 ), false ),
1536+ #= %6: =# GotoIfNot (Core. Argument (2 ), 8 ),
1537+ # block 4 → 5
1538+ #= %7: =# Expr (:(= ), Core. SlotNumber (4 ), true ),
1539+ # block 5
1540+ #= %8: =# ReturnNode (nothing ), # Must not insert a π-node here
1541+ ]
1542+ nstmts = length (src. code)
1543+ nslots = 4
1544+ src. ssavaluetypes = nstmts
1545+ src. codelocs = fill (Int32 (1 ), nstmts)
1546+ src. ssaflags = fill (Int32 (0 ), nstmts)
1547+ src. slotflags = fill (0 , nslots)
1548+ src. slottypes = Any[Any, Union{Bool, Nothing}, Bool, Union{Bool, Nothing}]
1549+ ir = Core. Compiler. inflate_ir (src)
1550+
1551+ mi = ccall (:jl_new_method_instance_uninit , Ref{Core. MethodInstance}, ());
1552+ mi. specTypes = Tuple{}
1553+ mi. def = Module ()
1554+
1555+ # Simulate the important results from inference
1556+ interp = Core. Compiler. NativeInterpreter ()
1557+ sv = Core. Compiler. OptimizationState (mi, src, interp)
1558+ slot_id = 4
1559+ for block_id = 3 : 5
1560+ # (_4 !== nothing) conditional narrows the type, triggering PiNodes
1561+ sv. bb_vartables[block_id][slot_id] = VarState (Bool, #= maybe_undef =# false )
1562+ end
1563+
1564+ ir = Core. Compiler. convert_to_ircode (src, sv)
1565+ ir = Core. Compiler. slot2reg (ir, src, sv)
1566+ ir = Core. Compiler. compact! (ir)
1567+
1568+ Core. Compiler. verify_ir (ir)
1569+ end
0 commit comments