Skip to content

Commit

Permalink
internal/core/adt: fix matching of "_" label
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mpvl committed Jan 11, 2022
1 parent 60d6503 commit 3d3f721
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 9 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 }
}
121 changes: 121 additions & 0 deletions cue/testdata/references/labels.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,38 @@ emptyLabel: {
a: emptyLabel[""]
}

underscore: a: {
// Issue #1454
foo: #Foo
foo: "_": "bar"
#Foo: [=~""]: string
}

underscore: b: {
foo: #Foo
// TODO: we should probably disallow the hidden label `_` to avoid confusion.
foo: _: "bar"
#Foo: [=~""]: string
}

underscore: c: {
foo: "_": "any"
foo: [=~""]: string
}

underscore: d: {
bar: "_": "any"
#bar: [string]: string
bar: #bar
}

underscore: e: {
baz: "_h": "any"
#baz: [=~"_"]: string
baz: #baz
}


// TODO: support. Also not yet supported in old implementation.
// c10: {
// C=[string]: {
Expand Down Expand Up @@ -104,6 +136,41 @@ emptyLabel: {
"": (int){ 1 }
a: (int){ 1 }
}
underscore: (struct){
a: (struct){
foo: (#struct){
"_": (string){ "bar" }
}
#Foo: (#struct){
}
}
b: (struct){
foo: (#struct){
_: (string){ "bar" }
}
#Foo: (#struct){
}
}
c: (struct){
foo: (struct){
"_": (string){ "any" }
}
}
d: (struct){
bar: (#struct){
"_": (string){ "any" }
}
#bar: (#struct){
}
}
e: (struct){
baz: (#struct){
"_h": (string){ "any" }
}
#baz: (#struct){
}
}
}
}
-- out/compile --
--- in.cue
Expand Down Expand Up @@ -188,4 +255,58 @@ emptyLabel: {
"": 1
a: 〈1;emptyLabel〉[""]
}
underscore: {
a: {
foo: 〈0;#Foo〉
foo: {
"_": "bar"
}
#Foo: {
[=~""]: string
}
}
}
underscore: {
b: {
foo: 〈0;#Foo〉
foo: {
_: "bar"
}
#Foo: {
[=~""]: string
}
}
}
underscore: {
c: {
foo: {
"_": "any"
}
foo: {
[=~""]: string
}
}
}
underscore: {
d: {
bar: {
"_": "any"
}
#bar: {
[string]: string
}
bar: 〈0;#bar〉
}
}
underscore: {
e: {
baz: {
"_h": "any"
}
#baz: {
[=~"_"]: string
}
baz: 〈0;#baz〉
}
}
}
6 changes: 2 additions & 4 deletions internal/core/adt/closed.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,10 @@ func Accept(ctx *OpContext, n *Vertex, f Feature) (found, required bool) {
optionalTypes |= s.types
}

var str Value
if f.Index() == MaxIndex {
f = 0
}

var str Value
if optionalTypes&(HasComplexPattern|HasDynamic) != 0 && f.IsString() && f > 0 {
} else if optionalTypes&(HasComplexPattern|HasDynamic) != 0 && f.IsString() {
str = f.ToValue(ctx)
}

Expand Down
3 changes: 0 additions & 3 deletions internal/core/adt/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ type StringIndexer interface {
// SelectorString reports the shortest string representation of f when used as a
// selector.
func (f Feature) SelectorString(index StringIndexer) string {
if f == 0 {
return "_"
}
x := f.safeIndex()
switch f.Typ() {
case IntLabel:
Expand Down

0 comments on commit 3d3f721

Please sign in to comment.