From 03e9ffa0f34a9bffacdae05e3cee1e8e56c1958b Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 23 Aug 2026 21:21:30 +0200 Subject: [PATCH] Pool a Float literal by its bits, not by its value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two floats can be equal without being the same number, and one can be unequal to itself. The constant pool asked the wrong question of both: - -0.0 == 0.0 is true and the two hash alike, so the second literal in a file was handed the first one's slot. `p 0.0` then `p(-0.0)` printed 0.0 twice; reversing the two lines printed -0.0 twice. Whichever the file happened to mention first won, and the sign is load-bearing — 1.0 / -0.0 is -Infinity. - NaN is the mirror image: equal to nothing, itself included, so it matched no entry and took a fresh slot on every mention. Keying Floats on their bits pools both correctly and leaves every other constant on the value-keyed map. The whitebox test pins the slots and the end-to-end one pins what the two lines print; both fail without the change. Co-Authored-By: Claude Opus 5 --- internal/compiler/compiler.go | 33 ++++++++++++++++++++---- internal/compiler/whitebox_test.go | 40 ++++++++++++++++++++++++++++++ internal/vm/literals_extra_test.go | 23 +++++++++++++++++ 3 files changed, 91 insertions(+), 5 deletions(-) diff --git a/internal/compiler/compiler.go b/internal/compiler/compiler.go index 7a94706f..7f8058bd 100644 --- a/internal/compiler/compiler.go +++ b/internal/compiler/compiler.go @@ -8,6 +8,7 @@ package compiler import ( "fmt" + "math" "math/big" "strconv" "strings" @@ -26,10 +27,13 @@ func (e compileError) Error() string { return e.msg } // links to the enclosing builder and isBlock is true, so local resolution can // reach enclosing locals by depth. type builder struct { - name string - insns []bytecode.Instr - consts []object.Value - constIdx map[object.Value]int + name string + insns []bytecode.Instr + consts []object.Value + constIdx map[object.Value]int + // floatIdx pools Float literals by their bits rather than by their value; + // see addConst for why they cannot share constIdx. + floatIdx map[uint64]int names []string locals []string params []string @@ -45,7 +49,7 @@ type builder struct { } func newBuilder(name string, params []string) *builder { - b := &builder{name: name, constIdx: map[object.Value]int{}, params: params, numRequired: len(params), splatIndex: -1, kwRestSlot: -1, blockSlot: -1} + b := &builder{name: name, constIdx: map[object.Value]int{}, floatIdx: map[uint64]int{}, params: params, numRequired: len(params), splatIndex: -1, kwRestSlot: -1, blockSlot: -1} for _, p := range params { b.localSlot(p) // params occupy slots 0..n-1, in order } @@ -83,6 +87,25 @@ func (b *builder) here() int { return len(b.insns) } func (b *builder) patch(at, target int) { b.insns[at].A = target } func (b *builder) addConst(v object.Value) int { + // A Float is pooled by its bits, not by its value. Two floats can be equal + // and not be the same number: -0.0 == 0.0 is true and the two hash alike, + // so a map keyed on the value hands the second literal the first one's + // slot — `p 0.0` followed by `p(-0.0)` printed 0.0 twice, and reversing + // the two lines printed -0.0 twice. Whichever the file mentioned first won. + // + // NaN is the mirror image: it is equal to nothing, itself included, so it + // matched no entry and took a fresh slot every time it appeared. Bits pool + // both correctly. + if f, ok := v.(object.Float); ok { + bits := math.Float64bits(float64(f)) + if i, ok := b.floatIdx[bits]; ok { + return i + } + i := len(b.consts) + b.consts = append(b.consts, v) + b.floatIdx[bits] = i + return i + } if i, ok := b.constIdx[v]; ok { return i } diff --git a/internal/compiler/whitebox_test.go b/internal/compiler/whitebox_test.go index 1cc6ee66..c423ac36 100644 --- a/internal/compiler/whitebox_test.go +++ b/internal/compiler/whitebox_test.go @@ -1,6 +1,7 @@ package compiler import ( + "math" "strings" "testing" @@ -301,3 +302,42 @@ func TestRewriteAnonKwSplatUnchanged(t *testing.T) { t.Fatalf("rewriteAnonKwSplat(explicit hash) = (%p, %v), want (%p, false)", got, changed, h) } } + +// TestAddConstPoolsFloatsByBits pins the reason Float literals do not share the +// value-keyed constant pool. Two floats can be equal without being the same +// number, and one can be unequal to itself, so equality is the wrong question +// to ask of a literal that has to survive to run time exactly as written. +func TestAddConstPoolsFloatsByBits(t *testing.T) { + b := newBuilder("", nil) + negZero := object.Float(math.Copysign(0, -1)) + posZero := object.Float(0) + nan := object.Float(math.NaN()) + + // -0.0 == 0.0, so a value-keyed pool hands the second literal the first + // one's slot and the file's later mention prints as its earlier one. + i, j := b.addConst(posZero), b.addConst(negZero) + if i == j { + t.Fatalf("0.0 and -0.0 share slot %d; they are equal but not the same number", i) + } + if got := math.Signbit(float64(b.consts[i].(object.Float))); got { + t.Errorf("slot %d holds a negative zero, want positive", i) + } + if got := math.Signbit(float64(b.consts[j].(object.Float))); !got { + t.Errorf("slot %d holds a positive zero, want negative", j) + } + + // The same number asked for twice keeps one slot, which is the whole point + // of pooling — including NaN, which is equal to nothing, itself included, + // and so took a fresh slot on every mention. + if a, c := b.addConst(posZero), b.addConst(posZero); a != c || a != i { + t.Errorf("0.0 pooled to %d then %d, want %d both times", a, c, i) + } + if a, c := b.addConst(nan), b.addConst(nan); a != c { + t.Errorf("NaN took slots %d and %d, want one slot", a, c) + } + + // Non-Float constants still go through the value-keyed pool. + if a, c := b.addConst(object.Integer(7)), b.addConst(object.Integer(7)); a != c { + t.Errorf("Integer(7) took slots %d and %d, want one slot", a, c) + } +} diff --git a/internal/vm/literals_extra_test.go b/internal/vm/literals_extra_test.go index a9f27f5f..52702d3b 100644 --- a/internal/vm/literals_extra_test.go +++ b/internal/vm/literals_extra_test.go @@ -49,3 +49,26 @@ func TestFloatExponentLiterals(t *testing.T) { } } } + +// TestFloatLiteralZeroSigns covers what the compiler's constant pool must not +// lose: a zero's sign, and a NaN's identity as a fresh constant. Both are +// literals whose printed form differs from a literal they compare equal (or +// unequal) to, so a pool keyed on the value gets them wrong — whichever of +// 0.0 / -0.0 the file mentioned first used to win both lines. Asserted +// against MRI Ruby 4.0.5. +func TestFloatLiteralZeroSigns(t *testing.T) { + cases := []struct{ src, want string }{ + {"p 0.0\np(-0.0)", "0.0\n-0.0\n"}, + {"p(-0.0)\np 0.0", "-0.0\n0.0\n"}, + {`p [0.0, -0.0]`, "[0.0, -0.0]\n"}, + {`p 0.0 == -0.0`, "true\n"}, // equal, and still not interchangeable + {`p (0.0).zero? && (-0.0).zero?`, "true\n"}, + {`p 1.0 / -0.0`, "-Infinity\n"}, // the sign is load-bearing, not cosmetic + {`p 1.0 / 0.0`, "Infinity\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want) + } + } +}