Skip to content

Commit 31d59cb

Browse files
simeonschaubKristofferC
authored andcommitted
improve constraint propagation with multiple || (#39618)
fixes #39611 (cherry picked from commit abd56cd)
1 parent 54a40c7 commit 31d59cb

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

src/julia-syntax.scm

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,8 +1926,8 @@
19261926
(blk? (and (pair? test) (eq? (car test) 'block)))
19271927
(stmts (if blk? (cdr (butlast test)) '()))
19281928
(test (if blk? (last test) test)))
1929-
(if (and (pair? test) (eq? (car test) '&&))
1930-
(let ((clauses `(&& ,@(map expand-forms (cdr (flatten-ex '&& test))))))
1929+
(if (and (pair? test) (memq (car test) '(&& |\|\||)))
1930+
(let ((clauses `(,(car test) ,@(map expand-forms (cdr (flatten-ex (car test) test))))))
19311931
`(if ,(if blk?
19321932
`(block ,@(map expand-forms stmts) ,clauses)
19331933
clauses)
@@ -4086,18 +4086,30 @@ f(x) = yt(x)
40864086
(compile (cadr e) break-labels value tail)
40874087
#f))
40884088
((if elseif)
4089-
(let ((tests (let* ((cond (cadr e))
4090-
(cond (if (and (pair? cond) (eq? (car cond) 'block))
4091-
(begin (if (length> cond 2) (compile (butlast cond) break-labels #f #f))
4092-
(last cond))
4093-
cond)))
4094-
(map (lambda (clause)
4095-
(emit `(gotoifnot ,(compile-cond clause break-labels) _)))
4096-
(if (and (pair? cond) (eq? (car cond) '&&))
4097-
(cdr cond)
4098-
(list cond)))))
4099-
(end-jump `(goto _))
4100-
(val (if (and value (not tail)) (new-mutable-var) #f)))
4089+
(let* ((cnd (cadr e))
4090+
(cnd (if (and (pair? cnd) (eq? (car cnd) 'block))
4091+
(begin (if (length> cnd 2) (compile (butlast cnd) break-labels #f #f))
4092+
(last cnd))
4093+
cnd))
4094+
(or? (and (pair? cnd) (eq? (car cnd) '|\|\||)))
4095+
(tests (if or?
4096+
(let ((short-circuit `(goto _)))
4097+
(for-each
4098+
(lambda (clause)
4099+
(let ((jmp (emit `(gotoifnot ,(compile-cond clause break-labels) _))))
4100+
(emit short-circuit)
4101+
(set-car! (cddr jmp) (make&mark-label))))
4102+
(butlast (cdr cnd)))
4103+
(let ((last-jmp (emit `(gotoifnot ,(compile-cond (last (cdr cnd)) break-labels) _))))
4104+
(set-car! (cdr short-circuit) (make&mark-label))
4105+
(list last-jmp)))
4106+
(map (lambda (clause)
4107+
(emit `(gotoifnot ,(compile-cond clause break-labels) _)))
4108+
(if (and (pair? cnd) (eq? (car cnd) '&&))
4109+
(cdr cnd)
4110+
(list cnd)))))
4111+
(end-jump `(goto _))
4112+
(val (if (and value (not tail)) (new-mutable-var) #f)))
41014113
(let ((v1 (compile (caddr e) break-labels value tail)))
41024114
(if val (emit-assignment val v1))
41034115
(if (and (not tail) (or (length> e 3) val))

test/broadcast.jl

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ Base.BroadcastStyle(::Type{T}) where {T<:AD2Dim} = AD2DimStyle()
516516
@test a .+ 1 .* 2 == @inferred(fadd2(aa))
517517
@test a .* a' == @inferred(fprod(aa))
518518
@test isequal(a .+ [missing; 1:9], fadd3(aa))
519-
@test_broken Core.Compiler.return_type(fadd3, (typeof(aa),)) <: Array19745{<:Union{Float64, Missing}}
519+
@test Core.Compiler.return_type(fadd3, (typeof(aa),)) <: Array19745{<:Union{Float64, Missing}}
520520
@test isa(aa .+ 1, Array19745)
521521
@test isa(aa .+ 1 .* 2, Array19745)
522522
@test isa(aa .* aa', Array19745)
@@ -953,29 +953,20 @@ p0 = copy(p)
953953

954954
@testset "Issue #28382: inferrability of broadcast with Union eltype" begin
955955
@test isequal([1, 2] .+ [3.0, missing], [4.0, missing])
956-
@test_broken Core.Compiler.return_type(broadcast, Tuple{typeof(+), Vector{Int},
957-
Vector{Union{Float64, Missing}}}) ==
958-
Vector{<:Union{Float64, Missing}}
959956
@test Core.Compiler.return_type(broadcast, Tuple{typeof(+), Vector{Int},
960957
Vector{Union{Float64, Missing}}}) ==
961-
AbstractVector{<:Union{Float64, Missing}}
958+
Vector{<:Union{Float64, Missing}}
962959
@test isequal([1, 2] + [3.0, missing], [4.0, missing])
963-
@test_broken Core.Compiler.return_type(+, Tuple{Vector{Int},
964-
Vector{Union{Float64, Missing}}}) ==
960+
@test Core.Compiler.return_type(+, Tuple{Vector{Int},
961+
Vector{Union{Float64, Missing}}}) ==
965962
Vector{<:Union{Float64, Missing}}
966963
@test Core.Compiler.return_type(+, Tuple{Vector{Int},
967964
Vector{Union{Float64, Missing}}}) ==
968-
AbstractVector{<:Union{Float64, Missing}}
969-
@test_broken Core.Compiler.return_type(+, Tuple{Vector{Int},
970-
Vector{Union{Float64, Missing}}}) ==
971965
Vector{<:Union{Float64, Missing}}
972966
@test isequal(tuple.([1, 2], [3.0, missing]), [(1, 3.0), (2, missing)])
973-
@test_broken Core.Compiler.return_type(broadcast, Tuple{typeof(tuple), Vector{Int},
974-
Vector{Union{Float64, Missing}}}) ==
975-
Vector{<:Tuple{Int, Any}}
976967
@test Core.Compiler.return_type(broadcast, Tuple{typeof(tuple), Vector{Int},
977968
Vector{Union{Float64, Missing}}}) ==
978-
AbstractVector{<:Tuple{Int, Any}}
969+
Vector{<:Tuple{Int, Any}}
979970
# Check that corner cases do not throw an error
980971
@test isequal(broadcast(x -> x === 1 ? nothing : x, [1, 2, missing]),
981972
[nothing, 2, missing])

test/compiler/inference.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2994,3 +2994,11 @@ end
29942994
# issue #40804
29952995
@test Base.return_types(()) do; ===(); end == Any[Union{}]
29962996
@test Base.return_types(()) do; typeassert(); end == Any[Union{}]
2997+
2998+
# issue #39611
2999+
Base.return_types((Union{Int,Nothing},)) do x
3000+
if x === nothing || x < 0
3001+
return 0
3002+
end
3003+
x
3004+
end == [Int]

0 commit comments

Comments
 (0)