this repo has no description
0
fork

Configure Feed

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

all: make use of adt.Pos consistently

To call adt.Node.Source().Pos() safely with nil checks.
We had two other "pos" funcs, in the cue and adt package,
as well as various hand-rolled versions in many func bodies.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I7d02082fc5759867751f1babd0f9a163d0203ff9
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1227393
Reviewed-by: Matthew Sackman <matthew@cue.works>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+51 -120
-31
cue/builtin.go
··· 1 - // Copyright 2018 The CUE Authors 2 - // 3 - // Licensed under the Apache License, Version 2.0 (the "License"); 4 - // you may not use this file except in compliance with the License. 5 - // You may obtain a copy of the License at 6 - // 7 - // http://www.apache.org/licenses/LICENSE-2.0 8 - // 9 - // Unless required by applicable law or agreed to in writing, software 10 - // distributed under the License is distributed on an "AS IS" BASIS, 11 - // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 - // See the License for the specific language governing permissions and 13 - // limitations under the License. 14 - 15 - package cue 16 - 17 - import ( 18 - "cuelang.org/go/cue/token" 19 - "cuelang.org/go/internal/core/adt" 20 - ) 21 - 22 - func pos(n adt.Node) (p token.Pos) { 23 - if n == nil { 24 - return 25 - } 26 - src := n.Source() 27 - if src == nil { 28 - return 29 - } 30 - return src.Pos() 31 - }
+2 -6
cue/errors.go
··· 58 58 if e.err.Err != nil { 59 59 return e.err.Err.Position() 60 60 } 61 - src := e.err.Source() 62 - if src == nil { 63 - return token.NoPos 64 - } 65 - return src.Pos() 61 + return adt.Pos(e.err) 66 62 } 67 63 68 64 func (e *valueError) InputPositions() []token.Pos { ··· 115 111 case string: 116 112 args := args[i+1:] 117 113 // Do not expand message so that errors can be localized. 118 - pos := pos(src) 114 + pos := adt.Pos(src) 119 115 if code < 0 { 120 116 code = 0 121 117 }
+1 -1
cue/types.go
··· 1040 1040 var p token.Pos 1041 1041 for c := range v.v.LeafConjuncts() { 1042 1042 x := c.Elem() 1043 - pp := pos(x) 1043 + pp := adt.Pos(x) 1044 1044 if pp == token.NoPos { 1045 1045 continue 1046 1046 }
+2 -6
encoding/openapi/cycle.go
··· 19 19 20 20 "cuelang.org/go/cue" 21 21 "cuelang.org/go/cue/errors" 22 - "cuelang.org/go/cue/token" 22 + "cuelang.org/go/internal/core/adt" 23 23 "cuelang.org/go/internal/core/dep" 24 24 "cuelang.org/go/internal/core/eval" 25 25 internalvalue "cuelang.org/go/internal/value" ··· 43 43 44 44 err := dep.Visit(nil, ctx, n, func(d dep.Dependency) error { 45 45 if slices.Contains(b.ctx.cycleNodes, d.Node) { 46 - var p token.Pos 47 - if src := d.Node.Source(); src != nil { 48 - p = src.Pos() 49 - } 50 - err := errors.Newf(p, 46 + err := errors.Newf(adt.Pos(d.Node), 51 47 "cycle in reference at %v: cyclic structures not allowed when reference expansion is requested", v.Path()) 52 48 b.ctx.errs = errors.Append(b.ctx.errs, err) 53 49 return err
+3
internal/core/adt/adt.go
··· 138 138 139 139 // Pos returns the file position of n, or token.NoPos if it is unknown. 140 140 func Pos(n Node) token.Pos { 141 + if n == nil { 142 + return token.NoPos 143 + } 141 144 src := n.Source() 142 145 if src == nil { 143 146 return token.NoPos
+1 -8
internal/core/adt/binop.go
··· 17 17 import ( 18 18 "bytes" 19 19 "strings" 20 - 21 - "cuelang.org/go/cue/token" 22 20 ) 23 21 24 22 var checkConcrete = &ValidateConfig{ ··· 55 53 // 56 54 // BinOp returns nil if not both left and right are concrete. 57 55 func BinOp(c *OpContext, node Node, op Op, left, right Value) Value { 58 - var p token.Pos 59 - if node != nil { 60 - if src := node.Source(); src != nil { 61 - p = src.Pos() 62 - } 63 - } 56 + p := Pos(node) 64 57 leftKind := left.Kind() 65 58 rightKind := right.Kind() 66 59
+3 -11
internal/core/adt/call.go
··· 15 15 package adt 16 16 17 17 import ( 18 - "cuelang.org/go/cue/ast" 19 18 "cuelang.org/go/cue/token" 20 19 ) 21 20 ··· 34 33 } 35 34 36 35 func (c *CallContext) Pos() token.Pos { 37 - var src ast.Node 38 - switch { 39 - case c.call != nil: 40 - src = c.call.Source() 41 - case c.builtin != nil: 42 - src = c.builtin.Source() 36 + if c.call != nil { 37 + return Pos(c.call) 43 38 } 44 - if src != nil { 45 - return src.Pos() 46 - } 47 - return token.NoPos 39 + return Pos(c.builtin) 48 40 } 49 41 50 42 func (c *CallContext) Value(i int) Value {
+1 -1
internal/core/adt/conjunct.go
··· 676 676 677 677 case Value: // *NullLit, *BoolLit, *NumLit, *StringLit, *BytesLit, *Builtin 678 678 n.unshare() 679 - if p, isData := pos(v).Priority(); isData { 679 + if p, isData := Pos(v).Priority(); isData { 680 680 id.Priority = p 681 681 } 682 682
+1 -1
internal/core/adt/constraints.go
··· 170 170 case *Bottom: 171 171 // TODO: hoist and reuse with the identical code in optional.go. 172 172 if x == cycle { 173 - err := ctx.NewPosf(pos(pattern), "cyclic pattern constraint") 173 + err := ctx.NewPosf(Pos(pattern), "cyclic pattern constraint") 174 174 for c := range ctx.vertex.LeafConjuncts() { 175 175 addPositions(ctx, err, c) 176 176 }
+5 -18
internal/core/adt/context.go
··· 948 948 return 949 949 } 950 950 if !IsConcrete(v) && v.Kind()&k != 0 { 951 - c.addErrf(IncompleteError, pos(v), "incomplete %s: %s", k, v) 951 + c.addErrf(IncompleteError, Pos(v), "incomplete %s: %s", k, v) 952 952 } else { 953 953 c.AddErrf("cannot use %s (type %s) as type %s", v, v.Kind(), k) 954 954 } ··· 963 963 return 964 964 } 965 965 if !IsConcrete(v) && v.Kind()&k != 0 { 966 - c.addErrf(IncompleteError, pos(v), 966 + c.addErrf(IncompleteError, Pos(v), 967 967 "incomplete %s in %v: %s", k, as, v) 968 968 } else { 969 969 c.AddErrf("cannot use %s (type %s) as type %s in %v", v, v.Kind(), k, as) ··· 972 972 973 973 var emptyNode = &Vertex{status: finalized} 974 974 975 - // TODO(mvdan) use this pos helper throughout the adt package 976 - 977 - func pos(x Node) token.Pos { 978 - if x == nil { 979 - return token.NoPos 980 - } 981 - src := x.Source() 982 - if src == nil { 983 - return token.NoPos 984 - } 985 - return src.Pos() 986 - } 987 - 988 975 // node is called by SelectorExpr.resolve and IndexExpr.resolve. 989 976 func (c *OpContext) node(orig Node, x Expr, scalar bool, state Flags) *Vertex { 990 977 // Do not treat inline structs as closed by default if within a schema. ··· 1023 1010 1024 1011 switch nv := v.(type) { 1025 1012 case nil: 1026 - c.addErrf(IncompleteError, pos(x), 1013 + c.addErrf(IncompleteError, Pos(x), 1027 1014 "%s undefined (%s is incomplete)", orig, x) 1028 1015 return emptyNode 1029 1016 ··· 1047 1034 } 1048 1035 default: 1049 1036 if kind := v.Kind(); kind&StructKind != 0 { 1050 - c.addErrf(IncompleteError, pos(x), 1037 + c.addErrf(IncompleteError, Pos(x), 1051 1038 "%s undefined as %s is incomplete (type %s)", orig, x, kind) 1052 1039 return emptyNode 1053 1040 1054 1041 } else if !ok { 1055 - c.addErrf(0, pos(x), // TODO(error): better message. 1042 + c.addErrf(0, Pos(x), // TODO(error): better message. 1056 1043 "invalid operand %s (found %s, want list or struct)", 1057 1044 x.Source(), v.Kind()) 1058 1045 return emptyNode
+2 -2
internal/core/adt/disjunct2.go
··· 397 397 398 398 leftDropsDefault := true 399 399 rightDropsDefault := true 400 - priority, _ := pos(dn.src).Priority() 400 + priority, _ := Pos(dn.src).Priority() 401 401 402 402 for i, p := range cross { 403 403 ID := n.nextCrossProduct(i, len(cross), p) ··· 580 580 v.status = unprocessed 581 581 582 582 if m == isDefault { 583 - c.CloseInfo.Priority, _ = pos(c.x).Priority() 583 + c.CloseInfo.Priority, _ = Pos(c.x).Priority() 584 584 } 585 585 d.scheduleConjunct(c, c.CloseInfo) 586 586
+2 -2
internal/core/adt/errors.go
··· 354 354 } 355 355 356 356 func (v *ValueError) AddPosition(n Node) { 357 - v.AddPos(pos(n)) 357 + v.AddPos(Pos(n)) 358 358 } 359 359 360 360 func (c *OpContext) errNode() *Vertex { ··· 382 382 } 383 383 384 384 func appendNodePositions(a []token.Pos, n Node) []token.Pos { 385 - if p := pos(n); p != token.NoPos { 385 + if p := Pos(n); p != token.NoPos { 386 386 a = append(a, p) 387 387 } 388 388 if v, ok := n.(*Vertex); ok {
+1 -1
internal/core/adt/eval.go
··· 628 628 for _, a := range s.Decls { 629 629 if x, ok := a.(*Field); ok && x.Label.IsRegular() { 630 630 f = x.Label 631 - p = pos(x) 631 + p = Pos(x) 632 632 break 633 633 } 634 634 }
+17 -20
internal/core/adt/expr.go
··· 398 398 case IntKind, FloatKind, NumberKind, StringKind, BytesKind: 399 399 case NullKind, StructKind, ListKind: 400 400 if x.Op != NotEqualOp && x.Op != EqualOp { 401 - err := ctx.NewPosf(pos(x.Expr), 401 + err := ctx.NewPosf(Pos(x.Expr), 402 402 "cannot use %s for bound %s", k, x.Op) 403 403 return &Bottom{ 404 404 Err: err, ··· 415 415 "non-concrete value %s for bound %s", x.Expr, x.Op) 416 416 return nil 417 417 } 418 - err := ctx.NewPosf(pos(x.Expr), 418 + err := ctx.NewPosf(Pos(x.Expr), 419 419 "invalid value %s (type %s) for bound %s", v, k, x.Op) 420 420 return &Bottom{ 421 421 Err: err, ··· 581 581 582 582 func (x *FieldReference) resolve(c *OpContext, state Flags) *Vertex { 583 583 n := c.relNode(x.UpCount) 584 - pos := pos(x) 584 + pos := Pos(x) 585 585 return c.lookup(n, pos, x.Label, state) 586 586 } 587 587 ··· 693 693 }) 694 694 ctx.PopState(frame) 695 695 f := ctx.Label(x.Label, v) 696 - return ctx.lookup(e.DerefVertex(ctx), pos(x), f, state) 696 + return ctx.lookup(e.DerefVertex(ctx), Pos(x), f, state) 697 697 } 698 698 699 699 // An ImportReference refers to an imported package. ··· 751 751 // an expression within n, in which case evaluation must already have 752 752 // started. 753 753 754 - arc := ctx.lookup(e.DerefVertex(ctx), pos(x), x.Label, state) 754 + arc := ctx.lookup(e.DerefVertex(ctx), Pos(x), x.Label, state) 755 755 if arc == nil { 756 756 return nil 757 757 } ··· 797 797 // ensure that Comprehensions, which may be wrapped in ConjunctGroups, 798 798 // are eliminated. 799 799 _, isGroup := expr.(*ConjunctGroup) 800 - ctx.Assertf(pos(expr), !isGroup, "unexpected number of expressions") 800 + ctx.Assertf(Pos(expr), !isGroup, "unexpected number of expressions") 801 801 802 802 // TODO(mem): add counter for let cache usage. 803 803 key := cacheKey{expr, arc} ··· 1083 1083 err = &Bottom{ 1084 1084 Code: err.Code, 1085 1085 Node: c.vertex, 1086 - Err: errors.Wrapf(err.Err, pos(x), "invalid interpolation"), 1086 + Err: errors.Wrapf(err.Err, Pos(x), "invalid interpolation"), 1087 1087 } 1088 1088 // c.AddBottom(err) 1089 1089 // return nil ··· 1152 1152 expectedKind = BoolKind 1153 1153 } 1154 1154 if k&expectedKind != BottomKind { 1155 - c.addErrf(IncompleteError, pos(x.X), 1155 + c.addErrf(IncompleteError, Pos(x.X), 1156 1156 "operand %s of '%s' not concrete (was %s)", x.X, op, k) 1157 1157 return nil 1158 1158 } ··· 1419 1419 case *Builtin: 1420 1420 call.builtin = f 1421 1421 if f.RawFunc != nil { 1422 - if !call.builtin.checkArgs(c, pos(x), len(x.Args)) { 1422 + if !call.builtin.checkArgs(c, Pos(x), len(x.Args)) { 1423 1423 return nil 1424 1424 } 1425 1425 return f.RawFunc(call) ··· 1495 1495 if c.errs == nil { 1496 1496 // There SHOULD be an error in the context. If not, we generate 1497 1497 // one. 1498 - c.Assertf(pos(x.Fun), c.HasErr(), 1498 + c.Assertf(Pos(x.Fun), c.HasErr(), 1499 1499 "argument %d to function %s is incomplete", i, x.Fun) 1500 1500 } 1501 1501 ··· 1652 1652 if b != nil { 1653 1653 code = b.Code 1654 1654 } 1655 - c.addErrf(code, pos(a), 1655 + c.addErrf(code, Pos(a), 1656 1656 "cannot use %s (type %s) as %s in argument %d to %v", 1657 1657 a, k, x.Params[i].Kind(), i+1, fun) 1658 1658 return nil ··· 1664 1664 n := c.newInlineVertex(nil, nil, Conjunct{env, x, c.ci}) 1665 1665 n.Finalize(c) 1666 1666 if n.IsErr() { 1667 - c.addErrf(0, pos(a), 1667 + c.addErrf(0, Pos(a), 1668 1668 "cannot use %s as %s in argument %d to %v", 1669 1669 a, v, i+1, fun) 1670 1670 return nil ··· 1712 1712 } 1713 1713 1714 1714 func (x *BuiltinValidator) Pos() token.Pos { 1715 - if src := x.Source(); src != nil { 1716 - return src.Pos() 1717 - } 1718 - return token.NoPos 1715 + return Pos(x) 1719 1716 } 1720 1717 1721 1718 func (x *BuiltinValidator) Kind() Kind { ··· 1993 1990 1994 1991 switch nv := v.(type) { 1995 1992 case nil: 1996 - c.addErrf(IncompleteError, pos(x), 1993 + c.addErrf(IncompleteError, Pos(x), 1997 1994 "cannot range over %s (incomplete)", x) 1998 1995 return emptyNode 1999 1996 ··· 2011 2008 2012 2009 default: 2013 2010 if kind := v.Kind(); kind&(StructKind|ListKind) != 0 { 2014 - c.addErrf(IncompleteError, pos(x), 2011 + c.addErrf(IncompleteError, Pos(x), 2015 2012 "cannot range over %s (incomplete type %s)", x, kind) 2016 2013 return emptyNode 2017 2014 2018 2015 } else if !ok { 2019 - c.addErrf(0, pos(x), // TODO(error): better message. 2016 + c.addErrf(0, Pos(x), // TODO(error): better message. 2020 2017 "cannot range over %s (found %s, want list or struct)", 2021 2018 x.Source(), v.Kind()) 2022 2019 return emptyNode ··· 2027 2024 // struct or list, which is the case if it may be struct or list, but 2028 2025 // is also at least some other type, such as is the case with top. 2029 2026 if kind&(StructKind|ListKind) != 0 && kind != StructKind && kind != ListKind { 2030 - c.addErrf(IncompleteError, pos(x), 2027 + c.addErrf(IncompleteError, Pos(x), 2031 2028 "cannot range over %s (incomplete type %s)", x, kind) 2032 2029 return emptyNode 2033 2030 }
+2 -2
internal/core/adt/feature.go
··· 239 239 case IntKind, NumberKind: 240 240 x, _ := Unwrap(v).(*Num) 241 241 if x == nil { 242 - c.addErrf(IncompleteError, pos(v), msgGround, v, "int") 242 + c.addErrf(IncompleteError, Pos(v), msgGround, v, "int") 243 243 return InvalidLabel 244 244 } 245 245 t = IntLabel ··· 268 268 case StringKind: 269 269 x, _ := Unwrap(v).(*String) 270 270 if x == nil { 271 - c.addErrf(IncompleteError, pos(v), msgGround, v, "string") 271 + c.addErrf(IncompleteError, Pos(v), msgGround, v, "string") 272 272 return InvalidLabel 273 273 } 274 274 t = StringLabel
+2 -2
internal/core/adt/tasks.go
··· 169 169 n.addBottom(&Bottom{ 170 170 Code: IncompleteError, 171 171 Node: n.node, 172 - Err: ctx.NewPosf(pos(field.Key), 172 + Err: ctx.NewPosf(Pos(field.Key), 173 173 "key value of dynamic field must be concrete, found %v", v), 174 174 }) 175 175 return ··· 178 178 f := ctx.Label(field.Key, v) 179 179 // TODO: remove this restriction. 180 180 if f.IsInt() { 181 - n.addErr(ctx.NewPosf(pos(field.Key), "integer fields not supported")) 181 + n.addErr(ctx.NewPosf(Pos(field.Key), "integer fields not supported")) 182 182 return 183 183 } 184 184
+3 -3
internal/core/adt/typocheck.go
··· 206 206 c.positionIndex[token.NoPos] = 0 207 207 } 208 208 209 - posIdx := c.internPosition(pos(n)) 209 + posIdx := c.internPosition(Pos(n)) 210 210 c.containments = append(c.containments, containment{id: 0, posIndex: posIdx}) 211 211 212 212 return c.nextDefID ··· 522 522 // We can then say that requirement 3 (node A) holds if all fields contain 523 523 // either label 3, or any field within 1 that is not 2. 524 524 func (n *nodeContext) injectEmbedNode(x Decl, id CloseInfo) CloseInfo { 525 - if pos(x).Experiment().ExplicitOpen { 525 + if Pos(x).Experiment().ExplicitOpen { 526 526 return id 527 527 } 528 528 ··· 550 550 // definition is embedded within a struct. It can be removed if we implement 551 551 // the #A vs #A... semantics. 552 552 func (n *nodeContext) splitStruct(s *StructLit, id CloseInfo) CloseInfo { 553 - if pos(s).Experiment().ExplicitOpen || n.ctx.OpenDef { 553 + if Pos(s).Experiment().ExplicitOpen || n.ctx.OpenDef { 554 554 return id 555 555 } 556 556
+2 -2
internal/core/adt/unify.go
··· 639 639 case a.ArcType > ArcRequired, !a.Label.IsString(): 640 640 case n.kind&StructKind == 0: 641 641 if !n.node.IsErr() && !a.IsErr() { 642 - n.reportFieldMismatch(pos(a.Value()), nil, a.Label, n.node.Value()) 642 + n.reportFieldMismatch(Pos(a.Value()), nil, a.Label, n.node.Value()) 643 643 } 644 644 // case !wasVoid: 645 645 // case n.kind == TopKind: ··· 702 702 Src: c.expr.Source(), 703 703 Code: CycleError, 704 704 Node: n.node, 705 - Err: ctx.NewPosf(pos(c.expr), 705 + Err: ctx.NewPosf(Pos(c.expr), 706 706 "circular dependency in evaluation of conditionals: %v changed after evaluation", 707 707 ctx.Str(c.expr)), 708 708 })
+1 -3
internal/core/toposort/vertex.go
··· 247 247 sMeta := &structMetas[metaIdx] 248 248 metaIdx++ 249 249 sMeta.structInfo = s 250 + sMeta.pos = adt.Pos(sl) 250 251 251 - if src := sl.Source(); src != nil { 252 - sMeta.pos = src.Pos() 253 - } 254 252 structMetaMap(sl)[sMeta] = true 255 253 for _, decl := range sl.Decls { 256 254 structMetaMap(decl)[sMeta] = true