Skip to content

Commit

Permalink
internal/core/adt: treat files as belonging to same struct
Browse files Browse the repository at this point in the history
The bug fix that treats {#A} as #A
(2ef72d8)
caused this to matter. This fixes that.

Also adds a previously forgotten test for embedding
definitions, related to this.

Change-Id: Ieae00bb2c4c3d537f21b8a5123741a7ffce9a7bd
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8229
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
  • Loading branch information
mpvl committed Jan 18, 2021
1 parent 826bd7b commit 82bda84
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
47 changes: 47 additions & 0 deletions cue/testdata/definitions/defembed.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- in.cue --
a: {
#A
}

a: c: 1

#A: b: 1
-- out/eval --
Errors:
a: field `c` not allowed:
./in.cue:1:4
./in.cue:2:5
./in.cue:5:4
./in.cue:7:5

Result:
(_|_){
// [eval]
a: (_|_){
// [eval]
b: (int){ 1 }
c: (_|_){
// [eval] a: field `c` not allowed:
// ./in.cue:1:4
// ./in.cue:2:5
// ./in.cue:5:4
// ./in.cue:7:5
}
}
#A: (#struct){
b: (int){ 1 }
}
}
-- out/compile --
--- in.cue
{
a: {
〈1;#A〉
}
a: {
c: 1
}
#A: {
b: 1
}
}
79 changes: 79 additions & 0 deletions cue/testdata/definitions/files.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Treat fields of different files as belonging to the same struct.
// This means that a closed embedding in one file should not restrict the
// fields of another.

-- in.cue --
#theme: {
color: string
ctermbg: string
}
dark: #theme & {
color: "dark"
ctermbg: "239"
}
light: #theme & {
color: "light"
ctermbg: "254"
}
#Config: {
console: dark | *light
}

-- box.cue --
#Config & {
console: dark
}

-- out/eval --
(#struct){
#theme: (#struct){
color: (string){ string }
ctermbg: (string){ string }
}
dark: (#struct){
color: (string){ "dark" }
ctermbg: (string){ "239" }
}
light: (#struct){
color: (string){ "light" }
ctermbg: (string){ "254" }
}
#Config: (#struct){
console: (struct){ |(*(#struct){
color: (string){ "light" }
ctermbg: (string){ "254" }
}, (#struct){
color: (string){ "dark" }
ctermbg: (string){ "239" }
}) }
}
console: (#struct){
color: (string){ "dark" }
ctermbg: (string){ "239" }
}
}
-- out/compile --
--- in.cue
{
#theme: {
color: string
ctermbg: string
}
dark: (〈0;#theme〉 & {
color: "dark"
ctermbg: "239"
})
light: (〈0;#theme〉 & {
color: "light"
ctermbg: "254"
})
#Config: {
console: (〈1;dark〉|*〈1;light〉)
}
}
--- box.cue
{
(〈0;#Config〉 & {
console: 〈1;dark〉
})
}
2 changes: 1 addition & 1 deletion internal/core/adt/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,7 @@ func (n *nodeContext) addStruct(

s.Init()

if s.HasEmbed {
if s.HasEmbed && !s.IsFile() {
closeInfo = closeInfo.SpawnGroup(nil)
}

Expand Down
5 changes: 5 additions & 0 deletions internal/core/adt/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ type StructLit struct {
// hasReferences bool
}

func (o *StructLit) IsFile() bool {
_, ok := o.Src.(*ast.File)
return ok
}

type FieldInfo struct {
Label Feature
Optional []Node
Expand Down

0 comments on commit 82bda84

Please sign in to comment.