this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/core/...: fully transition away from Vertex.VisitLeafConjuncts

These remaining cases were all simple, except value.go which can make
use of slices.SortedStableFunc, and constraints.go where we can simplify
the logic by removing a bool variable.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ieb4ec6b43fab3e027e724aabe278183ed131acce
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1224304
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>

+47 -86
+1 -8
internal/core/adt/composite.go
··· 231 231 // the final value of this Vertex. 232 232 // 233 233 // TODO: all access to Conjuncts should go through functions like 234 - // VisitLeafConjuncts and VisitAllConjuncts. We should probably make this 234 + // LeafConjuncts and VisitAllConjuncts. We should probably make this 235 235 // an unexported field. 236 236 Conjuncts ConjunctGroup 237 237 ··· 559 559 a.setParentDone() 560 560 } 561 561 } 562 - } 563 - 564 - // VisitLeafConjuncts visits all conjuncts that are leafs of the ConjunctGroup tree. 565 - // 566 - // TODO(mvdan): switch all to [Vertex.LeafConjuncts]. 567 - func (v *Vertex) VisitLeafConjuncts(f func(Conjunct) bool) { 568 - iterConjuncts(v.Conjuncts, f) 569 562 } 570 563 571 564 // LeafConjuncts iterates over all conjuncts that are leafs of the ConjunctGroup tree.
+4 -11
internal/core/adt/constraints.go
··· 98 98 Constraint: constraint, 99 99 }) 100 100 } else { 101 - found := false 102 - constraint.VisitLeafConjuncts(func(x Conjunct) bool { 101 + for x := range constraint.LeafConjuncts() { 103 102 if x.x == c.x && x.Env.Up == c.Env.Up && x.Env.Vertex == c.Env.Vertex { 104 - found = true 105 103 if c.CloseInfo.opID == n.ctx.opID { 106 104 // TODO: do we need this replacement? 107 105 src := x.CloseInfo.defID ··· 110 108 } else { 111 109 n.ctx.stats.MisalignedConstraint++ 112 110 } 111 + // The constraint already existed and the conjunct was already added. 113 112 return false 114 113 } 115 - return true 116 - }) 117 - // The constraint already existed and the conjunct was already added. 118 - if found { 119 - return false 120 114 } 121 115 } 122 116 ··· 177 171 // TODO: hoist and reuse with the identical code in optional.go. 178 172 if x == cycle { 179 173 err := ctx.NewPosf(pos(pattern), "cyclic pattern constraint") 180 - ctx.vertex.VisitLeafConjuncts(func(c Conjunct) bool { 174 + for c := range ctx.vertex.LeafConjuncts() { 181 175 addPositions(ctx, err, c) 182 - return true 183 - }) 176 + } 184 177 ctx.AddBottom(&Bottom{ 185 178 Err: err, 186 179 Node: ctx.vertex,
+6 -9
internal/core/adt/errors.go
··· 234 234 for _, p := range morePositions { 235 235 err.AddPosition(p) 236 236 } 237 - v.VisitLeafConjuncts(func(c Conjunct) bool { 237 + for c := range v.LeafConjuncts() { 238 238 if f, ok := c.x.(*Field); ok && f.ArcType == ArcRequired { 239 239 err.AddPosition(c.x) 240 240 } 241 241 if p := c.CloseInfo.Location(ctx); p != nil { 242 242 err.AddPosition(p) 243 243 } 244 - return true 245 - }) 244 + } 246 245 247 246 b := &Bottom{ 248 247 Code: IncompleteError, ··· 256 255 func newRequiredFieldInComprehensionError(ctx *OpContext, x *ForClause, v *Vertex) *Bottom { 257 256 err := ctx.Newf("missing required field in for comprehension: %v", v.Label) 258 257 err.AddPosition(x.Src) 259 - v.VisitLeafConjuncts(func(c Conjunct) bool { 258 + for c := range v.LeafConjuncts() { 260 259 addPositions(ctx, err, c) 261 - return true 262 - }) 260 + } 263 261 return &Bottom{ 264 262 Code: IncompleteError, 265 263 Err: err, ··· 365 363 a = append(a, p) 366 364 } 367 365 if v, ok := n.(*Vertex); ok { 368 - v.VisitLeafConjuncts(func(c Conjunct) bool { 366 + for c := range v.LeafConjuncts() { 369 367 a = appendNodePositions(a, c.Elem()) 370 - return true 371 - }) 368 + } 372 369 } 373 370 return a 374 371 }
+2 -3
internal/core/debug/compact.go
··· 32 32 case *adt.Vertex: 33 33 if x.BaseValue == nil || (w.cfg.Raw && !x.IsData()) { 34 34 i := 0 35 - x.VisitLeafConjuncts(func(c adt.Conjunct) bool { 35 + for c := range x.LeafConjuncts() { 36 36 if i > 0 { 37 37 w.string(" & ") 38 38 } 39 39 i++ 40 40 w.node(c.Elem()) 41 - return true 42 - }) 41 + } 43 42 return 44 43 } 45 44
+6 -9
internal/core/dep/dep.go
··· 236 236 } 237 237 }() 238 238 239 - n.VisitLeafConjuncts(func(x adt.Conjunct) bool { 239 + for x := range n.LeafConjuncts() { 240 240 v.markExpr(x.Env, x.Elem()) 241 - return true 242 - }) 241 + } 243 242 244 243 return nil 245 244 } ··· 543 542 544 543 // markConjuncts transitively marks all reference of the current node. 545 544 func (c *visitor) markConjuncts(v *adt.Vertex) { 546 - v.VisitLeafConjuncts(func(x adt.Conjunct) bool { 545 + for x := range v.LeafConjuncts() { 547 546 // Use Elem instead of Expr to preserve the Comprehension to, in turn, 548 547 // ensure an Environment is inserted for the Value clause. 549 548 c.markExpr(x.Env, x.Elem()) 550 - return true 551 - }) 549 + } 552 550 } 553 551 554 552 // markInternalResolvers marks dependencies for rootless nodes. As these ··· 561 559 // As lets have no path and we otherwise will not process them, we set 562 560 // processing all to true. 563 561 if c.marked != nil && hasLetParent(v) { 564 - v.VisitLeafConjuncts(func(x adt.Conjunct) bool { 562 + for x := range v.LeafConjuncts() { 565 563 c.marked.markExpr(x.Expr()) 566 - return true 567 - }) 564 + } 568 565 } 569 566 570 567 c.markConjuncts(v)
+2 -3
internal/core/dep/dep_test.go
··· 166 166 167 167 ctxt := eval.NewContext(r, n) 168 168 169 - n.VisitLeafConjuncts(func(c adt.Conjunct) bool { 169 + for c := range n.LeafConjuncts() { 170 170 str := debug.NodeString(ctxt, c.Elem(), nil) 171 171 t.Log(str) 172 - return true 173 - }) 172 + } 174 173 175 174 w := &strings.Builder{} 176 175 fmt.Fprintln(w)
+5 -7
internal/core/dep/mixed.go
··· 31 31 found := false 32 32 // TODO: Consider if we should only visit the conjuncts of the disjunction 33 33 // for dynamic mode. 34 - n.VisitLeafConjuncts(func(c adt.Conjunct) bool { 34 + for c := range n.LeafConjuncts() { 35 35 if v.marked[c.Expr()] { 36 36 found = true 37 - return false 37 + break 38 38 } 39 - return true 40 - }) 39 + } 41 40 42 41 if !found { 43 42 return ··· 70 69 71 70 case nil: 72 71 case *adt.Vertex: 73 - x.VisitLeafConjuncts(func(c adt.Conjunct) bool { 72 + for c := range x.LeafConjuncts() { 74 73 m.markExpr(c.Expr()) 75 - return true 76 - }) 74 + } 77 75 78 76 case *adt.BinaryExpr: 79 77 if x.Op == adt.AndOp {
+5 -7
internal/core/export/export.go
··· 197 197 // prevent the file comment from attaching to pkg when there is no pkg comment 198 198 PackagePos: token.NoPos.WithRel(token.NewSection), 199 199 } 200 - v.VisitLeafConjuncts(func(c adt.Conjunct) bool { 200 + for c := range v.LeafConjuncts() { 201 201 f, _ := c.Source().(*ast.File) 202 202 if f == nil { 203 - return true 203 + continue 204 204 } 205 205 206 206 if name := f.PackageName(); name != "" { ··· 219 219 ast.AddComment(fout, c) 220 220 } 221 221 } 222 - return true 223 - }) 222 + } 224 223 225 224 if pkgName != "" { 226 225 pkg.Name = ast.NewIdent(pkgName) ··· 450 449 switch x := n.(type) { 451 450 case *adt.Vertex: 452 451 if !x.IsData() { 453 - x.VisitLeafConjuncts(func(c adt.Conjunct) bool { 452 + for c := range x.LeafConjuncts() { 454 453 w.Elem(c.Elem()) 455 - return true 456 - }) 454 + } 457 455 } 458 456 459 457 case *adt.DynamicReference:
+5 -7
internal/core/export/expr.go
··· 81 81 } // Should this be the arcs label? 82 82 83 83 a := []conjunct{} 84 - x.VisitLeafConjuncts(func(c adt.Conjunct) bool { 84 + for c := range x.LeafConjuncts() { 85 85 if c, ok := c.Elem().(*adt.Comprehension); ok && !c.DidResolve() { 86 - return true 86 + continue 87 87 } 88 88 a = append(a, conjunct{c, 0}) 89 - return true 90 - }) 89 + } 91 90 92 91 return e.mergeValues(adt.InvalidLabel, x, a, x.Conjuncts...) 93 92 ··· 424 423 425 424 switch { 426 425 default: 427 - v.VisitLeafConjuncts(func(c adt.Conjunct) bool { 426 + for c := range v.LeafConjuncts() { 428 427 e.addExpr(c.Env, v, c.Elem(), false) 429 - return true 430 - }) 428 + } 431 429 432 430 case v.IsData(): 433 431 e.structs = append(e.structs, v.Structs...)
+8 -13
internal/core/export/extract.go
··· 36 36 fields := []*ast.Field{} 37 37 38 38 // Collect docs directly related to this Vertex. 39 - v.VisitLeafConjuncts(func(x adt.Conjunct) bool { 39 + for x := range v.LeafConjuncts() { 40 40 // TODO: Is this still being used? 41 41 if v, ok := x.Elem().(*adt.Vertex); ok { 42 42 docs = append(docs, extractDocs(v)...) 43 - return true 44 43 } 45 44 46 45 switch f := x.Field().Source().(type) { 47 46 case *ast.Field: 48 47 if hasShorthandValue(f) { 49 - return true 48 + continue 50 49 } 51 50 fields = append(fields, f) 52 51 for _, cg := range f.Comments() { ··· 59 58 fdocs, _ := internal.FileComments(f) 60 59 docs = append(docs, fdocs...) 61 60 } 62 - 63 - return true 64 - }) 61 + } 65 62 66 63 // Collect docs from parent scopes in collapsed fields. 67 64 for p := v.Parent; p != nil; p = p.Parent { 68 65 69 66 newFields := []*ast.Field{} 70 67 71 - p.VisitLeafConjuncts(func(x adt.Conjunct) bool { 68 + for x := range p.LeafConjuncts() { 72 69 f, ok := x.Source().(*ast.Field) 73 70 if !ok || !hasShorthandValue(f) { 74 - return true 71 + continue 75 72 } 76 73 77 74 nested := nestedField(f) ··· 85 82 } 86 83 } 87 84 } 88 - return true 89 - }) 85 + } 90 86 91 87 fields = newFields 92 88 } ··· 142 138 } 143 139 144 140 func ExtractFieldAttrs(v *adt.Vertex) (attrs []*ast.Attribute) { 145 - v.VisitLeafConjuncts(func(x adt.Conjunct) bool { 141 + for x := range v.LeafConjuncts() { 146 142 attrs = extractFieldAttrs(attrs, x.Field()) 147 - return true 148 - }) 143 + } 149 144 return attrs 150 145 } 151 146
+3 -9
internal/core/export/value.go
··· 56 56 e.popFrame(saved) 57 57 }() 58 58 59 - n.VisitLeafConjuncts(func(c adt.Conjunct) bool { 59 + for c := range n.LeafConjuncts() { 60 60 e.markLets(c.Expr().Source(), s) 61 - return true 62 - }) 61 + } 63 62 64 63 switch x := n.BaseValue.(type) { 65 64 case nil: ··· 103 102 } 104 103 if result == nil { 105 104 // fall back to expression mode 106 - a := []adt.Conjunct{} 107 - n.VisitLeafConjuncts(func(c adt.Conjunct) bool { 108 - a = append(a, c) 109 - return true 110 - }) 111 105 // Use stable sort to ensure that tie breaks (for instance if elements 112 106 // are not associated with a position) are deterministic. 113 - slices.SortStableFunc(a, cmpConjuncts) 107 + a := slices.SortedStableFunc(n.LeafConjuncts(), cmpConjuncts) 114 108 115 109 exprs := make([]ast.Expr, 0, len(a)) 116 110 for _, c := range a {