Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg.NewFuncWith: allowRedecl (for c2go) #270

Merged
merged 2 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,11 @@ func (p *Package) NewFuncWith(
} else if name != "_" { // skip underscore
old := p.Types.Scope().Insert(fn.Obj())
if old != nil {
oldPos := cb.position(old.Pos())
return nil, cb.newCodePosErrorf(
pos, "%s redeclared in this block\n\t%v: other declaration of %s", name, oldPos, name)
if !(p.allowRedecl && types.Identical(old.Type(), sig)) { // for c2go
oldPos := cb.position(old.Pos())
return nil, cb.newCodePosErrorf(
pos, "%s redeclared in this block\n\t%v: other declaration of %s", name, oldPos, name)
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ type Package struct {
autoIdx int
commentedStmts map[ast.Stmt]*ast.CommentGroup
implicitCast func(pkg *Package, V, T types.Type, pv *Element) bool
allowVarRedecl bool
allowRedecl bool // for c2go
isGopPkg bool
}

Expand Down Expand Up @@ -348,9 +348,9 @@ func (p *Package) setStmtComments(stmt ast.Stmt, comments *ast.CommentGroup) {
p.commentedStmts[stmt] = comments
}

// SetVarRedeclarable sets to allow redeclaration of variables or not.
func (p *Package) SetVarRedeclarable(allowVarRedecl bool) {
p.allowVarRedecl = allowVarRedecl
// SetRedeclarable sets to allow redeclaration of variables/functions or not.
func (p *Package) SetRedeclarable(allowRedecl bool) {
p.allowRedecl = allowRedecl
}

// Sizeof returns sizeof typ in bytes.
Expand Down
2 changes: 1 addition & 1 deletion package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ const (

func TestDeleteVarDecl(t *testing.T) {
pkg := newMainPackage()
pkg.SetVarRedeclarable(true)
pkg.SetRedeclarable(true)
scope := pkg.CB().Scope()
defs := pkg.NewVarDefs(scope)
decl := defs.New(token.NoPos, types.Typ[types.Int], "a", "b")
Expand Down
4 changes: 2 additions & 2 deletions type_var_and_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ func (p *Package) newValueDecl(
}
if typ != nil && tok == token.VAR {
if old := scope.Insert(types.NewVar(pos, p.Types, name, typ)); old != nil {
allowVarRedecl := p.allowVarRedecl && scope == p.Types.Scope()
if !allowVarRedecl || !types.Identical(old.Type(), typ) {
allowRedecl := p.allowRedecl && scope == p.Types.Scope()
if !(allowRedecl && types.Identical(old.Type(), typ)) { // for c2go
oldpos := p.cb.position(old.Pos())
p.cb.panicCodePosErrorf(
pos, "%s redeclared in this block\n\tprevious declaration at %v", name, oldpos)
Expand Down