cue icon indicating copy to clipboard operation
cue copied to clipboard

spec: discrepancies with the compiler when it comes to alias restrictions

Open mvdan opened this issue 2 years ago • 1 comments

What version of CUE are you using (cue version)?

$ cue version
cue version v0.6.0-alpha.2.0.20230727175350-c3eda1d599df

go version devel go1.22-8488309192 Fri Jul 28 02:46:31 2023 +0000
      -buildmode exe
       -compiler gc
        -ldflags -w -s
  DefaultGODEBUG panicnil=1
     CGO_ENABLED 1
          GOARCH amd64
            GOOS linux
         GOAMD64 v3

Does this issue reproduce with the latest stable release?

Yes, checked with v0.5.0.

What did you do?

Copying from @nicuveo's https://github.com/cue-lang/cue/issues/2381:

As a first sidenote, this also demonstrates a behaviour that is not documented in the specification: the fact that all aliases (and let clauses) MUST be used. The phrasing of the Aliases section suggests that this wasn't originally the case? (emphasis mine)

Aliases name values that CAN be referred to within the scope in which they are declared. The name of an alias MUST be unique within its scope.

Furthermore, this restriction that aliases must be unique within their scope doesn't seem to hold anymore either, which also suggests that this part of the documentation is outdated? For instance, the following is valid:

z = a: {
  z = b: 0
  c: z
}
b: z

What did you expect to see?

The spec and implementation should agree with each other.

What did you see instead?

  1. The spec does not require aliases to be used, but the compiler does. We might have borrowed this from Go, where the compiler requires all declarations to be used, but the spec does not mention this. However, the Go spec does make a mention of it, so if we are OK with the discrepancy, we should also include such a mention:

Implementation restriction: A compiler may make it illegal to declare a variable inside a function body if the variable is never used.

  1. The spec requires aliases to be unique within a scope, which the compiler seems to not enforce at all right now.

mvdan avatar Jul 28 '23 09:07 mvdan

Note that this spec deviation bug will disappear with the aliasv2 language change: https://github.com/cue-lang/cue/discussions/4014

mvdan avatar Dec 06 '25 14:12 mvdan