Skip to content

Commit

Permalink
Backport "Coverage: mark case bodies as branches; don't ignore branch…
Browse files Browse the repository at this point in the history
…es with synthetic spans" to LTS (#19232)

Backports #18437 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
Kordyjan committed Dec 13, 2023
2 parents 6bd403e + 3661710 commit 0b33d5b
Show file tree
Hide file tree
Showing 9 changed files with 576 additions and 91 deletions.
11 changes: 3 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,9 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
* If the tree is empty, return itself and don't instrument.
*/
private def transformBranch(tree: Tree)(using Context): Tree =
import dotty.tools.dotc.core.Decorators.{show,i}
if tree.isEmpty || tree.span.isSynthetic then
if tree.isEmpty then
// - If t.isEmpty then `transform(t) == t` always hold,
// so we can avoid calling transform in that case.
// - If tree.span.isSynthetic then the branch has been generated
// by the frontend phases, so we don't want to instrument it.
tree
else
val transformed = transform(tree)
Expand Down Expand Up @@ -353,10 +350,8 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
// recursively transform the guard, but keep the pat
val transformedGuard = transform(guard)

// ensure that the body is always instrumented by inserting a call to Invoker.invoked at its beginning
val coverageCall = createInvokeCall(tree.body, pos)
val transformedBody = transform(tree.body)
val instrumentedBody = InstrumentedParts.singleExprTree(coverageCall, transformedBody)
// ensure that the body is always instrumented as a branch
val instrumentedBody = transformBranch(tree.body)

cpy.CaseDef(tree)(pat, transformedGuard, instrumentedBody)

Expand Down
71 changes: 61 additions & 10 deletions tests/coverage/pos/Inlined.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ Inlined$package$
Object
covtest.Inlined$package$
testInlined
330
330
10
<none>
Literal
true
0
false


3
Inlined.scala
covtest
Inlined$package$
Object
covtest.Inlined$package$
testInlined
155
162
6
Expand All @@ -69,7 +86,7 @@ false
false
List(l)

3
4
Inlined.scala
covtest
Inlined$package$
Expand All @@ -86,7 +103,7 @@ false
false
List

4
5
Inlined.scala
covtest
Inlined$package$
Expand All @@ -103,7 +120,7 @@ false
false
List(l).length

5
6
Inlined.scala
covtest
Inlined$package$
Expand All @@ -120,7 +137,7 @@ false
false
scala.runtime.Scala3RunTime.assertFailed()

6
7
Inlined.scala
covtest
Inlined$package$
Expand All @@ -137,7 +154,24 @@ true
false
scala.runtime.Scala3RunTime.assertFailed()

7
8
Inlined.scala
covtest
Inlined$package$
Object
covtest.Inlined$package$
testInlined
330
330
10
<none>
Literal
true
0
false


9
Inlined.scala
covtest
Inlined$package$
Expand All @@ -154,7 +188,7 @@ false
false
List(l)

8
10
Inlined.scala
covtest
Inlined$package$
Expand All @@ -171,7 +205,7 @@ false
false
List

9
11
Inlined.scala
covtest
Inlined$package$
Expand All @@ -188,7 +222,7 @@ false
false
List(l).length

10
12
Inlined.scala
covtest
Inlined$package$
Expand All @@ -205,7 +239,7 @@ false
false
scala.runtime.Scala3RunTime.assertFailed()

11
13
Inlined.scala
covtest
Inlined$package$
Expand All @@ -222,7 +256,24 @@ true
false
scala.runtime.Scala3RunTime.assertFailed()

12
14
Inlined.scala
covtest
Inlined$package$
Object
covtest.Inlined$package$
testInlined
330
330
10
<none>
Literal
true
0
false


15
Inlined.scala
covtest
Inlined$package$
Expand Down
106 changes: 53 additions & 53 deletions tests/coverage/pos/MatchCaseClasses.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,6 @@ MatchCaseClasses$
Object
covtest.MatchCaseClasses$
f
135
147
7
<none>
Block
false
0
false
case Pat1(0)

1
MatchCaseClasses.scala
covtest
MatchCaseClasses$
Object
covtest.MatchCaseClasses$
f
151
163
7
Expand All @@ -52,24 +35,24 @@ false
false
println("a")

2
1
MatchCaseClasses.scala
covtest
MatchCaseClasses$
Object
covtest.MatchCaseClasses$
f
168
180
8
148
163
7
<none>
Block
false
true
0
false
case Pat1(_)
=> println("a")

3
2
MatchCaseClasses.scala
covtest
MatchCaseClasses$
Expand All @@ -86,24 +69,24 @@ false
false
println("b")

4
3
MatchCaseClasses.scala
covtest
MatchCaseClasses$
Object
covtest.MatchCaseClasses$
f
201
221
9
181
196
8
<none>
Block
false
true
0
false
case p @ Pat2(1, -1)
=> println("b")

5
4
MatchCaseClasses.scala
covtest
MatchCaseClasses$
Expand All @@ -120,24 +103,24 @@ false
false
println("c")

6
5
MatchCaseClasses.scala
covtest
MatchCaseClasses$
Object
covtest.MatchCaseClasses$
f
242
265
10
222
237
9
<none>
Block
false
true
0
false
case Pat2(_, y: String)
=> println("c")

7
6
MatchCaseClasses.scala
covtest
MatchCaseClasses$
Expand All @@ -154,7 +137,7 @@ false
false
println(y)

8
7
MatchCaseClasses.scala
covtest
MatchCaseClasses$
Expand All @@ -171,24 +154,24 @@ false
false
println("d")

9
8
MatchCaseClasses.scala
covtest
MatchCaseClasses$
Object
covtest.MatchCaseClasses$
f
309
321
13
275
304
11
<none>
Block
false
true
0
false
case p: Pat2
println(y)\n println("d")

10
9
MatchCaseClasses.scala
covtest
MatchCaseClasses$
Expand All @@ -205,24 +188,24 @@ false
false
println("e")

11
10
MatchCaseClasses.scala
covtest
MatchCaseClasses$
Object
covtest.MatchCaseClasses$
f
342
348
14
322
337
13
<none>
Block
false
true
0
false
case _
=> println("e")

12
11
MatchCaseClasses.scala
covtest
MatchCaseClasses$
Expand All @@ -239,6 +222,23 @@ false
false
println("other")

12
MatchCaseClasses.scala
covtest
MatchCaseClasses$
Object
covtest.MatchCaseClasses$
f
349
368
14
<none>
Block
true
0
false
=> println("other")

13
MatchCaseClasses.scala
covtest
Expand Down

0 comments on commit 0b33d5b

Please sign in to comment.