Commit adf88837 authored by Robert Griesemer's avatar Robert Griesemer

go/types: avoid race condition with dot-imported objects

It would be nice to have a test, but it requires running
this under the race detector which is a bit complicated
to set up; yet the fix is trivial. Verified manually that
it doesn't trip the race detector.

Fixes #32154.

Change-Id: I20bd746a07945c802f0476a1d8b1dfd83c87dae8
Reviewed-on: https://go-review.googlesource.com/c/go/+/183849
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 6d1aaf14
...@@ -301,15 +301,15 @@ func (check *Checker) collectObjects() { ...@@ -301,15 +301,15 @@ func (check *Checker) collectObjects() {
// A package scope may contain non-exported objects, // A package scope may contain non-exported objects,
// do not import them! // do not import them!
if obj.Exported() { if obj.Exported() {
// TODO(gri) When we import a package, we create // declare dot-imported object
// a new local package object. We should do the // (Do not use check.declare because it modifies the object
// same for each dot-imported object. That way // via Object.setScopePos, which leads to a race condition;
// they can have correct position information. // the object may be imported into more than one file scope
// (We must not modify their existing position // concurrently. See issue #32154.)
// information because the same package - found if alt := fileScope.Insert(obj); alt != nil {
// via Config.Packages - may be dot-imported in check.errorf(s.Name.Pos(), "%s redeclared in this block", obj.Name())
// another package!) check.reportAltDecl(alt)
check.declare(fileScope, nil, obj, token.NoPos) }
} }
} }
// add position to set of dot-import positions for this file // add position to set of dot-import positions for this file
...@@ -317,6 +317,7 @@ func (check *Checker) collectObjects() { ...@@ -317,6 +317,7 @@ func (check *Checker) collectObjects() {
check.addUnusedDotImport(fileScope, imp, s.Pos()) check.addUnusedDotImport(fileScope, imp, s.Pos())
} else { } else {
// declare imported package object in file scope // declare imported package object in file scope
// (no need to provide s.Name since we called check.recordDef earlier)
check.declare(fileScope, nil, obj, token.NoPos) check.declare(fileScope, nil, obj, token.NoPos)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment