Skip to content

Commit 67ace83

Browse files
committed
cue/parser: disallow deprecated language features in API
In the tool this was already the case for a long time. This basically means that API operations will now fail on deprectaed features whereas before they were handled in compatibility mode. The parser.FromVersion feature can be used to reenable these deprecated features. Issue cue-lang#1543 Signed-off-by: Marcel van Lohuizen <[email protected]> Change-Id: Ie40afd22143a46799373745cbeb4973a3f387487 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/546763 Reviewed-by: Paul Jolly <[email protected]> Unity-Result: CUEcueckoo <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 3c0b2ec commit 67ace83

File tree

14 files changed

+62
-45
lines changed

14 files changed

+62
-45
lines changed

cmd/cue/cmd/common.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,19 @@ import (
3434
"cuelang.org/go/cue/load"
3535
"cuelang.org/go/cue/parser"
3636
"cuelang.org/go/cue/token"
37+
"cuelang.org/go/internal"
3738
"cuelang.org/go/internal/core/adt"
3839
"cuelang.org/go/internal/encoding"
3940
"cuelang.org/go/internal/filetypes"
4041
"cuelang.org/go/internal/value"
4142
)
4243

43-
// Disallow
44-
// - block comments
45-
// - old-style field comprehensions
46-
// - space separator syntax
47-
const syntaxVersion = -1000 + 100*2 + 1
48-
4944
var requestedVersion = os.Getenv("CUE_SYNTAX_OVERRIDE")
5045

