this repo has no description
0
fork

Configure Feed

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

cue/build: standardise on using the Context's loader

In cue/build, Context.NewInstance will accept a LoadFunc as an argument.
If it's not supplied, then the LoadFunc in the Context itself is used.

In several places we were supplying both, leading to puzzling code. It
turns out that none of our uses required the explicit argument, and
instead some simplification is possible by relying on the LoadFunc from
Context being used.

Signed-off-by: Matthew Sackman <matthew@cue.works>
Change-Id: I0724849d7e6b870b7b7f5df4ceacb5fbdb0e3783
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1215457
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>

+12 -13
+2 -1
cue/build/context.go
··· 39 39 imports map[string]*Instance 40 40 } 41 41 42 - // NewInstance creates an instance for this Context. 42 + // NewInstance creates an instance for this Context. If the [LoadFunc] 43 + // is nil, then the LoadFunc in the [Context] is used. 43 44 func (c *Context) NewInstance(dir string, f LoadFunc) *Instance { 44 45 if c == nil { 45 46 c = &Context{}
+3 -2
cue/build_test.go
··· 233 233 234 234 func makeInstances(insts []*bimport) (instances []*build.Instance) { 235 235 b := builder{ 236 - ctxt: build.NewContext(), 237 236 imports: map[string]*bimport{}, 238 237 } 238 + b.ctxt = build.NewContext(build.Loader(b.load)) 239 + 239 240 for _, bi := range insts { 240 241 if bi.path != "" { 241 242 b.imports[bi.path] = bi ··· 254 255 if path == "" { 255 256 path = "dir" 256 257 } 257 - p := b.ctxt.NewInstance(path, b.load) 258 + p := b.ctxt.NewInstance(path, nil) 258 259 for i, f := range bi.files { 259 260 _ = p.AddFile(fmt.Sprintf("file%d.cue", i), f) 260 261 }
+2 -2
cue/load/import.go
··· 311 311 panic(fmt.Errorf("non-relative import path %q passed to newRelInstance", path)) 312 312 } 313 313 314 - p := l.cfg.Context.NewInstance(path, l.loadFunc()) 314 + p := l.cfg.Context.NewInstance(path, nil) 315 315 p.PkgName = pkgName 316 316 p.DisplayPath = filepath.ToSlash(path) 317 317 // p.ImportPath = string(dir) // compute unique ID. ··· 387 387 388 388 func (l *loader) newInstance(pos token.Pos, p importPath) *build.Instance { 389 389 dir, modPath, err := l.absDirFromImportPath(pos, p) 390 - i := l.cfg.Context.NewInstance(dir, l.loadFunc()) 390 + i := l.cfg.Context.NewInstance(dir, nil) 391 391 i.Err = errors.Append(i.Err, err) 392 392 i.Dir = dir 393 393
+1 -1
cue/load/loader.go
··· 76 76 // (typically named on the command line). 77 77 func (l *loader) cueFilesPackage(files []*build.File) *build.Instance { 78 78 // ModInit() // TODO: support modules 79 - pkg := l.cfg.Context.NewInstance(l.cfg.Dir, l.loadFunc()) 79 + pkg := l.cfg.Context.NewInstance(l.cfg.Dir, nil) 80 80 81 81 for _, bf := range files { 82 82 f := bf.Filename
+2 -2
cue/marshal.go
··· 59 59 } 60 60 61 61 func (b *unmarshaller) build(bi *instanceData) *build.Instance { 62 - p := b.ctxt.NewInstance(bi.Path, b.load) 62 + p := b.ctxt.NewInstance(bi.Path, nil) 63 63 p.ImportPath = bi.Path 64 64 for _, f := range bi.Files { 65 65 _ = p.AddFile(f.Name, f.Data) ··· 70 70 71 71 func compileInstances(r *Runtime, data []*instanceData) (instances []*Instance, err error) { 72 72 b := unmarshaller{ 73 - ctxt: build.NewContext(), 74 73 imports: map[string]*instanceData{}, 75 74 } 75 + b.ctxt = build.NewContext(build.Loader(b.load)) 76 76 for _, i := range data { 77 77 if i.Path == "" { 78 78 if !i.Root {
+2 -5
internal/core/runtime/build.go
··· 22 22 "cuelang.org/go/cue/build" 23 23 "cuelang.org/go/cue/errors" 24 24 "cuelang.org/go/cue/stats" 25 - "cuelang.org/go/cue/token" 26 25 "cuelang.org/go/internal/core/adt" 27 26 "cuelang.org/go/internal/core/compile" 28 27 ) ··· 91 90 return v, errs 92 91 } 93 92 94 - func dummyLoad(token.Pos, string) *build.Instance { return nil } 95 - 96 93 func (r *Runtime) Compile(cfg *Config, source interface{}) (*adt.Vertex, *build.Instance) { 97 94 ctx := build.NewContext() 98 95 var filename string 99 96 if cfg != nil && cfg.Filename != "" { 100 97 filename = cfg.Filename 101 98 } 102 - p := ctx.NewInstance(filename, dummyLoad) 99 + p := ctx.NewInstance(filename, nil) 103 100 if err := p.AddFile(filename, source); err != nil { 104 101 return nil, p 105 102 } ··· 113 110 if cfg != nil && cfg.Filename != "" { 114 111 filename = cfg.Filename 115 112 } 116 - p := ctx.NewInstance(filename, dummyLoad) 113 + p := ctx.NewInstance(filename, nil) 117 114 err := p.AddSyntax(file) 118 115 if err != nil { 119 116 return nil, p