# Inconsistent bounds-checking of tuples

**URL:** <https://discourse.julialang.org/t/inconsistent-bounds-checking-of-tuples/12102>\
**Category:** General Usage\
**Created:** [July 2, 2018, 5:48pm UTC](https://discourse.julialang.org/t/inconsistent-bounds-checking-of-tuples/12102 "2018-07-02T17:48:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cafaxo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cafaxo/32/5234_2.png) [@cafaxo](https://discourse.julialang.org/u/cafaxo)\
**Post date:** [July 2, 2018, 5:48pm UTC](https://discourse.julialang.org/t/inconsistent-bounds-checking-of-tuples/12102/1 "2018-07-02T17:48:48Z")

</div>

As expected, `(1,)[2]` throws an out-of-bounds error:

```julia
julia> (1,)[2]
ERROR: BoundsError: attempt to access (1,)
  at index [2]

```

Doing this, however, does not throw an error:

```julia
julia> struct Test end

julia> (Test(),)[2]
Test()

```

Why is that?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [July 2, 2018, 5:58pm UTC](https://discourse.julialang.org/t/inconsistent-bounds-checking-of-tuples/12102/2 "2018-07-02T17:58:33Z")

</div>

That’s incredibly strange. It looks like a bug.

For me it only occurs when types are declared interactively, types from pre-compiled modules behave as expected. The `@edit` macro isn’t able to find the `getindex` call when I do this interactively either.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [July 2, 2018, 7:45pm UTC](https://discourse.julialang.org/t/inconsistent-bounds-checking-of-tuples/12102/3 "2018-07-02T19:45:52Z")

</div>

It looks to happen when there is no fields because `(Void(),)[2]` does not throw on `v0.6.3`.

```julia-repl
julia> a = (Void(),)
(nothing,)

julia> a[1]

julia> a[2]

julia> a[3]

```

but

```julia-repl
julia> struct Zs; x::Int end

julia> a = (Zs(2),)
(Zs(2),)

julia> a[4]
ERROR: BoundsError: attempt to access (Zs(2),)
  at index [4]
Stacktrace:
 [1] getindex(::Tuple{Zs}, ::Int64) at ./tuple.jl:21

```

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [July 2, 2018, 8:34pm UTC](https://discourse.julialang.org/t/inconsistent-bounds-checking-of-tuples/12102/4 "2018-07-02T20:34:32Z")

</div>

That’s amusing! Also does the same on 0.7; as far as I could gather, whenever you have a homogeneous tuple of singletons like also

```julia
julia> ((),())[500]
()
julia> (((),()),)[100]
((), ())
julia> ((((),),),)[100][100][100]
()

```

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [July 2, 2018, 8:39pm UTC](https://discourse.julialang.org/t/inconsistent-bounds-checking-of-tuples/12102/5 "2018-07-02T20:39:14Z")

</div>

Yes, this is a bug. Please file an issue.

---

<div class="post-metadata">

**Author:** ![cafaxo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cafaxo/32/5234_2.png) [@cafaxo](https://discourse.julialang.org/u/cafaxo)\
**Post date:** [July 2, 2018, 9:51pm UTC](https://discourse.julialang.org/t/inconsistent-bounds-checking-of-tuples/12102/6 "2018-07-02T21:51:04Z")

</div>

It looks like `Base.getindex(%%x, 2)` is incorrectly inferred to `Core.Compiler.Const(Test(), false)`:

```julia
julia> struct Test end

julia> g2(x) = x[2];

julia> m = first(methods(g2));

julia> params = Core.Compiler.Params(typemax(UInt));

julia> Core.Compiler.typeinf_code(m, Tuple{Tuple{Test}}, Core.svec(), false, params)
(CodeInfo(
1 1 ─ %1 = Base.getindex(%%x, 2)::Core.Compiler.Const(Test(), false) │
  └── return %1 │
), Test)

```

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [July 2, 2018, 9:55pm UTC](https://discourse.julialang.org/t/inconsistent-bounds-checking-of-tuples/12102/7 "2018-07-02T21:55:17Z")

</div>

That inference is correct. I suspect the problem is that the optimizer later doesn’t recognize that it can’t rely on it without performing the bounds checking.
