Skip to content

Commit

Permalink
Merge pull request #401 from xushiwei/q
Browse files Browse the repository at this point in the history
don't define GopPackage for main package
  • Loading branch information
xushiwei authored Mar 8, 2024
2 parents 429f123 + 8d818e1 commit 6ff7f61
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 23 deletions.
6 changes: 3 additions & 3 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ func matchTypeCast(pkg *Package, typ types.Type, fn *internal.Elem, args []*inte
o := t.Obj()
if at := o.Pkg(); at != nil {
tname := o.Name()
if checkUntypedOverflows(pkg, at.Scope(), tname, args[0]) {
if checkUntypedOverflows(at.Scope(), tname, args[0]) {
src, pos := pkg.cb.loadExpr(args[0].Src)
err = pkg.cb.newCodeError(pos, fmt.Sprintf("cannot convert %v (untyped int constant %v) to type %v", src, args[0].CVal, tname))
return
Expand All @@ -815,7 +815,7 @@ func matchTypeCast(pkg *Package, typ types.Type, fn *internal.Elem, args []*inte
name := tname + "_Cast"
if cast := scope.Lookup(name); cast != nil {
if len(args) == 1 && args[0].CVal != nil {
if checkUntypedOverflows(pkg, scope, tname, args[0]) {
if checkUntypedOverflows(scope, tname, args[0]) {
src, pos := pkg.cb.loadExpr(args[0].Src)
err = pkg.cb.newCodeError(pos, fmt.Sprintf("cannot convert %v (untyped int constant %v) to type %v", src, args[0].CVal, tname))
return
Expand Down Expand Up @@ -1358,7 +1358,7 @@ func checkUntypedType(scope *types.Scope, tname string) bool {
return false
}

func checkUntypedOverflows(pkg *Package, scope *types.Scope, tname string, arg *internal.Elem) bool {
func checkUntypedOverflows(scope *types.Scope, tname string, arg *internal.Elem) bool {
cmax, ok1 := scope.Lookup(tname + "_Max").(*types.Const)
cmin, ok2 := scope.Lookup(tname + "_Min").(*types.Const)
if ok1 || ok2 {
Expand Down
2 changes: 1 addition & 1 deletion c.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (p *UnionFields) Len() int {
}

func (p *UnionFields) getField(
cb *CodeBuilder, tfld *types.Named, name string, src ast.Node, ref bool) MemberKind {
cb *CodeBuilder, tfld *types.Named, name string, _ ast.Node, ref bool) MemberKind {
for _, v := range p.flds {
if v.Name == name {
obj := cb.stk.Pop()
Expand Down
6 changes: 3 additions & 3 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,7 @@ retry:
return kind
}
case *types.Basic, *types.Slice, *types.Map, *types.Chan:
return p.btiMethod(p.getBuiltinTI(o), name, aliasName, flag, arg, srcExpr)
return p.btiMethod(p.getBuiltinTI(o), name, aliasName, flag, srcExpr)
}
return MemberInvalid
}
Expand Down Expand Up @@ -1752,7 +1752,7 @@ func (p *CodeBuilder) method(
return MemberMethod
}
if t, ok := o.(*types.Named); ok {
kind = p.btiMethod(p.getBuiltinTI(t), name, aliasName, flag, arg, src)
kind = p.btiMethod(p.getBuiltinTI(t), name, aliasName, flag, src)
}
return
}
Expand All @@ -1768,7 +1768,7 @@ func isTypeConvert(otyp, typ types.Type) (string, bool) {
}

func (p *CodeBuilder) btiMethod(
o *builtinTI, name, aliasName string, flag MemberFlag, arg *Element, src ast.Node) MemberKind {
o *builtinTI, name, aliasName string, flag MemberFlag, src ast.Node) MemberKind {
if o != nil {
for i, n := 0, o.NumMethods(); i < n; i++ {
method := o.Method(i)
Expand Down
4 changes: 2 additions & 2 deletions gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func newGopMainPackage() *gox.Package {
// ----------------------------------------------------------------------------

func TestGopoConst(t *testing.T) {
pkg := newMainPackage()
pkg := newPackage("foo")
pkg.CB().NewConstStart(nil, "Gopo_x").
Val("Hello").EndInit(1)
domTest(t, pkg, `package main
domTest(t, pkg, `package foo
const GopPackage = true
const Gopo_x = "Hello"
Expand Down
2 changes: 1 addition & 1 deletion import.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ type expDeps struct {
}

func checkGopPkg(pkg *Package) (val ast.Expr, ok bool) {
if pkg.Types.Scope().Lookup(gopPackage) != nil {
if pkg.Types.Name() == "main" || pkg.Types.Scope().Lookup(gopPackage) != nil {
return
}
ed := expDeps{pkg.Types, make(map[*types.Package]none), make(map[types.Type]none)}
Expand Down
17 changes: 8 additions & 9 deletions package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ func ctxRef(pkg *gox.Package, name string) gox.Ref {

type eventRecorder struct{}

func (p eventRecorder) Member(id ast.Node, obj types.Object) {
}
func (p eventRecorder) Call(fn ast.Node, obj types.Object) {
}
func (p eventRecorder) Member(id ast.Node, obj types.Object) {}
func (p eventRecorder) Call(fn ast.Node, obj types.Object) {}

func newMainPackage(
implicitCast ...func(pkg *gox.Package, V, T types.Type, pv *gox.Element) bool) *gox.Package {
return newPackage("main", implicitCast...)
}

func newPackage(
name string, implicitCast ...func(pkg *gox.Package, V, T types.Type, pv *gox.Element) bool) *gox.Package {
conf := &gox.Config{
Fset: gblFset,
Importer: gblImp,
Expand All @@ -71,7 +74,7 @@ func newMainPackage(
conf.HandleErr = handleErr
handleErr = nil
}
return gox.NewPackage("", "main", conf)
return gox.NewPackage("", name, conf)
}

func domTest(t *testing.T, pkg *gox.Package, expected string) {
Expand Down Expand Up @@ -1612,8 +1615,6 @@ func TestDefOverloadFunc(t *testing.T) {
End()
domTest(t, pkg, `package main
const GopPackage = true
func bar__0() {
}
`)
Expand All @@ -1625,8 +1626,6 @@ func TestDefTemplateMethod(t *testing.T) {
End()
domTest(t, pkg, `package main
const GopPackage = true
func Gopt_bar() {
}
`)
Expand Down
2 changes: 1 addition & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func assignable(pkg *Package, v types.Type, t *types.Named, pv *internal.Elem) b
}
}
if pv.CVal != nil {
if checkUntypedOverflows(pkg, scope, tname, pv) {
if checkUntypedOverflows(scope, tname, pv) {
return false
}
}
Expand Down
6 changes: 3 additions & 3 deletions typeparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func infer(pkg *Package, posn positioner, tparams []*types.TypeParam, targs []ty
return
}

func getParamsTypes(pkg *Package, tuple *types.Tuple, variadic bool) string {
func getParamsTypes(tuple *types.Tuple, variadic bool) string {
n := tuple.Len()
if n == 0 {
return ""
Expand All @@ -279,7 +279,7 @@ func checkInferArgs(pkg *Package, fn *internal.Elem, sig *types.Signature, args
if nargs < nreq-1 {
caller := types.ExprString(fn.Val)
return nil, fmt.Errorf(
"not enough arguments in call to %s\n\thave (%v)\n\twant (%v)", caller, getTypes(args), getParamsTypes(pkg, sig.Params(), true))
"not enough arguments in call to %s\n\thave (%v)\n\twant (%v)", caller, getTypes(args), getParamsTypes(sig.Params(), true))
}
if flags&InstrFlagEllipsis != 0 {
return args, nil
Expand Down Expand Up @@ -307,7 +307,7 @@ func checkInferArgs(pkg *Package, fn *internal.Elem, sig *types.Signature, args
}
caller := types.ExprString(fn.Val)
return nil, fmt.Errorf(
"%s arguments in call to %s\n\thave (%v)\n\twant (%v)", fewOrMany, caller, getTypes(args), getParamsTypes(pkg, sig.Params(), false))
"%s arguments in call to %s\n\thave (%v)\n\twant (%v)", fewOrMany, caller, getTypes(args), getParamsTypes(sig.Params(), false))
}
return args, nil
}
Expand Down

0 comments on commit 6ff7f61

Please sign in to comment.