Skip to content

Commit

Permalink
internal/core/adt: propagate closedness up
Browse files Browse the repository at this point in the history
Whether a struct is a definition should be propagated up. Adds tests and logic.

As a consequence, some nodes that were marked as
embedding only before, are now marked as a
embedding as well as definition.

Issue #2884

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I4d97a4087734719471588f0b39561fbbca8f53b4
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1174003
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
mpvl committed Mar 7, 2024
1 parent 37c0260 commit 9d951f6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 15 deletions.
11 changes: 7 additions & 4 deletions internal/core/adt/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,17 +448,20 @@ func (c *closeContext) decDependent(ctx *OpContext, kind depKind, dependant *clo

p := c.parent

if c.isDef {
c.isClosed = true
if p != nil {
p.isDef = true
}
}

for _, a := range c.arcs {
cc := a.cc
cc.decDependent(ctx, a.kind, c) // REF(arcs)
}

c.finalizePattern()

if c.isDef {
c.isClosed = true
}

if p == nil {
// Root pattern, set allowed patterns.
if pcs := v.PatternConstraints; pcs != nil {
Expand Down
69 changes: 58 additions & 11 deletions internal/core/adt/fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func TestCloseContext(t *testing.T) {
}
c: {
"bar"
[e]{
[ed]{
[d]{1}
[d]{2}
}
Expand Down Expand Up @@ -349,7 +349,7 @@ func TestCloseContext(t *testing.T) {
arcs: `b: {
4
[d]{
[e]{
[ed]{
[d]{3}
}
4
Expand All @@ -376,7 +376,7 @@ func TestCloseContext(t *testing.T) {
arcs: `b: {
[e]{
4
[e]{
[ed]{
[d]{4}
}
}
Expand Down Expand Up @@ -432,17 +432,17 @@ func TestCloseContext(t *testing.T) {
[d]{2}
3
}
[e]{
[ed]{
[d]{4}
}
5
}
[d]{
[e]{
[ed]{
[d]{7}
8
}
[e]{
[ed]{
[d]{10}
}
11
Expand All @@ -454,23 +454,23 @@ func TestCloseContext(t *testing.T) {
[e]{
[d]{
"bar"
[e]{
[ed]{
[d]{1}
[d]{2}
3
}
[e]{
[ed]{
[d]{3}
[d]{4}
}
5
}
[d]{
[e]{
[ed]{
[d]{7}
8
}
[e]{
[ed]{
[d]{10}
}
"bar"
Expand Down Expand Up @@ -532,6 +532,53 @@ func TestCloseContext(t *testing.T) {
}
a: {1}`,
err: `a: field not allowed`,
}, {
// a: {#A}
// a: c: 1
// #A: b: 1
name: "def embed",
run: func(x *adt.FieldTester) {
x.Run(
x.Group(x.EmbedDef(x.Field("b", "foo"))),
x.Field("c", "bar"),
)
},
arcs: `
b: {
[]{
[e]{
[d]{"foo"}
}
}
}
c: {"bar"}`,
err: `c: field not allowed`,
}, {
// a: {#A}
// a: c: 1
// #A: b: 1
name: "def embed",
run: func(x *adt.FieldTester) {
x.Run(
x.Group(x.EmbedDef(x.Field("b", "foo")),
x.Field("c", "foo"),
),
x.Field("d", "bar"),
)
},
arcs: `
b: {
[]{
[e]{
[d]{"foo"}
}
}
}
c: {
[d]{"foo"}
}
d: {"bar"}`,
err: `d: field not allowed`,
}, {
// This test is for debugging and can be changed.
name: "X",
Expand All @@ -547,7 +594,7 @@ func TestCloseContext(t *testing.T) {
// TODO: could probably be b: {"foo" 4}. See TODO(constraintNode).
arcs: `b: {
"foo"
[e]{
[ed]{
[d]{4}
}
}`,
Expand Down

0 comments on commit 9d951f6

Please sign in to comment.