Skip to content

Commit 3d7aa6e

Browse files
authored
Remove broken conversion of @fastmath x[i] += 1 (#50347)
Fixes #47241
1 parent 3ddceee commit 3d7aa6e

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

base/fastmath.jl

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,10 @@ function make_fastmath(expr::Expr)
112112
if isa(var, Symbol)
113113
# simple assignment
114114
expr = :($var = $op($var, $rhs))
115-
elseif isa(var, Expr) && var.head === :ref
116-
var = var::Expr
117-
# array reference
118-
arr = var.args[1]
119-
inds = var.args[2:end]
120-
arrvar = gensym()
121-
indvars = Any[gensym() for _ in inds]
122-
expr = quote
123-
$(Expr(:(=), arrvar, arr))
124-
$(Expr(:(=), Base.exprarray(:tuple, indvars), Base.exprarray(:tuple, inds)))
125-
$arrvar[$(indvars...)] = $op($arrvar[$(indvars...)], $rhs)
126-
end
127115
end
116+
# It is hard to optimize array[i += 1] += 1
117+
# and array[end] += 1 without bugs. (#47241)
118+
# We settle for not optimizing the op= call.
128119
end
129120
Base.exprarray(make_fastmath(expr.head), Base.mapany(make_fastmath, expr.args))
130121
end

test/fastmath.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,12 @@ end
284284
end
285285
end
286286
end
287+
288+
@testset "+= with indexing (#47241)" begin
289+
i = 0
290+
x = zeros(2)
291+
@fastmath x[i += 1] += 1
292+
@fastmath x[end] += 1
293+
@test x == [1, 1]
294+
@test i == 1
295+
end

0 commit comments

Comments
 (0)