Skip to content

Commit

Permalink
core/adt: Combine Vertex.Structs when inserting Conjunct for evalv3
Browse files Browse the repository at this point in the history
Two issues:

1. The old evalv2 code, in nodeContext.addValueConjunct, does combine
   Vertex.Structs if the value being merged in is a Vertex. But the new
   evalv3 code, in nodeContext.insertValueConjunct, did not do that. Add
   the necessary code.

2. Even with (1), Vertex Structs from core/convert were being trimmed
   out because the StructLits were not initialised. So make sure whenever
   we create an adt.StructLit, in convert, we Initialize it.

Outcome:

Vertices that are returned from builtin functions, such as time.Split,
which have StructInfos/StructLits which themselves contain field
ordering information, are no longer lost. Consequently, toposort orders
such vertex fields correctly, matching the field order in the Go struct
declaration.

Fixes #3554. Better than before.

Signed-off-by: Matthew Sackman <[email protected]>
Change-Id: I5bb7ad09c328495758ed454fd539cf20fd550ef8
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1208079
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Marcel van Lohuizen <[email protected]>
  • Loading branch information
cuematthew committed Jan 31, 2025
1 parent 6a125a9 commit 2d65404
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions internal/core/adt/conjunct.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ func (n *nodeContext) insertValueConjunct(env *Environment, v Value, id CloseInf
c := MakeConjunct(nil, a, id)
n.insertArc(a.Label, a.ArcType, c, id, true)
}
n.node.Structs = append(n.node.Structs, x.Structs...)

case Value:
n.insertValueConjunct(env, v, id)
Expand Down
2 changes: 2 additions & 0 deletions internal/core/convert/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ func (c *goConverter) convertRec(nilIsTop bool, x interface{}) (result adt.Value

case reflect.Struct:
sl := &adt.StructLit{Src: c.setNextPos(ast.NewStruct())}
sl.Init(c.ctx)
v := &adt.Vertex{}

t := value.Type()
Expand Down Expand Up @@ -476,6 +477,7 @@ func (c *goConverter) convertRec(nilIsTop bool, x interface{}) (result adt.Value

case reflect.Map:
obj := &adt.StructLit{Src: c.setNextPos(ast.NewStruct())}
obj.Init(c.ctx)
v := &adt.Vertex{}

t := value.Type()
Expand Down
6 changes: 3 additions & 3 deletions pkg/time/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ parse: {
}
split: {
t1: {
year: 2017
month: 7
day: 14
hour: 2
minute: 40
month: 7
nanosecond: 123456
second: 0
year: 2017
nanosecond: 123456
}
}

0 comments on commit 2d65404

Please sign in to comment.