Skip to content

Commit

Permalink
internal/core/export: fix bug 473
Browse files Browse the repository at this point in the history
Ensure that exporter always exports a struct literal,
and not an arbitrary expression.

TODO: this can be enforced at compile time by
changing the type of the Value field to *ast.StructLit.
This is a breaking change in the API though. At some
point this will be useful, as it will help prevent users
of the API to make similar errors.

Fixes #473

Change-Id: I26d1590c9b793ad5c82b02f69af47795a4bbd1a4
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6950
Reviewed-by: Paul Jolly <[email protected]>
Reviewed-by: CUE cueckoo <[email protected]>
  • Loading branch information
mpvl committed Aug 22, 2020
1 parent 0b57e43 commit c6ade67
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
36 changes: 36 additions & 0 deletions cmd/cue/cmd/testdata/script/issue473.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cue eval --out json x.cue

-- cue.mod/module.cue --
module: "mod.com"
-- x.cue --
package x

#Guide: {
Terminals: [string]: #Terminal

Steps: [string]: #Step

#TerminalName: or([ for k, _ in Terminals {k}])

#Step: {
Terminal: #TerminalName
Cmd: string
}

#Terminal: {
Image: string
}
}

g: #Guide & {
Terminals: client: {
Image: "golang"
}

Steps: {
list: {
Terminal: "client"
Cmd: "ls"
}
}
}
1 change: 1 addition & 0 deletions internal/core/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ func (c *compiler) comprehension(x *ast.Comprehension) adt.Elem {
prev = next
}

// TODO: make x.Value an *ast.StructLit and this is redundant.
if y, ok := x.Value.(*ast.StructLit); !ok {
return c.errf(x.Value,
"comprehension value must be struct, found %T", y)
Expand Down
6 changes: 5 additions & 1 deletion internal/core/export/adt.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,11 @@ func (e *exporter) comprehension(y adt.Yielder) ast.Expr {
y = x.Dst

case *adt.ValueClause:
c.Value = e.expr(x.StructLit)
v := e.expr(x.StructLit)
if _, ok := v.(*ast.StructLit); !ok {
v = ast.NewStruct(ast.Embed(v))
}
c.Value = v
return c

default:
Expand Down

0 comments on commit c6ade67

Please sign in to comment.