Skip to content

Commit

Permalink
internal/core/export: fix crash in dynamic fields
Browse files Browse the repository at this point in the history
Silly alignment issue in comprehensions.

Fixes #1910

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: Ie578d03978eac7ef4ccee164100ad5ef7a9b0095
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/543534
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Aram Hăvărneanu <aram@mgk.ro>
  • Loading branch information
mpvl committed Sep 15, 2022
1 parent 519d326 commit ad253ed
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
13 changes: 11 additions & 2 deletions internal/core/export/adt.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func (e *exporter) adt(env *adt.Environment, expr adt.Elem) ast.Expr {
// s := e.frame(0).scope

s := &ast.StructLit{}
// TODO: ensure e.node() is set in more cases. Right now it is not
// always set in mergeValues, even in cases where it could be. Better
// to be conservative for now, though.
env := &adt.Environment{Up: env, Vertex: e.node()}

for _, d := range x.Decls {
Expand Down Expand Up @@ -566,7 +569,7 @@ loop:
for {
switch x := y.(type) {
case *adt.ForClause:
env := &adt.Environment{Up: env, Vertex: empty}
env = &adt.Environment{Up: env, Vertex: empty}
value := e.ident(x.Value)
src := e.innerExpr(env, x.Src)
clause := &ast.ForClause{Value: value, Source: src}
Expand All @@ -592,7 +595,7 @@ loop:
y = x.Dst

case *adt.LetClause:
env := &adt.Environment{Up: env, Vertex: empty}
env = &adt.Environment{Up: env, Vertex: empty}
expr := e.innerExpr(env, x.Expr)
clause := &ast.LetClause{
Ident: e.ident(x.Label),
Expand All @@ -615,6 +618,12 @@ loop:
}
}

// If this is an "unwrapped" comprehension, we need to also
// account for the curly braces of the original comprehension.
if comp.Nest() > 0 {
env = &adt.Environment{Up: env, Vertex: empty}
}

v := e.expr(env, adt.ToExpr(comp.Value))
if _, ok := v.(*ast.StructLit); !ok {
v = ast.NewStruct(ast.Embed(v))
Expand Down
31 changes: 31 additions & 0 deletions internal/core/export/testdata/main/adt.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ d2: C="foo\(bar)": {
foo: C.name
}

// Issue #1910
a: _
comp: {
for k, v in [0]
let w = v {
"\(a)": w
"\(bar)": w
}
}

bytes: '\xeb \x1a\xf5\xaa\xf0\xd6\x06)'

c1: mystrings.Contains("aa", "a")
Expand Down Expand Up @@ -141,6 +151,13 @@ d2: {
foo: C.name
}
}
a: _
comp: {
for k, v in [0] let w = v {
"\(a)": w
"bar": w
}
}
bytes: '\xeb \x1a\xf5\xaa\xf0\xd6\x06)'
c1: mystrings.Contains("aa", "a")
s1: """
Expand Down Expand Up @@ -230,6 +247,11 @@ errorListDef: {
[d2 foobar]
[d2 foobar name]
[d2 foobar foo]
[a]
- Issue #1910

[comp]
[comp bar]
[bytes]
[c1]
[s1]
Expand Down Expand Up @@ -312,6 +334,15 @@ _|_ // e3: index out of range [2] with length 2
foo: "xx"
}
}

// Issue #1910
a: _
comp: {
for k, v in [0] let w = v {
"\(a)": w
"bar": w
}
}
bytes: '\xeb \x1a\xf5\xaa\xf0\xd6\x06)'
c1: true
s1: """
Expand Down

0 comments on commit ad253ed

Please sign in to comment.