Skip to content

Commit

Permalink
Add deprecations to the old iteration protocol shims
Browse files Browse the repository at this point in the history
When we originally Switched over the iteration protocol,
we kept these without depwarns to avoid spamming everybody.
Now that packages have had some time, add the depwarns.
  • Loading branch information
Keno committed Jul 30, 2018
1 parent 33e8b62 commit 2e09298
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
7 changes: 6 additions & 1 deletion base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,11 @@ function iterate(x, state)
return next(x, state)
end
const old_iterate_line_prev = (@__LINE__)
iterate(x) = (@_inline_meta; iterate(x, start(x)))
function iterate(x)
r = iterate(x, start(x))
depwarn("The start/next/done iteration protocol is deprecated. Implement `iterate(::$(typeof(x)))`.", :start)
r
end

struct LegacyIterationCompat{I,T,S}
done::Bool
Expand All @@ -872,6 +876,7 @@ end
const compat_start_line_prev = (@__LINE__)
function start(itr::T) where {T}
has_non_default_iterate(T) || throw(MethodError(iterate, (itr,)))
depwarn("The start/next/done iteration protocol is deprecated. Use `iterate` instead.", :start)
y = iterate(itr)
y === nothing && return LegacyIterationCompat{T, Union{}, Union{}}()
val, state = y
Expand Down
14 changes: 7 additions & 7 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6426,13 +6426,13 @@ Base.start(itr::DelegateIterator) = start(itr.x)
Base.next(itr::DelegateIterator, state) = next(itr.x, state)
Base.done(itr::DelegateIterator, state) = done(itr.x, state)
let A = [1], B = [], C = DelegateIterator([1]), D = DelegateIterator([]), E = Any[1,"abc"]
@test next(A, start(A))[1] == 1
@test done(A, next(A, start(A))[2])
@test done(B, start(B))
@test next(C, start(C))[1] == 1
@test done(C, next(C, start(C))[2])
@test done(D, start(D))
@test next(E, next(E, start(E))[2])[1] == "abc"
@test (@test_deprecated next(A, start(A))[1]) == 1
@test @test_deprecated done(A, next(A, start(A))[2])
@test @test_deprecated done(B, start(B))
@test (@test_deprecated next(C, start(C))[1]) == 1
@test @test_deprecated done(C, next(C, start(C))[2])
@test @test_deprecated done(D, start(D))
@test (@test_deprecated next(E, next(E, start(E))[2])[1]) == "abc"
end

# Issue 27103
Expand Down
2 changes: 1 addition & 1 deletion test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ let s = "hello"
@test c isa Vector{Char}
end
# rest with state from old iteration protocol
@test collect(rest(1:6, start(1:6))) == collect(1:6)
@test (@test_deprecated collect(rest(1:6, start(1:6)))) == collect(1:6)

@test_throws MethodError collect(rest(countfrom(1), 5))

Expand Down

0 comments on commit 2e09298

Please sign in to comment.