Skip to content

Commit 4f26f24

Browse files
cmd/cgo: recognize untyped constants defined in different files
An untyped constant can be defined in any input file, we shouldn't segregate them by file. Updates #28772 Change-Id: I0347f15236833bb511eb49f86c449ee9241b0a25 Reviewed-on: https://go-review.googlesource.com/c/151600 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Bonventre <andybons@golang.org>
1 parent 048580d commit 4f26f24

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

misc/cgo/test/issue28545.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ const issue28772Constant = C.issue28772Constant
2222
func issue28545G(p **C.char) {
2323
C.issue28545F(p, -1, (0))
2424
C.issue28545F(p, 2+3, complex(1, 1))
25-
C.issue28545F(p, issue28772Constant, (0))
25+
C.issue28545F(p, issue28772Constant, issue28772Constant2)
2626
}

misc/cgo/test/issue28772.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package cgotest
6+
7+
// Constants didn't work if defined in different source file.
8+
9+
// #define issue28772Constant2 2
10+
import "C"
11+
12+
const issue28772Constant2 = C.issue28772Constant2

src/cmd/cgo/ast.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ func (f *File) ParseGo(name string, src []byte) {
6666
f.Package = ast1.Name.Name
6767
f.Name = make(map[string]*Name)
6868
f.NamePos = make(map[*Name]token.Pos)
69-
f.Consts = make(map[string]bool)
7069

7170
// In ast1, find the import "C" line and get any extra C preamble.
7271
sawC := false
@@ -198,7 +197,7 @@ func (f *File) saveExprs(x interface{}, context astContext) {
198197
vs := spec.(*ast.ValueSpec)
199198
if vs.Type == nil {
200199
for _, name := range spec.(*ast.ValueSpec).Names {
201-
f.Consts[name.Name] = true
200+
consts[name.Name] = true
202201
}
203202
}
204203
}

src/cmd/cgo/gcc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ func (p *Package) isConst(f *File, x ast.Expr) bool {
12331233
strings.HasPrefix(x.Name, "_Ciconst_") ||
12341234
strings.HasPrefix(x.Name, "_Cfconst_") ||
12351235
strings.HasPrefix(x.Name, "_Csconst_") ||
1236-
f.Consts[x.Name]
1236+
consts[x.Name]
12371237
case *ast.UnaryExpr:
12381238
return p.isConst(f, x.X)
12391239
case *ast.BinaryExpr:

src/cmd/cgo/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ type File struct {
6262
Name map[string]*Name // map from Go name to Name
6363
NamePos map[*Name]token.Pos // map from Name to position of the first reference
6464
Edit *edit.Buffer
65-
Consts map[string]bool // untyped constants
6665
}
6766

67+
// Untyped constants in the current package.
68+
var consts = make(map[string]bool)
69+
6870
func (f *File) offset(p token.Pos) int {
6971
return fset.Position(p).Offset
7072
}

0 commit comments

Comments
 (0)