Skip to content

Commit 06f5289

Browse files
committed
internal/core/adt: split addDependency
This function is only used for two kinds of dependencies. Split it so that the functions can be made more specific with less moving parts per type. Signed-off-by: Marcel van Lohuizen <[email protected]> Change-Id: I766a28d1bb5e40da02a9d4ea6ada11d00f179288 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1207449 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent f3d367a commit 06f5289

File tree

2 files changed

+70
-64
lines changed

2 files changed

+70
-64
lines changed

internal/core/adt/dep.go

+60-61
Original file line numberDiff line numberDiff line change
@@ -133,72 +133,40 @@ func (c *closeContext) matchDecrement(ctx *OpContext, v *Vertex, kind depKind, d
133133
}
134134
}
135135

136-
// addDependency adds a dependent arc to c. If child is an arc, child.src == key
137-
func (c *closeContext) addDependency(ctx *OpContext, kind depKind, matched bool, key, child, root *closeContext) {
136+
// addArc adds a dependent arc to c. If child is an arc, child.src == key
137+
func (c *closeContext) addArcDependency(ctx *OpContext, matched bool, key, child, root *closeContext) {
138+
const kind = ARC
139+
138140
// NOTE: do not increment
139141
// - either root closeContext or otherwise resulting from sub closeContext
140142
// all conjuncts will be added now, notified, or scheduled as task.
141-
switch kind {
142-
case ARC:
143-
for _, a := range c.arcs {
144-
if a.key == key {
145-
panic("addArc: Label already exists")
146-
}
147-
}
148-
child.incDependent(ctx, kind, c) // matched in decDependent REF(arcs)
149-
150-
c.arcs = append(c.arcs, ccArc{
151-
matched: matched,
152-
key: key,
153-
cc: child,
154-
})
155-
156-
// TODO: this tests seems sensible, but panics. Investigate what could
157-
// trigger this.
158-
// if child.src.Parent != c.src {
159-
// panic("addArc: inconsistent parent")
160-
// }
161-
if child.src.cc() != root.src.cc() {
162-
panic("addArc: inconsistent root")
163-
}
164-
165-
root.externalDeps = append(root.externalDeps, ccArcRef{
166-
src: c,
167-
kind: kind,
168-
index: len(c.arcs) - 1,
169-
})
170-
case NOTIFY:
171-
for _, a := range c.notify {
172-
if a.key == key {
173-
panic("addArc: Label already exists")
174-
}
175-
}
176-
child.incDependent(ctx, kind, c) // matched in decDependent REF(arcs)
177-
178-
c.notify = append(c.notify, ccArc{
179-
matched: matched,
180-
key: key,
181-
cc: child,
182-
})
183-
184-
// TODO: this tests seems sensible, but panics. Investigate what could
185-
// trigger this.
186-
// if child.src.Parent != c.src {
187-
// panic("addArc: inconsistent parent")
188-
// }
189-
if child.src.cc() != root.src.cc() {
190-
panic("addArc: inconsistent root")
143+
for _, a := range c.arcs {
144+
if a.key == key {
145+
panic("addArc: Label already exists")
191146
}
192-
193-
root.externalDeps = append(root.externalDeps, ccArcRef{
194-
src: c,
195-
kind: kind,
196-
index: len(c.notify) - 1,
197-
})
198-
default:
199-
panic(kind)
147+
}
148+
child.incDependent(ctx, kind, c) // matched in decDependent REF(arcs)
149+
150+
c.arcs = append(c.arcs, ccArc{
151+
matched: matched,
152+
key: key,
153+
cc: child,
154+
})
155+
156+
// TODO: this tests seems sensible, but panics. Investigate what could
157+
// trigger this.
158+
// if child.src.Parent != c.src {
159+
// panic("addArc: inconsistent parent")
160+
// }
161+
if child.src.cc() != root.src.cc() {
162+
panic("addArc: inconsistent root")
200163
}
201164

165+
root.externalDeps = append(root.externalDeps, ccArcRef{
166+
src: c,
167+
kind: kind,
168+
index: len(c.arcs) - 1,
169+
})
202170
}
203171

204172
func (cc *closeContext) linkNotify(ctx *OpContext, key *closeContext) bool {
@@ -208,10 +176,41 @@ func (cc *closeContext) linkNotify(ctx *OpContext, key *closeContext) bool {
208176
}
209177
}
210178

211-
cc.addDependency(ctx, NOTIFY, false, key, key, key.src.cc())
179+
cc.addNotificationDependency(ctx, false, key, key, key.src.cc())
212180
return true
213181
}
214182

183+
func (c *closeContext) addNotificationDependency(ctx *OpContext, matched bool, key, child, root *closeContext) {
184+
const kind = NOTIFY
185+
for _, a := range c.notify {
186+
if a.key == key {
187+
panic("addArc: Label already exists")
188+
}
189+
}
190+
child.incDependent(ctx, kind, c) // matched in decDependent REF(arcs)
191+
192+
c.notify = append(c.notify, ccArc{
193+
matched: matched,
194+
key: key,
195+
cc: child,
196+
})
197+
198+
// TODO: this tests seems sensible, but panics. Investigate what could
199+
// trigger this.
200+
// if child.src.Parent != c.src {
201+
// panic("addArc: inconsistent parent")
202+
// }
203+
if child.src.cc() != root.src.cc() {
204+
panic("addArc: inconsistent root")
205+
}
206+
207+
root.externalDeps = append(root.externalDeps, ccArcRef{
208+
src: c,
209+
kind: kind,
210+
index: len(c.notify) - 1,
211+
})
212+
}
213+
215214
// incDisjunct increases disjunction-related counters. We require kind to be
216215
// passed explicitly so that we can easily find the points where certain kinds
217216
// are used.

internal/core/adt/fields.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,19 @@ func (c *closeContext) updateArcType(ctx *OpContext, t ArcType) {
322322
}
323323

324324
type ccArc struct {
325+
// decremented indicates whether [decDependant] has been called for this
326+
// dependency.
325327
decremented bool
326328
// matched indicates the arc is only added to track the destination of a
327329
// matched pattern and that it is not explicitly defined as a field.
330+
// This is only used for arcs and not for notify.
328331
matched bool
329-
key *closeContext
330-
cc *closeContext
332+
// key is the closeContext is used to find the destination of the arc, which
333+
// is the root context.
334+
key *closeContext
335+
// cc is the closeContext for which the counters are incremented and
336+
// decremented and which is the actual destination of the dependency.
337+
cc *closeContext
331338
}
332339

333340
// A ccArcRef x refers to the x.src.arcs[x.index].
@@ -458,7 +465,7 @@ func (cc *closeContext) getKeyedCC(ctx *OpContext, key *closeContext, c CycleInf
458465
// prevent a dependency on self.
459466
if key.src != cc.src {
460467
matched := !checkClosed
461-
cc.addDependency(ctx, ARC, matched, key, arc, key)
468+
cc.addArcDependency(ctx, matched, key, arc, key)
462469
}
463470
}
464471

0 commit comments

Comments
 (0)