Skip to content

Commit a49ee92

Browse files
NoamTDmvdan
authored andcommitted
internal: replace and remove PackageInfo
The PackageInfo method was declared deprecated. This CL replaces all usages of this method with the newer GetPackageInfo method (which is what PackageInfo called behind the scenes), and removes the PackageInfo function. In addition, the code is reorganized slightly to move some vars to a smaller scope closer to their usage. Updates #2480. Signed-off-by: Noam Dolovich <[email protected]> Change-Id: Idb5a0d24d2cb5ef6d34bd17fdd5718b55c4bb465 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194646 Reviewed-by: Daniel Martí <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 9a0cae9 commit a49ee92

File tree

8 files changed

+13
-24
lines changed

8 files changed

+13
-24
lines changed

cmd/cue/cmd/add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func initFile(cmd *Command, file string, getBuild func(path string) *build.Insta
252252
if err != nil {
253253
return nil, err
254254
}
255-
if _, pkgName, _ := internal.PackageInfo(f); pkgName != "" {
255+
if pkgName := internal.GetPackageInfo(f).Name; pkgName != "" {
256256
if pkg := flagPackage.String(cmd); pkg != "" && pkgName != pkg {
257257
return nil, fmt.Errorf("package mismatch (%s vs %s) for file %s", pkgName, pkg, file)
258258
}

cmd/cue/cmd/get_go.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ func (e *extractor) importCUEFiles(p *packages.Package, dir, args string) error
591591
return err
592592
}
593593

594-
if _, pkg, _ := internal.PackageInfo(f); pkg != "" && pkg == p.Name {
594+
if pkg := internal.GetPackageInfo(f).Name; pkg != "" && pkg == p.Name {
595595
file := filepath.Base(path)
596596
file = file[:len(file)-len(".cue")]
597597
file += "_gen.cue"

cmd/cue/cmd/orphans.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ func placeOrphans(b *buildPlan, d *encoding.Decoder, pkg string, objs ...*ast.Fi
166166
astutil.CopyMeta(f, file)
167167
}
168168
expr := internal.ToExpr(file)
169-
p, _, _ := internal.PackageInfo(file)
170169

171170
var path cue.Path
172171
var labels []ast.Label
@@ -272,7 +271,7 @@ func placeOrphans(b *buildPlan, d *encoding.Decoder, pkg string, objs ...*ast.Fi
272271
} else {
273272
field := &ast.Field{Label: labels[0]}
274273
f.Decls = append(f.Decls, field)
275-
if p != nil {
274+
if p := internal.GetPackageInfo(file).Package; p != nil {
276275
astutil.CopyComments(field, p)
277276
}
278277
for _, e := range labels[1:] {

cue/build/instance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ func (inst *Instance) AddSyntax(file *ast.File) errors.Error {
239239
astutil.Resolve(file, func(pos token.Pos, msg string, args ...interface{}) {
240240
inst.Err = errors.Append(inst.Err, errors.Newf(pos, msg, args...))
241241
})
242-
_, pkg, pos := internal.PackageInfo(file)
242+
pkg := internal.GetPackageInfo(file).Name
243243
if pkg != "" && pkg != "_" && !inst.User && !inst.setPkg(pkg) && pkg != inst.PkgName {
244-
err := errors.Newf(pos,
244+
err := errors.Newf(file.Pos(),
245245
"package name %q conflicts with previous package name %q",
246246
pkg, inst.PkgName)
247247
inst.ReportError(err)

cue/load/loader_common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,11 @@ func (fp *fileProcessor) add(root string, file *build.File, mode importMode) (ad
212212
return true
213213
}
214214

215-
_, pkg, pos := internal.PackageInfo(pf)
215+
pkg := internal.GetPackageInfo(pf).Name
216216
if pkg == "" {
217217
pkg = "_"
218218
}
219+
pos := pf.Pos()
219220

220221
switch {
221222
case pkg == p.PkgName, mode&allowAnonymous != 0:

internal/core/export/export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (e *exporter) toFile(v *adt.Vertex, x ast.Expr) *ast.File {
189189
return true
190190
}
191191

192-
if _, name, _ := internal.PackageInfo(f); name != "" {
192+
if name := internal.GetPackageInfo(f).Name; name != "" {
193193
pkgName = name
194194
}
195195

internal/core/runtime/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (r *Runtime) CompileFile(cfg *Config, file *ast.File) (*adt.Vertex, *build.
119119
if err != nil {
120120
return nil, p
121121
}
122-
_, p.PkgName, _ = internal.PackageInfo(file)
122+
p.PkgName = internal.GetPackageInfo(file).Name
123123
v, _ := r.Build(cfg, p)
124124
return v, p
125125
}

internal/internal.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,13 @@ func GetPackageInfo(f *ast.File) PkgInfo {
158158
return PkgInfo{}
159159
}
160160

161-
// Deprecated: use GetPackageInfo
162-
func PackageInfo(f *ast.File) (p *ast.Package, name string, tok token.Pos) {
163-
x := GetPackageInfo(f)
164-
if p := x.Package; p != nil {
165-
return p, x.Name, p.Name.Pos()
166-
}
167-
return nil, "", f.Pos()
168-
}
169-
170161
func SetPackage(f *ast.File, name string, overwrite bool) {
171-
p, str, _ := PackageInfo(f)
172-
if p != nil {
173-
if !overwrite || str == name {
162+
if pi := GetPackageInfo(f); pi.Package != nil {
163+
if !overwrite || pi.Name == name {
174164
return
175165
}
176166
ident := ast.NewIdent(name)
177-
astutil.CopyMeta(ident, p.Name)
167+
astutil.CopyMeta(ident, pi.Package.Name)
178168
return
179169
}
180170

@@ -235,9 +225,8 @@ func NewComment(isDoc bool, s string) *ast.CommentGroup {
235225
}
236226

237227
func FileComment(f *ast.File) *ast.CommentGroup {
238-
pkg, _, _ := PackageInfo(f)
239228
var cgs []*ast.CommentGroup
240-
if pkg != nil {
229+
if pkg := GetPackageInfo(f).Package; pkg != nil {
241230
cgs = pkg.Comments()
242231
} else if cgs = f.Comments(); len(cgs) > 0 {
243232
// Use file comment.

0 commit comments

Comments
 (0)