Skip to content

Commit

Permalink
Remove broken conversion of @fastmath x[i] += 1 (#50347)
Browse files Browse the repository at this point in the history
Fixes #47241
  • Loading branch information
LilithHafner committed Jun 29, 2023
1 parent 3ddceee commit 3d7aa6e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
15 changes: 3 additions & 12 deletions base/fastmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,10 @@ function make_fastmath(expr::Expr)
if isa(var, Symbol)
# simple assignment
expr = :($var = $op($var, $rhs))
elseif isa(var, Expr) && var.head === :ref
var = var::Expr
# array reference
arr = var.args[1]
inds = var.args[2:end]
arrvar = gensym()
indvars = Any[gensym() for _ in inds]
expr = quote
$(Expr(:(=), arrvar, arr))
$(Expr(:(=), Base.exprarray(:tuple, indvars), Base.exprarray(:tuple, inds)))
$arrvar[$(indvars...)] = $op($arrvar[$(indvars...)], $rhs)
end
end
# It is hard to optimize array[i += 1] += 1
# and array[end] += 1 without bugs. (#47241)
# We settle for not optimizing the op= call.
end
Base.exprarray(make_fastmath(expr.head), Base.mapany(make_fastmath, expr.args))
end
Expand Down
9 changes: 9 additions & 0 deletions test/fastmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,12 @@ end
end
end
end

@testset "+= with indexing (#47241)" begin
i = 0
x = zeros(2)
@fastmath x[i += 1] += 1
@fastmath x[end] += 1
@test x == [1, 1]
@test i == 1
end

0 comments on commit 3d7aa6e

Please sign in to comment.