Skip to content

Commit 3c8241c

Browse files
JeffBezansonKristofferC
authored andcommitted
fix #44239, regression in keyword args in getindex (#44246)
(cherry picked from commit 4061e8f)
1 parent 8c43f36 commit 3c8241c

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/julia-syntax.scm

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@
120120
;; inside ref only replace within the first argument
121121
(list* 'ref (replace-beginend (cadr ex) a n tuples last)
122122
(cddr ex)))
123+
;; TODO: this probably should not be allowed since keyword args aren't
124+
;; positional, but in this context we have just used their positions anyway
125+
((eq? (car ex) 'kw)
126+
(list 'kw (cadr ex) (replace-beginend (caddr ex) a n tuples last)))
123127
(else
124128
(cons (car ex)
125129
(map (lambda (x) (replace-beginend x a n tuples last))
@@ -142,16 +146,20 @@
142146
(idx (if (vararg? idx0) (cadr idx0) idx0))
143147
(last (null? (cdr lst)))
144148
(replaced (replace-beginend idx a n tuples last))
145-
(idx (if (or (not has-va?) (simple-atom? replaced)) replaced (make-ssavalue))))
149+
(val (if (kwarg? replaced) (caddr replaced) replaced))
150+
(idx (if (or (not has-va?) (simple-atom? val))
151+
val (make-ssavalue))))
146152
(loop (cdr lst) (+ n 1)
147-
(if (eq? idx replaced)
153+
(if (eq? idx val)
148154
stmts
149-
(cons `(= ,idx ,replaced)
155+
(cons `(= ,idx ,val)
150156
stmts))
151157
(if (vararg? idx0) (cons idx tuples) tuples)
152158
(cons (if (vararg? idx0)
153159
`(... ,idx)
154-
idx)
160+
(if (eq? val replaced)
161+
idx
162+
(list 'kw (cadr replaced) idx)))
155163
ret)))))))
156164

157165
;; GF method does not need to keep decl expressions on lambda args

test/syntax.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,17 @@ let a = [], b = [4,3,2,1]
12011201
@test a == [1,2]
12021202
end
12031203

1204+
# issue #44239
1205+
struct KWGetindex end
1206+
Base.getindex(::KWGetindex, args...; kws...) = (args, NamedTuple(kws))
1207+
let A = KWGetindex(), a = [], b = [4,3,2,1]
1208+
f() = (push!(a, 1); 2)
1209+
g() = (push!(a, 2); ())
1210+
@test A[f(), g()..., k = f()] === ((2,), (k = 2,))
1211+
@test a == [1, 2, 1]
1212+
@test A[var"end"=1] === ((), (var"end" = 1,))
1213+
end
1214+
12041215
@testset "raw_str macro" begin
12051216
@test raw"$" == "\$"
12061217
@test raw"\n" == "\\n"

0 commit comments

Comments
 (0)