1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
package eval
import "testing"
// slotOf gives an expression a fresh slot of its own, standing in for the AST
// field maybeThunk is normally handed. Fresh per call, as each AST field is.
func slotOf(e Expr) *Expr { return &e }
// TestMaybeThunkElidesLambda pins that a lambda argument is built as a closure
// rather than thunked, and that nothing observable changes: the closure
// captures the env it was given, and NrThunks still counts it (C++ would have
// allocated a thunk here, and NrThunks is compared against Lix's nrThunks).
func TestMaybeThunkElidesLambda(t *testing.T) {
s := &EvalState{}
env := &Env{}
lam := &ExprLambda{}
v := s.maybeThunk(slotOf(lam), env)
l, ok := v.(*VLambda)
if !ok {
t.Fatalf("maybeThunk(lambda) = %T, want *VLambda", v)
}
if l.Env != env || l.Fun != lam {
t.Fatal("elided lambda did not capture the given env and node")
}
if s.Stats.NrThunks != 1 || s.Stats.NrThunksElided != 1 {
t.Fatalf("NrThunks=%d NrThunksElided=%d, want 1 and 1",
s.Stats.NrThunks, s.Stats.NrThunksElided)
}
}
// TestMaybeThunkPeek pins the boundary of peek: an expression is answered
// without a thunk only when its value is already fixed, and every case where
// evaluating it could fail, force something, or depend on an unforced value
// keeps its thunk. Answering early in any of those would make an error or a
// computation eager, which is observable.
//
// Each "answered" case also states the exact value, so a peek that answers
// the right cases with the wrong result fails too. The last group of
// mustThunk cases is answerable but deliberately left alone; see peek.
func TestMaybeThunkPeek(t *testing.T) {
// Bindings are sorted by symbol ID and nothing here needs names.
a, b, missing := Symbol(1), Symbol(2), Symbol(3)
leafThunk := &VThunk{Expr: slotOf(&ExprInt{})} // unforced; must be returned as is
inner := VAttrs{Bindings: &Bindings{attrs: []Attr{{Name: b, Value: leafThunk}}}}
forcedInner := &VThunk{Result: inner}
outer := VAttrs{Bindings: &Bindings{attrs: []Attr{{Name: a, Value: forcedInner}}}}
unforced := &VThunk{Expr: slotOf(&ExprInt{})}
one := VInt{V: 1}
forcedTrue := &VThunk{Result: VTrueSingleton}
// Slots: 0 outer set, 1 unforced thunk, 2 int 1, 3 true (via a forced
// thunk), 4 false, 5 a string, 6 the outer set via a forced thunk.
env := &Env{Values: []Value{outer, unforced, one, forcedTrue, VFalseSingleton, VString{V: "s"}, &VThunk{Result: outer}}}
v := func(displ uint32) *ExprVar { return &ExprVar{Displ: displ} }
sel := func(base Expr, path ...Symbol) *ExprSelect {
e := &ExprSelect{E: base}
for _, p := range path {
e.AttrPath = append(e.AttrPath, AttrName{Symbol: p})
}
return e
}
has := func(base Expr, path ...Symbol) *ExprOpHasAttr {
e := &ExprOpHasAttr{E: base}
for _, p := range path {
e.AttrPath = append(e.AttrPath, AttrName{Symbol: p})
}
return e
}
lit := NewExprInt(NoPosIdx, 7)
emptySet := &ExprSet{}
answered := []struct {
name string
e Expr
want Value
}{
{"x.a.b, leaf unforced", sel(v(0), a, b), leafThunk},
{"missing with literal `or`", &ExprSelect{E: v(0), AttrPath: []AttrName{{Symbol: missing}}, Def: lit}, lit.val},
{"non-set with `or` {}", &ExprSelect{E: v(2), AttrPath: []AttrName{{Symbol: a}}, Def: emptySet}, VAttrs{Bindings: EmptyBindings}},
{"missing with `or` []", &ExprSelect{E: v(0), AttrPath: []AttrName{{Symbol: missing}}, Def: &ExprList{}}, EmptyList},
// `or false` is a variable bound to a builtin; its filled slot is
// returned as is, forced or not, like maybeThunk's ExprVar case.
{"missing with `or` <var>", &ExprSelect{E: v(0), AttrPath: []AttrName{{Symbol: missing}}, Def: v(4)}, VFalseSingleton},
{"x ? a", has(v(0), a), VTrueSingleton},
{"x ? a.b", has(v(0), a, b), VTrueSingleton},
{"x ? missing", has(v(0), missing), VFalseSingleton},
{"1 ? a", has(v(2), a), VFalseSingleton},
{"forced-thunk base ? a", has(v(6), a), VTrueSingleton},
}
for _, c := range answered {
s := &EvalState{}
got := s.maybeThunk(slotOf(c.e), env)
if got != c.want {
t.Errorf("%s: got %#v, want %#v", c.name, got, c.want)
}
if s.Stats.NrThunks != 1 || s.Stats.NrThunksElided != 1 {
t.Errorf("%s: NrThunks=%d NrThunksElided=%d, want 1 and 1",
c.name, s.Stats.NrThunks, s.Stats.NrThunksElided)
}
}
mustThunk := map[string]Expr{
"missing attr": sel(v(0), missing),
"missing with computed `or`": &ExprSelect{E: v(0), AttrPath: []AttrName{{Symbol: missing}}, Def: &ExprCall{}},
"missing with `or` with-var": &ExprSelect{E: v(0), AttrPath: []AttrName{{Symbol: missing}}, Def: &ExprVar{FromWith: true}},
"unforced base": sel(v(1), a),
"base not a set": sel(v(2), a),
"step into non-set": sel(v(0), a, b, a),
"dynamic attr name": &ExprSelect{E: v(0), AttrPath: []AttrName{{Expr: &ExprString{}}}},
"base is a with var": sel(&ExprVar{FromWith: true}, a),
"base is a call": sel(&ExprCall{}, a),
"base is a set literal": sel(&ExprSet{}, a),
"? on unforced": has(v(1), a),
"? through unforced leaf": has(v(0), a, b, a),
"non-empty list": &ExprList{Elems: []Expr{lit}},
"non-empty set": &ExprSet{Attrs: []AttrDef{{Name: a, Expr: lit}}},
"rec {} (allocates a frame)": &ExprSet{Recursive: true},
// Deliberately *not* peeked: answerable in principle, but they
// measured as costing more to check than the thunks they saved.
// A test failure here means someone widened peek again; see
// peek's comment and OPTIMIZATION.md first.
"!true": &ExprOpNot{E: v(3)},
"false && <unforced>": &ExprOpAnd{E1: v(4), E2: v(1)},
"true || <unforced>": &ExprOpOr{E1: v(3), E2: v(1)},
"1 == 7": &ExprOpEq{E1: v(2), E2: lit},
"if true then 7 else <unforced>": &ExprIf{Cond: v(3), Then: lit, Else: v(1)},
"assert false (must fail)": &ExprAssert{Cond: v(4), Body: lit},
}
for name, e := range mustThunk {
s := &EvalState{}
if _, ok := s.maybeThunk(slotOf(e), env).(*VThunk); !ok {
t.Errorf("%s: answered early, want a thunk", name)
}
if s.Stats.NrThunksElided != 0 {
t.Errorf("%s: NrThunksElided = %d, want 0", name, s.Stats.NrThunksElided)
}
}
}
|