Skip to content

Commit

Permalink
pkg: clean up builtin errors
Browse files Browse the repository at this point in the history
- delete dead code and unused fields
- remove unused arguments
- dedup errors
- use OpContext-based error methods (for positions)

Fixes #902
Issue #52

Change-Id: Idf96e569400a6a5230a8783b0881a895026a41ae
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9449
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
mpvl committed Apr 20, 2021
1 parent 908614e commit d5ff672
Show file tree
Hide file tree
Showing 26 changed files with 166 additions and 159 deletions.
3 changes: 2 additions & 1 deletion cue/testdata/comprehensions/fields.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ user: {
#User: (#struct){
tags_str: (string){ string }
tags_map: (_|_){
// [incomplete] error in call to strings.Split: non-concrete value string
// [incomplete] error in call to strings.Split: non-concrete value string:
// ./in.cue:6:22
"{a}": (string){ string }
}
}
Expand Down
26 changes: 20 additions & 6 deletions cue/testdata/cycle/compbottomnofinal.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ large: {
}
-- out/eval --
Errors:
error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax
large.p1.Y: cycle: new field port inserted by if clause that was previously evaluated by another if clause
large.p2.#Y: cycle: field userinfo was added after an if clause evaluated it
large.p2.Y: cycle: new field port inserted by if clause that was previously evaluated by another if clause
Expand All @@ -372,6 +371,16 @@ medium.p3.Y: cycle: new field port inserted by if clause that was previously eva
medium.p4.Y: cycle: new field port inserted by if clause that was previously evaluated by another if clause
minimal.b: cycle: new field port inserted by if clause that was previously evaluated by another if clause
small.p1.#Y: cycle: field port was added after an if clause evaluated it
error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax:
./in.cue:104:23
error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax:
./in.cue:126:23
error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax:
./in.cue:228:23
error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax:
./in.cue:269:23
error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax:
./in.cue:310:23

Result:
(_|_){
Expand Down Expand Up @@ -441,7 +450,8 @@ Result:
Y: (_|_){
// [eval] medium.p3.Y: cycle: new field port inserted by if clause that was previously evaluated by another if clause
port: (_|_){
// [eval] error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax
// [eval] error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax:
// ./in.cue:104:23
}
}
#Y: (#struct){
Expand All @@ -456,7 +466,8 @@ Result:
Y: (_|_){
// [eval] medium.p4.Y: cycle: new field port inserted by if clause that was previously evaluated by another if clause
port: (_|_){
// [eval] error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax
// [eval] error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax:
// ./in.cue:126:23
}
}
#X: (#struct){
Expand Down Expand Up @@ -501,7 +512,8 @@ Result:
userinfo: (string){ "user" }
host: (string){ "example.com" }
port: (_|_){
// [eval] error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax
// [eval] error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax:
// ./in.cue:228:23
}
}
X: (string){ "user@example.com" }
Expand All @@ -524,7 +536,8 @@ Result:
userinfo: (string){ "user" }
host: (string){ "example.com" }
port: (_|_){
// [eval] error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax
// [eval] error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax:
// ./in.cue:269:23
}
}
#X: (#struct){
Expand Down Expand Up @@ -552,7 +565,8 @@ Result:
userinfo: (string){ "user" }
host: (string){ "example.com" }
port: (_|_){
// [eval] error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax
// [eval] error in call to strconv.Atoi: strconv.Atoi: parsing "": invalid syntax:
// ./in.cue:310:23
}
}
#Y: (_|_){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ s3: strings.ContainsAny(str, "dd")
}
s2: (string){ strings.ContainsAny("dd") }
s3: (_|_){
// [incomplete] error in call to strings.ContainsAny: non-concrete value string
// [incomplete] error in call to strings.ContainsAny: non-concrete value string:
// ./in.cue:15:5
}
str: (string){ string }
num: (number){ <4 }
Expand Down
6 changes: 4 additions & 2 deletions cue/testdata/fulleval/044_Issue_#178.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ bar: hex.EncodedLen(len)
-- out/eval --
(struct){
foo: (_|_){
// [incomplete] error in call to encoding/csv.Decode: non-concrete value bytes
// [incomplete] error in call to encoding/csv.Decode: non-concrete value bytes:
// ./in.cue:5:7
}
data: (bytes){ bytes }
len: (int){ int }
bar: (_|_){
// [incomplete] error in call to encoding/hex.EncodedLen: non-concrete value int
// [incomplete] error in call to encoding/hex.EncodedLen: non-concrete value int:
// ./in.cue:9:6
}
}
-- out/compile --
Expand Down
3 changes: 2 additions & 1 deletion cue/testdata/fulleval/056_issue314.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ x:
#T: (#struct){
s: (string){ string }
out: (_|_){
// [incomplete] error in call to text/template.Execute: cannot convert incomplete value "string" to JSON
// [incomplete] error in call to text/template.Execute: cannot convert incomplete value "string" to JSON:
// ./in.cue:14:7
}
}
#V: (#struct){
Expand Down
18 changes: 12 additions & 6 deletions cue/testdata/resolve/048_builtins.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ a3.a: invalid value "bar" (out of bound =~"oo"):
a3.a: invalid value "bar" (out of bound =~"fo"):
./in.cue:9:5
./in.cue:12:14
stringListErrors.b.result: invalid type element 0 (int) of string list argument 0:
stringListErrors.b.result: invalid list element 0 in argument 0 to call: cannot use value int (int) as string:
./in.cue:31:17
./in.cue:30:13
stringListErrors.c.result: cannot use int (type int) as string in argument 2 to strings.Join:
./in.cue:36:33
decimalListErrors.a.result: invalid type element 0 (string) of number list argument 0:
decimalListErrors.a.result: invalid list element 0 in argument 0 to call: cannot use value string (string) as number:
./in.cue:43:17
./in.cue:42:13

