all: adjustments package to work with new evaluator

encoding/openapi:
- need to use UnifyAccept
- alternative mechansim to recognize formats

encoding/protobuf:
- remove generation of close() calls.

tools/trim:
- need to use UnifyAccept
- a few more changes due to subtle differences

cue/load:
- using new internal API mostly to remove
  dependencies.

error updates:
- changes due to fixes in the evaluator
- error messages are worse for now
  (especially locations)
- Kubernets quick is now failing, but this is due
  to cue correctly catching errors.

Change-Id: I24efdd5eedcf6cd48bae4a6207f96afbdd895c5f
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6658
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/openapi/build.go b/encoding/openapi/build.go
index 5a7bff8..5bcd485 100644
--- a/encoding/openapi/build.go
+++ b/encoding/openapi/build.go
@@ -323,7 +323,9 @@
 	} else {
 		dedup := map[string]bool{}
 		hasNoRef := false
-		for _, v := range appendSplit(nil, cue.AndOp, v) {
+		accept := v
+		conjuncts := appendSplit(nil, cue.AndOp, v)
+		for _, v := range conjuncts {
 			// This may be a reference to an enum. So we need to check references before
 			// dissecting them.
 			switch p, r := v.Reference(); {
@@ -344,7 +346,7 @@
 			}
 			hasNoRef = true
 			count++
-			values = values.Unify(v)
+			values = values.UnifyAccept(v, accept)
 		}
 		isRef = !hasNoRef && len(dedup) == 1
 	}
diff --git a/encoding/openapi/testdata/nested.json b/encoding/openapi/testdata/nested.json
index c9bcc7b..e11ce76 100644
--- a/encoding/openapi/testdata/nested.json
+++ b/encoding/openapi/testdata/nested.json
@@ -10,6 +10,9 @@
          "Struct": {
             "type": "object",
             "properties": {
+               "b": {
+                  "$ref": "#/components/schemas/Struct.T"
+               },
                "a": {
                   "$ref": "#/components/schemas/Struct.T"
                },
@@ -18,9 +21,6 @@
                   "items": {
                      "$ref": "#/components/schemas/Struct.T"
                   }
-               },
-               "b": {
-                  "$ref": "#/components/schemas/Struct.T"
                }
             }
          },
diff --git a/encoding/openapi/testdata/oneof-funcs.json b/encoding/openapi/testdata/oneof-funcs.json
index 6b6b548..042688a 100644
--- a/encoding/openapi/testdata/oneof-funcs.json
+++ b/encoding/openapi/testdata/oneof-funcs.json
@@ -137,14 +137,11 @@
             "description": "Randomly picked description from a set of size one.",
             "type": "object",
             "required": [
-               "count",
                "include",
-               "exclude"
+               "exclude",
+               "count"
             ],
             "properties": {
-               "count": {
-                  "$ref": "#/components/schemas/MYINT"
-               },
                "include": {
                   "$ref": "#/components/schemas/T"
                },
@@ -154,6 +151,9 @@
                   "items": {
                      "$ref": "#/components/schemas/T"
                   }
+               },
+               "count": {
+                  "$ref": "#/components/schemas/MYINT"
                }
             }
          },
diff --git a/encoding/openapi/testdata/oneof-resolve.json b/encoding/openapi/testdata/oneof-resolve.json
index 7130198..2d6183e 100644
--- a/encoding/openapi/testdata/oneof-resolve.json
+++ b/encoding/openapi/testdata/oneof-resolve.json
@@ -110,14 +110,11 @@
          "Foo": {
             "type": "object",
             "required": [
-               "count",
                "include",
-               "exclude"
+               "exclude",
+               "count"
             ],
             "properties": {
-               "count": {
-                  "type": "integer"
-               },
                "include": {
                   "type": "object",
                   "properties": {
@@ -302,6 +299,9 @@
                         }
                      ]
                   }
+               },
+               "count": {
+                  "type": "integer"
                }
             }
          },
diff --git a/encoding/openapi/testdata/oneof.json b/encoding/openapi/testdata/oneof.json
index 66b46b3..e58cb6d 100644
--- a/encoding/openapi/testdata/oneof.json
+++ b/encoding/openapi/testdata/oneof.json
@@ -128,14 +128,11 @@
          "Foo": {
             "type": "object",
             "required": [
-               "count",
                "include",
-               "exclude"
+               "exclude",
+               "count"
             ],
             "properties": {
-               "count": {
-                  "$ref": "#/components/schemas/MyInt"
-               },
                "include": {
                   "$ref": "#/components/schemas/T"
                },
@@ -144,6 +141,9 @@
                   "items": {
                      "$ref": "#/components/schemas/T"
                   }
+               },
+               "count": {
+                  "$ref": "#/components/schemas/MyInt"
                }
             }
          },
