Skip to content

Commit 00c3111

Browse files
committed
fix code coverage bug in tail position
This was due to lowering keeping the same location info for the inserted `return` statement, even though the last seen location might not have executed.
1 parent e460d35 commit 00c3111

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

src/julia-syntax.scm

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4380,8 +4380,9 @@ f(x) = yt(x)
43804380
(begin (emit `(= ,tmp ,x)) tmp)
43814381
x)))
43824382
(define (actually-return x)
4383-
(let* ((x (if rett
4384-
(compile (convert-for-type-decl (emit- x) rett #t lam) '() #t #f)
4383+
(let* ((x (begin0 (emit- x) (emit '(line #f))))
4384+
(x (if rett
4385+
(compile (convert-for-type-decl x rett #t lam) '() #t #f)
43854386
x))
43864387
(x (emit- x)))
43874388
(let ((pexc (pop-exc-expr catch-token-stack '())))
@@ -5046,20 +5047,26 @@ f(x) = yt(x)
50465047
(let ((e (car stmts)))
50475048
(cond ((atom? e) (emit e))
50485049
((eq? (car e) 'line)
5049-
(if (and (= current-line 0) (length= e 2) (pair? linetable))
5050-
;; (line n) after push_loc just updates the line for the new file
5051-
(begin (set-lineno! (car linetable) (cadr e))
5052-
(set! current-line (cadr e)))
5053-
(begin
5054-
(set! current-line (cadr e))
5055-
(if (pair? (cddr e))
5056-
(set! current-file (caddr e)))
5057-
(set! linetable (cons (if (null? locstack)
5058-
(make-lineinfo name current-file current-line)
5059-
(make-lineinfo name current-file current-line (caar locstack)))
5060-
linetable))
5061-
(set! linetablelen (+ linetablelen 1))
5062-
(set! current-loc linetablelen))))
5050+
(cond ((and (length= e 2) (not (cadr e)))
5051+
;; (line #f) marks that we are entering a generated statement
5052+
;; that should not be counted as belonging to the previous marked location,
5053+
;; for example `return` after a not-executed `if` arm in tail position.
5054+
(set! current-loc 0))
5055+
((and (= current-line 0) (length= e 2) (pair? linetable))
5056+
;; (line n) after push_loc just updates the line for the new file
5057+
(begin (set-lineno! (car linetable) (cadr e))
5058+
(set! current-line (cadr e))))
5059+
(else
5060+
(begin
5061+
(set! current-line (cadr e))
5062+
(if (pair? (cddr e))
5063+
(set! current-file (caddr e)))
5064+
(set! linetable (cons (if (null? locstack)
5065+
(make-lineinfo name current-file current-line)
5066+
(make-lineinfo name current-file current-line (caar locstack)))
5067+
linetable))
5068+
(set! linetablelen (+ linetablelen 1))
5069+
(set! current-loc linetablelen)))))
50635070
((and (length> e 2) (eq? (car e) 'meta) (eq? (cadr e) 'push_loc))
50645071
(set! locstack (cons (list current-loc current-line current-file) locstack))
50655072
(set! current-file (caddr e))

test/cmdlineargs.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,41 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
545545
got = read(covfile, String)
546546
@test isempty(got)
547547
rm(covfile)
548+
549+
function coverage_info_for(src::String)
550+
mktemp(dir) do srcfile, io
551+
write(io, src); close(io)
552+
outfile = tempname(dir, cleanup=false)*".info"
553+
run(`$exename --code-coverage=$outfile $srcfile`)
554+
result = read(outfile, String)
555+
rm(outfile, force=true)
556+
result
557+
end
558+
end
559+
@test contains(coverage_info_for("""
560+
function cov_bug(x, p)
561+
if p > 2
562+
print("") # runs
563+
end
564+
if Base.compilerbarrier(:const, false)
565+
println("Does not run")
566+
end
567+
end
568+
function do_test()
569+
cov_bug(5, 3)
570+
end
571+
do_test()
572+
"""), """
573+
DA:1,1
574+
DA:2,1
575+
DA:3,1
576+
DA:5,1
577+
DA:6,0
578+
DA:9,1
579+
DA:10,1
580+
LH:6
581+
LF:7
582+
""")
548583
end
549584

550585
# --track-allocation

0 commit comments

Comments
 (0)