File tree Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -69,9 +69,11 @@ function union!(s::AbstractSet, sets...)
6969end
7070
7171max_values (:: Type ) = typemax (Int)
72- max_values (T:: Type{<:Union{Nothing,BitIntegerSmall}} ) = 1 << (8 * sizeof (T))
73- max_values (T:: Union ) = max (max_values (T. a), max_values (T. b))
72+ max_values (T:: Union{map(X -> Type{X}, BitIntegerSmall_types)...} ) = 1 << (8 * sizeof (T))
73+ # saturated addition to prevent overflow with typemax(Int)
74+ max_values (T:: Union ) = max (max_values (T. a), max_values (T. b), max_values (T. a) + max_values (T. b))
7475max_values (:: Type{Bool} ) = 2
76+ max_values (:: Type{Nothing} ) = 1
7577
7678function union! (s:: AbstractSet{T} , itr) where T
7779 haslength (itr) && sizehint! (s, length (s) + length (itr))
Original file line number Diff line number Diff line change 628628 end
629629end
630630
631+ @testset " optimized union! with max_values" begin
632+ # issue #30315
633+ T = Union{Nothing, Bool}
634+ @test Base. max_values (T) == 3
635+ d = Set {T} ()
636+ union! (d, (nothing , true , false ))
637+ @test length (d) == 3
638+ @test d == Set ((nothing , true , false ))
639+ @test nothing in d
640+ @test true in d
641+ @test false in d
642+
643+ for X = (Int8, Int16, Int32, Int64)
644+ @test Base. max_values (Union{Nothing, X}) == (sizeof (X) < sizeof (Int) ?
645+ 2 ^ (8 * sizeof (X)) + 1 :
646+ typemax (Int))
647+ end
648+ # this does not account for non-empty intersections of the unioned types
649+ @test Base. max_values (Union{Int8,Int16}) == 2 ^ 8 + 2 ^ 16
650+ end
651+
631652struct OpenInterval{T}
632653 lower:: T
633654 upper:: T
You can’t perform that action at this time.
0 commit comments