Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue/ast: fully remove support for old-style comprehensions
Browse files Browse the repository at this point in the history
Change-Id: I845641df208d09b3b89119db986dcb73a2c293d6
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9562
Reviewed-by: CUE cueckoo <[email protected]>
Reviewed-by: Paul Jolly <[email protected]>
  • Loading branch information
mpvl committed Apr 30, 2021
1 parent f5213be commit aa70399
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 141 deletions.
88 changes: 37 additions & 51 deletions cue/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,17 +601,6 @@ type Ellipsis struct {
expr
}

// A ListComprehension node represents as list comprehension.
type ListComprehension struct {
Lbrack token.Pos // position of "["
Expr Expr
Clauses []Clause // Feed or Guard (TODO let)
Rbrack token.Pos // position of "]"

comments
expr
}

// A ForClause node represents a for clause in a comprehension.
type ForClause struct {
For token.Pos
Expand Down Expand Up @@ -770,34 +759,32 @@ func (x *StructLit) pos() *token.Pos {
return &x.Lbrace
}

func (x *ListLit) Pos() token.Pos { return x.Lbrack }
func (x *ListLit) pos() *token.Pos { return &x.Lbrack }
func (x *Ellipsis) Pos() token.Pos { return x.Ellipsis }
func (x *Ellipsis) pos() *token.Pos { return &x.Ellipsis }
func (x *ListComprehension) Pos() token.Pos { return x.Lbrack }
func (x *ListComprehension) pos() *token.Pos { return &x.Lbrack }
func (x *LetClause) Pos() token.Pos { return x.Let }
func (x *LetClause) pos() *token.Pos { return &x.Let }
func (x *ForClause) Pos() token.Pos { return x.For }
func (x *ForClause) pos() *token.Pos { return &x.For }
func (x *IfClause) Pos() token.Pos { return x.If }
func (x *IfClause) pos() *token.Pos { return &x.If }
func (x *ParenExpr) Pos() token.Pos { return x.Lparen }
func (x *ParenExpr) pos() *token.Pos { return &x.Lparen }
func (x *SelectorExpr) Pos() token.Pos { return x.X.Pos() }
func (x *SelectorExpr) pos() *token.Pos { return x.X.pos() }
func (x *IndexExpr) Pos() token.Pos { return x.X.Pos() }
func (x *IndexExpr) pos() *token.Pos { return x.X.pos() }
func (x *SliceExpr) Pos() token.Pos { return x.X.Pos() }
func (x *SliceExpr) pos() *token.Pos { return x.X.pos() }
func (x *CallExpr) Pos() token.Pos { return x.Fun.Pos() }
func (x *CallExpr) pos() *token.Pos { return x.Fun.pos() }
func (x *UnaryExpr) Pos() token.Pos { return x.OpPos }
func (x *UnaryExpr) pos() *token.Pos { return &x.OpPos }
func (x *BinaryExpr) Pos() token.Pos { return x.X.Pos() }
func (x *BinaryExpr) pos() *token.Pos { return x.X.pos() }
func (x *BottomLit) Pos() token.Pos { return x.Bottom }
func (x *BottomLit) pos() *token.Pos { return &x.Bottom }
func (x *ListLit) Pos() token.Pos { return x.Lbrack }
func (x *ListLit) pos() *token.Pos { return &x.Lbrack }
func (x *Ellipsis) Pos() token.Pos { return x.Ellipsis }
func (x *Ellipsis) pos() *token.Pos { return &x.Ellipsis }
func (x *LetClause) Pos() token.Pos { return x.Let }
func (x *LetClause) pos() *token.Pos { return &x.Let }
func (x *ForClause) Pos() token.Pos { return x.For }
func (x *ForClause) pos() *token.Pos { return &x.For }
func (x *IfClause) Pos() token.Pos { return x.If }
func (x *IfClause) pos() *token.Pos { return &x.If }
func (x *ParenExpr) Pos() token.Pos { return x.Lparen }
func (x *ParenExpr) pos() *token.Pos { return &x.Lparen }
func (x *SelectorExpr) Pos() token.Pos { return x.X.Pos() }
func (x *SelectorExpr) pos() *token.Pos { return x.X.pos() }
func (x *IndexExpr) Pos() token.Pos { return x.X.Pos() }
func (x *IndexExpr) pos() *token.Pos { return x.X.pos() }
func (x *SliceExpr) Pos() token.Pos { return x.X.Pos() }
func (x *SliceExpr) pos() *token.Pos { return x.X.pos() }
func (x *CallExpr) Pos() token.Pos { return x.Fun.Pos() }
func (x *CallExpr) pos() *token.Pos { return x.Fun.pos() }
func (x *UnaryExpr) Pos() token.Pos { return x.OpPos }
func (x *UnaryExpr) pos() *token.Pos { return &x.OpPos }
func (x *BinaryExpr) Pos() token.Pos { return x.X.Pos() }
func (x *BinaryExpr) pos() *token.Pos { return x.X.pos() }
func (x *BottomLit) Pos() token.Pos { return x.Bottom }
func (x *BottomLit) pos() *token.Pos { return &x.Bottom }

func (x *BadExpr) End() token.Pos { return x.To }
func (x *Ident) End() token.Pos {
Expand All @@ -819,18 +806,17 @@ func (x *Ellipsis) End() token.Pos {
}
return x.Ellipsis.Add(3) // len("...")
}
func (x *ListComprehension) End() token.Pos { return x.Rbrack }
func (x *LetClause) End() token.Pos { return x.Expr.End() }
func (x *ForClause) End() token.Pos { return x.Source.End() }
func (x *IfClause) End() token.Pos { return x.Condition.End() }
func (x *ParenExpr) End() token.Pos { return x.Rparen.Add(1) }
func (x *SelectorExpr) End() token.Pos { return x.Sel.End() }
func (x *IndexExpr) End() token.Pos { return x.Rbrack.Add(1) }
func (x *SliceExpr) End() token.Pos { return x.Rbrack.Add(1) }
func (x *CallExpr) End() token.Pos { return x.Rparen.Add(1) }
func (x *UnaryExpr) End() token.Pos { return x.X.End() }
func (x *BinaryExpr) End() token.Pos { return x.Y.End() }
func (x *BottomLit) End() token.Pos { return x.Bottom.Add(1) }
func (x *LetClause) End() token.Pos { return x.Expr.End() }
func (x *ForClause) End() token.Pos { return x.Source.End() }
func (x *IfClause) End() token.Pos { return x.Condition.End() }
func (x *ParenExpr) End() token.Pos { return x.Rparen.Add(1) }
func (x *SelectorExpr) End() token.Pos { return x.Sel.End() }
func (x *IndexExpr) End() token.Pos { return x.Rbrack.Add(1) }
func (x *SliceExpr) End() token.Pos { return x.Rbrack.Add(1) }
func (x *CallExpr) End() token.Pos { return x.Rparen.Add(1) }
func (x *UnaryExpr) End() token.Pos { return x.X.End() }
func (x *BinaryExpr) End() token.Pos { return x.Y.End() }
func (x *BottomLit) End() token.Pos { return x.Bottom.Add(1) }

// ----------------------------------------------------------------------------
// Convenience functions for Idents
Expand Down
7 changes: 0 additions & 7 deletions cue/ast/astutil/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,6 @@ func applyCursor(v applyVisitor, c Cursor) {
case *ast.Package:
apply(v, c, &n.Name)

case *ast.ListComprehension:
apply(v, c, &n.Expr)
clauses := n.Clauses
for i := range clauses {
apply(v, c, &clauses[i])
}

case *ast.ForClause:
if n.Key != nil {
apply(v, c, &n.Key)
Expand Down
3 changes: 0 additions & 3 deletions cue/ast/astutil/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,6 @@ func (s *scope) Before(n ast.Node) (w visitor) {
case *ast.Comprehension:
s = scopeClauses(s, x.Clauses)

case *ast.ListComprehension:
s = scopeClauses(s, x.Clauses)

case *ast.Field:
var n ast.Node = x.Label
alias, ok := x.Label.(*ast.Alias)
Expand Down
6 changes: 0 additions & 6 deletions cue/ast/astutil/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,6 @@ func walk(v visitor, node ast.Node) {
case *ast.Package:
// The package identifier isn't really an identifier. Skip it.

case *ast.ListComprehension:
walk(v, n.Expr)
for _, c := range n.Clauses {
walk(v, c)
}

case *ast.LetClause:
walk(v, n.Ident)
walk(v, n.Expr)
Expand Down
6 changes: 0 additions & 6 deletions cue/ast/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,6 @@ func walk(v visitor, node Node) {
case *Package:
walk(v, n.Name)

case *ListComprehension:
walk(v, n.Expr)
for _, c := range n.Clauses {
walk(v, c)
}

case *ForClause:
if n.Key != nil {
walk(v, n.Key)
Expand Down
16 changes: 1 addition & 15 deletions cue/format/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (f *formatter) decl(decl ast.Decl) {

if n.Value != nil {
switch n.Value.(type) {
case *ast.ListComprehension, *ast.ListLit, *ast.StructLit:
case *ast.ListLit, *ast.StructLit:
f.expr(n.Value)
default:
f.print(indent)
Expand Down Expand Up @@ -670,20 +670,6 @@ func (f *formatter) exprRaw(expr ast.Expr, prec1, depth int) {
case *ast.Ellipsis:
f.ellipsis(x)

case *ast.ListComprehension:
f.print(x.Lbrack, token.LBRACK, blank, indent)
f.print(blank)
f.walkClauseList(x.Clauses, blank)
f.print(blank, nooverride)
if _, ok := x.Expr.(*ast.StructLit); ok {
f.expr(x.Expr)
} else {
f.print(token.LBRACE, blank)
f.expr(x.Expr)
f.print(blank, token.RBRACE)
}
f.print(unindent, f.wsOverride(blank), x.Rbrack, token.RBRACK)

default:
panic(fmt.Sprintf("unimplemented type %T", x))
}
Expand Down
8 changes: 3 additions & 5 deletions cue/format/testdata/expressions.golden
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,13 @@ package expressions
e: [ for x in someObject if x > 9 {
x
}]
e: [ for x in someObject if x > 9 { x } ]
e: [ for x in someObject if x > 9 {x}]
e: [
for x in someObject
if x > 9 { x } ]
if x > 9 {x}]
e: [
for x in someObject
if x > 9 { x // TODO(deprecated): remove!
}
]
if x > 9 {x}]

e: [
if x > 1 {},
Expand Down
10 changes: 5 additions & 5 deletions cue/format/testdata/expressions.input
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ package expressions
e: [ for x in someObject if x > 9 {
x
}]
e: [ x for x in someObject if x > 9 ]
e: [ x
e: [ for x in someObject if x > 9 {x}]
e: [
for x in someObject
if x > 9 ]
e: [ x // TODO(deprecated): remove!
if x > 9 {x}]
e: [
for x in someObject
if x > 9
]
{x}]

e: [
if x > 1 {},
Expand Down
20 changes: 0 additions & 20 deletions cue/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1180,25 +1180,6 @@ func (p *parser) parseList() (expr ast.Expr) {

elts := p.parseListElements()

if clauses, _ := p.parseComprehensionClauses(false); clauses != nil {
var expr ast.Expr
p.assertV0(p.pos, 1, 3, "old-style list comprehensions")
if len(elts) != 1 {
p.errf(lbrack.Add(1), "list comprehension must have exactly one element")
}
if len(elts) > 0 {
expr = elts[0]
}
rbrack := p.expectClosing(token.RBRACK, "list comprehension")

return &ast.ListComprehension{
Lbrack: lbrack,
Expr: expr,
Clauses: clauses,
Rbrack: rbrack,
}
}

if p.tok == token.ELLIPSIS {
ellipsis := &ast.Ellipsis{
Ellipsis: p.pos,
Expand Down Expand Up @@ -1326,7 +1307,6 @@ func (p *parser) checkExpr(x ast.Expr) ast.Expr {
case *ast.Interpolation:
case *ast.StructLit:
case *ast.ListLit:
case *ast.ListComprehension:
case *ast.ParenExpr:
panic("unreachable")
case *ast.SelectorExpr:
Expand Down
8 changes: 0 additions & 8 deletions internal/astinternal/debugstr.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,6 @@ func DebugStr(x interface{}) (out string) {
}
return out

case *ast.ListComprehension:
out := "["
out += DebugStr(v.Expr)
out += " "
out += DebugStr(v.Clauses)
out += "]"
return out

case *ast.ForClause:
out := "for "
if v.Key != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/core/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func (c *compiler) markAlias(d ast.Decl) {
c.insertAlias(x.Ident, a)

case *ast.Alias:
c.errf(x, "old-style alias no longer supported: use let clause.\nuse cue fix to update.")
c.errf(x, "old-style alias no longer supported: use let clause; use cue fix to update.")
}
}

Expand Down Expand Up @@ -626,7 +626,7 @@ func (c *compiler) decl(d ast.Decl) adt.Decl {

// Handled in addLetDecl.
case *ast.LetClause:
// case: *ast.Alias: // TODO(value aliases)
// case: *ast.Alias: // TODO(value alias)

case *ast.CommentGroup:
// Nothing to do for a free-floating comment group.
Expand Down Expand Up @@ -665,7 +665,7 @@ func (c *compiler) addLetDecl(d ast.Decl) {
c.updateAlias(x.Ident, expr)

case *ast.Alias:
c.errf(x, "old-style alias no longer supported: use let clause.\nuse cue fix to update.")
c.errf(x, "old-style alias no longer supported: use let clause; use cue fix to update.")
}
}

Expand Down
3 changes: 1 addition & 2 deletions internal/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,7 @@ func (v *validator) validate(n ast.Node) bool {
}

case *ast.BinaryExpr, *ast.ParenExpr, *ast.IndexExpr, *ast.SliceExpr,
*ast.CallExpr, *ast.Comprehension, *ast.ListComprehension,
*ast.Interpolation:
*ast.CallExpr, *ast.Comprehension, *ast.Interpolation:
check(n, constraints, "expressions", true)

case *ast.Ellipsis:
Expand Down
4 changes: 2 additions & 2 deletions internal/encoding/yaml/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ f4: {} # line 4
a: 1
b: 3
}
c: [1, [ x for x in m ]]
c: [1, [ for x in m {x}]]
`,
out: "yaml: unsupported node [x for x in m ] (*ast.ListComprehension)",
out: "yaml: unsupported node for x in m {x} (*ast.Comprehension)",
}, {
name: "disallowMultipleEmbeddings",
in: `
Expand Down
8 changes: 0 additions & 8 deletions tools/fix/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ a: {
X1="in": 3
ref: X1 & x
}
`,
}, {
in: `
y: [1, 2, 3, 4]
a: [ x for x in y ]
`,
out: `y: [1, 2, 3, 4]
a: [ for x in y { x } ]
`,
}, {
in: `
Expand Down

0 comments on commit aa70399

Please sign in to comment.