From 4140492d5664798c894d6b4cd33c2f34dd83b0d2 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Tue, 5 Mar 2024 20:12:09 -0500 Subject: [PATCH] lang: ast: Embedded imports have function scopes too If we're importing an embedded module, we need to also include any possible system scope imports (like functions) that might be using the same namespace. These might be used by a custom CLI frontend to extend the code with values from the CLI parsing, for example. --- lang/ast/structs.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lang/ast/structs.go b/lang/ast/structs.go index f22d4cf4..09af6075 100644 --- a/lang/ast/structs.go +++ b/lang/ast/structs.go @@ -3255,6 +3255,18 @@ func (obj *StmtProg) importScope(info *interfaces.ImportData, scope *interfaces. return nil, err } + // Our embedded scope might also have some functions to add in! + systemScope, err := obj.importSystemScope(info.Name) + if err != nil { + return nil, errwrap.Wrapf(err, "embedded system import of `%s` failed", info.Name) + } + newScope := scope.Copy() + if err := newScope.Merge(systemScope); err != nil { // errors if something was overwritten + // XXX: we get a false positive b/c we overwrite the initial scope! + // XXX: when we switch to append, this problem will go away... + //return nil, errwrap.Wrapf(err, "duplicate scope element(s) in module found") + } + //tree, err := util.FsTree(fs, "/") //if err != nil { // return nil, err @@ -3272,7 +3284,7 @@ func (obj *StmtProg) importScope(info *interfaces.ImportData, scope *interfaces. // not try to copy them in from disk or it won't succeed. input.Files = []string{} // clear - embeddedScope, err := obj.importScopeWithParsedInputs(input, scope, nextVertex) + embeddedScope, err := obj.importScopeWithParsedInputs(input, newScope, nextVertex) if err != nil { return nil, errwrap.Wrapf(err, "embedded import of `%s` failed", info.Name) }