Skip to content

Commit 320e00d

Browse files
authored
lowering: try to ignore special metadata nodes in tail position (#49963)
A number of packages are relying on lowering removing or ignoring line number nodes that they place in incorrect position in the AST. Accommodate those packages accordingly. Fix #49920
1 parent 0d89d8b commit 320e00d

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/julia-syntax.scm

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
,(if (equal? rett '(core Any))
176176
body
177177
(let ((meta (take-while (lambda (x) (and (pair? x)
178-
(memq (car x) '(line meta))))
178+
(memq (car x) '(lineinfo line meta))))
179179
(cdr body)))
180180
(R (make-ssavalue)))
181181
`(,(car body) ,@meta
@@ -3652,12 +3652,22 @@ f(x) = yt(x)
36523652
(loop (cdr xs))
36533653
elt)))))
36543654

3655+
; try to ignore some metadata expressions for implicit return sometimes
3656+
(define (only-meta? blk)
3657+
(let loop ((xs blk))
3658+
(if (null? xs)
3659+
#t
3660+
(let ((elt (car xs)))
3661+
(if (and (pair? elt) (memq (car elt) '(lineinfo line loopinfo)))
3662+
(loop (cdr xs))
3663+
#f)))))
3664+
36553665
;; return `body` with `stmts` inserted after any meta nodes
36563666
(define (insert-after-meta body stmts)
36573667
(if (null? stmts)
36583668
body
36593669
(let ((meta (take-while (lambda (x) (and (pair? x)
3660-
(memq (car x) '(line meta))))
3670+
(memq (car x) '(lineinfo line meta))))
36613671
(cdr body))))
36623672
`(,(car body)
36633673
,@meta
@@ -3689,7 +3699,7 @@ f(x) = yt(x)
36893699
(lambda (x) (and (pair? x) (not (eq? (car x) 'lambda)))))))
36903700

36913701
(define lambda-opt-ignored-exprs
3692-
(Set '(quote top core line inert local-def unnecessary copyast
3702+
(Set '(quote top core lineinfo line inert local-def unnecessary copyast
36933703
meta inbounds boundscheck loopinfo decl aliasscope popaliasscope
36943704
thunk with-static-parameters toplevel-only
36953705
global globalref outerref const-if-global thismodule
@@ -3919,7 +3929,7 @@ f(x) = yt(x)
39193929
((atom? e) e)
39203930
(else
39213931
(case (car e)
3922-
((quote top core globalref outerref thismodule line break inert module toplevel null true false meta) e)
3932+
((quote top core globalref outerref thismodule lineinfo line break inert module toplevel null true false meta) e)
39233933
((toplevel-only)
39243934
;; hack to avoid generating a (method x) expr for struct types
39253935
(if (eq? (cadr e) 'struct)
@@ -4553,12 +4563,14 @@ f(x) = yt(x)
45534563
(file-diff (not (eq? fname last-fname)))
45544564
;; don't need a filename node for start of function
45554565
(need-meta (and file-diff last-fname
4556-
(not (eq? e (lam:body lam))))))
4566+
(not (eq? e (lam:body lam)))))
4567+
(emit-final-meta (lambda ())))
45574568
(if file-diff (set! filename fname))
45584569
(if need-meta (emit `(meta push_loc ,fname)))
45594570
(let ((v (let loop ((xs (cdr e)))
4560-
(if (null? (cdr xs))
4561-
(compile (car xs) break-labels value tail)
4571+
(if (only-meta? (cdr xs))
4572+
(begin (set! emit-final-meta (lambda () (map (lambda (v) (compile v break-labels #f #f)) (cdr xs))))
4573+
(compile (car xs) break-labels value tail))
45624574
(begin (compile (car xs) break-labels #f #f)
45634575
(loop (cdr xs)))))))
45644576
(if need-meta
@@ -4574,16 +4586,20 @@ f(x) = yt(x)
45744586
(let ((tmp (make-ssavalue)))
45754587
(emit `(= ,tmp ,retv))
45764588
(set! retv tmp)))
4589+
(emit-final-meta)
45774590
(emit '(meta pop_loc))
45784591
(emit `(return ,retv)))
45794592
(emit '(meta pop_loc))))
45804593
((and v value (not (simple-atom? v)))
45814594
(let ((tmp (make-ssavalue)))
45824595
(emit `(= ,tmp ,v))
45834596
(set! v tmp)
4597+
(emit-final-meta)
45844598
(emit `(meta pop_loc))))
45854599
(else
4586-
(emit `(meta pop_loc)))))
4600+
(emit-final-meta)
4601+
(emit `(meta pop_loc))))
4602+
(emit-final-meta))
45874603
(if file-diff (set! filename last-fname))
45884604
v)))
45894605
((return)
@@ -4829,7 +4845,7 @@ f(x) = yt(x)
48294845
(cons (car e) args)))
48304846

48314847
;; metadata expressions
4832-
((line meta inbounds loopinfo gc_preserve_end aliasscope popaliasscope inline noinline)
4848+
((lineinfo line meta inbounds loopinfo gc_preserve_end aliasscope popaliasscope inline noinline)
48334849
(let ((have-ret? (and (pair? code) (pair? (car code)) (eq? (caar code) 'return))))
48344850
(cond ((eq? (car e) 'line)
48354851
(set! current-loc e)

test/syntax.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,6 +3480,13 @@ end
34803480
:(global (; x::$(GlobalRef(m, :S)), y::T) = $(GlobalRef(m, :a)))
34813481
end
34823482

3483+
# issue #49920
3484+
let line1 = (quote end).args[1],
3485+
line2 = (quote end).args[1],
3486+
line3 = (quote end).args[1]
3487+
@test 1 === eval(Meta.lower(Main, Expr(:block, line1, 1, line2, line3)))
3488+
end
3489+
34833490
# issue #49984
34843491
macro z49984(s); :(let a; $(esc(s)); end); end
34853492
@test let a = 1; @z49984(a) === 1; end

0 commit comments

Comments
 (0)