Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: absolutely minimal way to close #18823 #25169

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,7 @@
;; (for (= lhs X) body)
(let* ((coll (make-ssavalue))
(state (gensy))
(nxt (make-ssavalue))
(outer? (and (pair? lhs) (eq? (car lhs) 'outer)))
(lhs (if outer? (cadr lhs) lhs)))
`(block (= ,coll ,(expand-forms X))
Expand All @@ -1646,8 +1647,9 @@
,@(if (and (not outer?) (or *depwarn* *deperror*))
(map (lambda (v) `(warn-if-existing ,v)) (lhs-vars lhs))
'())
,(lower-tuple-assignment (list lhs state)
`(call (top next) ,coll ,state))
(= ,nxt (call (top next) ,coll ,state))
(if (call (core ===) ,nxt (core nothing)) (break))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this check gets optimized out in the common case of a next function that always returns a tuple?

Hopefully it gets optimized out even for iteration over a type-unstable container, e.g. where next returns Tuple{ ...inference fails... , MyState}?

,(lower-tuple-assignment (list lhs state) nxt)
,body))))))

;; convert an operator parsed as (op a b) to (call op a b)
Expand Down
19 changes: 19 additions & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,22 @@ end
@test Iterators.reverse(Iterators.reverse(t)) === t
end
end

struct Itr18823
pred::Function
rng::AbstractRNG
Itr18823(pred::Function) = new(pred, MersenneTwister())
end

Base.start(itr::Itr18823) = nothing
Base.done(itr::Itr18823, state::Void) = false

function Base.next(itr::Itr18823, state::Void)
x = rand(itr.rng)
itr.pred(x) || return nothing
return x, nothing
end

@testset "zero look-ahead iterators" begin
@test all(x -> x ≥ 0.01, Itr18823(x -> x ≥ 0.01))
end