diff --git a/cue/builtin.go b/cue/builtin.go deleted file mode 100644 index 74aa56bbf..000000000 --- a/cue/builtin.go +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2018 The CUE Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cue - -import ( - "cuelang.org/go/cue/token" - "cuelang.org/go/internal/core/adt" -) - -func pos(n adt.Node) (p token.Pos) { - if n == nil { - return - } - src := n.Source() - if src == nil { - return - } - return src.Pos() -} diff --git a/cue/errors.go b/cue/errors.go index 241d80dbc..fa4a93d0a 100644 --- a/cue/errors.go +++ b/cue/errors.go @@ -58,11 +58,7 @@ func (e *valueError) Position() token.Pos { if e.err.Err != nil { return e.err.Err.Position() } - src := e.err.Source() - if src == nil { - return token.NoPos - } - return src.Pos() + return adt.Pos(e.err) } func (e *valueError) InputPositions() []token.Pos { @@ -115,7 +111,7 @@ outer: case string: args := args[i+1:] // Do not expand message so that errors can be localized. - pos := pos(src) + pos := adt.Pos(src) if code < 0 { code = 0 } diff --git a/cue/types.go b/cue/types.go index 93f1e6c5e..af228c803 100644 --- a/cue/types.go +++ b/cue/types.go @@ -1040,7 +1040,7 @@ func (v Value) Pos() token.Pos { var p token.Pos for c := range v.v.LeafConjuncts() { x := c.Elem() - pp := pos(x) + pp := adt.Pos(x) if pp == token.NoPos { continue } diff --git a/encoding/openapi/cycle.go b/encoding/openapi/cycle.go index 8c39c25d4..7f0fea589 100644 --- a/encoding/openapi/cycle.go +++ b/encoding/openapi/cycle.go @@ -19,7 +19,7 @@ import ( "cuelang.org/go/cue" "cuelang.org/go/cue/errors" - "cuelang.org/go/cue/token" + "cuelang.org/go/internal/core/adt" "cuelang.org/go/internal/core/dep" "cuelang.org/go/internal/core/eval" internalvalue "cuelang.org/go/internal/value" @@ -43,11 +43,7 @@ func (b *builder) checkCycle(v cue.Value) bool { err := dep.Visit(nil, ctx, n, func(d dep.Dependency) error { if slices.Contains(b.ctx.cycleNodes, d.Node) { - var p token.Pos - if src := d.Node.Source(); src != nil { - p = src.Pos() - } - err := errors.Newf(p, + err := errors.Newf(adt.Pos(d.Node), "cycle in reference at %v: cyclic structures not allowed when reference expansion is requested", v.Path()) b.ctx.errs = errors.Append(b.ctx.errs, err) return err diff --git a/internal/core/adt/adt.go b/internal/core/adt/adt.go index dee94207c..d2c021889 100644 --- a/internal/core/adt/adt.go +++ b/internal/core/adt/adt.go @@ -138,6 +138,9 @@ type Validator interface { // Pos returns the file position of n, or token.NoPos if it is unknown. func Pos(n Node) token.Pos { + if n == nil { + return token.NoPos + } src := n.Source() if src == nil { return token.NoPos diff --git a/internal/core/adt/binop.go b/internal/core/adt/binop.go index 3256a6028..b2b2a3b2a 100644 --- a/internal/core/adt/binop.go +++ b/internal/core/adt/binop.go @@ -17,8 +17,6 @@ package adt import ( "bytes" "strings" - - "cuelang.org/go/cue/token" ) var checkConcrete = &ValidateConfig{ @@ -55,12 +53,7 @@ func BinOpBool(c *OpContext, node Node, op Op, left, right Value) bool { // // BinOp returns nil if not both left and right are concrete. func BinOp(c *OpContext, node Node, op Op, left, right Value) Value { - var p token.Pos - if node != nil { - if src := node.Source(); src != nil { - p = src.Pos() - } - } + p := Pos(node) leftKind := left.Kind() rightKind := right.Kind() diff --git a/internal/core/adt/call.go b/internal/core/adt/call.go index 670e931a3..f8a2cdcec 100644 --- a/internal/core/adt/call.go +++ b/internal/core/adt/call.go @@ -15,7 +15,6 @@ package adt import ( - "cuelang.org/go/cue/ast" "cuelang.org/go/cue/token" ) @@ -34,17 +33,10 @@ func (c *CallContext) OpContext() *OpContext { } func (c *CallContext) Pos() token.Pos { - var src ast.Node - switch { - case c.call != nil: - src = c.call.Source() - case c.builtin != nil: - src = c.builtin.Source() + if c.call != nil { + return Pos(c.call) } - if src != nil { - return src.Pos() - } - return token.NoPos + return Pos(c.builtin) } func (c *CallContext) Value(i int) Value { diff --git a/internal/core/adt/conjunct.go b/internal/core/adt/conjunct.go index 662723347..b3d169bf5 100644 --- a/internal/core/adt/conjunct.go +++ b/internal/core/adt/conjunct.go @@ -676,7 +676,7 @@ func (n *nodeContext) insertValueConjunct(env *Environment, v Value, id CloseInf case Value: // *NullLit, *BoolLit, *NumLit, *StringLit, *BytesLit, *Builtin n.unshare() - if p, isData := pos(v).Priority(); isData { + if p, isData := Pos(v).Priority(); isData { id.Priority = p } diff --git a/internal/core/adt/constraints.go b/internal/core/adt/constraints.go index 7ab3da8a9..9d68d880a 100644 --- a/internal/core/adt/constraints.go +++ b/internal/core/adt/constraints.go @@ -170,7 +170,7 @@ func matchPatternValue(ctx *OpContext, pattern Value, f Feature, label Value) (r case *Bottom: // TODO: hoist and reuse with the identical code in optional.go. if x == cycle { - err := ctx.NewPosf(pos(pattern), "cyclic pattern constraint") + err := ctx.NewPosf(Pos(pattern), "cyclic pattern constraint") for c := range ctx.vertex.LeafConjuncts() { addPositions(ctx, err, c) } diff --git a/internal/core/adt/context.go b/internal/core/adt/context.go index 60cb199ec..082be9054 100644 --- a/internal/core/adt/context.go +++ b/internal/core/adt/context.go @@ -948,7 +948,7 @@ func (c *OpContext) typeError(v Value, k Kind) { return } if !IsConcrete(v) && v.Kind()&k != 0 { - c.addErrf(IncompleteError, pos(v), "incomplete %s: %s", k, v) + c.addErrf(IncompleteError, Pos(v), "incomplete %s: %s", k, v) } else { c.AddErrf("cannot use %s (type %s) as type %s", v, v.Kind(), k) } @@ -963,7 +963,7 @@ func (c *OpContext) typeErrorAs(v Value, k Kind, as interface{}) { return } if !IsConcrete(v) && v.Kind()&k != 0 { - c.addErrf(IncompleteError, pos(v), + c.addErrf(IncompleteError, Pos(v), "incomplete %s in %v: %s", k, as, v) } else { c.AddErrf("cannot use %s (type %s) as type %s in %v", v, v.Kind(), k, as) @@ -972,19 +972,6 @@ func (c *OpContext) typeErrorAs(v Value, k Kind, as interface{}) { var emptyNode = &Vertex{status: finalized} -// TODO(mvdan) use this pos helper throughout the adt package - -func pos(x Node) token.Pos { - if x == nil { - return token.NoPos - } - src := x.Source() - if src == nil { - return token.NoPos - } - return src.Pos() -} - // node is called by SelectorExpr.resolve and IndexExpr.resolve. func (c *OpContext) node(orig Node, x Expr, scalar bool, state Flags) *Vertex { // Do not treat inline structs as closed by default if within a schema. @@ -1023,7 +1010,7 @@ func (c *OpContext) node(orig Node, x Expr, scalar bool, state Flags) *Vertex { switch nv := v.(type) { case nil: - c.addErrf(IncompleteError, pos(x), + c.addErrf(IncompleteError, Pos(x), "%s undefined (%s is incomplete)", orig, x) return emptyNode @@ -1047,12 +1034,12 @@ func (c *OpContext) node(orig Node, x Expr, scalar bool, state Flags) *Vertex { } default: if kind := v.Kind(); kind&StructKind != 0 { - c.addErrf(IncompleteError, pos(x), + c.addErrf(IncompleteError, Pos(x), "%s undefined as %s is incomplete (type %s)", orig, x, kind) return emptyNode } else if !ok { - c.addErrf(0, pos(x), // TODO(error): better message. + c.addErrf(0, Pos(x), // TODO(error): better message. "invalid operand %s (found %s, want list or struct)", x.Source(), v.Kind()) return emptyNode diff --git a/internal/core/adt/disjunct2.go b/internal/core/adt/disjunct2.go index 576839524..1f671e5c1 100644 --- a/internal/core/adt/disjunct2.go +++ b/internal/core/adt/disjunct2.go @@ -397,7 +397,7 @@ func (n *nodeContext) crossProduct(dst, cross []*nodeContext, dn *envDisjunct, m leftDropsDefault := true rightDropsDefault := true - priority, _ := pos(dn.src).Priority() + priority, _ := Pos(dn.src).Priority() for i, p := range cross { ID := n.nextCrossProduct(i, len(cross), p) @@ -580,7 +580,7 @@ func (n *nodeContext) doDisjunct(c Conjunct, m defaultMode, mode runMode, orig * v.status = unprocessed if m == isDefault { - c.CloseInfo.Priority, _ = pos(c.x).Priority() + c.CloseInfo.Priority, _ = Pos(c.x).Priority() } d.scheduleConjunct(c, c.CloseInfo) diff --git a/internal/core/adt/errors.go b/internal/core/adt/errors.go index 10f0a8a86..c31312671 100644 --- a/internal/core/adt/errors.go +++ b/internal/core/adt/errors.go @@ -354,7 +354,7 @@ type ValueError struct { } func (v *ValueError) AddPosition(n Node) { - v.AddPos(pos(n)) + v.AddPos(Pos(n)) } func (c *OpContext) errNode() *Vertex { @@ -382,7 +382,7 @@ func (c *OpContext) Newf(format string, args ...interface{}) *ValueError { } func appendNodePositions(a []token.Pos, n Node) []token.Pos { - if p := pos(n); p != token.NoPos { + if p := Pos(n); p != token.NoPos { a = append(a, p) } if v, ok := n.(*Vertex); ok { diff --git a/internal/core/adt/eval.go b/internal/core/adt/eval.go index 3b512cc1f..b98056086 100644 --- a/internal/core/adt/eval.go +++ b/internal/core/adt/eval.go @@ -628,7 +628,7 @@ func (n *nodeContext) reportFieldMismatch( for _, a := range s.Decls { if x, ok := a.(*Field); ok && x.Label.IsRegular() { f = x.Label - p = pos(x) + p = Pos(x) break } } diff --git a/internal/core/adt/expr.go b/internal/core/adt/expr.go index 22c1d2adb..0a5002859 100644 --- a/internal/core/adt/expr.go +++ b/internal/core/adt/expr.go @@ -398,7 +398,7 @@ func (x *BoundExpr) evaluate(ctx *OpContext, state Flags) Value { case IntKind, FloatKind, NumberKind, StringKind, BytesKind: case NullKind, StructKind, ListKind: if x.Op != NotEqualOp && x.Op != EqualOp { - err := ctx.NewPosf(pos(x.Expr), + err := ctx.NewPosf(Pos(x.Expr), "cannot use %s for bound %s", k, x.Op) return &Bottom{ Err: err, @@ -415,7 +415,7 @@ func (x *BoundExpr) evaluate(ctx *OpContext, state Flags) Value { "non-concrete value %s for bound %s", x.Expr, x.Op) return nil } - err := ctx.NewPosf(pos(x.Expr), + err := ctx.NewPosf(Pos(x.Expr), "invalid value %s (type %s) for bound %s", v, k, x.Op) return &Bottom{ Err: err, @@ -581,7 +581,7 @@ func (x *FieldReference) Source() ast.Node { func (x *FieldReference) resolve(c *OpContext, state Flags) *Vertex { n := c.relNode(x.UpCount) - pos := pos(x) + pos := Pos(x) return c.lookup(n, pos, x.Label, state) } @@ -693,7 +693,7 @@ func (x *DynamicReference) resolve(ctx *OpContext, state Flags) *Vertex { }) ctx.PopState(frame) f := ctx.Label(x.Label, v) - return ctx.lookup(e.DerefVertex(ctx), pos(x), f, state) + return ctx.lookup(e.DerefVertex(ctx), Pos(x), f, state) } // An ImportReference refers to an imported package. @@ -751,7 +751,7 @@ func (x *LetReference) resolve(ctx *OpContext, state Flags) *Vertex { // an expression within n, in which case evaluation must already have // started. - arc := ctx.lookup(e.DerefVertex(ctx), pos(x), x.Label, state) + arc := ctx.lookup(e.DerefVertex(ctx), Pos(x), x.Label, state) if arc == nil { return nil } @@ -797,7 +797,7 @@ func (x *LetReference) resolve(ctx *OpContext, state Flags) *Vertex { // ensure that Comprehensions, which may be wrapped in ConjunctGroups, // are eliminated. _, isGroup := expr.(*ConjunctGroup) - ctx.Assertf(pos(expr), !isGroup, "unexpected number of expressions") + ctx.Assertf(Pos(expr), !isGroup, "unexpected number of expressions") // TODO(mem): add counter for let cache usage. key := cacheKey{expr, arc} @@ -1083,7 +1083,7 @@ func (x *Interpolation) evaluate(c *OpContext, state Flags) Value { err = &Bottom{ Code: err.Code, Node: c.vertex, - Err: errors.Wrapf(err.Err, pos(x), "invalid interpolation"), + Err: errors.Wrapf(err.Err, Pos(x), "invalid interpolation"), } // c.AddBottom(err) // return nil @@ -1152,7 +1152,7 @@ func (x *UnaryExpr) evaluate(c *OpContext, state Flags) Value { expectedKind = BoolKind } if k&expectedKind != BottomKind { - c.addErrf(IncompleteError, pos(x.X), + c.addErrf(IncompleteError, Pos(x.X), "operand %s of '%s' not concrete (was %s)", x.X, op, k) return nil } @@ -1419,7 +1419,7 @@ func (x *CallExpr) evaluate(c *OpContext, state Flags) Value { case *Builtin: call.builtin = f if f.RawFunc != nil { - if !call.builtin.checkArgs(c, pos(x), len(x.Args)) { + if !call.builtin.checkArgs(c, Pos(x), len(x.Args)) { return nil } return f.RawFunc(call) @@ -1495,7 +1495,7 @@ func (x *CallExpr) evaluate(c *OpContext, state Flags) Value { if c.errs == nil { // There SHOULD be an error in the context. If not, we generate // one. - c.Assertf(pos(x.Fun), c.HasErr(), + c.Assertf(Pos(x.Fun), c.HasErr(), "argument %d to function %s is incomplete", i, x.Fun) } @@ -1652,7 +1652,7 @@ func (x *Builtin) call(call *CallContext) Expr { if b != nil { code = b.Code } - c.addErrf(code, pos(a), + c.addErrf(code, Pos(a), "cannot use %s (type %s) as %s in argument %d to %v", a, k, x.Params[i].Kind(), i+1, fun) return nil @@ -1664,7 +1664,7 @@ func (x *Builtin) call(call *CallContext) Expr { n := c.newInlineVertex(nil, nil, Conjunct{env, x, c.ci}) n.Finalize(c) if n.IsErr() { - c.addErrf(0, pos(a), + c.addErrf(0, Pos(a), "cannot use %s as %s in argument %d to %v", a, v, i+1, fun) return nil @@ -1712,10 +1712,7 @@ func (x *BuiltinValidator) Source() ast.Node { } func (x *BuiltinValidator) Pos() token.Pos { - if src := x.Source(); src != nil { - return src.Pos() - } - return token.NoPos + return Pos(x) } func (x *BuiltinValidator) Kind() Kind { @@ -1993,7 +1990,7 @@ func (c *OpContext) forSource(x Expr) *Vertex { switch nv := v.(type) { case nil: - c.addErrf(IncompleteError, pos(x), + c.addErrf(IncompleteError, Pos(x), "cannot range over %s (incomplete)", x) return emptyNode @@ -2011,12 +2008,12 @@ func (c *OpContext) forSource(x Expr) *Vertex { default: if kind := v.Kind(); kind&(StructKind|ListKind) != 0 { - c.addErrf(IncompleteError, pos(x), + c.addErrf(IncompleteError, Pos(x), "cannot range over %s (incomplete type %s)", x, kind) return emptyNode } else if !ok { - c.addErrf(0, pos(x), // TODO(error): better message. + c.addErrf(0, Pos(x), // TODO(error): better message. "cannot range over %s (found %s, want list or struct)", x.Source(), v.Kind()) return emptyNode @@ -2027,7 +2024,7 @@ func (c *OpContext) forSource(x Expr) *Vertex { // struct or list, which is the case if it may be struct or list, but // is also at least some other type, such as is the case with top. if kind&(StructKind|ListKind) != 0 && kind != StructKind && kind != ListKind { - c.addErrf(IncompleteError, pos(x), + c.addErrf(IncompleteError, Pos(x), "cannot range over %s (incomplete type %s)", x, kind) return emptyNode } diff --git a/internal/core/adt/feature.go b/internal/core/adt/feature.go index 5e1749703..a8938c2b9 100644 --- a/internal/core/adt/feature.go +++ b/internal/core/adt/feature.go @@ -239,7 +239,7 @@ func LabelFromValue(c *OpContext, src Expr, v Value) Feature { case IntKind, NumberKind: x, _ := Unwrap(v).(*Num) if x == nil { - c.addErrf(IncompleteError, pos(v), msgGround, v, "int") + c.addErrf(IncompleteError, Pos(v), msgGround, v, "int") return InvalidLabel } t = IntLabel @@ -268,7 +268,7 @@ func LabelFromValue(c *OpContext, src Expr, v Value) Feature { case StringKind: x, _ := Unwrap(v).(*String) if x == nil { - c.addErrf(IncompleteError, pos(v), msgGround, v, "string") + c.addErrf(IncompleteError, Pos(v), msgGround, v, "string") return InvalidLabel } t = StringLabel diff --git a/internal/core/adt/tasks.go b/internal/core/adt/tasks.go index 5273162d2..7036e6b7e 100644 --- a/internal/core/adt/tasks.go +++ b/internal/core/adt/tasks.go @@ -169,7 +169,7 @@ func processDynamic(ctx *OpContext, t *task, mode runMode) { n.addBottom(&Bottom{ Code: IncompleteError, Node: n.node, - Err: ctx.NewPosf(pos(field.Key), + Err: ctx.NewPosf(Pos(field.Key), "key value of dynamic field must be concrete, found %v", v), }) return @@ -178,7 +178,7 @@ func processDynamic(ctx *OpContext, t *task, mode runMode) { f := ctx.Label(field.Key, v) // TODO: remove this restriction. if f.IsInt() { - n.addErr(ctx.NewPosf(pos(field.Key), "integer fields not supported")) + n.addErr(ctx.NewPosf(Pos(field.Key), "integer fields not supported")) return } diff --git a/internal/core/adt/typocheck.go b/internal/core/adt/typocheck.go index 2b428d798..d55d97737 100644 --- a/internal/core/adt/typocheck.go +++ b/internal/core/adt/typocheck.go @@ -206,7 +206,7 @@ func (c *OpContext) getNextDefID(n Node) defID { c.positionIndex[token.NoPos] = 0 } - posIdx := c.internPosition(pos(n)) + posIdx := c.internPosition(Pos(n)) c.containments = append(c.containments, containment{id: 0, posIndex: posIdx}) return c.nextDefID @@ -522,7 +522,7 @@ func (v *Vertex) AddOpenConjunct(ctx *OpContext, w *Vertex) { // We can then say that requirement 3 (node A) holds if all fields contain // either label 3, or any field within 1 that is not 2. func (n *nodeContext) injectEmbedNode(x Decl, id CloseInfo) CloseInfo { - if pos(x).Experiment().ExplicitOpen { + if Pos(x).Experiment().ExplicitOpen { return id } @@ -550,7 +550,7 @@ func (n *nodeContext) injectEmbedNode(x Decl, id CloseInfo) CloseInfo { // definition is embedded within a struct. It can be removed if we implement // the #A vs #A... semantics. func (n *nodeContext) splitStruct(s *StructLit, id CloseInfo) CloseInfo { - if pos(s).Experiment().ExplicitOpen || n.ctx.OpenDef { + if Pos(s).Experiment().ExplicitOpen || n.ctx.OpenDef { return id } diff --git a/internal/core/adt/unify.go b/internal/core/adt/unify.go index 57efb72e2..bf9c65052 100644 --- a/internal/core/adt/unify.go +++ b/internal/core/adt/unify.go @@ -639,7 +639,7 @@ func (n *nodeContext) completeAllArcs(needs condition, mode runMode, checkTypos case a.ArcType > ArcRequired, !a.Label.IsString(): case n.kind&StructKind == 0: if !n.node.IsErr() && !a.IsErr() { - n.reportFieldMismatch(pos(a.Value()), nil, a.Label, n.node.Value()) + n.reportFieldMismatch(Pos(a.Value()), nil, a.Label, n.node.Value()) } // case !wasVoid: // case n.kind == TopKind: @@ -702,7 +702,7 @@ func (n *nodeContext) completeAllArcs(needs condition, mode runMode, checkTypos Src: c.expr.Source(), Code: CycleError, Node: n.node, - Err: ctx.NewPosf(pos(c.expr), + Err: ctx.NewPosf(Pos(c.expr), "circular dependency in evaluation of conditionals: %v changed after evaluation", ctx.Str(c.expr)), }) diff --git a/internal/core/toposort/vertex.go b/internal/core/toposort/vertex.go index 41e6af832..28bd109fa 100644 --- a/internal/core/toposort/vertex.go +++ b/internal/core/toposort/vertex.go @@ -247,10 +247,8 @@ func analyseStructs(v *adt.Vertex, builder *GraphBuilder) []structMeta { sMeta := &structMetas[metaIdx] metaIdx++ sMeta.structInfo = s + sMeta.pos = adt.Pos(sl) - if src := sl.Source(); src != nil { - sMeta.pos = src.Pos() - } structMetaMap(sl)[sMeta] = true for _, decl := range sl.Decls { structMetaMap(decl)[sMeta] = true