diff options
Diffstat (limited to 'src/cmd/compile/internal/noder/import.go')
-rw-r--r-- | src/cmd/compile/internal/noder/import.go | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/src/cmd/compile/internal/noder/import.go b/src/cmd/compile/internal/noder/import.go index c4a57806eb..24d911ba38 100644 --- a/src/cmd/compile/internal/noder/import.go +++ b/src/cmd/compile/internal/noder/import.go @@ -175,36 +175,44 @@ func resolveImportPath(path string) (string, error) { return path, nil } -// TODO(mdempsky): Return an error instead. func importfile(decl *syntax.ImportDecl) *types.Pkg { - if decl.Path.Kind != syntax.StringLit { - base.Errorf("import path must be a string") + path, err := parseImportPath(decl.Path) + if err != nil { + base.Errorf("%s", err) return nil } - path, err := strconv.Unquote(decl.Path.Value) - if err != nil { - base.Errorf("import path must be a string") - return nil + pkg := readImportFile(typecheck.Target, path) + if pkg != ir.Pkgs.Unsafe && pkg.Height >= myheight { + myheight = pkg.Height + 1 } + return pkg +} - if err := checkImportPath(path, false); err != nil { - base.Errorf("%s", err.Error()) - return nil +func parseImportPath(pathLit *syntax.BasicLit) (string, error) { + if pathLit.Kind != syntax.StringLit { + return "", errors.New("import path must be a string") } - path, err = resolveImportPath(path) + path, err := strconv.Unquote(pathLit.Value) if err != nil { - base.Errorf("%s", err) - return nil + return "", errors.New("import path must be a string") + } + + if err := checkImportPath(path, false); err != nil { + return "", err } + return resolveImportPath(path) +} + +func readImportFile(target *ir.Package, path string) *types.Pkg { importpkg := types.NewPkg(path, "") if importpkg.Direct { return importpkg // already fully loaded } importpkg.Direct = true - typecheck.Target.Imports = append(typecheck.Target.Imports, importpkg) + target.Imports = append(target.Imports, importpkg) if path == "unsafe" { return importpkg // initialized with universe @@ -324,10 +332,6 @@ func importfile(decl *syntax.ImportDecl) *types.Pkg { base.Ctxt.AddImport(file[len(file)-len(path)-len(".a"):], fingerprint) } - if importpkg.Height >= myheight { - myheight = importpkg.Height + 1 - } - return importpkg } |