Skip to content

Commit 6797b32

Browse files
cmd/cgo: recognized untyped Go constants as untyped constants
Fixes #28772 Change-Id: I9446d95fb73fbcbb1cd9a4d2156ebc91bc9e91cb Reviewed-on: https://go-review.googlesource.com/c/149858 Run-TryBot: Brad Fitzpatrick <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 3faab79 commit 6797b32

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

misc/cgo/test/issue28545.go

+6
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@
33
// license that can be found in the LICENSE file.
44

55
// Failed to add type conversion for negative constant.
6+
// Issue 28772: Failed to add type conversion for Go constant set to C constant.
67
// No runtime test; just make sure it compiles.
78

89
package cgotest
910

1011
/*
1112
#include <complex.h>
1213
14+
#define issue28772Constant 1
15+
1316
static void issue28545F(char **p, int n, complex double a) {}
1417
*/
1518
import "C"
1619

20+
const issue28772Constant = C.issue28772Constant
21+
1722
func issue28545G(p **C.char) {
1823
C.issue28545F(p, -1, (0))
1924
C.issue28545F(p, 2+3, complex(1, 1))
25+
C.issue28545F(p, issue28772Constant, (0))
2026
}

src/cmd/cgo/ast.go

+13
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ 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)
6970

7071
// In ast1, find the import "C" line and get any extra C preamble.
7172
sawC := false
@@ -191,6 +192,18 @@ func (f *File) saveExprs(x interface{}, context astContext) {
191192
}
192193
case *ast.CallExpr:
193194
f.saveCall(x, context)
195+
case *ast.GenDecl:
196+
if x.Tok == token.CONST {
197+
for _, spec := range x.Specs {
198+
vs := spec.(*ast.ValueSpec)
199+
if vs.Type == nil {
200+
for _, name := range spec.(*ast.ValueSpec).Names {
201+
f.Consts[name.Name] = true
202+
}
203+
}
204+
}
205+
}
206+
194207
}
195208
}
196209

src/cmd/cgo/gcc.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,8 @@ func (p *Package) isConst(f *File, x ast.Expr) bool {
12321232
return x.Name == "nil" ||
12331233
strings.HasPrefix(x.Name, "_Ciconst_") ||
12341234
strings.HasPrefix(x.Name, "_Cfconst_") ||
1235-
strings.HasPrefix(x.Name, "_Csconst_")
1235+
strings.HasPrefix(x.Name, "_Csconst_") ||
1236+
f.Consts[x.Name]
12361237
case *ast.UnaryExpr:
12371238
return p.isConst(f, x.X)
12381239
case *ast.BinaryExpr:

src/cmd/cgo/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ 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
6566
}
6667

6768
func (f *File) offset(p token.Pos) int {

0 commit comments

Comments
 (0)