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

Commit

Permalink
internal/core/export: fix definition wrapping
Browse files Browse the repository at this point in the history
- use hidden definitions (for real)
- also handle recurisve cases

Change-Id: Ie44a9e5f67db36cd8bad0a255ddb8053684a1723
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9486
Reviewed-by: CUE cueckoo <[email protected]>
Reviewed-by: Paul Jolly <[email protected]>
  • Loading branch information
mpvl committed Apr 23, 2021
1 parent c62b750 commit b937727
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cmd/cue/cmd/testdata/script/issue304.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ cmp stdout expect-stdout

-- expect-stdout --

#_def
#_def: {
_#def
_#def: {
x: int
body?: {
a: int
Expand Down
23 changes: 23 additions & 0 deletions internal/core/adt/closed.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,29 @@ package adt
// TODO(errors): return a dedicated ConflictError that can track original
// positions on demand.

func (v *Vertex) IsInOneOf(t SpanType) bool {
for _, s := range v.Structs {
if s.CloseInfo.IsInOneOf(t) {
return true
}
}
return false
}

// IsRecursivelyClosed returns true if this value is either a definition or unified
// with a definition.
func (v *Vertex) IsRecursivelyClosed() bool {
if v.IsInOneOf(DefinitionSpan) {
return true
}
for p := v; p != nil; p = p.Parent {
if p.Label.IsDef() {
return true
}
}
return false
}

type closeNodeType uint8

const (
Expand Down
9 changes: 5 additions & 4 deletions internal/core/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,19 @@ func (p *Profile) Def(r adt.Runtime, pkgID string, v *adt.Vertex) (*ast.File, er
e := newExporter(p, r, pkgID, v)
e.markUsedFeatures(v)

if v.Label.IsDef() {
isDef := v.IsRecursivelyClosed()
if isDef {
e.inDefinition++
}

expr := e.expr(v)

if v.Label.IsDef() {
if isDef {
e.inDefinition--
if s, ok := expr.(*ast.StructLit); ok {
expr = ast.NewStruct(
ast.Embed(ast.NewIdent("#_def")),
ast.NewIdent("#_def"), s,
ast.Embed(ast.NewIdent("_#def")),
ast.NewIdent("_#def"), s,
)
}
}
Expand Down
16 changes: 16 additions & 0 deletions internal/core/export/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func TestGenerated(t *testing.T) {
return n, nil
},
out: `#Provider: {ID: string, notConcrete: bool, a: int, b: a+1}, providers: {foo: {ID: "12345", notConcrete: bool, a: int, b: a+1}}`,
p: export.All,
}, {
// Issue #882
in: func(r *adt.OpContext) (adt.Expr, error) {
Expand All @@ -173,6 +174,21 @@ func TestGenerated(t *testing.T) {
},
out: `#One: {version: string}, ones: {[string]: #One}`,
p: export.All,
}, {
// Indicate closedness in an element that is closed and misses parent
// context.
// Issue #882
in: func(r *adt.OpContext) (adt.Expr, error) {
v := ctx.CompileString(`
#A: b: c: string
`)
v = v.LookupPath(cue.ParsePath("#A.b"))

_, n := value.ToInternal(v)
return n, nil
},
out: `_#def, _#def: {c: string}`,
p: export.All,
}}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
Expand Down

0 comments on commit b937727

Please sign in to comment.