Skip to content

Commit

Permalink
internal/core/compile: fix let resolution bug
Browse files Browse the repository at this point in the history
Fixes #767

Change-Id: Idf04a9af01552f2cdd709a5f17eda67eb310efa2
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8708
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Feb 16, 2021
1 parent 830cfbc commit e12e26e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
102 changes: 102 additions & 0 deletions cue/testdata/compile/let.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,48 @@ b: {
b: X
c: 5
}

fieldOffset: {
a: {
p1: {
let X = { value: Y }
let Y = 2
x: X
}
p2: {
x: X
let Y = 2
let X = { value: Y }
}
}
b: {
p1: {
let X = { x: y: Y }
let Y = 2
x: X
}
p2: {
x: X
let Y = 2
let X = { x: y: Y }
}
}
}

issue767: {
#Foo: {
let _#bar = {
value: ""
}
let _#volmnts = {
x: _#baz.value
}
let _#baz = {
_#bar
}
out: _#volmnts
}
}
-- out/compile --
--- in.cue
{
Expand All @@ -25,6 +67,29 @@ b: {
b: 〈0;let X〉
c: 5
}
fieldOffset: {
a: {
p1: {
x: 〈0;let X〉
}
p2: {
x: 〈0;let X〉
}
}
b: {
p1: {
x: 〈0;let X〉
}
p2: {
x: 〈0;let X〉
}
}
}
issue767: {
#Foo: {
out: 〈0;let _#volmnts〉
}
}
}
-- out/eval --
Errors:
Expand All @@ -46,4 +111,41 @@ Result:
}
c: (int){ 5 }
}
fieldOffset: (struct){
a: (struct){
p1: (struct){
x: (struct){
value: (int){ 2 }
}
}
p2: (struct){
x: (struct){
value: (int){ 2 }
}
}
}
b: (struct){
p1: (struct){
x: (struct){
x: (struct){
y: (int){ 2 }
}
}
}
p2: (struct){
x: (struct){
x: (struct){
y: (int){ 2 }
}
}
}
}
}
issue767: (struct){
#Foo: (#struct){
out: (#struct){
x: (string){ "" }
}
}
}
}
6 changes: 5 additions & 1 deletion internal/core/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (c compiler) lookupAlias(k int, id *ast.Ident) aliasEntry {
entry.srcExpr = nil // mark to allow detecting cycles
m[name] = entry

entry.expr = c.labeledExpr(nil, entry.label, src)
entry.expr = c.labeledExprAt(k, nil, entry.label, src)
entry.label = nil
}

Expand Down Expand Up @@ -753,6 +753,10 @@ func (c *compiler) embed(expr ast.Expr) adt.Expr {

func (c *compiler) labeledExpr(f *ast.Field, lab labeler, expr ast.Expr) adt.Expr {
k := len(c.stack) - 1
return c.labeledExprAt(k, f, lab, expr)
}

func (c *compiler) labeledExprAt(k int, f *ast.Field, lab labeler, expr ast.Expr) adt.Expr {
if c.stack[k].field != nil {
panic("expected nil field")
}
Expand Down

0 comments on commit e12e26e

Please sign in to comment.