Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
internal/core/adt: apply Default also to list
Browse files Browse the repository at this point in the history
A closed list with current elements is the default
of an open list.

Change-Id: I2ea041c979219267d486c0c6fbec5ec9b192f771
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6653
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
  • Loading branch information
mpvl committed Jul 23, 2020
1 parent 88b4b1f commit f159888
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
3 changes: 2 additions & 1 deletion internal/core/adt/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,8 @@ func (c *OpContext) lookup(x *Vertex, pos token.Pos, l Feature) *Vertex {
}
// TODO: if the struct was a literal struct, we can also treat it as
// closed and make this a permanent error.
c.addErrf(code, pos, "undefined field %s", l.SelectorString(c.Runtime))
label := l.SelectorString(c.Runtime)
c.addErrf(code, pos, "undefined field %s", label)
}
return a
}
Expand Down
56 changes: 33 additions & 23 deletions internal/core/adt/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,45 @@ func (d *Disjunction) Default() Value {

// Default returns the default value or itself if there is no default.
func (v *Vertex) Default() *Vertex {
d, ok := v.Value.(*Disjunction)
if !ok {
switch d := v.Value.(type) {
default:
return v
}

var w *Vertex
case *Disjunction:
var w *Vertex

switch d.NumDefaults {
case 0:
return v
case 1:
w = d.Values[0]
default:
x := *v
x.Value = &Disjunction{
Src: d.Src,
Values: d.Values[:d.NumDefaults],
NumDefaults: 0,
switch d.NumDefaults {
case 0:
return v
case 1:
w = d.Values[0]
default:
x := *v
x.Value = &Disjunction{
Src: d.Src,
Values: d.Values[:d.NumDefaults],
NumDefaults: 0,
}
w = &x
}
w = &x
}

w.Conjuncts = nil
for _, c := range v.Conjuncts {
// TODO: preserve field information.
expr, _ := stripNonDefaults(c.Expr())
w.AddConjunct(MakeConjunct(c.Env, expr))
w.Conjuncts = nil
for _, c := range v.Conjuncts {
// TODO: preserve field information.
expr, _ := stripNonDefaults(c.Expr())
w.Conjuncts = append(w.Conjuncts, MakeConjunct(c.Env, expr))
}
return w

case *ListMarker:
m := *d
m.IsOpen = false

w := *v
w.Closed = nil
w.Value = &m
return &w
}
return w
}

// TODO: this should go: record preexpanded disjunctions in Vertex.
Expand Down

0 comments on commit f159888

Please sign in to comment.