# Strange behavior of @views with reverse indexing

**URL:** <https://discourse.julialang.org/t/strange-behavior-of-views-with-reverse-indexing/75508>\
**Category:** General Usage\
**Tags:** bug\
**Created:** [January 31, 2022, 1:54pm UTC](https://discourse.julialang.org/t/strange-behavior-of-views-with-reverse-indexing/75508 "2022-01-31T13:54:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![romainvieme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romainvieme/32/33349_2.png) [@romainvieme](https://discourse.julialang.org/u/romainvieme)\
**Post date:** [January 31, 2022, 1:54pm UTC](https://discourse.julialang.org/t/strange-behavior-of-views-with-reverse-indexing/75508/1 "2022-01-31T13:54:39Z")

</div>

I’ve noticed a strange behavior using @views with reverse indexing. When I execute the following code:

```julia
using LinearAlgebra
A=[1.0; 2.0]; B=[3.0; 4.0]; C = zeros(1); D = zeros(1);
mul!(C, A[1:end]', B[end:-1:1])
@views mul!(D, A[1:end]', B[end:-1:1])

```

I get C=10.0 (which is expected) and D=8.0 (which is strange).  
The same code with integers does not lead to such surprises:

```julia
A=[1; 2]; B=[3; 4]; C = zeros(Int64, 1); D = zeros(Int64, 1);
mul!(C, A[1:end]', B[end:-1:1])
@views mul!(D, A[1:end]', B[end:-1:1])

```

leads to C=10 and D=10 (expected).  
Does anyone know what happens here?

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [January 31, 2022, 2:10pm UTC](https://discourse.julialang.org/t/strange-behavior-of-views-with-reverse-indexing/75508/2 "2022-01-31T14:10:31Z")

</div>

```julia
mul!(D, (@view A[1:end]'), (@view B[end:-1:1]))

```

yields

```julia
ERROR: LoadError: ArgumentError: Invalid use of @view macro: argument must be a reference expression A[...].

```

With

```julia
mul!(D, A[1:end]', (@view B[end:-1:1]))

```

the result is wrong. This looks like bug in `@view`.

Edit: @BioTurboNick double checked with parentheses, the analysis still holds.  
Edit: corrected my misunderstanding regarding @views.

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [January 31, 2022, 2:11pm UTC](https://discourse.julialang.org/t/strange-behavior-of-views-with-reverse-indexing/75508/3 "2022-01-31T14:11:17Z")

</div>

You have to wrap `@view` in parentheses so it knows not to continue past the end bracket.

```julia
 mul!(D, (@view A[1:end])', (@view B[end:-1:1]))

```

---

<div class="post-metadata">

**Author:** ![romainvieme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romainvieme/32/33349_2.png) [@romainvieme](https://discourse.julialang.org/u/romainvieme)\
**Post date:** [January 31, 2022, 2:16pm UTC](https://discourse.julialang.org/t/strange-behavior-of-views-with-reverse-indexing/75508/4 "2022-01-31T14:16:26Z")

</div>

@BioTurboNick Thanks for your message. I just tried it:

```julia
julia> mul!(D, (@view A[1:end])', (@view B[end:-1:1]))
1-element Vector{Float64}:
 8.0

```

so it’s executing but the result is not correct (we expect 10.0)

---

<div class="post-metadata">

**Author:** ![romainvieme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romainvieme/32/33349_2.png) [@romainvieme](https://discourse.julialang.org/u/romainvieme)\
**Post date:** [January 31, 2022, 2:18pm UTC](https://discourse.julialang.org/t/strange-behavior-of-views-with-reverse-indexing/75508/5 "2022-01-31T14:18:58Z")

</div>

@goerch Thanks, I also suspect a bug but it’s good to hear it from someone else

---

<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:** [January 31, 2022, 2:19pm UTC](https://discourse.julialang.org/t/strange-behavior-of-views-with-reverse-indexing/75508/6 "2022-01-31T14:19:02Z")

</div>

Yeah, the invalid use of `@view` thing is a non-sequitur. This is a true bug; the MWE is simply:

```julia
julia> B = [3.0; 4.0];

julia> [1. 2.]*B[end:-1:1]
1-element Vector{Float64}:
 10.0

julia> [1. 2.]*(@view B[end:-1:1])
1-element Vector{Float64}:
 8.0

```

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [January 31, 2022, 2:21pm UTC](https://discourse.julialang.org/t/strange-behavior-of-views-with-reverse-indexing/75508/7 "2022-01-31T14:21:49Z")

</div>

This is [#30765](https://github.com/JuliaLang/julia/issues/30765), which is fixed on master.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [January 31, 2022, 2:26pm UTC](https://discourse.julialang.org/t/strange-behavior-of-views-with-reverse-indexing/75508/8 "2022-01-31T14:26:08Z")

</div>

The fix didn’t find its way into Julia 1.7.1? Hm, this is silent data corruption…

---

<div class="post-metadata">

**Author:** ![romainvieme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romainvieme/32/33349_2.png) [@romainvieme](https://discourse.julialang.org/u/romainvieme)\
**Post date:** [January 31, 2022, 2:30pm UTC](https://discourse.julialang.org/t/strange-behavior-of-views-with-reverse-indexing/75508/9 "2022-01-31T14:30:09Z")

</div>

@sostock thanks, I think that’s what I was looking for though the bug is still present in Julia 1.7.1. Any idea when this will be fixed in stable?

---

<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:** [January 31, 2022, 2:35pm UTC](https://discourse.julialang.org/t/strange-behavior-of-views-with-reverse-indexing/75508/10 "2022-01-31T14:35:03Z")

</div>

I just flagged its fix for backporting. The next patch release is under testing; it’ll probably be in the subsequent one, but we probably should fix accompanying regression first and ensure they come together. @sostock could you make it clearer why one might prefer [#42054](https://github.com/JuliaLang/julia/pull/42054) to [#42012](https://github.com/JuliaLang/julia/pull/42012)?

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [January 31, 2022, 6:02pm UTC](https://discourse.julialang.org/t/strange-behavior-of-views-with-reverse-indexing/75508/11 "2022-01-31T18:02:09Z")

</div>

> [@mbauman](#):
>
> @sostock could you make it clearer why one might prefer [#42054](https://github.com/JuliaLang/julia/pull/42054) to [#42012](https://github.com/JuliaLang/julia/pull/42012)?

[#42054](https://github.com/JuliaLang/julia/pull/42054) allows the _matrix_ to have negative strides as well (in the second dimension), not just the vector. [#42012](https://github.com/JuliaLang/julia/pull/42012) allows this only in trivial cases (the ones that worked correctly before [#41513](https://github.com/JuliaLang/julia/pull/41513), i.e., if the matrix is empty or has only one column) and otherwise errors for matrices with negative `stride(A, 2)`.
