Skip to content

Commit

Permalink
internal/core/runtime: assign first label to _.
Browse files Browse the repository at this point in the history
Change-Id: I55a3291e8aecf93bb9465f2cccc52ed7121520a9
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7841
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Dec 2, 2020
1 parent e77ccb1 commit 63594ef
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 21 deletions.
4 changes: 2 additions & 2 deletions cue/testdata/export/004.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ _bar: int
{
{
$type: 3
"_": int
_: int
"_foo": int
_bar: int
}
}
-- out/eval --
(struct){
$type: (int){ 3 }
"_": (int){ int }
_: (int){ int }
"_foo": (int){ int }
_bar: (int){ int }
}
4 changes: 2 additions & 2 deletions cue/testdata/references/index.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ indexDoesNotDistribute: index out of range [0] with length 0:
./in.cue:10:41
doesNotDistributeType.a: invalid list index "3" (type string):
./in.cue:13:24
stringIndexToList: invalid list index _ (type string):
stringIndexToList: invalid list index "" (type string):
./in.cue:16:24
indexOutOfBounds2: invalid operand 2 (found int, want list or struct):
./in.cue:17:20
Expand Down Expand Up @@ -77,7 +77,7 @@ Result:
}
}
stringIndexToList: (_|_){
// [eval] stringIndexToList: invalid list index _ (type string):
// [eval] stringIndexToList: invalid list index "" (type string):
// ./in.cue:16:24
}
indexOutOfBounds2: (_|_){
Expand Down
13 changes: 13 additions & 0 deletions cue/testdata/references/labels.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ c1: C="foo\(bar)": [D=string]: {
}
c1: foo3: x: _

emptyLabel: {
"": 1
a: emptyLabel[""]
}

// TODO: support. Also not yet supported in old implementation.
// c10: {
// C=[string]: {
Expand Down Expand Up @@ -95,6 +100,10 @@ c1: foo3: x: _
}
}
}
emptyLabel: (struct){
"": (int){ 1 }
a: (int){ 1 }
}
}
-- out/compile --
--- in.cue
Expand Down Expand Up @@ -175,4 +184,8 @@ c1: foo3: x: _
x: _
}
}
emptyLabel: {
"": 1
a: 〈1;emptyLabel〉[""]
}
}
4 changes: 2 additions & 2 deletions cue/testdata/resolve/016_index.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ c: invalid list index "3" (type string):
./in.cue:3:20
d: index out of range [0] with length 0:
./in.cue:4:16
e1: invalid list index _ (type string):
e1: invalid list index "" (type string):
./in.cue:6:9
e2: invalid operand 2 (found int, want list or struct):
./in.cue:7:5
Expand Down Expand Up @@ -132,7 +132,7 @@ Result:
l: (#list){
}
e1: (_|_){
// [eval] e1: invalid list index _ (type string):
// [eval] e1: invalid list index "" (type string):
// ./in.cue:6:9
}
e2: (_|_){
Expand Down
8 changes: 4 additions & 4 deletions internal/core/debug/compact.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (w *compactPrinter) node(n adt.Node) {
w.label(x.ImportPath)

case *adt.LetReference:
w.label(x.Label)
w.ident(x.Label)

case *adt.SelectorExpr:
w.node(x.X)
Expand Down Expand Up @@ -300,9 +300,9 @@ func (w *compactPrinter) node(n adt.Node) {

case *adt.ForClause:
w.string("for ")
w.label(x.Key)
w.ident(x.Key)
w.string(", ")
w.label(x.Value)
w.ident(x.Value)
w.string(" in ")
w.node(x.Src)
w.string(" ")
Expand All @@ -316,7 +316,7 @@ func (w *compactPrinter) node(n adt.Node) {

case *adt.LetClause:
w.string("let ")
w.label(x.Label)
w.ident(x.Label)
w.string(" = ")
w.node(x.Expr)
w.string(" ")
Expand Down
12 changes: 8 additions & 4 deletions internal/core/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func (w *printer) label(f adt.Feature) {
w.string(w.labelString(f))
}

func (w *printer) ident(f adt.Feature) {
w.string(f.IdentString(w.index))
}

// TODO: fold into label once :: is no longer supported.
func (w *printer) labelString(f adt.Feature) string {
if f.IsHidden() {
Expand Down Expand Up @@ -378,7 +382,7 @@ func (w *printer) node(n adt.Node) {
w.string(openTuple)
w.string(strconv.Itoa(int(x.UpCount)))
w.string(";let ")
w.label(x.Label)
w.ident(x.Label)
w.string(closeTuple)

case *adt.SelectorExpr:
Expand Down Expand Up @@ -490,9 +494,9 @@ func (w *printer) node(n adt.Node) {

case *adt.ForClause:
w.string("for ")
w.label(x.Key)
w.ident(x.Key)
w.string(", ")
w.label(x.Value)
w.ident(x.Value)
w.string(" in ")
w.node(x.Src)
w.string(" ")
Expand All @@ -506,7 +510,7 @@ func (w *printer) node(n adt.Node) {

case *adt.LetClause:
w.string("let ")
w.label(x.Label)
w.ident(x.Label)
w.string(" = ")
w.node(x.Expr)
w.string(" ")
Expand Down
5 changes: 1 addition & 4 deletions internal/core/export/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ import (
)

func (e *exporter) stringLabel(f adt.Feature) ast.Label {
if f == 0 {
return ast.NewIdent("_")
}
x := f.Index()
switch f.Typ() {
case adt.IntLabel:
Expand All @@ -38,7 +35,7 @@ func (e *exporter) stringLabel(f adt.Feature) ast.Label {

case adt.StringLabel:
s := e.ctx.IndexToString(int64(x))
if !ast.IsValidIdent(s) {
if f == 0 || !ast.IsValidIdent(s) {
return ast.NewLit(token.STRING, literal.Label.Quote(s))
}
fallthrough
Expand Down
3 changes: 2 additions & 1 deletion internal/core/runtime/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ var (
)

func init() {
getKey("")
// Ensure label 0 is assigned to _.
getKey("_")
}

func getKey(s string) int64 {
Expand Down
4 changes: 2 additions & 2 deletions internal/filetypes/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 63594ef

Please sign in to comment.