From 12e45d01a4e94dbb9c4abeb2b2af9040d58d150f Mon Sep 17 00:00:00 2001 From: Miles Date: Wed, 15 May 2024 01:07:36 +0200 Subject: [PATCH] [SparseArrayDOKs] Fix escaping in @maybe_grow (#1442) --- NDTensors/src/lib/SparseArrayDOKs/src/sparsearraydok.jl | 2 +- NDTensors/src/lib/SparseArrayDOKs/test/runtests.jl | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/lib/SparseArrayDOKs/src/sparsearraydok.jl b/NDTensors/src/lib/SparseArrayDOKs/src/sparsearraydok.jl index 3949abac5e..d3b2e913ca 100644 --- a/NDTensors/src/lib/SparseArrayDOKs/src/sparsearraydok.jl +++ b/NDTensors/src/lib/SparseArrayDOKs/src/sparsearraydok.jl @@ -144,5 +144,5 @@ macro maybe_grow(expr) ) end @capture(expr, array_[indices__] = value_) - return :(setindex_maybe_grow!($(esc(array)), $value, $indices...)) + return :(setindex_maybe_grow!($(esc(array)), $(esc(value)), $(esc.(indices)...))) end diff --git a/NDTensors/src/lib/SparseArrayDOKs/test/runtests.jl b/NDTensors/src/lib/SparseArrayDOKs/test/runtests.jl index 8d786b9525..ee2262bea0 100644 --- a/NDTensors/src/lib/SparseArrayDOKs/test/runtests.jl +++ b/NDTensors/src/lib/SparseArrayDOKs/test/runtests.jl @@ -119,6 +119,15 @@ using SparseArrays: SparseMatrixCSC, nnz @maybe_grow v[5] = 50 @test size(v) == (5,) @test v[5] == 50 + # Test setting from a variable (to test macro escaping) + i = 6 + val = 60 + @maybe_grow v[i] = val + @test v[i] == val + i, j = 1, 2 + val = 120 + @maybe_grow a[i, j] = val + @test a[i, j] == val end @testset "Test Lower Level Constructor" begin d = Dictionary{CartesianIndex{2},elt}()