Skip to content

Commit

Permalink
internal/core/compile: reject "_" as an alias name
Browse files Browse the repository at this point in the history
"_" stands for "top", and it was already being rejected as a label,
but it was still being allowed for alias or let declarations,
causing confusing errors such as:

    unreferenced alias or let clause _

Reject all three the same way.

Fixes #2381.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: Iad909813756c5df4421e96e0bdb2a071dc7c66f1
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/556985
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Marcel van Lohuizen <[email protected]>
  • Loading branch information
mvdan committed Jul 28, 2023
1 parent c8e51a5 commit bcbe967
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
48 changes: 48 additions & 0 deletions cue/testdata/compile/err_top.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- in.cue --
disallowTopAsAlias: {
_=X: 1
}

disallowTopAsLet: {
let _ = 1
}

disallowTopAsLabel: {
_: 1
a: _ // Should not compile to a reference.
}

// TODO: disallow dollar as label? This is according to the spec, but it
// will be a breaking change and $ was reserved for referring to the root of
// a file, which we very likely will never implement.
// disallowDollarAsLabel: {
// $: 1
// }

-- out/compile --
disallowTopAsAlias: cannot use _ as alias or let clause:
./in.cue:2:2
disallowTopAsLet: cannot use _ as alias or let clause:
./in.cue:6:6
disallowTopAsLabel: cannot use _ as label:
./in.cue:10:2
--- in.cue
{
disallowTopAsAlias: {
X: 1
}
disallowTopAsLet: {
let _ = 1
}
disallowTopAsLabel: {
_|_(cannot use _ as label)
a: _
}
}
-- out/eval --
disallowTopAsAlias: cannot use _ as alias or let clause:
./in.cue:2:2
disallowTopAsLet: cannot use _ as alias or let clause:
./in.cue:6:6
disallowTopAsLabel: cannot use _ as label:
./in.cue:10:2
20 changes: 0 additions & 20 deletions cue/testdata/compile/labels.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ ok11: [list.FlattenN([string], 1)]: string // disallowed in evaluator

bad1: [ for x in [1, 2, 3] {x}]: string

disallowTopAsLabel: {
_: 1
a: _ // Should not compile to a reference.
}

// TODO: disallow dollar as label? This is according to the spec, but it
// will be a breaking change and $ was reserved for referring to the root of
// a file, which we very likely will never implement.
// disallowDollarAsLabel: {
// $: 1
// }

saneReferencesInComprehensions: {
for _ in [1] {
a: _ // Should not compile to a reference.
Expand All @@ -44,8 +32,6 @@ saneReferencesInComprehensions: {
-- out/compile --
bad1: comprehension values not allowed in this position:
./in.cue:24:9
disallowTopAsLabel: cannot use _ as label:
./in.cue:27:2
--- in.cue
{
dis1: ("dev"|"prd")
Expand Down Expand Up @@ -95,10 +81,6 @@ disallowTopAsLabel: cannot use _ as label:
bad1: {
[_|_(comprehension values not allowed in this position)]: string
}
disallowTopAsLabel: {
_|_(cannot use _ as label)
a: _
}
saneReferencesInComprehensions: {
for _, _ in [
1,
Expand All @@ -110,5 +92,3 @@ disallowTopAsLabel: cannot use _ as label:
-- out/eval --
bad1: comprehension values not allowed in this position:
./in.cue:24:9
disallowTopAsLabel: cannot use _ as label:
./in.cue:27:2
3 changes: 3 additions & 0 deletions internal/core/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ func (c *compiler) insertAlias(id *ast.Ident, a aliasEntry) *adt.Bottom {
"alias %q already declared; previous declaration at %s",
id.Name, e.source.Pos())
}
if id.Name == "_" {
return c.errf(id, "cannot use _ as alias or let clause")
}

m[id.Name] = a
return nil
Expand Down

0 comments on commit bcbe967

Please sign in to comment.