Skip to content

Commit

Permalink
internal/core/export: fix dropping of optional field
Browse files Browse the repository at this point in the history
Lookup dereferences all vertex indirections, including those
for structure-sharing. If indirection happens too
early it may lead to incorrect arc types. This changes
postpones the dereference.

Issue #3060

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: Iae18d29d4d32b2acc762df68d63f46e99e36da56
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194402
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
mpvl committed May 7, 2024
1 parent c023c4a commit a080c35
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 101 deletions.
108 changes: 8 additions & 100 deletions internal/core/export/testdata/main/alias.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ was known to compile and is known to be correct.
simple: {
"a-b": 4
foo: 4
bar: 5
"a-c": 5
}
cross: {
Expand Down Expand Up @@ -271,7 +270,7 @@ was known to compile and is known to be correct.
simple: {
"a-b": 4
foo: 4
bar: 5
bar?: 5
"a-c": 5
}
cross: {
Expand Down Expand Up @@ -340,7 +339,6 @@ was known to compile and is known to be correct.
simple: {
"a-b": 4
foo: 4
bar: 5
"a-c": 5
}
cross: {
Expand Down Expand Up @@ -392,7 +390,7 @@ was known to compile and is known to be correct.
simple: {
"a-b": 4
foo: 4
bar: 5
bar?: 5
"a-c": 5
}
cross: {
Expand Down Expand Up @@ -461,7 +459,7 @@ was known to compile and is known to be correct.
simple: {
"a-b": 4
foo: 4
bar: 5
bar?: 5
"a-c": 5
}
cross: {
Expand Down Expand Up @@ -513,107 +511,17 @@ was known to compile and is known to be correct.
diff old new
--- old
+++ new
@@ -4,6 +4,7 @@
simple: {
"a-b": 4
foo: 4
+ bar: 5
"a-c": 5
@@ -164,7 +164,7 @@
}
cross: {
@@ -68,7 +69,7 @@
simple: {
"a-b": 4
foo: 4
- bar?: 5
+ bar: 5
"a-c": 5
}
cross: {
@@ -137,34 +138,35 @@
simple: {
"a-b": 4
foo: 4
- "a-c": 5
- }
- cross: {
- baz: 3
- "d-2": {}
- }
- }
- valueAlias: {
- merge: {
- value: {
- b: 2
- v: {
- X: 3
- }
- }
- }
- selfRef: {
- struct: {
- a: {
- b: {
- b: 2
- }
- }
- }
- }
- selfRefValue: {
- struct: {
selfRefValue: {
struct: {
- a: _|_ // cycle error
+ bar: 5
+ "a-c": 5
+ }
+ cross: {
+ baz: 3
+ "d-2": {}
+ }
+ }
+ valueAlias: {
+ merge: {
+ value: {
+ b: 2
+ v: {
+ X: 3
+ }
+ }
+ }
+ selfRef: {
+ struct: {
+ a: {
+ b: {
+ b: 2
+ }
+ }
+ }
+ }
+ selfRefValue: {
+ struct: {
+ a: _|_ // valueAlias.selfRefValue.struct.a: incomplete list: _ (and 3 more errors)
}
pattern: {
a: {}
@@ -188,7 +190,7 @@
simple: {
"a-b": 4
foo: 4
- bar?: 5
+ bar: 5
"a-c": 5
}
cross: {
@@ -257,7 +259,7 @@
simple: {
"a-b": 4
foo: 4
- bar?: 5
+ bar: 5
"a-c": 5
}
cross: {
-- diff/value/todo/p0 --
Dropped optional field marker.
-- diff/value/explanation --
Improved error message.
Reorderings?
-- out/value --
== Simplified
Expand Down
3 changes: 2 additions & 1 deletion internal/core/export/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func (e *exporter) structComposite(v *adt.Vertex, attrs []*ast.Attribute) ast.Ex
e.inDefinition++
}

arc := v.Lookup(label)
arc := v.LookupRaw(label)
if arc == nil {
continue
}
Expand All @@ -446,6 +446,7 @@ func (e *exporter) structComposite(v *adt.Vertex, attrs []*ast.Attribute) ast.Ex

internal.SetConstraint(f, arc.ArcType.Token())

arc = arc.DerefValue()
f.Value = e.vertex(arc)

if label.IsDef() {
Expand Down
5 changes: 5 additions & 0 deletions internal/core/export/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import (

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/errors"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/compile"
"cuelang.org/go/internal/core/eval"
"cuelang.org/go/internal/core/export"
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/cuedebug"
"cuelang.org/go/internal/cuetdtest"
"cuelang.org/go/internal/cuetxtar"
"golang.org/x/tools/txtar"
Expand Down Expand Up @@ -96,6 +98,9 @@ func TestValueX(t *testing.T) {
a := cuetxtar.Load(archive, t.TempDir())

r := runtime.New()
(*runtime.Runtime)(r).SetVersion(internal.DevVersion)
(*runtime.Runtime)(r).SetDebugOptions(&cuedebug.Config{Sharing: true})

v, errs := compile.Files(nil, r, "", a[0].Files...)
if errs != nil {
t.Fatal(errs)
Expand Down

0 comments on commit a080c35

Please sign in to comment.