Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: fix optional handling in subsumption
Browse files Browse the repository at this point in the history
Note that it is overly conservative for bulk optionals.
At some point we should endeavor to relax this.

Change-Id: Ia6908619add410dfd3230e683c720f0171320fe3
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4241
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Dec 3, 2019
1 parent 8e72f0b commit 52b04d4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 4 additions & 1 deletion cue/subsume.go
Expand Up @@ -87,7 +87,7 @@ func (x *structLit) subsumesImpl(ctx *context, v value, mode subsumeMode) bool {
if o, ok := v.(*structLit); ok {
// TODO: consider what to do with templates. Perhaps we should always
// do subsumption on fully evaluated structs.
if len(x.comprehensions) > 0 { //|| x.template != nil {
if len(x.comprehensions) > 0 || x.optionals != nil {
return false
}
if x.emit != nil {
Expand All @@ -114,6 +114,9 @@ func (x *structLit) subsumesImpl(ctx *context, v value, mode subsumeMode) bool {
}
// For closed structs, all arcs in b must exist in a.
if x.closeStatus.shouldClose() {
if !o.closeStatus.shouldClose() {
return false
}
for _, b := range o.arcs {
a := x.lookup(ctx, b.feature)
if a.val() == nil {
Expand Down
10 changes: 9 additions & 1 deletion cue/subsume_test.go
Expand Up @@ -364,8 +364,14 @@ func TestSubsume(t *testing.T) {

// The one exception of the rule: there is no value of foo that can be
// added to b which would cause the unification of a and b to fail.
// So an optional field with a value of top is equivalent to not
// defining one at all.
420: {subsumes: true, in: `a: {foo?: _}, b: {}`},

430: {subsumes: false, in: `a: {[_]: 4}, b: {[_]: int}`},
// TODO: handle optionals.
431: {subsumes: false, in: `a: {[_]: int}, b: {[_]: 2}`},

// Lists
506: {subsumes: true, in: `a: [], b: [] `},
507: {subsumes: true, in: `a: [1], b: [1] `},
Expand All @@ -379,11 +385,13 @@ func TestSubsume(t *testing.T) {

// Closed structs.
600: {subsumes: false, in: `a: close({}), b: {a: 1}`},
601: {subsumes: true, in: `a: close({a: 1}), b: {a: 1}`},
601: {subsumes: false, in: `a: close({a: 1}), b: {a: 1}`},
602: {subsumes: false, in: `a: close({a: 1, b: 1}), b: {a: 1}`},
603: {subsumes: false, in: `a: {a: 1}, b: close({})`},
604: {subsumes: true, in: `a: {a: 1}, b: close({a: 1})`},
605: {subsumes: true, in: `a: {a: 1}, b: close({a: 1, b: 1})`},
606: {subsumes: true, in: `a: close({b?: 1}), b: close({b: 1})`},
607: {subsumes: false, in: `a: close({b: 1}), b: close({b?: 1})`},

// Definitions are not values.
610: {subsumes: false, in: `a: {a :: 1}, b: {a: 1}`},
Expand Down

0 comments on commit 52b04d4

Please sign in to comment.