Skip to content

Commit 65278e4

Browse files
JeffBezansonKristofferC
authored andcommitted
fix JuliaLang#29175, invalid lowered IR from repeating code for declared types (JuliaLang#29194)
(cherry picked from commit 3255f28)
1 parent 6c2eb8a commit 65278e4

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/julia-syntax.scm

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,12 +2750,37 @@ f(x) = yt(x)
27502750
,(delete-duplicates (append (lam:sp lam) capt-sp)))
27512751
,body)))
27522752

2753+
;; renumber ssavalues assigned in an expr, allowing it to be repeated
2754+
(define (renumber-assigned-ssavalues e)
2755+
(let ((vals (expr-find-all (lambda (x) (and (assignment? x) (ssavalue? (cadr x))))
2756+
e
2757+
cadadr)))
2758+
(if (null? vals)
2759+
e
2760+
(let ((repl (table)))
2761+
(for-each (lambda (id) (put! repl id (make-ssavalue)))
2762+
vals)
2763+
(let do-replace ((x e))
2764+
(if (or (atom? x) (quoted? x))
2765+
x
2766+
(if (eq? (car x) 'ssavalue)
2767+
(or (get repl (cadr x) #f) x)
2768+
(cons (car x)
2769+
(map do-replace (cdr x))))))))))
2770+
27532771
(define (convert-for-type-decl rhs t)
27542772
(if (equal? t '(core Any))
27552773
rhs
2756-
`(call (core typeassert)
2757-
(call (top convert) ,t ,rhs)
2758-
,t)))
2774+
(let* ((temp (if (or (atom? t) (ssavalue? t) (quoted? t))
2775+
#f
2776+
(make-ssavalue)))
2777+
(ty (or temp t))
2778+
(ex `(call (core typeassert)
2779+
(call (top convert) ,ty ,rhs)
2780+
,ty)))
2781+
(if temp
2782+
`(block (= ,temp ,(renumber-assigned-ssavalues t)) ,ex)
2783+
ex))))
27592784

27602785
;; convert assignment to a closed variable to a setfield! call.
27612786
;; while we're at it, generate `convert` calls for variables with

test/core.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6710,3 +6710,12 @@ struct T29145{A,B}
67106710
end
67116711
end
67126712
@test_throws TypeError T29145()
6713+
6714+
# issue #29175
6715+
function f29175(tuple::T) where {T<:Tuple}
6716+
prefix::Tuple{T.parameters[1:end-1]...} = tuple[1:length(T.parameters)-1]
6717+
x = prefix
6718+
prefix = x # force another conversion to declared type
6719+
return prefix
6720+
end
6721+
@test f29175((1,2,3)) === (1,2)

0 commit comments

Comments
 (0)