tools/trim: fix defaults bug

handle defaults in both directions.

This really seems to be a sumbsumption bug more than anything,
but this makes trim more robust either way.

Fixes #759

Change-Id: Iaadaf2c628f0e9a3a8edb268abdeb1d991adde18
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8703
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
diff --git a/tools/trim/testdata/defaults.txtar b/tools/trim/testdata/defaults.txtar
index 130de1a..0b4dae8 100644
--- a/tools/trim/testdata/defaults.txtar
+++ b/tools/trim/testdata/defaults.txtar
@@ -1,7 +1,25 @@
 -- in.cue --
-foo: [string]: a: *1 | int
-foo: b: a: 1
+domToSub: {
+    foo: [string]: a: *1 | int
+    foo: b: a: 1
+}
+
+// Issue #759
+subToDom: {
+    #maybeString: {ip?: string}
+    something: ip: *"default" | string
+    something: #maybeString
+}
 -- out/trim --
 == in.cue
-foo: [string]: a: *1 | int
-foo: b: {}
+domToSub: {
+	foo: [string]: a: *1 | int
+	foo: b: {}
+}
+
+// Issue #759
+subToDom: {
+	#maybeString: {ip?: string}
+	something: ip: *"default" | string
+	something: #maybeString
+}
diff --git a/tools/trim/trim.go b/tools/trim/trim.go
index 7058259..02a807f 100644
--- a/tools/trim/trim.go
+++ b/tools/trim/trim.go
@@ -235,6 +235,11 @@
 		doms.Finalize(t.ctx)
 		doms = doms.Default()
 
+		// This should normally not be necessary, as subsume should catch this.
+		// But as we already take the default value for doms, it doesn't hurt to
+		// do it.
+		v = v.Default()
+
 		// This is not necessary, but seems like it may result in more
 		// user-friendly semantics.
 		if doms.IsErr() || v.IsErr() {