Skip to content

Commit

Permalink
Add deprecations to the old iteration protocol shims (#28365)
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 authored Jul 31, 2018
1 parent 7a84599 commit 47bd0ce
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 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
17 changes: 0 additions & 17 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6418,23 +6418,6 @@ end
@test !Base.isvatuple(Tuple{T,Vararg{Int,2}} where T)
@test !Base.isvatuple(Tuple{Int,Int,Vararg{Int,2}})

# The old iteration protocol shims deprecation test
struct DelegateIterator{T}
x::T
end
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"
end

# Issue 27103
function f27103()
a = @isdefined x
Expand Down
23 changes: 23 additions & 0 deletions test/deprecation_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,27 @@ end # @testset

end

# The old iteration protocol shims deprecation test
struct DelegateIterator{T}
x::T
end
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)

@testset "Iteration protocol" begin
let A = [1], B = [], C = DelegateIterator([1]), D = DelegateIterator([]), E = Any[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

# rest with state from old iteration protocol
@test (@test_deprecated collect(Iterators.rest(1:6, Base.start(1:6)))) == collect(1:6)
end

# END 0.7 deprecations
2 changes: 0 additions & 2 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ let s = "hello"
@test c == ['e','l','l','o']
@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_throws MethodError collect(rest(countfrom(1), 5))

Expand Down

0 comments on commit 47bd0ce

Please sign in to comment.