From c2fb5b39abb05e73c3297019e10e6dfdc3bd0ef5 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Fri, 24 Aug 2018 06:10:29 -0500 Subject: [PATCH] issubset: check IteratorSize trait before calling length --- base/abstractset.jl | 17 +++++++++-------- test/sets.jl | 13 +++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/base/abstractset.jl b/base/abstractset.jl index 2c2ec47c0c041..cb5de8c41b423 100644 --- a/base/abstractset.jl +++ b/base/abstractset.jl @@ -226,14 +226,15 @@ end <=(l::AbstractSet, r::AbstractSet) = l ⊆ r function issubset(l, r) - - rlen = length(r) - #This threshold was empirically determined by repeatedly - #sampling using these two methods. - lenthresh = 70 - - if rlen > lenthresh && !isa(r, AbstractSet) - return issubset(l, Set(r)) + if IteratorSize(r) isa Union{HasLength,HasShape} + rlen = length(r) + #This threshold was empirically determined by repeatedly + #sampling using these two methods (see #26198) + lenthresh = 70 + + if rlen > lenthresh && !isa(r, AbstractSet) + return issubset(l, Set(r)) + end end for elt in l diff --git a/test/sets.jl b/test/sets.jl index ea523d43afb72..35155633abb9d 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -604,3 +604,16 @@ end end end end + +struct OpenInterval{T} + lower::T + upper::T +end +Base.in(x, i::OpenInterval) = i.lower < x < i.upper +Base.IteratorSize(::Type{<:OpenInterval}) = Base.SizeUnknown() + +@testset "Continuous sets" begin + i = OpenInterval(2, 4) + @test 3 ∈ i + @test issubset(3, i) +end