# @cfunctions with keyword arguments

**URL:** <https://discourse.julialang.org/t/cfunctions-with-keyword-arguments/11088>\
**Category:** General Usage\
**Tags:** question, ccall, bug\
**Created:** [May 23, 2018, 4:38am UTC](https://discourse.julialang.org/t/cfunctions-with-keyword-arguments/11088 "2018-05-23T04:38:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkozdon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jkozdon/32/7327_2.png) [@jkozdon](https://discourse.julialang.org/u/jkozdon)\
**Post date:** [May 23, 2018, 4:38am UTC](https://discourse.julialang.org/t/cfunctions-with-keyword-arguments/11088/1 "2018-05-23T04:38:12Z")

</div>

I believe that this is a bug in the nightly build but wanted to check before submitting an issue.

When using the `@cfunction` macro within a closure with keyword arguments, I get an error message from Julia if the keyword arguments are not specified. Namely, in the following example

```nohighlight
function foo()::Cint
  return 1
end

function test_no_kw(fn)
  callback = @cfunction $fn Cint ()
end

function test_kw(fn; dummy = 1)
  callback = @cfunction $fn Cint ()
end

```

These two calls produce no error messages:

```nohighlight
# No error
let
  test_no_kw(foo)
end

# No error
let
  test_kw(foo; dummy = 1)
end

```

But when the keyword arguments are not specified:

```nohighlight
# error message
let
  test_kw(foo)
end

```

I get the following error messages:

```nohighlight
Internal error: encountered unexpected error in runtime:
BoundsError(a=Array{Bool, (2,)}[false, false], i=(4,))
rec_backtrace at /Users/osx/buildbot/slave/package_osx64/build/src/stackwalk.c:94
record_backtrace at /Users/osx/buildbot/slave/package_osx64/build/src/task.c:246 [inlined]
jl_throw at /Users/osx/buildbot/slave/package_osx64/build/src/task.c:577
jl_bounds_error_ints at /Users/osx/buildbot/slave/package_osx64/build/src/rtutils.c:187
setindex! at ./array.jl:707 [inlined]
_widen_all_consts! at ./compiler/optimize.jl:677
_widen_all_consts! at ./compiler/optimize.jl:662
widen_all_consts! at ./compiler/optimize.jl:695
optimize at ./compiler/optimize.jl:331
typeinf at ./compiler/typeinfer.jl:440
typeinf_frame at ./compiler/typeinfer.jl:96 [inlined]
typeinf_code at ./compiler/typeinfer.jl:191
typeinf_ext at ./compiler/typeinfer.jl:233
jfptr_typeinf_ext_12.clone_1 at /Applications/Julia-0.7.app/Contents/Resources/julia/lib/julia/sys.dylib (unknown line)
jl_apply at /Users/osx/buildbot/slave/package_osx64/build/src/./julia.h:1540 [inlined]
jl_apply_with_saved_exception_state at /Users/osx/buildbot/slave/package_osx64/build/src/rtutils.c:257
jl_type_infer at /Users/osx/buildbot/slave/package_osx64/build/src/gf.c:274
jl_compile_method_internal at /Users/osx/buildbot/slave/package_osx64/build/src/gf.c:1771
jl_fptr_trampoline at /Users/osx/buildbot/slave/package_osx64/build/src/gf.c:1815
do_call at /Users/osx/buildbot/slave/package_osx64/build/src/interpreter.c:324
eval_body at /Users/osx/buildbot/slave/package_osx64/build/src/interpreter.c:559
jl_interpret_toplevel_thunk_callback at /Users/osx/buildbot/slave/package_osx64/build/src/interpreter.c:798
unknown function (ip: 0xfffffffffffffffe)
unknown function (ip: 0x11548042f)
unknown function (ip: 0xffffffffffffffff)
jl_interpret_toplevel_thunk at /Users/osx/buildbot/slave/package_osx64/build/src/interpreter.c:807
jl_toplevel_eval_flex at /Users/osx/buildbot/slave/package_osx64/build/src/toplevel.c:856
jl_toplevel_eval_in at /Users/osx/buildbot/slave/package_osx64/build/src/builtins.c:631
eval at ./boot.jl:317 [inlined]
eval at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v0.7/REPL/src/REPL.jl:5
eval_user_input at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v0.7/REPL/src/REPL.jl:85
macro expansion at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v0.7/REPL/src/REPL.jl:116 [inlined]
#28 at ./event.jl:92
jl_fptr_trampoline at /Users/osx/buildbot/slave/package_osx64/build/src/gf.c:1816
jl_apply at /Users/osx/buildbot/slave/package_osx64/build/src/./julia.h:1540 [inlined]
start_task at /Users/osx/buildbot/slave/package_osx64/build/src/task.c:268
Base.CFunction(Ptr{Nothing} @0x000000012ba7af40, foo, Ptr{Nothing} @0x0000000000000000, Ptr{Nothing} @0x0000000000000000)

```

The for more complex examples, e.g., `qsort`, the function pointer returned in the error prone case seems to be fine, but I have not exstensively tested this.

Here is my `versioninfo()`

```nohighlight
Julia Version 0.7.0-DEV.5105
Commit a1a62f98e7 (2018-05-15 09:52 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin14.5.0)
  CPU: Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, haswell)

```

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [May 23, 2018, 11:10pm UTC](https://discourse.julialang.org/t/cfunctions-with-keyword-arguments/11088/2 "2018-05-23T23:10:34Z")

</div>

Yep, it’s a big with the new optimizer. Keno just put up a PR to fix it

---

<div class="post-metadata">

**Author:** ![jkozdon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jkozdon/32/7327_2.png) [@jkozdon](https://discourse.julialang.org/u/jkozdon)\
**Post date:** [May 24, 2018, 12:34am UTC](https://discourse.julialang.org/t/cfunctions-with-keyword-arguments/11088/3 "2018-05-24T00:34:21Z")

</div>

Awesome. Thanks!
