Menu ▾ ▴

#5873 Mode declared inside a translated function leaks into later translations

None
open
nobody
5
3 days ago
3 days ago
No

By Claude:

A mode_declare for a local variable of a translated function stays on the variable after the translation. Functions translated later then treat the variable as declared, even when they declare nothing:

(%i1) f(u) := block([n], mode_declare(n, fixnum), n : 1, n)$
(%i2) translate(f)$
(%i3) g(v) := block([n], v + n)$
(%i4) g(1);
(%o4)                                n + 1
(%i5) translate(g)$
(%i6) g(1);
(%o6)                                  1

The translated g should return n + 1 like the interpreted one. Its local n starts as 0 because n still has mode fixnum from f (?get('n, '?mode) returns fixnum after translate(f)). With mode float the local starts as 0.0, with list as []. kill(n) before translate(g) makes the problem go away.

The translator's own declaration of a for loop variable leaks the same way:

(%i1) p(m) := block([s : 0], for i thru m do s : s + i, s)$
(%i2) translate(p)$
(%i3) h(v) := block([i], [v, i])$
(%i4) translate(h)$
(%i5) h(1);
(%o5)                               [1, 0]

The expected result is [1, i]. A global mode, as set by define_variable or a top-level mode_declare, is overwritten the same way by a different mode declared for a local of that name.

On the current master, where boolean locals get a Lisp type declaration, a leaked boolean mode makes the translated g fail outright. With mode_declare(b, boolean) in f and g(v) := block([b], b : v, b), translating g warns variable b (declared type boolean) assigned type any, and g(5) fails with the Lisp error Value of '$B ... is $B, not a BOOLEAN.

The problem first appeared in 5.42.0 and is still present in 5.50.0 and the current master.

Discussion


Log in to post a comment.