diff --git a/encoding/openapi/testdata/openapi-norefs.json b/encoding/openapi/testdata/openapi-norefs.json
index 4a34623..b4112f9 100644
--- a/encoding/openapi/testdata/openapi-norefs.json
+++ b/encoding/openapi/testdata/openapi-norefs.json
@@ -108,18 +108,12 @@
                   }
                },
                "foo": {
-                  "type": "number",
+                  "type": "integer",
                   "minimum": 10,
                   "exclusiveMinimum": true,
                   "maximum": 1000,
                   "exclusiveMaximum": true
                },
-               "bar": {
-                  "type": "array",
-                  "items": {
-                     "type": "string"
-                  }
-               },
                "a": {
                   "description": "Field a.",
                   "type": "integer",
@@ -127,6 +121,12 @@
                      1
                   ]
                },
+               "bar": {
+                  "type": "array",
+                  "items": {
+                     "type": "string"
+                  }
+               },
                "b": {
                   "type": "string"
                }
diff --git a/encoding/openapi/testdata/openapi.json b/encoding/openapi/testdata/openapi.json
index 50bcf83..90fd2cb 100644
--- a/encoding/openapi/testdata/openapi.json
+++ b/encoding/openapi/testdata/openapi.json
@@ -2,8 +2,8 @@
    "openapi": "3.0.0",
    "info": {
       "contact": {
-         "name": "John Doe",
-         "url": "https://cuelang.org"
+         "url": "https://cuelang.org",
+         "name": "John Doe"
       },
       "title": "An OpenAPI testing package.",
       "version": "v1beta2"
@@ -96,7 +96,7 @@
                   "$ref": "#/components/schemas/Port"
                },
                "foo": {
-                  "type": "number",
+                  "type": "integer",
                   "allOf": [
                      {
                         "$ref": "#/components/schemas/Int32"
diff --git a/encoding/openapi/testdata/script/basics.txtar b/encoding/openapi/testdata/script/basics.txtar
index 6b63b02..9078eb4 100644
--- a/encoding/openapi/testdata/script/basics.txtar
+++ b/encoding/openapi/testdata/script/basics.txtar
@@ -37,8 +37,8 @@
 }
 // A User uses something.
 #User: {
-	name?:    string
 	id?:      int
+	name?:    string
 	address?: #PhoneNumber
 	...
 }
diff --git a/encoding/openapi/types.go b/encoding/openapi/types.go
index b86c3ac..a216a08 100644
--- a/encoding/openapi/types.go
+++ b/encoding/openapi/types.go
@@ -21,7 +21,6 @@
 	"github.com/cockroachdb/apd/v2"
 
 	"cuelang.org/go/cue/ast"
-	"cuelang.org/go/cue/format"
 	"cuelang.org/go/cue/literal"
 	"cuelang.org/go/cue/token"
 	"cuelang.org/go/internal/legacy/cue"
@@ -45,6 +44,11 @@
 	`time.Format ("2006-01-02T15:04:05.999999999Z07:00")`: "date-time",
 
 	// TODO:  password.
+
+	">=-2147483648 & <=2147483647 & int":                                                                   "int32",
+	">=-9223372036854775808 & <=9223372036854775807 & int":                                                 "int64",
+	">=-340282346638528859811704183484516925440 & <=340282346638528859811704183484516925440":               "float",
+	">=-1.797693134862315708145274237317043567981e+308 & <=1.797693134862315708145274237317043567981e+308": "double",
 }
 
 func extractFormat(v cue.Value) string {
@@ -65,12 +69,8 @@
 		expr = inst.ImportPath + "." + strings.Join(ref, ".")
 		expr += arg
 	} else {
-		// TODO: have some function to extract normalized builtin types.
-		b, err := format.Node(v.Syntax(cue.Final()))
-		if err != nil {
-			return ""
-		}
-		expr = string(b)
+		expr = fmt.Sprint(v.Eval())
+		expr += arg
 	}
 	if s, ok := cueToOpenAPI[expr]; ok {
 		return s