From 6013a45bc18cbf6a71606e7034b94509079214ae Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sun, 15 Aug 2021 20:25:28 +0800 Subject: [PATCH] pkg.NewType/AliasType bugfix: should insert into global scope --- codebuild.go | 11 +++++++++-- type_var_and_const.go | 7 +++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/codebuild.go b/codebuild.go index 0c438197..11ac56df 100644 --- a/codebuild.go +++ b/codebuild.go @@ -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 diff --git a/type_var_and_const.go b/type_var_and_const.go index 3493ee23..2b4362db 100644 --- a/type_var_and_const.go +++ b/type_var_and_const.go @@ -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 } @@ -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) }