]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: handle import "C" more like cmd/compile
authorRobert Griesemer <gri@golang.org>
Thu, 17 Sep 2015 23:08:57 +0000 (16:08 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 18 Sep 2015 17:46:42 +0000 (17:46 +0000)
Fixes #12667.

Change-Id: I68e73e26da9938606304163ae2637e3c6bacd6f6
Reviewed-on: https://go-review.googlesource.com/14722
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/types/check_test.go
src/go/types/resolver.go
src/go/types/testdata/importC.src [new file with mode: 0644]

index 5e34c65b6361926197d2aa52ef774deefc731e9a..5e2043be84b22ec33d0c0cf3ccd8af1f06b24737 100644 (file)
@@ -55,6 +55,7 @@ var tests = [][]string{
        {"testdata/errors.src"},
        {"testdata/importdecl0a.src", "testdata/importdecl0b.src"},
        {"testdata/importdecl1a.src", "testdata/importdecl1b.src"},
+       {"testdata/importC.src"}, // special handling in checkFiles
        {"testdata/cycles.src"},
        {"testdata/cycles1.src"},
        {"testdata/cycles2.src"},
@@ -245,6 +246,10 @@ func checkFiles(t *testing.T, testfiles []string) {
 
        // typecheck and collect typechecker errors
        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.Error = func(err error) {
                if *listErrors {
index c31ef423d9a4a74d136d1734103efdcf80e75bf5..b52c3b2283d4c70b6ee41eaf9d2a6c449b4432a8 100644 (file)
@@ -202,6 +202,11 @@ func (check *Checker) collectObjects() {
                                                name := imp.name
                                                if s.Name != nil {
                                                        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" {
                                                                check.errorf(s.Name.Pos(), "cannot declare init - must be func")
                                                                continue
@@ -216,6 +221,11 @@ func (check *Checker) collectObjects() {
                                                        check.recordImplicit(s, obj)
                                                }
 
+                                               if path == "C" {
+                                                       // match cmd/compile (not prescribed by spec)
+                                                       obj.used = true
+                                               }
+
                                                // add import to file scope
                                                if name == "." {
                                                        // merge imported scope with file scope
diff --git a/src/go/types/testdata/importC.src b/src/go/types/testdata/importC.src
new file mode 100644 (file)
index 0000000..31436be
--- /dev/null
@@ -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"