1
0
mirror of https://github.com/golang/go synced 2024-09-30 08:38:40 -06:00

go/types: handle import "C" more like cmd/compile

Fixes #12667.

Change-Id: I68e73e26da9938606304163ae2637e3c6bacd6f6
Reviewed-on: https://go-review.googlesource.com/14722
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Robert Griesemer 2015-09-17 16:08:57 -07:00
parent 3d91624fad
commit 8bd222f046
3 changed files with 25 additions and 0 deletions

View File

@ -55,6 +55,7 @@ var tests = [][]string{
{"testdata/errors.src"}, {"testdata/errors.src"},
{"testdata/importdecl0a.src", "testdata/importdecl0b.src"}, {"testdata/importdecl0a.src", "testdata/importdecl0b.src"},
{"testdata/importdecl1a.src", "testdata/importdecl1b.src"}, {"testdata/importdecl1a.src", "testdata/importdecl1b.src"},
{"testdata/importC.src"}, // special handling in checkFiles
{"testdata/cycles.src"}, {"testdata/cycles.src"},
{"testdata/cycles1.src"}, {"testdata/cycles1.src"},
{"testdata/cycles2.src"}, {"testdata/cycles2.src"},
@ -245,6 +246,10 @@ func checkFiles(t *testing.T, testfiles []string) {
// typecheck and collect typechecker errors // typecheck and collect typechecker errors
var conf Config var conf Config
// special case for importC.src
if len(testfiles) == 1 && testfiles[0] == "testdata/importC.src" {
conf.FakeImportC = true
}
conf.Importer = importer.Default() conf.Importer = importer.Default()
conf.Error = func(err error) { conf.Error = func(err error) {
if *listErrors { if *listErrors {

View File

@ -202,6 +202,11 @@ func (check *Checker) collectObjects() {
name := imp.name name := imp.name
if s.Name != nil { if s.Name != nil {
name = s.Name.Name name = s.Name.Name
if path == "C" {
// match cmd/compile (not prescribed by spec)
check.errorf(s.Name.Pos(), `cannot rename import "C"`)
continue
}
if name == "init" { if name == "init" {
check.errorf(s.Name.Pos(), "cannot declare init - must be func") check.errorf(s.Name.Pos(), "cannot declare init - must be func")
continue continue
@ -216,6 +221,11 @@ func (check *Checker) collectObjects() {
check.recordImplicit(s, obj) check.recordImplicit(s, obj)
} }
if path == "C" {
// match cmd/compile (not prescribed by spec)
obj.used = true
}
// add import to file scope // add import to file scope
if name == "." { if name == "." {
// merge imported scope with file scope // merge imported scope with file scope

10
src/go/types/testdata/importC.src vendored Normal file
View File

@ -0,0 +1,10 @@
// Copyright 2015 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 importC
import "C"
import _ /* ERROR cannot rename import "C" */ "C"
import foo /* ERROR cannot rename import "C" */ "C"
import . /* ERROR cannot rename import "C" */ "C"