this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cue/parser: prohibit definition identifiers for package names

This is specifically prohibited by the spec already
(see https://cuelang.org/docs/reference/spec/#package-clause).
This just makes the implementation agree with the spec.

Specifically:
- you cannot use a definition identifier in an import clause
- you cannot use a definition identifier in a package clause

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Ic888b271984e5532ec23b64995fafb9c82b713d9
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1216867
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+41 -17
+10 -16
cmd/cue/cmd/testdata/script/issue3968.txtar
··· 1 - exec cue def ./a 2 - cmp stdout a-stdout 1 + ! exec cue def ./a 2 + cmp stderr a-stderr 3 3 4 4 exec cue def ./b 5 5 cmp stdout b-stdout ··· 7 7 exec cue def ./c 8 8 cmp stdout c-stdout 9 9 10 - exec cue def ./d 11 - cmp stdout d-stdout 12 - 13 - -- a-stdout -- 14 - package a 15 - 16 - import #s "strings" 10 + ! exec cue def ./d 11 + cmp stderr d-stderr 17 12 18 - x: #s.ToUpper("foo") 13 + -- a-stderr -- 14 + cannot import package as definition identifier: 15 + ./a/a.cue:3:11 19 16 -- b-stdout -- 20 17 package b 21 18 ··· 28 25 import "test.example/other1:_x" 29 26 30 27 _x 31 - -- d-stdout -- 32 - package d 33 - 34 - import "test.example/other2:#x" 35 - 36 - #x 28 + -- d-stderr -- 29 + test.example/d@v0: import failed: cannot find package "test.example/other2": cannot get imports: cannot read "other2/other.cue": invalid package name #x: 30 + ./d/d.cue:3:8 37 31 -- cue.mod/module.cue -- 38 32 module: "test.example" 39 33 language: version: "v0.11.0"
+11 -1
cue/parser/parser.go
··· 1622 1622 var ident *ast.Ident 1623 1623 if p.tok == token.IDENT { 1624 1624 ident = p.parseIdent() 1625 + if isDefinition(ident) { 1626 + p.errf(p.pos, "cannot import package as definition identifier") 1627 + } 1625 1628 } 1626 1629 1627 1630 pos := p.pos ··· 1719 1722 if name.Name == "_" && p.mode&declarationErrorsMode != 0 { 1720 1723 p.errf(p.pos, "invalid package name _") 1721 1724 } 1722 - 1725 + if isDefinition(name) { 1726 + p.errf(p.pos, "invalid package name %s", name.Name) 1727 + } 1723 1728 pkg := &ast.Package{ 1724 1729 PackagePos: pos, 1725 1730 Name: name, ··· 1756 1761 c.closeNode(p, f) 1757 1762 return f 1758 1763 } 1764 + 1765 + func isDefinition(ident *ast.Ident) bool { 1766 + return strings.HasPrefix(ident.Name, "#") || 1767 + strings.HasPrefix(ident.Name, "_#") 1768 + }
+20
cue/parser/parser_test.go
··· 235 235 `, 236 236 `package k8s, {}`, 237 237 }, { 238 + "invalid package identifier: definition", 239 + `package #x`, 240 + `package #x 241 + invalid package name #x`, 242 + }, { 243 + "invalid package identifier: hidden definition", 244 + `package _#x`, 245 + `package _#x 246 + invalid package name _#x`, 247 + }, { 248 + "invalid import identifier: definition", 249 + `import #x "foo"`, 250 + `import #x "foo" 251 + cannot import package as definition identifier`, 252 + }, { 253 + "invalid import identifier: hidden definition", 254 + `import _#x "foo"`, 255 + `import _#x "foo" 256 + cannot import package as definition identifier`, 257 + }, { 238 258 "imports group", 239 259 `package k8s 240 260