5146
var defaultConfig = config{
5247
loadCfg: &load.Config{
5348
ParseFile: func(name string, src interface{}) (*ast.File, error) {
54-
version := syntaxVersion
49+
version := internal.APIVersionSupported
5550
if requestedVersion != "" {
5651
switch {
5752
case strings.HasPrefix(requestedVersion, "v0.1"):

cue/format/testdata/simplify.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ quux: 5
3232

3333
(x): "foo"
3434

35-
a :: {
35+
a: {
3636
foo: 2
3737
...
3838
}

cue/format/testdata/simplify.input

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ a:
3434

3535
(x): "foo"
3636

37-
a :: {
37+
a: {
3838
[string]: _
3939
foo: 2
4040
}

cue/parser/interface.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"cuelang.org/go/cue/ast/astutil"
2222
"cuelang.org/go/cue/errors"
2323
"cuelang.org/go/cue/token"
24+
"cuelang.org/go/internal"
2425
"cuelang.org/go/internal/source"
2526
)
2627

@@ -83,10 +84,6 @@ func FromVersion(version int) Option {
8384
return func(p *parser) { p.version = version }
8485
}
8586

86-
func version0(minor, patch int) int {
87-
return -1000 + 100*minor + patch
88-
}
89-
9087
// DeprecationError is a sentinel error to indicate that an error is
9188
// related to an unsupported old CUE syntax.
9289
type DeprecationError struct {
@@ -97,11 +94,19 @@ func (e *DeprecationError) Error() string {
9794
return "try running `cue fix` (possibly with an earlier version, like v0.2.2) to upgrade"
9895
}
9996

100-
// Latest specifies the latest version of the parser, effectively setting
101-
// the strictest implementation.
102-
const Latest = latest
97+
const (
98+
// Latest specifies the latest version of the parser, effectively setting
99+
// the strictest implementation.
100+
Latest = latest
101+
102+
latest = -1000 + (100 * internal.MinorCurrent) + 0
103+
104+
// FullBackwardCompatibility enables all deprecated features that are
105+
// currently still supported by the parser.
106+
FullBackwardCompatibility = fullCompatibility
103107

104-
const latest = -600
108+
fullCompatibility = -1000
109+
)
105110

106111
// FileOffset specifies the File position info to use.
107112
func FileOffset(pos int) Option {

cue/parser/parser.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"cuelang.org/go/cue/literal"
2525
"cuelang.org/go/cue/scanner"
2626
"cuelang.org/go/cue/token"
27+
"cuelang.org/go/internal"
2728
"cuelang.org/go/internal/astinternal"
2829
)
2930

@@ -393,8 +394,12 @@ func (p *parser) next() {
393394
// assertV0 indicates the last version at which a certain feature was
394395
// supported.
395396
func (p *parser) assertV0(pos token.Pos, minor, patch int, name string) {
396-
v := version0(minor, patch)
397-
if p.version != 0 && p.version > v {
397+
v := internal.Version(minor, patch)
398+
base := p.version
399+
if base == 0 {
400+
base = internal.APIVersionSupported
401+
}
402+
if base > v {
398403
p.errors = errors.Append(p.errors,
399404
errors.Wrapf(&DeprecationError{v}, pos,
400405
"use of deprecated %s (deprecated as of v0.%d.%d)", name, minor, patch+1))

cue/parser/parser_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,12 @@ func TestParse(t *testing.T) {
212212
`package k8s, import a "foo", import "bar/baz"`,
213213
}, {
214214
"collapsed fields",
215-
`a: b:: c?: [Name=_]: d: 1
215+
`a: #b: c?: [Name=_]: d: 1
216216
"g\("en")"?: 4
217217
// job foo { bar: 1 } // TODO error after foo
218218
job: "foo": [_]: { bar: 1 }
219219
`,
220-
`a: {b :: {c?: {[Name=_]: {d: 1}}}}, "g\("en")"?: 4, job: {"foo": {[_]: {bar: 1}}}`,
220+
`a: {#b: {c?: {[Name=_]: {d: 1}}}}, "g\("en")"?: 4, job: {"foo": {[_]: {bar: 1}}}`,
221221
}, {
222222
"identifiers",
223223
`// $_: 1,

cue/testdata/compile/erralias.txtar

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
[Y="foo"]: 3
55
a: Y
66

7-
"\(b)" :: 3
8-
b: "foo"
9-
107
c: {}
118
for x in c {a: E}
129

@@ -16,17 +13,13 @@ unreferenced alias or let clause X:
1613
./in.cue:1:2
1714
a: reference "Y" not found:
1815
./in.cue:4:12
19-
definitions not supported for interpolations:
20-
./in.cue:6:1
2116
for[].a: reference "E" not found:
22-
./in.cue:10:16
17+
./in.cue:7:16
2318
--- in.cue
2419
{
2520
let X#1 = {}
2621
["foo"]: 3
2722
a: _|_(reference "Y" not found)
28-
"\(〈0;b〉)": 3
29-
b: "foo"
3023
c: {}
3124
for _, x in 〈0;c〉 {
3225
a: _|_(reference "E" not found)

cue/testdata/cycle/issue242.txtar

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
# TODO: remove support for '::'; cuelang.org/issue/1543
2-
31
-- in.cue --
4-
size :: 2
2+
#size: 2
53
#CellValue: 0 | 1 | 2 | 3
64
cell: "0": "0": #CellValue
75
cell: "0": "0": !=cell["0"]["1"]
@@ -100,7 +98,7 @@ Conjuncts: 191
10098
Disjuncts: 79
10199
-- out/eval --
102100
(struct){
103-
size: (int){ 2 }
101+
#size: (int){ 2 }
104102
#CellValue: (int){ |((int){ 0 }, (int){ 1 }, (int){ 2 }, (int){ 3 }) }
105103
cell: (struct){
106104
"0": (struct){
@@ -136,7 +134,7 @@ Disjuncts: 79
136134
-- out/compile --
137135
--- in.cue
138136
{
139-
size:: 2
137+
#size: 2
140138
#CellValue: (0|1|2|3)
141139
cell: {
142140
"0": {

internal/encoding/encoding_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import (
2525

2626
func TestValidate(t *testing.T) {
2727
testCases := []struct {
28-
form build.Form
29-
in string
30-
err string
28+
form build.Form
29+
in string
30+
err string
31+
compat bool
3132
}{{
3233
form: "data",
3334
in: `
@@ -40,7 +41,7 @@ func TestValidate(t *testing.T) {
4041
}, {
4142
form: "graph",
4243
in: `
43-
X=3
44+
let X = 3
4445
a: X
4546
"b-b": 3
4647
s: a
@@ -51,12 +52,12 @@ func TestValidate(t *testing.T) {
5152
{form: "data", err: "references", in: `a: a`},
5253
{form: "data", err: "expressions", in: `a: 1 + 3`},
5354
{form: "data", err: "expressions", in: `a: 1 + 3`},
54-
{form: "data", err: "definitions", in: `a :: 1`},
55+
{form: "data", err: "definitions", in: `#a: 1`},
5556
{form: "data", err: "constraints", in: `a: <1`},
5657
{form: "data", err: "expressions", in: `a: !true`},
5758
{form: "data", err: "expressions", in: `a: 1 | 2`},
5859
{form: "data", err: "expressions", in: `a: 1 | *2`},
59-
{form: "data", err: "references", in: `X=3, a: X`},
60+
{form: "data", err: "references", in: `let X = 3, a: X`, compat: true},
6061
{form: "data", err: "expressions", in: `2+2`},
6162
{form: "data", err: "expressions", in: `"\(3)"`},
6263
{form: "data", err: "expressions", in: `for x in [2] { a: 2 }`},
@@ -65,7 +66,11 @@ func TestValidate(t *testing.T) {
6566
}
6667
for _, tc := range testCases {
6768
t.Run(path.Join(string(tc.form), tc.in), func(t *testing.T) {
68-
f, err := parser.ParseFile("", tc.in, parser.ParseComments)
69+
opts := []parser.Option{parser.ParseComments}
70+
if tc.compat {
71+
opts = append(opts, parser.FromVersion(-1000))
72+
}
73+
f, err := parser.ParseFile("", tc.in, opts...)
6974
if err != nil {
7075
t.Fatal(err)
7176
}

internal/encoding/json/encode.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"cuelang.org/go/cue/errors"
2525
"cuelang.org/go/cue/literal"
2626
"cuelang.org/go/cue/token"
27+
"cuelang.org/go/internal"
2728
"cuelang.org/go/internal/astinternal"
2829
)
2930

@@ -230,7 +231,7 @@ func (e *encoder) encodeDecls(decls []ast.Decl, endPos token.Pos) error {
230231
continue
231232

232233
case *ast.Field:
233-
if x.Token == token.ISA {
234+
if internal.IsDefinition(x.Label) {
234235
return errors.Newf(x.TokenPos, "json: definition not allowed")
235236
}
236237
if x.Optional != token.NoPos {

0 commit comments

Comments
 (0)