Skip to content

Commit

Permalink
internal/core/compile: fix aliases to dynamic fields
Browse files Browse the repository at this point in the history
For all orderings that is.

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: I34156d7f22b3056737694de20132719a07166d1b
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/542797
Unity-Result: CUEcueckoo <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Paul Jolly <[email protected]>
  • Loading branch information
mpvl committed Aug 29, 2022
1 parent 8e8f1cf commit 353833c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
45 changes: 45 additions & 0 deletions cue/testdata/compile/alias.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- in.cue --
dynamic: t1: {
X=(a): 1
x: X
a: "name"
}

dynamic: t2: {
x: X
X=(a): 1
a: "name"
}
-- out/compile --
--- in.cue
{
dynamic: {
t1: {
〈0;a〉: 1
x: 〈0;(〈0;a〉)〉
a: "name"
}
}
dynamic: {
t2: {
x: 〈0;(〈0;a〉)〉
〈0;a〉: 1
a: "name"
}
}
}
-- out/eval --
(struct){
dynamic: (struct){
t1: (struct){
x: (int){ 1 }
a: (string){ "name" }
name: (int){ 1 }
}
t2: (struct){
x: (int){ 1 }
a: (string){ "name" }
name: (int){ 1 }
}
}
}
28 changes: 20 additions & 8 deletions internal/core/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@ func (c *compiler) resolve(n *ast.Ident) adt.Expr {
// Local expressions
case *ast.LetClause:
entry := c.lookupAlias(k, n)
if entry.expr == nil {
panic("unreachable")
}

// let x = y
return &adt.LetReference{
Expand Down Expand Up @@ -544,14 +547,6 @@ func (c *compiler) decl(d ast.Decl) adt.Decl {
if lab, ok = a.Expr.(ast.Label); !ok {
return c.errf(a, "alias expression is not a valid label")
}

switch lab.(type) {
case *ast.Ident, *ast.BasicLit, *ast.ListLit:
// Even though we won't need the alias, we still register it
// for duplicate and failed reference detection.
default:
c.updateAlias(a.Ident, c.expr(a.Expr))
}
}

v := x.Value
Expand Down Expand Up @@ -678,6 +673,23 @@ func (c *compiler) addLetDecl(d ast.Decl) {
expr := c.labeledExpr(nil, (*letScope)(x), x.Expr)
c.updateAlias(x.Ident, expr)

case *ast.Field:
lab := x.Label
if a, ok := lab.(*ast.Alias); ok {
if lab, ok = a.Expr.(ast.Label); !ok {
// error reported elsewhere
return
}

switch lab.(type) {
case *ast.Ident, *ast.BasicLit, *ast.ListLit:
// Even though we won't need the alias, we still register it
// for duplicate and failed reference detection.
default:
c.updateAlias(a.Ident, c.expr(a.Expr))
}
}

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

0 comments on commit 353833c

Please sign in to comment.