]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: emit error message on mismatch import path
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 3 Jul 2024 06:13:05 +0000 (13:13 +0700)
committerGopher Robot <gobot@golang.org>
Wed, 3 Jul 2024 13:03:08 +0000 (13:03 +0000)
Fixes #54542

Change-Id: I16cfb84fc54892923106d0a6f0b3ba810886d077
Reviewed-on: https://go-review.googlesource.com/c/go/+/596396
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
src/cmd/compile/internal/noder/unified.go
test/fixedbugs/issue54542.go [new file with mode: 0644]

index 22d6f71329c3754f3fa2ce486f34833e518c62a0..6bc23cc5f922742b684455350749074df0ce20df 100644 (file)
@@ -7,6 +7,7 @@ package noder
 import (
        "fmt"
        "internal/pkgbits"
+       "internal/types/errors"
        "io"
        "runtime"
        "sort"
@@ -403,7 +404,10 @@ func readPackage(pr *pkgReader, importpkg *types.Pkg, localStub bool) {
                r := pr.newReader(pkgbits.RelocMeta, pkgbits.PublicRootIdx, pkgbits.SyncPublic)
 
                pkg := r.pkg()
-               base.Assertf(pkg == importpkg, "have package %q (%p), want package %q (%p)", pkg.Path, pkg, importpkg.Path, importpkg)
+               if pkg != importpkg {
+                       base.ErrorfAt(base.AutogeneratedPos, errors.BadImportPath, "mismatched import path, have %q (%p), want %q (%p)", pkg.Path, pkg, importpkg.Path, importpkg)
+                       base.ErrorExit()
+               }
 
                r.Bool() // TODO(mdempsky): Remove; was "has init"
 
diff --git a/test/fixedbugs/issue54542.go b/test/fixedbugs/issue54542.go
new file mode 100644 (file)
index 0000000..165bcc5
--- /dev/null
@@ -0,0 +1,67 @@
+// run
+
+//go:build !js && !wasip1
+
+// Copyright 2024 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+       "bytes"
+       "fmt"
+       "os"
+       "os/exec"
+       "path/filepath"
+)
+
+const aSrc = `package a
+
+func A() { println("a") }
+`
+
+const mainSrc = `package main
+
+import "a"
+
+func main() { a.A() }
+`
+
+var srcs = map[string]string{
+       "a.go":    aSrc,
+       "main.go": mainSrc,
+}
+
+func main() {
+       dir, err := os.MkdirTemp("", "issue54542")
+       if err != nil {
+               panic(err)
+       }
+       defer os.RemoveAll(dir)
+
+       for fn, src := range srcs {
+               if err := os.WriteFile(filepath.Join(dir, fn), []byte(src), 0644); err != nil {
+                       panic(err)
+               }
+       }
+
+       if _, err := runInDir(dir, "tool", "compile", "-p=lie", "a.go"); err != nil {
+               panic(err)
+       }
+
+       out, err := runInDir(dir, "tool", "compile", "-I=.", "-p=main", "main.go")
+       if err == nil {
+               panic("compiling succeed unexpectedly")
+       }
+
+       if bytes.Contains(out, []byte("internal compiler error:")) {
+               panic(fmt.Sprintf("unexpected ICE:\n%s", string(out)))
+       }
+}
+
+func runInDir(dir string, args ...string) ([]byte, error) {
+       cmd := exec.Command("go", args...)
+       cmd.Dir = dir
+       return cmd.CombinedOutput()
+}