Result:
(_|_){
Expand Down Expand Up @@ -167,8 +169,9 @@ Result:
0: (string){ string }
}
result: (_|_){
// [incomplete] stringListErrors.a.result: non-concrete string value for element 0 of string list argument 0:
// [incomplete] stringListErrors.a.result: non-concrete value string for element 0 of string list argument 0:
// ./in.cue:26:17
// ./in.cue:25:13
}
}
b: (_|_){
Expand All @@ -177,8 +180,9 @@ Result:
0: (int){ int }
}
result: (_|_){
// [eval] stringListErrors.b.result: invalid type element 0 (int) of string list argument 0:
// [eval] stringListErrors.b.result: invalid list element 0 in argument 0 to call: cannot use value int (int) as string:
// ./in.cue:31:17
// ./in.cue:30:13
}
}
c: (_|_){
Expand All @@ -199,17 +203,19 @@ Result:
0: (string){ string }
}
result: (_|_){
// [eval] decimalListErrors.a.result: invalid type element 0 (string) of number list argument 0:
// [eval] decimalListErrors.a.result: invalid list element 0 in argument 0 to call: cannot use value string (string) as number:
// ./in.cue:43:17
// ./in.cue:42:13
}
}
b: (struct){
x: (list){
0: (int){ int }
}
result: (_|_){
// [incomplete] decimalListErrors.b.result: non-concrete number value for element 0 of number list argument 0:
// [incomplete] decimalListErrors.b.result: non-concrete value int for element 0 of number list argument 0:
// ./in.cue:48:17
// ./in.cue:47:13
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions internal/core/adt/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,7 @@ func (c *OpContext) evalState(v Expr, state VertexStatus) (result Value) {
if b, ok := result.(*Bottom); ok {
if c.src != nil &&
b.Code == CycleError &&
b.Err.Position() == token.NoPos &&
len(b.Err.InputPositions()) == 0 {
len(errors.Positions(b.Err)) == 0 {
bb := *b
bb.Err = errors.Wrapf(b.Err, c.src.Pos(), "")
result = &bb
Expand Down
1 change: 1 addition & 0 deletions internal/core/adt/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,7 @@ func (x *Builtin) call(c *OpContext, p token.Pos, args []Value) Expr {
c.addErrf(0, pos(a),
"cannot use %s as %s in argument %d to %s",
a, v, i+1, fun)
return nil
}
args[i] = n
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/encoding/base64/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ package base64

import (
"encoding/base64"
"fmt"

"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
)

// EncodedLen returns the length in bytes of the base64 encoding
// of an input buffer of length n. Encoding needs to be set to null
// as only StdEncoding is supported for now.
func EncodedLen(encoding cue.Value, n int) (int, error) {
if err := encoding.Null(); err != nil {
return 0, fmt.Errorf("base64: unsupported encoding: %v", err)
return 0, errors.Wrapf(err, token.NoPos, "base64: unsupported encoding")
}
return base64.StdEncoding.EncodedLen(n), nil
}
Expand All @@ -37,7 +38,7 @@ func EncodedLen(encoding cue.Value, n int) (int, error) {
// null as only StdEncoding is supported for now.
func DecodedLen(encoding cue.Value, x int) (int, error) {
if err := encoding.Null(); err != nil {
return 0, fmt.Errorf("base64: unsupported encoding: %v", err)
return 0, errors.Wrapf(err, token.NoPos, "base64: unsupported encoding")
}
return base64.StdEncoding.DecodedLen(x), nil
}
Expand All @@ -46,7 +47,7 @@ func DecodedLen(encoding cue.Value, x int) (int, error) {
// as only StdEncoding is supported for now.
func Encode(encoding cue.Value, src []byte) (string, error) {
if err := encoding.Null(); err != nil {
return "", fmt.Errorf("base64: unsupported encoding: %v", err)
return "", errors.Wrapf(err, token.NoPos, "base64: unsupported encoding")
}
return base64.StdEncoding.EncodeToString(src), nil
}
Expand All @@ -55,7 +56,7 @@ func Encode(encoding cue.Value, src []byte) (string, error) {
// to be set to null as only StdEncoding is supported for now.
func Decode(encoding cue.Value, s string) ([]byte, error) {
if err := encoding.Null(); err != nil {
return nil, fmt.Errorf("base64: unsupported encoding: %v", err)
return nil, errors.Wrapf(err, token.NoPos, "base64: unsupported encoding")
}
return base64.StdEncoding.DecodeString(s)
}
6 changes: 4 additions & 2 deletions pkg/encoding/base64/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ t3: base64.Decode(null, "foo")
t4: base64.Decode({}, "foo")
-- out/base64 --
Errors:
error in call to encoding/base64.Decode: base64: unsupported encoding: cannot use value {} (type struct) as null
error in call to encoding/base64.Decode: illegal base64 data at input byte 0
error in call to encoding/base64.Decode: illegal base64 data at input byte 0:
./in.cue:5:5
error in call to encoding/base64.Decode: base64: unsupported encoding: cannot use value {} (type struct) as null:
./in.cue:6:5

Result:
t1: "Zm9v"
Expand Down
3 changes: 2 additions & 1 deletion pkg/encoding/hex/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ t3: hex.Decode("foo")
t4: hex.Dump('foo')
-- out/hex --
Errors:
error in call to encoding/hex.Decode: encoding/hex: invalid byte: U+006F 'o'
error in call to encoding/hex.Decode: encoding/hex: invalid byte: U+006F 'o':
./in.cue:5:5

Result:
t1: "666f6f"
Expand Down
4 changes: 3 additions & 1 deletion pkg/encoding/json/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (

"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
)

Expand Down Expand Up @@ -98,7 +100,7 @@ func Unmarshal(b []byte) (ast.Expr, error) {
expr, err := parser.ParseExpr("json", b)
if err != nil {
// NOTE: should never happen.
return nil, fmt.Errorf("json: could not parse JSON: %v", err)
return nil, errors.Wrapf(err, token.NoPos, "json: could not parse JSON")
}
return expr, nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/encoding/json/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ t9: json.MarshalStream([{a: 1}, {b: int | *2}])
-- out/json --
Errors:
a: error in call to encoding/json.Validate: invalid value 10 (out of bound <3):
./in.cue:4:5
./in.cue:4:36
json.Validate:1:6

Expand Down
5 changes: 4 additions & 1 deletion pkg/encoding/yaml/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ t8: yaml.Marshal({b: int | *2})
t9: yaml.MarshalStream([{a: 1}, {b: int | *2}])
-- out/yaml --
Errors:
b: error in call to encoding/yaml.Validate: incomplete value int
a: error in call to encoding/yaml.Validate: invalid value 4 (out of bound <3):
./in.cue:3:5
./in.cue:3:41
yaml.Validate:3:5
b: error in call to encoding/yaml.Validate: incomplete value int:
./in.cue:5:5
a: error in call to encoding/yaml.ValidatePartial: invalid value 4 (out of bound <3):
./in.cue:6:5
./in.cue:6:48
yaml.ValidatePartial:3:5

Expand Down
13 changes: 0 additions & 13 deletions pkg/internal/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/compile"
Expand Down Expand Up @@ -126,7 +125,6 @@ func toBuiltin(ctx *adt.OpContext, b *Builtin) *adt.Builtin {
x.Func = func(ctx *adt.OpContext, args []adt.Value) (ret adt.Expr) {
// call, _ := ctx.Source().(*ast.CallExpr)
c := &CallCtxt{
// src: call,
ctx: ctx,
args: args,
builtin: b,
Expand Down Expand Up @@ -181,17 +179,6 @@ func mustParseConstBuiltin(ctx adt.Runtime, name, val string) adt.Expr {

}

func pos(n adt.Node) (p token.Pos) {
if n == nil {
return
}
src := n.Source()
if src == nil {
return
}
return src.Pos()
}

func (x *Builtin) name(ctx *adt.OpContext) string {
if x.Pkg == 0 {
return x.Name
Expand Down

0 comments on commit d5ff672

Please sign in to comment.