# Does \`@inbounds\` work for functions with keyword arguments?

**URL:** <https://discourse.julialang.org/t/does-inbounds-work-for-functions-with-keyword-arguments/105032>\
**Category:** General Usage\
**Created:** [October 16, 2023, 6:32pm UTC](https://discourse.julialang.org/t/does-inbounds-work-for-functions-with-keyword-arguments/105032 "2023-10-16T18:32:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [October 16, 2023, 6:32pm UTC](https://discourse.julialang.org/t/does-inbounds-work-for-functions-with-keyword-arguments/105032/1 "2023-10-16T18:32:48Z")

</div>

Consider the following functions:

```julia
@inline function f1(n)
    @boundscheck n == 0 && error("zero")
    n
end

@inline function f2(n; m = 0)
    @boundscheck n == 0 && error("zero")
    n+m
end

g1(n) = @inbounds f1(n)
g2(n) = @inbounds f2(n)
g3(n) = @inbounds f2(n; m = 0)

```

I thought that `g1(0)`, `g2(0)` and `g3(0)` would all return `0`. However,

```julia
julia> g1(0)
0

julia> g2(0)
ERROR: zero

julia> g3(0)
ERROR: zero

```

Is this a bug, or am I missing anything here? I couldn’t find anything in the documentation suggesting that `@inbounds` doesn’t work for functions with keyword arguments.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [October 16, 2023, 6:39pm UTC](https://discourse.julialang.org/t/does-inbounds-work-for-functions-with-keyword-arguments/105032/2 "2023-10-16T18:39:05Z")

</div>

I think `@inline` is not sufficient to ellide the boundchecks. Try with `@propagate_inbounds` instead.

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [October 16, 2023, 6:50pm UTC](https://discourse.julialang.org/t/does-inbounds-work-for-functions-with-keyword-arguments/105032/3 "2023-10-16T18:50:23Z")

</div>

Yes, with `@propagate_inbounds` I get `g2(0) == g3(0) == 0`. But at the same time, this applies `@inbounds` to all functions called in `f2`. This may not be desired. Example: Keep `g1`, `g2` and `g3` and define

```julia
@inline function f0(n)
    @boundscheck n == 0 && error("zero")
    n
end

@inline function f1(n)
    @boundscheck n == 0 && error("zero")
    f0(n)
end

Base.@propagate_inbounds function f2(n; m = 0)
    @boundscheck n == 0 && error("zero")
    f0(n+m)
end

```

Now

```julia
julia> g1(0)
ERROR: zero

julia> g2(0)
0

```

I think there should be a way to disable bounds checking in `f2` without disabling it in all functions called from `f2`. In this sense, I’m still asking myself whether the current behavior is intended or a bug.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [October 16, 2023, 6:55pm UTC](https://discourse.julialang.org/t/does-inbounds-work-for-functions-with-keyword-arguments/105032/4 "2023-10-16T18:55:18Z")

</div>

I think this is effectively [Functions with default arguments with `@boundscheck` can be confusing · Issue #30411 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/30411) — `@inbounds` only propagates through one function call but kwargs (and default positional args) are implemented with two function calls.

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [October 16, 2023, 7:09pm UTC](https://discourse.julialang.org/t/does-inbounds-work-for-functions-with-keyword-arguments/105032/5 "2023-10-16T19:09:01Z")

</div>

Yes, that explains it. Since the issue you referenced is still open, I wonder what the developers are planning. Wouldn’t the problem disappear if the compiler automatically added `@propagate_inbounds` to every additional function it needs to handle keyword and default arguments?
