Skip to content

Commit

Permalink
interp: delete incomplete type on pkg import
Browse files Browse the repository at this point in the history
When a package is imported, it creates a symbol with a name like "errors/_.go". If a statement such as x := errors.New(...) is executed before import "errors", it creates an incomplete type symbol with a name like "errors". Importing the package after the incomplete type symbol has been created does not fix the compile issue because the incomplete type still exists.

To fix this, this PR deletes the incomplete type symbol, if one exists.

Closes #1388.
  • Loading branch information
firelizzard18 authored Apr 26, 2022
1 parent ad9db37 commit 4e77fc9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions interp/gta.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ func (interp *Interpreter) gta(root *node, rpath, importPath, pkgName string) ([
if name == "" {
name = interp.pkgNames[ipath]
}

// If an incomplete type exists, delete it
if sym, exists := sc.sym[name]; exists && sym.kind == typeSym && sym.typ.incomplete {
delete(sc.sym, name)
}

// Imports of a same package are all mapped in the same scope, so we cannot just
// map them by their names, otherwise we could have collisions from same-name
// imports in different source files of the same package. Therefore, we suffix
Expand Down
23 changes: 23 additions & 0 deletions interp/interp_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,29 @@ func TestRestrictedEnv(t *testing.T) {
}
}

func TestIssue1388(t *testing.T) {
i := interp.New(interp.Options{Env: []string{"foo=bar"}})
err := i.Use(stdlib.Symbols)
if err != nil {
t.Fatal(err)
}

_, err = i.Eval(`x := errors.New("")`)
if err == nil {
t.Fatal("Expected an error")
}

_, err = i.Eval(`import "errors"`)
if err != nil {
t.Fatal(err)
}

_, err = i.Eval(`x := errors.New("")`)
if err != nil {
t.Fatal(err)
}
}

func TestIssue1383(t *testing.T) {
const src = `
package main
Expand Down

0 comments on commit 4e77fc9

Please sign in to comment.