this repo has no description
0
fork

Configure Feed

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

internal/core/adt: add the Vertex.LeafConjuncts iterator

Which is the iter.Seq form of Vertex.VisitLeafConjuncts.
We leave the latter for now, to allow us to transition bit by bit
without a huge refactor that is hard to review.
For now, we switch the cue and tools/trim packages.

Updates #2953.

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

+34 -38
+8 -11
cue/types.go
··· 954 954 } 955 955 count := 0 956 956 var src ast.Node 957 - v.v.VisitLeafConjuncts(func(c adt.Conjunct) bool { 957 + for c := range v.v.LeafConjuncts() { 958 958 src = c.Source() 959 959 count++ 960 - return true 961 - }) 960 + // TODO(mvdan): break early on count > 1? 961 + } 962 962 if count > 1 || src == nil { 963 963 src = v.v.Value().Source() 964 964 } ··· 999 999 } 1000 1000 // Pick the most-concrete field. 1001 1001 var p token.Pos 1002 - v.v.VisitLeafConjuncts(func(c adt.Conjunct) bool { 1002 + for c := range v.v.LeafConjuncts() { 1003 1003 x := c.Elem() 1004 1004 pp := pos(x) 1005 1005 if pp == token.NoPos { 1006 - return true 1006 + continue 1007 1007 } 1008 1008 p = pp 1009 1009 // TODO: Prefer struct conjuncts with actual fields. 1010 - return true 1011 - }) 1010 + } 1012 1011 return p 1013 1012 } 1014 1013 ··· 2175 2174 default: 2176 2175 a := []Value{} 2177 2176 ctx := v.ctx() 2178 - v.v.VisitLeafConjuncts(func(c adt.Conjunct) bool { 2177 + for c := range v.v.LeafConjuncts() { 2179 2178 // Keep parent here. TODO: do we need remove the requirement 2180 2179 // from other conjuncts? 2181 2180 n := &adt.Vertex{ ··· 2185 2184 n.AddConjunct(c) 2186 2185 n.Finalize(ctx) 2187 2186 a = append(a, makeValue(v.idx, n, v.parent_)) 2188 - return true 2189 - }) 2190 - 2187 + } 2191 2188 return adt.AndOp, a 2192 2189 } 2193 2190
+12 -6
internal/core/adt/composite.go
··· 562 562 } 563 563 564 564 // VisitLeafConjuncts visits all conjuncts that are leafs of the ConjunctGroup tree. 565 + // 566 + // TODO(mvdan): switch all to [Vertex.LeafConjuncts]. 565 567 func (v *Vertex) VisitLeafConjuncts(f func(Conjunct) bool) { 566 568 iterConjuncts(v.Conjuncts, f) 569 + } 570 + 571 + // LeafConjuncts iterates over all conjuncts that are leafs of the ConjunctGroup tree. 572 + func (v *Vertex) LeafConjuncts() iter.Seq[Conjunct] { 573 + return func(yield func(Conjunct) bool) { 574 + _ = iterConjuncts(v.Conjuncts, yield) 575 + } 567 576 } 568 577 569 578 func iterConjuncts(a []Conjunct, yield func(Conjunct) bool) bool { ··· 620 629 if v == nil { 621 630 return c, 0 622 631 } 623 - v.VisitLeafConjuncts(func(x Conjunct) bool { 624 - c = x 632 + for c = range v.LeafConjuncts() { 625 633 if count++; count > 1 { 626 - return false 634 + break 627 635 } 628 - return true 629 - }) 630 - 636 + } 631 637 return c, count 632 638 } 633 639
+14 -21
tools/trim/trimv3.go
··· 476 476 pair.Constraint.Finalize(t.ctx) 477 477 t.logf("pattern %d %p::%T", i, pair.Constraint, pair.Constraint) 478 478 t.inc() 479 - pair.Constraint.VisitLeafConjuncts(func(c adt.Conjunct) bool { 479 + for c := range pair.Constraint.LeafConjuncts() { 480 480 field := c.Field() 481 481 elem := c.Elem() 482 482 expr := c.Expr() ··· 489 489 nm.ignoreConjunct = true 490 490 nm.markRequired() 491 491 } 492 - 493 - return true 494 - }) 492 + } 495 493 t.dec() 496 494 } 497 495 } ··· 546 544 } 547 545 } 548 546 549 - v.VisitLeafConjuncts(func(c adt.Conjunct) bool { 547 + for c := range v.LeafConjuncts() { 550 548 switch disj := c.Elem().(type) { 551 549 case *adt.Disjunction: 552 550 t.logf("found disjunction") ··· 576 574 branches = append(branches, branch) 577 575 } 578 576 } 579 - return true 580 - }) 577 + } 581 578 582 579 t.dec() 583 580 ··· 598 595 } 599 596 seen[v] = struct{}{} 600 597 601 - v.VisitLeafConjuncts(func(c adt.Conjunct) bool { 598 + for c := range v.LeafConjuncts() { 602 599 if src := c.Field().Source(); src != nil { 603 600 nm := t.getNodeMeta(src) 604 601 t.logf(" ignoring %v", nm.src.Pos()) ··· 608 605 t.resolveElemAll(c, func(resolver adt.Resolver, resolvedTo *adt.Vertex) { 609 606 worklist = append(worklist, resolvedTo.Arcs...) 610 607 }) 611 - return true 612 - }) 608 + } 613 609 worklist = append(worklist, v.Arcs...) 614 610 } 615 611 } ··· 682 678 } 683 679 684 680 var nodeMetas, winners, disjDefaultWinners []*nodeMeta 685 - v.VisitLeafConjuncts(func(c adt.Conjunct) bool { 681 + for c := range v.LeafConjuncts() { 686 682 field := c.Field() 687 683 elem := c.Elem() 688 684 expr := c.Expr() ··· 690 686 if src == nil { 691 687 t.logf("conjunct field: %p::%T, elem: %p::%T, expr: %p::%T, src nil", 692 688 field, field, elem, elem, expr, expr) 693 - return true 689 + continue 694 690 } 695 691 696 692 t.logf("conjunct field: %p::%T, elem: %p::%T, expr: %p::%T, src: %v", ··· 765 761 } 766 762 767 763 t.linkResolvers(c, false) 768 - return true 769 - }) 764 + } 770 765 771 766 if keepAll { 772 767 t.logf("keeping all %d nodes", len(nodeMetas)) ··· 846 841 } 847 842 848 843 t.resolveElemAll(c, func(resolver adt.Resolver, resolvedTo *adt.Vertex) { 849 - resolvedTo.VisitLeafConjuncts(func(resolvedToC adt.Conjunct) bool { 844 + for resolvedToC := range resolvedTo.LeafConjuncts() { 850 845 src := resolvedToC.Source() 851 846 if src == nil { 852 - return true 847 + continue 853 848 } 854 849 resolvedToNm := t.getNodeMeta(src) 855 850 resolverNm := t.getNodeMeta(resolver.Source()) ··· 885 880 resolvedToNm.src.Pos(), origNm.src.Pos()) 886 881 resolvedToNm.addRequiredBy(origNm) 887 882 } 888 - return true 889 - }) 883 + } 890 884 }) 891 885 } 892 886 ··· 967 961 if r, ok := c.Elem().(adt.Resolver); ok { 968 962 v1, bot := t.ctx.Resolve(c, r) 969 963 if bot == nil { 970 - v1.VisitLeafConjuncts(func(c adt.Conjunct) bool { 964 + for c := range v1.LeafConjuncts() { 971 965 conjVertex.InsertConjunct(c) 972 - return true 973 - }) 966 + } 974 967 continue 975 968 } 976 969 }