Skip to content

Commit 37293f9

Browse files
committed
internal/core/adt: add methods for concreteness
This is the first step to make marker value a different type as to let the compiler prevent them from being passed around as values. Issue #598 Change-Id: Iaf58635bd2ca4591e6ce4589f237076ff8359ede Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7764 Reviewed-by: Marcel van Lohuizen <[email protected]> Reviewed-by: CUE cueckoo <[email protected]>
1 parent 19f6f3f commit 37293f9

File tree

6 files changed

+37
-35
lines changed

6 files changed

+37
-35
lines changed

cue/context.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,7 @@ func (v Value) eval(ctx *context) adt.Value {
7676
panic("undefined value")
7777
}
7878
x := ctx.manifest(v.v)
79-
switch x.Kind() {
80-
case adt.StructKind, adt.ListKind:
81-
return x
82-
default:
83-
return x.Value
84-
}
79+
return x.ActualValue()
8580
}
8681

8782
// func (v Value) evalFull(u value) (Value, adt.Value) {

cue/load/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,12 +536,12 @@ func (c Config) complete() (cfg *Config, err error) {
536536
v.Finalize(ctx)
537537
prefix := v.Lookup(ctx.StringLabel("module"))
538538
if prefix != nil {
539-
name := ctx.StringValue(prefix.Value)
539+
name := ctx.StringValue(prefix.ActualValue())
540540
if err := ctx.Err(); err != nil {
541541
return &c, err.Err
542542
}
543543
pos := token.NoPos
544-
src := prefix.Value.Source()
544+
src := prefix.ActualValue().Source()
545545
if src != nil {
546546
pos = src.Pos()
547547
}

cue/types.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ func (v Value) Kind() Kind {
844844
return BottomKind
845845
}
846846
c := v.v.Value
847-
if !adt.IsConcrete(c) {
847+
if !v.v.IsConcrete() {
848848
return BottomKind
849849
}
850850
if v.IncompleteKind() == adt.ListKind && !v.IsClosed() {
@@ -2161,25 +2161,15 @@ func (v Value) Expr() (Op, []Value) {
21612161
var env *adt.Environment
21622162

21632163
if v.v.IsData() {
2164-
switch x := v.v.Value.(type) {
2165-
case *adt.ListMarker, *adt.StructMarker:
2166-
expr = v.v
2167-
default:
2168-
expr = x
2169-
}
2164+
expr = v.v.ActualValue()
21702165

21712166
} else {
21722167
switch len(v.v.Conjuncts) {
21732168
case 0:
21742169
if v.v.Value == nil {
21752170
return NoOp, []Value{makeValue(v.idx, v.v)}
21762171
}
2177-
switch x := v.v.Value.(type) {
2178-
case *adt.ListMarker, *adt.StructMarker:
2179-
expr = v.v
2180-
default:
2181-
expr = x
2182-
}
2172+
expr = v.v.ActualValue()
21832173

21842174
case 1:
21852175
// the default case, processed below.

internal/core/adt/adt.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,16 @@ func Pos(n Node) token.Pos {
136136

137137
func (x *Vertex) Concreteness() Concreteness {
138138
// Depends on concreteness of value.
139-
if x.Value == nil {
139+
switch v := x.Value.(type) {
140+
case nil:
140141
return Concrete // Should be indetermined.
142+
143+
case Value:
144+
return v.Concreteness()
145+
146+
default: // *StructMarker, *ListMarker:
147+
return Concrete
141148
}
142-
return x.Value.Concreteness()
143149
}
144150

145151
func (x *NodeLink) Concreteness() Concreteness { return Concrete }

internal/core/adt/composite.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,25 @@ func (v *Vertex) UpdateStatus(s VertexStatus) {
255255
v.status = s
256256
}
257257

258+
// ActualValue returns the Value of v without definitions if it is a scalar
259+
// or itself otherwise.
260+
func (v *Vertex) ActualValue() Value {
261+
// TODO: rename to Value.
262+
switch x := v.Value.(type) {
263+
// XXX: remove
264+
case *StructMarker, *ListMarker:
265+
return v
266+
case Value:
267+
return x
268+
default:
269+
return v
270+
}
271+
}
272+
273+
func (x *Vertex) IsConcrete() bool {
274+
return x.Concreteness() <= Concrete
275+
}
276+
258277
// IsData reports whether v should be interpreted in data mode. In other words,
259278
// it tells whether optional field matching and non-regular fields, like
260279
// definitions and hidden fields, should be ignored.
@@ -385,12 +404,7 @@ func Unwrap(v Value) Value {
385404
if !ok {
386405
return v
387406
}
388-
switch x.Value.(type) {
389-
case *StructMarker, *ListMarker:
390-
return v
391-
default:
392-
return x.Value
393-
}
407+
return x.ActualValue()
394408
}
395409

396410
// Acceptor is a single interface that reports whether feature f is a valid

internal/core/adt/context.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -449,17 +449,14 @@ func (c *OpContext) getDefault(v Value, scalar bool) (result Value, ok bool) {
449449
case *Disjunction:
450450
d = t
451451

452-
case *StructMarker, *ListMarker:
453-
return v, true
454-
455452
case *Vertex:
456453
return c.getDefault(t, scalar)
457454

458455
default:
459456
if !scalar {
460-
return v, true
457+
return x, true
461458
}
462-
return t, true
459+
return x.ActualValue(), true
463460
}
464461

465462
case *Disjunction:
@@ -551,7 +548,7 @@ func (c *OpContext) evalState(v Expr, state VertexStatus) (result Value) {
551548
}
552549
if isIncomplete(arc) {
553550
if arc != nil {
554-
return arc.Value
551+
return arc.ActualValue() // *Bottom
555552
}
556553
return nil
557554
}

0 commit comments

Comments
 (0)