From 806f1dbf43c4420f2800829f79e73b75af4663a8 Mon Sep 17 00:00:00 2001 From: Nathan John Youngman Date: Thu, 27 Nov 2014 15:36:14 +1100 Subject: [PATCH] tools/cmd/stringer: fake import C. Avoid error "could not import C (can't find import: C)" Fixes golang/go#9169. LGTM=adonovan, r R=golang-codereviews, adonovan, r CC=golang-codereviews https://golang.org/cl/184730043 --- cmd/stringer/stringer.go | 2 +- cmd/stringer/testdata/cgo.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 cmd/stringer/testdata/cgo.go diff --git a/cmd/stringer/stringer.go b/cmd/stringer/stringer.go index 33343def..f7039aa2 100644 --- a/cmd/stringer/stringer.go +++ b/cmd/stringer/stringer.go @@ -261,7 +261,7 @@ func (g *Generator) parsePackage(directory string, names []string, text interfac // check type-checks the package. The package must be OK to proceed. func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) { pkg.defs = make(map[*ast.Ident]types.Object) - var config types.Config + config := types.Config{FakeImportC: true} info := &types.Info{ Defs: pkg.defs, } diff --git a/cmd/stringer/testdata/cgo.go b/cmd/stringer/testdata/cgo.go new file mode 100644 index 00000000..ef38f959 --- /dev/null +++ b/cmd/stringer/testdata/cgo.go @@ -0,0 +1,32 @@ +// Copyright 2014 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. + +// Import "C" shouldn't be imported. + +package main + +/* +#define HELLO 1 +*/ +import "C" + +import "fmt" + +type Cgo uint32 + +const ( + // MustScanSubDirs indicates that events were coalesced hierarchically. + MustScanSubDirs Cgo = 1 << iota +) + +func main() { + _ = C.HELLO + ck(MustScanSubDirs, "MustScanSubDirs") +} + +func ck(day Cgo, str string) { + if fmt.Sprint(day) != str { + panic("cgo.go: " + str) + } +}