Skip to content

Commit 3d3f721

Browse files
committed
internal/core/adt: fix matching of "_" label
The introduction of the wildcards in the API introduced a regression that caused the string label "_" (which has the feature label 0), to be incorrectly singled out. The logic has now changed to reflect this. Note that without the changed logic, TestAllow in cue/types_test.go will fail. This also fixes printing of Selectors. Again, here "_" was incorrectly singled out and would convert a string label to a hidden label. This only seems to affect debug output. Fixes #1454 Signed-off-by: Marcel van Lohuizen <mpvl@golang.org> Change-Id: If08166862a677738af47937710774f6af5448953 Signed-off-by: Marcel van Lohuizen <mpvl@golang.org> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/530860 Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
1 parent 60d6503 commit 3d3f721

File tree

4 files changed

+125
-9
lines changed

4 files changed

+125
-9
lines changed

cue/testdata/export/004.txtar

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ _bar: int
1717
{
1818
{
1919
$type: 3
20-
_: int
20+
"_": int
2121
"_foo": int
2222
_bar: int
2323
}
2424
}
2525
-- out/eval --
2626
(struct){
2727
$type: (int){ 3 }
28-
_: (int){ int }
28+
"_": (int){ int }
2929
"_foo": (int){ int }
3030
_bar: (int){ int }
3131
}

cue/testdata/references/labels.txtar

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,38 @@ emptyLabel: {
5050
a: emptyLabel[""]
5151
}
5252

53+
underscore: a: {
54+
// Issue #1454
55+
foo: #Foo
56+
foo: "_": "bar"
57+
#Foo: [=~""]: string
58+
}
59+
60+
underscore: b: {
61+
foo: #Foo
62+
// TODO: we should probably disallow the hidden label `_` to avoid confusion.
63+
foo: _: "bar"
64+
#Foo: [=~""]: string
65+
}
66+
67+
underscore: c: {
68+
foo: "_": "any"
69+
foo: [=~""]: string
70+
}
71+
72+
underscore: d: {
73+
bar: "_": "any"
74+
#bar: [string]: string
75+
bar: #bar
76+
}
77+
78+
underscore: e: {
79+
baz: "_h": "any"
80+
#baz: [=~"_"]: string
81+
baz: #baz
82+
}
83+
84+
5385
// TODO: support. Also not yet supported in old implementation.
5486
// c10: {
5587
// C=[string]: {
@@ -104,6 +136,41 @@ emptyLabel: {
104136
"": (int){ 1 }
105137
a: (int){ 1 }
106138
}
139+
underscore: (struct){
140+
a: (struct){
141+
foo: (#struct){
142+
"_": (string){ "bar" }
143+
}
144+
#Foo: (#struct){
145+
}
146+
}
147+
b: (struct){
148+
foo: (#struct){
149+
_: (string){ "bar" }
150+
}
151+
#Foo: (#struct){
152+
}
153+
}
154+
c: (struct){
155+
foo: (struct){
156+
"_": (string){ "any" }
157+
}
158+
}
159+
d: (struct){
160+
bar: (#struct){
161+
"_": (string){ "any" }
162+
}
163+
#bar: (#struct){
164+
}
165+
}
166+
e: (struct){
167+
baz: (#struct){
168+
"_h": (string){ "any" }
169+
}
170+
#baz: (#struct){
171+
}
172+
}
173+
}
107174
}
108175
-- out/compile --
109176
--- in.cue
@@ -188,4 +255,58 @@ emptyLabel: {
188255
"": 1
189256
a: 〈1;emptyLabel〉[""]
190257
}
258+
underscore: {
259+
a: {
260+
foo: 〈0;#Foo〉
261+
foo: {
262+
"_": "bar"
263+
}
264+
#Foo: {
265+
[=~""]: string
266+
}
267+
}
268+
}
269+
underscore: {
270+
b: {
271+
foo: 〈0;#Foo〉
272+
foo: {
273+
_: "bar"
274+
}
275+
#Foo: {
276+
[=~""]: string
277+
}
278+
}
279+
}
280+
underscore: {
281+
c: {
282+
foo: {
283+
"_": "any"
284+
}
285+
foo: {
286+
[=~""]: string
287+
}
288+
}
289+
}
290+
underscore: {
291+
d: {
292+
bar: {
293+
"_": "any"
294+
}
295+
#bar: {
296+
[string]: string
297+
}
298+
bar: 〈0;#bar〉
299+
}
300+
}
301+
underscore: {
302+
e: {
303+
baz: {
304+
"_h": "any"
305+
}
306+
#baz: {
307+
[=~"_"]: string
308+
}
309+
baz: 〈0;#baz〉
310+
}
311+
}
191312
}

internal/core/adt/closed.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,10 @@ func Accept(ctx *OpContext, n *Vertex, f Feature) (found, required bool) {
340340
optionalTypes |= s.types
341341
}
342342

343+
var str Value
343344
if f.Index() == MaxIndex {
344345
f = 0
345-
}
346-
347-
var str Value
348-
if optionalTypes&(HasComplexPattern|HasDynamic) != 0 && f.IsString() && f > 0 {
346+
} else if optionalTypes&(HasComplexPattern|HasDynamic) != 0 && f.IsString() {
349347
str = f.ToValue(ctx)
350348
}
351349

internal/core/adt/feature.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ type StringIndexer interface {
6868
// SelectorString reports the shortest string representation of f when used as a
6969
// selector.
7070
func (f Feature) SelectorString(index StringIndexer) string {
71-
if f == 0 {
72-
return "_"
73-
}
7471
x := f.safeIndex()
7572
switch f.Typ() {
7673
case IntLabel:

0 commit comments

Comments
 (0)