Refactor optimizer to track upvalue usage

The `UsageInfo` struct has been updated to include tracking for
`used_upvalues` and `assigned_upvalues`. The `collect_usage` function
has been modified to handle these new fields when encountering
`Address::Upvalue`.

The `map_params_to_args` function now takes `body_usage` as an argument
to ensure that parameters assigned to or used in the body are correctly
substituted. A new helper function, `collect_parameter_slots_set`, has
been added to gather all parameter slots within a pattern.

The inlining logic in `Optimizer::inline_call` has been enhanced to
prevent inlining if a parameter is used or assigned in the function body
but cannot be substituted. This addresses a bug where aggressive
inlining could lead to incorrect code generation when parameters were
reassigned.

A new integration test, `test_closure_reassignment_optimization_bug`,
has been added to specifically target and verify the fix for this
inlining issue.
This commit is contained in:
Michael Schimmel
2026-02-24 12:53:06 +01:00
parent 2c652e0140
commit e07acc0208
4 changed files with 121 additions and 39 deletions
-20
View File
@@ -1,20 +0,0 @@
(do
(def val1 4) ; korrekt
(def val2 [4 5]) ; korrekt
(def [val3] 5) ; falsch
(def [val4] [5]) ; korrekt
(def [[x1 [y1 y3]] z1] [[1 [2 3]] 4]) ; korrekt
(def [[x2 [y2 y4]] z2] [1 2 3 4]) ; falsch
;(def [[x [y1 y2]] z] [1] [2 3] 4) ; falsch
;(def [[x [y1 y2]] z] [1 [2 3]] 4) ; falsch
(def f (fn [[x [y1 y2]] z] (+ x y1 y2 z)))
(f [1 [2 3]] 4) ; korrekt
(f [1 2 3 4]) ; falsch
(f [1 2 [3] 4]) ; falsch
(f 1 2 3 4) ; falsch
(f 1 [2 [3]] 4) ; falsch
(f [[1 2] [3 4]]) ; falsch
)