Skip to content

Commit

Permalink
pkg.NewType/AliasType bugfix: should insert into global scope
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Aug 15, 2021
1 parent 0de0c45 commit 6013a45
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 9 additions & 2 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,19 @@ func (p *CodeBuilder) NewClosureWith(sig *types.Signature) *Func {

// NewType func
func (p *CodeBuilder) NewType(name string) *TypeDecl {
return p.pkg.NewType(name)
if debugInstr {
log.Println("NewType", name)
}
return p.pkg.doNewType(p.current.scope, name, nil, 0)
}

// AliasType func
func (p *CodeBuilder) AliasType(name string, typ types.Type) *types.Named {
return p.pkg.AliasType(name, typ)
if debugInstr {
log.Println("AliasType", name, typ)
}
decl := p.pkg.doNewType(p.current.scope, name, typ, 1)
return decl.typ
}

// NewConstStart func
Expand Down
7 changes: 3 additions & 4 deletions type_var_and_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (p *Package) AliasType(name string, typ types.Type) *types.Named {
if debugInstr {
log.Println("AliasType", name, typ)
}
decl := p.newType(name, typ, 1)
decl := p.doNewType(p.Types.Scope(), name, typ, 1)
return decl.typ
}

Expand All @@ -76,12 +76,11 @@ func (p *Package) NewType(name string) *TypeDecl {
if debugInstr {
log.Println("NewType", name)
}
return p.newType(name, nil, 0)
return p.doNewType(p.Types.Scope(), name, nil, 0)
}

func (p *Package) newType(name string, typ types.Type, alias token.Pos) *TypeDecl {
func (p *Package) doNewType(scope *types.Scope, name string, typ types.Type, alias token.Pos) *TypeDecl {
typName := types.NewTypeName(token.NoPos, p.Types, name, typ)
scope := p.cb.current.scope
if scope.Insert(typName) != nil {
log.Panicln("TODO: type already defined -", name)
}
Expand Down

0 comments on commit 6013a45

Please sign in to comment.