Skip to content

Commit

Permalink
fix JuliaLang#44239, regression in keyword args in getindex (JuliaLan…
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Feb 19, 2022
1 parent 749a658 commit 4061e8f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@
;; inside ref only replace within the first argument
(list* 'ref (replace-beginend (cadr ex) a n tuples last)
(cddr ex)))
;; TODO: this probably should not be allowed since keyword args aren't
;; positional, but in this context we have just used their positions anyway
((eq? (car ex) 'kw)
(list 'kw (cadr ex) (replace-beginend (caddr ex) a n tuples last)))
(else
(cons (car ex)
(map (lambda (x) (replace-beginend x a n tuples last))
Expand All @@ -142,16 +146,20 @@
(idx (if (vararg? idx0) (cadr idx0) idx0))
(last (null? (cdr lst)))
(replaced (replace-beginend idx a n tuples last))
(idx (if (or (not has-va?) (simple-atom? replaced)) replaced (make-ssavalue))))
(val (if (kwarg? replaced) (caddr replaced) replaced))
(idx (if (or (not has-va?) (simple-atom? val))
val (make-ssavalue))))
(loop (cdr lst) (+ n 1)
(if (eq? idx replaced)
(if (eq? idx val)
stmts
(cons `(= ,idx ,replaced)
(cons `(= ,idx ,val)
stmts))
(if (vararg? idx0) (cons idx tuples) tuples)
(cons (if (vararg? idx0)
`(... ,idx)
idx)
(if (eq? val replaced)
idx
(list 'kw (cadr replaced) idx)))
ret)))))))

;; GF method does not need to keep decl expressions on lambda args
Expand Down
11 changes: 11 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,17 @@ let a = [], b = [4,3,2,1]
@test a == [1,2]
end

# issue #44239
struct KWGetindex end
Base.getindex(::KWGetindex, args...; kws...) = (args, NamedTuple(kws))
let A = KWGetindex(), a = [], b = [4,3,2,1]
f() = (push!(a, 1); 2)
g() = (push!(a, 2); ())
@test A[f(), g()..., k = f()] === ((2,), (k = 2,))
@test a == [1, 2, 1]
@test A[var"end"=1] === ((), (var"end" = 1,))
end

@testset "raw_str macro" begin
@test raw"$" == "\$"
@test raw"\n" == "\\n"
Expand Down

0 comments on commit 4061e8f

Please sign in to comment.