Skip to content

Commit

Permalink
internal/core/adt: allow required dynamic fields
Browse files Browse the repository at this point in the history
Fixes #2107

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I2d6bf186ddc787fdc7aff3cfb84f1bf84563b5ec
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/551207
Reviewed-by: Aram Hăvărneanu <aram@cue.works>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
mpvl committed Mar 23, 2023
1 parent 21f494d commit c3138e3
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 27 deletions.
4 changes: 3 additions & 1 deletion cue/format/testdata/simplify.golden
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ quux: 5
// Issue #294
"\("x")": "x"

(x): "foo"
(x): "foo"
(x)?: "foo"
(x)!: "foo"

a: {
foo: 2
Expand Down
2 changes: 2 additions & 0 deletions cue/format/testdata/simplify.input
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ a:
"\("x")": "x"

(x): "foo"
(x)?: "foo"
(x)!: "foo"

a: {
[string]: _
Expand Down
36 changes: 30 additions & 6 deletions cue/testdata/eval/dynamic_field.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,23 @@ noCycleError: {
foo: baz: {}
}

-- constraints.cue --
constraints: {
t1: "foo"
t2: "bar"
(t1)?: (t2)!: 3
}

-- out/eval/stats --
Leaks: 2
Freed: 54
Reused: 47
Freed: 59
Reused: 52
Allocs: 9
Retain: 9
Retain: 11

Unifications: 56
Conjuncts: 73
Disjuncts: 61
Unifications: 61
Conjuncts: 79
Disjuncts: 66
-- out/eval --
Errors:
invalid interpolation: conflicting values 2 and 1:
Expand All @@ -83,6 +90,13 @@ invalid interpolation: conflicting values 2 and 1:
Result:
(_|_){
// [eval]
constraints: (struct){
t1: (string){ "foo" }
t2: (string){ "bar" }
foo?: (struct){
bar!: (int){ 3 }
}
}
a: (string){ "foo" }
e: (int){ 2 }
b: (string){ "bar" }
Expand Down Expand Up @@ -178,6 +192,16 @@ Result:
}
}
-- out/compile --
--- constraints.cue
{
constraints: {
t1: "foo"
t2: "bar"
〈0;t1〉?: {
〈1;t2〉!: 3
}
}
}
--- in.cue
{
a: "foo"
Expand Down
2 changes: 1 addition & 1 deletion cue/testdata/resolve/009_optional_field_unification.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ Disjuncts: 11
foo?: (string){ "bar" }
}
g1: (int){ 1 }
g2: (int){ 2 }
g2?: (int){ 2 }
}
2 changes: 2 additions & 0 deletions internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,8 @@ func (v *Vertex) hasConjunct(c Conjunct) (added bool) {
case *BulkOptionalField, *Ellipsis:
case *Field:
v.UpdateArcType(f.ArcType)
case *DynamicField:
v.UpdateArcType(f.ArcType)
default:
v.ArcType = ArcMember
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/adt/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,7 @@ func (n *nodeContext) injectDynamic() (progress bool) {
if f.IsInt() {
n.addErr(ctx.NewPosf(pos(d.field.Key), "integer fields not supported"))
}
n.insertField(f, ArcMember, MakeConjunct(d.env, d.field, d.id))
n.insertField(f, d.field.ArcType, MakeConjunct(d.env, d.field, d.id))
}

progress = k < len(n.dynamicFields)
Expand Down
10 changes: 4 additions & 6 deletions internal/core/adt/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,11 @@ func (x *Ellipsis) Source() ast.Node {
// "\(expr)": expr
// (expr): expr
type DynamicField struct {
Src *ast.Field
Key Expr
Value Expr
}
Src *ast.Field

func (x *DynamicField) IsOptional() bool {
return x.Src.Optional != token.NoPos
ArcType ArcType
Key Expr
Value Expr
}

func (x *DynamicField) Source() ast.Node {
Expand Down
18 changes: 12 additions & 6 deletions internal/core/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,17 +626,23 @@ func (c *compiler) decl(d ast.Decl) adt.Decl {
}

case *ast.ParenExpr:
t, _ := internal.ConstraintToken(x)

return &adt.DynamicField{
Src: x,
Key: c.expr(l),
Value: value,
Src: x,
Key: c.expr(l),
ArcType: adt.ConstraintFromToken(t),
Value: value,
}

case *ast.Interpolation:
t, _ := internal.ConstraintToken(x)

return &adt.DynamicField{
Src: x,
Key: c.expr(l),
Value: value,
Src: x,
Key: c.expr(l),
ArcType: adt.ConstraintFromToken(t),
Value: value,
}
}

Expand Down
4 changes: 1 addition & 3 deletions internal/core/debug/compact.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ func (w *compactPrinter) node(n adt.Node) {

case *adt.DynamicField:
w.node(x.Key)
if x.IsOptional() {
w.string("?")
}
w.string(x.ArcType.Suffix())
w.string(":")
w.node(x.Value)

Expand Down
4 changes: 1 addition & 3 deletions internal/core/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,7 @@ func (w *printer) node(n adt.Node) {

case *adt.DynamicField:
w.node(x.Key)
if x.IsOptional() {
w.string("?")
}
w.string(x.ArcType.Suffix())
w.string(": ")
w.node(x.Value)

Expand Down
1 change: 1 addition & 0 deletions internal/core/export/adt.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ func (e *exporter) decl(env *adt.Environment, d adt.Decl) ast.Decl {
srcKey := x.Key

f := &ast.Field{}
internal.SetConstraint(f, x.ArcType.Token())

v, _ := e.ctx.Evaluate(env, x.Key)

Expand Down
25 changes: 25 additions & 0 deletions internal/core/export/testdata/main/dynamic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ a: {
labelZ: Z
}
E=["cue"]: name: E | null

q: "q"
q1: "q1"
r: "r"
r1: "r1"
("s-"+q)?: 1
("s-"+r)!: 2
("s-"+q1)?: (r1)!: 3
-- out/definition --
x: string
X=(x): 4
Expand All @@ -22,15 +30,32 @@ a: {
E=["cue"]: {
name: E | null
}
q: "q"
q1: "q1"
r: "r"
r1: "r1"
"s-q"?: 1
"s-r"!: 2
"s-q1"?: {
(r1)!: 3
}
-- out/doc --
[]
[x]
[a]
[a labelX]
[a labelY]
[a labelZ]
[q]
[q1]
[r]
[r1]
[_]
[y]
["s-q"]
["s-r"]
["s-q1"]
["s-q1" r1]
-- out/value --
== Simplified
_|_ // invalid non-ground value string (must be concrete string) (and 1 more errors)
Expand Down

0 comments on commit c3138e3

Please sign in to comment.