Skip to content

Commit 8bbf9b4

Browse files
committed
internal/core/export: avoid panic on non-regular identifiers for packages
The logic assumed that package identifiers are all regular strings, but that's not necessarily the case. The specification does explicitly disallow definition identifiers for package names, but does not disallow "private" identifiers. We should probably make a subsequent change to error when the identifier is a definition, but as that might have more wide-ranging effects, we'll do that in another CL. Fixes #3968 Signed-off-by: Roger Peppe <[email protected]> Change-Id: I46cfb3777d434e0d18041ea42bcd28ec4d404e06 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1216862 Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent e0010dd commit 8bbf9b4

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

cmd/cue/cmd/testdata/script/issue3968.txtar

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1-
! exec cue def ./a
2-
stderr 'panic: not a string label'
1+
exec cue def ./a
2+
cmp stdout a-stdout
33

4-
! exec cue def ./b
5-
stderr 'panic: not a string label'
4+
exec cue def ./b
5+
cmp stdout b-stdout
66

7-
! exec cue def ./c
8-
stderr 'panic: not a string label'
7+
exec cue def ./c
8+
cmp stdout c-stdout
99

10-
! exec cue def ./d
11-
stderr 'panic: not a string label'
10+
exec cue def ./d
11+
cmp stdout d-stdout
1212

13+
-- a-stdout --
14+
package a
15+
16+
import #s "strings"
17+
18+
x: #s.ToUpper("foo")
19+
-- b-stdout --
20+
package b
21+
22+
import _s "strings"
23+
24+
x: _s.ToUpper("foo")
25+
-- c-stdout --
26+
package c
27+
28+
import "test.example/other1:_x"
29+
30+
_x
31+
-- d-stdout --
32+
package d
33+
34+
import "test.example/other2:#x"
35+
36+
#x
1337
-- cue.mod/module.cue --
1438
module: "test.example"
1539
language: version: "v0.11.0"

internal/core/export/adt.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,13 +484,13 @@ func (e *exporter) resolve(env *adt.Environment, r adt.Resolver) ast.Expr {
484484

485485
case *adt.ImportReference:
486486
importPath := x.ImportPath.StringValue(e.index)
487+
info := ast.ParseImportPath(importPath)
487488
spec := ast.NewImport(nil, importPath)
488489

489-
info, _ := astutil.ParseImportSpec(spec)
490-
name := info.PkgName
490+
name := info.Qualifier
491491
if x.Label != 0 {
492-
name = x.Label.StringValue(e.index)
493-
if name != info.PkgName {
492+
name = x.Label.IdentString(e.index)
493+
if name != info.Qualifier {
494494
spec.Name = ast.NewIdent(name)
495495
}
496496
}

0 commit comments

Comments
 (0)