Skip to content

Commit

Permalink
Merge pull request #76 from pigpigyyy/setFinalizer-check
Browse files Browse the repository at this point in the history
Fixed issue #72.
  • Loading branch information
3dgen authored Jan 7, 2025
2 parents 4440cb5 + 40eac15 commit d6da586
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/types/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,27 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
// TODO(gri) "use" all arguments?
return
}
switch i {
case 0:
if !isPointer(x.typ) {
check.invalidArg(x.pos(), "%s is not a pointer", x)
return
}
case 1:
sig, ok := x.typ.(*Signature)
if !ok {
check.invalidArg(x.pos(), "%s is not a function", x)
return
}
if sig.Params().Len() != 1 || sig.Results().Len() != 0 {
check.invalidArg(x.pos(), "expect 1 parameter and 0 result for function, but found %d and %d", sig.Params().Len(), sig.Results().Len())
return
}
if sig.Params().At(0).Type().String() != "u32" {
check.invalidArg(x.pos(), "expect u32 as parameter for function, but found %s", sig.Params().At(0).Type().String())
return
}
}
params[i] = x.typ
}
}
Expand Down
7 changes: 7 additions & 0 deletions internal/types/testdata/builtins.src
Original file line number Diff line number Diff line change
Expand Up @@ -900,3 +900,10 @@ func trace2() {
// trace(f2(), 1, 2, 3)
// trace(f3(), 1, 2, 3, 4)
}

func setFinalizer1() {
i := 1
setFinalizer(i, func(ptr: i32) {}) // ERROR is not a pointer
setFinalizer(&i, func(ptr: i32) {}) // ERROR expect u32 as parameter
setFinalizer(&i, func(ptr: u32, y: i64) {}) // ERROR expect 1 parameter and 0 result
}

0 comments on commit d6da586

Please sign in to comment.