Skip to content

Commit

Permalink
[dev.typeparams] cmd/compile/internal/types2: must not import a packa…
Browse files Browse the repository at this point in the history
…ge called "init"

Updates #43962.

Change-Id: I070153c55baec62d13ca9284f02781b8c1276844
Reviewed-on: https://go-review.googlesource.com/c/go/+/287494
Trust: Robert Griesemer <[email protected]>
Run-TryBot: Robert Griesemer <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
  • Loading branch information
griesemer committed Jan 28, 2021
1 parent 217a461 commit f7d1c59
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
25 changes: 13 additions & 12 deletions src/cmd/compile/internal/types2/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,6 @@ func (check *Checker) collectObjects() {
continue
}

// add package to list of explicit imports
// (this functionality is provided as a convenience
// for clients; it is not needed for type-checking)
if !pkgImports[imp] {
pkgImports[imp] = true
pkg.imports = append(pkg.imports, imp)
}

// local name overrides imported package name
name := imp.name
if s.LocalPkgName != nil {
Expand All @@ -267,10 +259,19 @@ func (check *Checker) collectObjects() {
check.errorf(s.LocalPkgName, `cannot rename import "C"`)
continue
}
if name == "init" {
check.errorf(s.LocalPkgName, "cannot declare init - must be func")
continue
}
}

if name == "init" {
check.errorf(s.LocalPkgName, "cannot import package as init - init must be a func")
continue
}

// add package to list of explicit imports
// (this functionality is provided as a convenience
// for clients; it is not needed for type-checking)
if !pkgImports[imp] {
pkgImports[imp] = true
pkg.imports = append(pkg.imports, imp)
}

pkgName := NewPkgName(s.Pos(), pkg, name, imp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// we can have multiple blank imports (was bug)
_ "math"
_ "net/rpc"
init /* ERROR "cannot declare init" */ "fmt"
init /* ERROR "cannot import package as init" */ "fmt"
// reflect defines a type "flag" which shows up in the gc export data
"reflect"
. /* ERROR "imported but not used" */ "reflect"
Expand Down
5 changes: 5 additions & 0 deletions test/fixedbugs/issue43962.dir/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2021 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 init
7 changes: 7 additions & 0 deletions test/fixedbugs/issue43962.dir/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2021 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 b

import "./a" // ERROR "cannot import package as init"
9 changes: 9 additions & 0 deletions test/fixedbugs/issue43962.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// errorcheckdir

// Copyright 2021 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.

// Issue 43962: Importing a package called "init" is an error.

package ignored

0 comments on commit f7d1c59

Please sign in to comment.