Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: fix import shortname mapping
Browse files Browse the repository at this point in the history
- resolution was broken for builtin packages
- allow exporting variables in addition to builtins

Change-Id: I5ae4381f66a9a5b6f24ba98656e79cfbd8ef6fa0
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2715
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Aug 5, 2019
1 parent 29236f2 commit 1372f3e
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 50 deletions.
8 changes: 6 additions & 2 deletions cue/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (v *astVisitor) resolve(n *ast.Ident) value {
}
if v.inSelector > 0 {
if p := getBuiltinShorthandPkg(ctx, n.Name); p != nil {
return &nodeRef{baseValue: newExpr(n), node: p}
return &nodeRef{newExpr(n), p, label}
}
}
}
Expand Down Expand Up @@ -454,7 +454,11 @@ func (v *astVisitor) walk(astNode ast.Node) (ret value) {
ret = &selectorExpr{newExpr(n), ret, f}
} else {
n2 := v.mapScope(n.Node)
ret = &nodeRef{baseValue: newExpr(n), node: n2}
ref := &nodeRef{baseValue: newExpr(n), node: n2}
ret = ref
if inst := v.ctx().getImportFromNode(n2); inst != nil {
ref.short = f
}
}

case *ast.BottomLit:
Expand Down
6 changes: 3 additions & 3 deletions cue/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,13 @@ func resolveFile(idx *index, f *ast.File, p *build.Instance, allFields map[strin
name := path.Base(id)
if imp := p.LookupImport(id); imp != nil {
name = imp.PkgName
if spec.Name != nil {
name = spec.Name.Name
}
} else if _, ok := builtins[id]; !ok {
// continue
return nodeErrorf(spec, "package %q not found", id)
}
if spec.Name != nil {
name = spec.Name.Name
}
if n, ok := fields[name]; ok {
return nodeErrorf(spec,
"%s redeclared as imported package name\n"+
Expand Down
38 changes: 28 additions & 10 deletions cue/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,24 @@ func TestBuild(t *testing.T) {
}{{
insts(&bimport{"", files(`test: "ok"`)}),
`{test: "ok"}`,
// }, {
// insts(pkg1, &bimport{"",
// files(
// `package test
}, {
insts(&bimport{"",
files(
`package test
// import "math"
import "math"
// "Pi: \(math.Pi)!"`)}),
// `"Pi: 3.14159265358979323846264338327950288419716939937510582097494459!"`,
"Pi: \(math.Pi)!"`)}),
`"Pi: 3.14159265358979323846264338327950288419716939937510582097494459!"`,
}, {
insts(&bimport{"",
files(
`package test
import math2 "math"
"Pi: \(math2.Pi)!"`)}),
`"Pi: 3.14159265358979323846264338327950288419716939937510582097494459!"`,
}, {
insts(pkg1, &bimport{"",
files(
Expand All @@ -172,12 +181,21 @@ func TestBuild(t *testing.T) {
`package test
import "pkg1"
pkg1: 1
"Hello \(pkg1.Object)!"`),
}),
`pkg1 redeclared as imported package name
previous declaration at file0.cue:4:5`,
`"Hello World!"`,
}, {
insts(pkg1, &bimport{"",
files(
`package test
import pkg2 "pkg1"
pkg1: pkg2.Object
"Hello \(pkg1)!"`),
}),
`"Hello World!"`,
}, {
insts(pkg1, &bimport{"",
files(
Expand Down
2 changes: 1 addition & 1 deletion cue/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func rewriteCopy(ctx *context, v value) (value, bool) {
if node == x.node {
return x, false
}
return &nodeRef{x.baseValue, node}, false
return &nodeRef{x.baseValue, node, x.short}, false

case *structLit:
arcs := make(arcs, len(x.arcs))
Expand Down
79 changes: 47 additions & 32 deletions cue/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,43 @@ func (p *exporter) clause(v value) (n ast.Clause, next yielder) {
panic(fmt.Sprintf("unsupported clause type %T", v))
}

func (p *exporter) shortName(preferred, pkg string) string {
info, ok := p.imports[pkg]
short := info.short
if !ok {
short = pkg
if i := strings.LastIndexByte(pkg, '.'); i >= 0 {
short = pkg[i+1:]
}
if _, ok := p.top[p.ctx.label(short, true)]; ok && preferred != "" {
short = preferred
info.name = short
}
for {
if _, ok := p.top[p.ctx.label(short, true)]; !ok {
break
}
short += "x"
info.name = short
}
info.short = short
p.top[p.ctx.label(short, true)] = true
p.imports[pkg] = info
}
f := p.ctx.label(short, true)
for _, e := range p.stack {
if e.from == f {
if info.alias == "" {
info.alias = p.unique(short)
p.imports[pkg] = info
}
short = info.alias
break
}
}
return short
}

func (p *exporter) expr(v value) ast.Expr {
if doEval(p.mode) {
e := v.evalPartial(p.ctx)
Expand All @@ -189,41 +226,19 @@ func (p *exporter) expr(v value) ast.Expr {
if x.pkg == 0 {
return name
}
pkg := p.ctx.labelStr(x.pkg)
info, ok := p.imports[pkg]
short := info.short
if !ok {
info.short = ""
short = pkg
if i := strings.LastIndexByte(pkg, '.'); i >= 0 {
short = pkg[i+1:]
}
for {
if _, ok := p.top[p.ctx.label(short, true)]; !ok {
break
}
short += "x"
info.name = short
}
info.short = short
p.top[p.ctx.label(short, true)] = true
p.imports[pkg] = info
}
f := p.ctx.label(short, true)
for _, e := range p.stack {
if e.from == f {
if info.alias == "" {
info.alias = p.unique(short)
p.imports[pkg] = info
}
short = info.alias
break
}
}
short := p.shortName("", p.ctx.labelStr(x.pkg))
return &ast.SelectorExpr{X: ast.NewIdent(short), Sel: name}

case *nodeRef:
return nil
if x.short == 0 {
return nil
}
inst := p.ctx.getImportFromNode(x.node)
if inst == nil {
return nil // should not happen!
}
short := p.ctx.labelStr(x.short)
return ast.NewIdent(p.shortName(short, inst.ImportPath))

case *selectorExpr:
n := p.expr(x.x)
Expand Down
46 changes: 45 additions & 1 deletion cue/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,50 @@ func TestExportFile(t *testing.T) {
import "strings"
a: strings.ContainsAny("c")`),
}, {
in: `
import "time"
a: time.Time
`,
out: unindent(`
import "time"
a: time.Time`),
}, {
in: `
import "time"
{
a: time.Time
} & {
time: int
} `,
out: unindent(`
import timex "time"
time: int
a: timex.Time`),
}, {
in: `
import time2 "time"
a: time2.Time`,
out: unindent(`
import "time"
a: time.Time`),
}, {
in: `
import time2 "time"
time: int
a: time2.Time`,
out: unindent(`
import time2 "time"
time: int
a: time2.Time`),
}, {
in: `
import "strings"
Expand Down Expand Up @@ -406,7 +450,7 @@ func TestExportFile(t *testing.T) {
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
var r Runtime
inst, err := r.Parse("test", tc.in)
inst, err := r.Compile("test", tc.in)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion cue/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,8 @@ func (x *structLit) insertValue(ctx *context, f label, optional bool, value valu
// A nodeRef is a reference to a node.
type nodeRef struct {
baseValue
node scope
node scope
short label // only for packages, otherwise 0
}

func (x *nodeRef) kind() kind {
Expand Down

0 comments on commit 1372f3e

Please sign in to comment.