Skip to content
Draft
Changes from 1 commit
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
47 changes: 42 additions & 5 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
old, _ := p.SetCurFile(goFile, true)
defer p.RestoreCurFile(old)

preloadFuncDecl := func(d *ast.FuncDecl) {
preloadFuncDecl := func(d *ast.FuncDecl, overload bool) {
if ctx.classRecv != nil { // in class file (.spx/.gmx)
if recv := d.Recv; recv == nil || len(recv.List) == 0 {
d.Recv = ctx.classRecv
Expand All @@ -1018,7 +1018,13 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
fn := func() {
old, _ := p.SetCurFile(goFile, true)
defer p.RestoreCurFile(old)
loadFunc(ctx, nil, fname, d, genFnBody)
if overload {
pkg := ctx.pkg.Types
fn := gogen.NewOverloadFunc(d.Name.Pos(), pkg, fname)
pkg.Scope().Insert(fn)
} else {
loadFunc(ctx, nil, fname, d, genFnBody)
}
}
if fname == "init" {
if genFnBody {
Expand Down Expand Up @@ -1060,7 +1066,13 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
defer p.RestoreCurFile(old)
doInitType(ld)
recv := toRecv(ctx, d.Recv)
loadFunc(ctx, recv, fname, d, genFnBody)
if overload {
pkg := ctx.pkg.Types
typ := pkg.Scope().Lookup(tname).Type().(*types.Named)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security & Quality Issue: Unsafe type lookup/assertion chain. This will panic if Lookup(tname) returns nil or type is not *types.Named.

Suggested change
typ := pkg.Scope().Lookup(tname).Type().(*types.Named)
obj := pkg.Scope().Lookup(tname)
if obj == nil {
ctx.handleErrorf(d.Name.Pos(), d.Name.End(), "type %s not found", tname)
return
}
typ, ok := obj.Type().(*types.Named)
if !ok {
ctx.handleErrorf(d.Name.Pos(), d.Name.End(), "type %s is not a named type", tname)
return
}
gogen.NewOverloadMethod(typ, d.Name.Pos(), pkg, fname)

gogen.NewOverloadMethod(typ, d.Name.Pos(), pkg, fname)
} else {
loadFunc(ctx, recv, fname, d, genFnBody)
}
}
ld.methods = append(ld.methods, fn)
}
Expand Down Expand Up @@ -1194,7 +1206,7 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
}

case *ast.FuncDecl:
preloadFuncDecl(d)
preloadFuncDecl(d, false)

case *ast.OverloadFuncDecl:
var recv *ast.Ident
Expand Down Expand Up @@ -1282,12 +1294,33 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
Name: id,
Type: expr.Type,
Body: expr.Body,
})
}, false)
default:
ctx.handleErrorf(expr.Pos(), expr.End(), "unknown func %v", ctx.LoadExpr(expr))
break LoopFunc
}
}
if d.Recv == nil {
ctx.lbinames = append(ctx.lbinames, d.Name.Name)
}
preloadFuncDecl(&ast.FuncDecl{
Doc: d.Doc,
Recv: d.Recv,
Name: d.Name,
Type: &ast.FuncType{
Params: &ast.FieldList{
List: []*ast.Field{
&ast.Field{
Names: []*ast.Ident{
ast.NewIdent(overloadArgs),
},
Type: ast.NewIdent("any"),
},
},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: Unnecessary & in Field literal (slice already takes address). Also consider adding a comment explaining this placeholder signature.

Suggested change
List: []*ast.Field{
&ast.Field{
Names: []*ast.Ident{
ast.NewIdent(overloadArgs),
},
Type: ast.NewIdent("any"),
},
},
// Create placeholder function signature for overload preloading
// with single parameter (__xgo_overload_args__: any)
Params: &ast.FieldList{
List: []*ast.Field{
{
Names: []*ast.Ident{ast.NewIdent(overloadArgs)},
Type: ast.NewIdent("any"),
},
},
},

},
},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Type field of this ast.FuncDecl is constructed with a specific signature, but it appears to be unused. When preloadFuncDecl is called with overload=true, as it is here, the code path that uses the Type field is not executed. This makes the Type definition unnecessary and potentially confusing.

To improve clarity and remove this dead code, you can replace this block with Type: nil.

Type: nil,

}, true)

if exov { // need Gopo_xxx
oname, err := overloadName(recv, name.Name, d.Operator)
if err != nil {
Expand Down Expand Up @@ -1324,6 +1357,10 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
}
}

const (
overloadArgs = "__xgo_overload_args__"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation: Add doc comment explaining this constant's purpose as placeholder parameter name for preloading overload signatures.

Suggested change
overloadArgs = "__xgo_overload_args__"
const (
// overloadArgs is the placeholder parameter name used when preloading overload function
// signatures. This allows overload functions to be registered in the package scope
// during the preload phase before actual implementations are processed.
overloadArgs = "__xgo_overload_args__"
)

)

func checkOverloadMethodRecvType(ot *ast.Ident, recv ast.Expr) (*ast.Ident, bool) {
rtyp, _, ok := getRecvType(recv)
if !ok {
Expand Down
Loading