Skip to content

Commit

Permalink
Coverage: don't instrument super() calls of Template, fix #16127
Browse files Browse the repository at this point in the history
  • Loading branch information
TheElectronWill committed Oct 24, 2022
1 parent c3424ce commit 7fc537c
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 299 deletions.
27 changes: 27 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
// to the enclosing method, not a tree to instrument.
cpy.Return(tree)(expr = transform(tree.expr), from = tree.from)

case tree: Template =>
// only transform:
// - the arguments of the `Apply` trees in the parents
// - the template body
cpy.Template(tree)(
transformSub(tree.constr),
transformTemplateParents(tree.parents)(using ctx.superCallContext),
tree.derived,
tree.self,
transformStats(tree.body, tree.symbol)
)

// For everything else just recurse and transform
case _ =>
super.transform(tree)
Expand Down Expand Up @@ -332,6 +344,21 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:

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

/** Transforms the parents of a Template. */
private def transformTemplateParents(parents: List[Tree])(using Context): List[Tree] =
def transformParent(parent: Tree): Tree = parent match
case tree: Apply =>
// only instrument the args, not the constructor call
cpy.Apply(tree)(tree.fun, tree.args.mapConserve(transform))
case tree: TypeApply =>
// args are types, instrument the fun with transformParent
cpy.TypeApply(tree)(transformParent(tree.fun), tree.args)
case other =>
// should always be a TypeTree, nothing to instrument
other

parents.mapConserve(transformParent)

/** Instruments the body of a DefDef. Handles corner cases.
* Given a DefDef f like this:
* ```
Expand Down
162 changes: 13 additions & 149 deletions tests/coverage/pos/Enum.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -72,142 +72,6 @@ def surfaceWeight
3
Enum.scala
covtest
$anon
Class
covtest.$anon
<init>
485
512
17
<init>
Apply
false
0
false
Planet(3.303e+23, 2.4397e6)

4
Enum.scala
covtest
$anon
Class
covtest.$anon
<init>
538
565
18
<init>
Apply
false
0
false
Planet(4.869e+24, 6.0518e6)

5
Enum.scala
covtest
$anon
Class
covtest.$anon
<init>
591
619
19
<init>
Apply
false
0
false
Planet(5.976e+24, 6.37814e6)

6
Enum.scala
covtest
$anon
Class
covtest.$anon
<init>
645
672
20
<init>
Apply
false
0
false
Planet(6.421e+23, 3.3972e6)

7
Enum.scala
covtest
$anon
Class
covtest.$anon
<init>
698
725
21
<init>
Apply
false
0
false
Planet(1.9e+27, 7.1492e7)

8
Enum.scala
covtest
$anon
Class
covtest.$anon
<init>
751
778
22
<init>
Apply
false
0
false
Planet(5.688e+26, 6.0268e7)

9
Enum.scala
covtest
$anon
Class
covtest.$anon
<init>
804
831
23
<init>
Apply
false
0
false
Planet(8.686e+25, 2.5559e7)

10
Enum.scala
covtest
$anon
Class
covtest.$anon
<init>
857
884
24
<init>
Apply
false
0
false
Planet(1.024e+26, 2.4746e7)

11
Enum.scala
covtest
EnumTypes$
Object
covtest.EnumTypes$
Expand All @@ -222,7 +86,7 @@ false
false
println("Example 1: \n"+emptyList)

12
4
Enum.scala
covtest
EnumTypes$
Expand All @@ -239,7 +103,7 @@ false
false
"Example 1: \n"+emptyList

13
5
Enum.scala
covtest
EnumTypes$
Expand All @@ -256,7 +120,7 @@ false
false
println(s"${list}\n")

14
6
Enum.scala
covtest
EnumTypes$
Expand All @@ -273,7 +137,7 @@ false
false
s"${list}\n"

15
7
Enum.scala
covtest
EnumTypes$
Expand All @@ -290,7 +154,7 @@ false
false
Planet.Earth.surfaceGravity

16
8
Enum.scala
covtest
EnumTypes$
Expand All @@ -308,7 +172,7 @@ false
for p <- Planet.values do
println(s"Your weight on $p is ${p.surfaceWeight(mass)}")

17
9
Enum.scala
covtest
EnumTypes$
Expand All @@ -325,7 +189,7 @@ false
false
Planet.values

18
10
Enum.scala
covtest
EnumTypes$
Expand All @@ -342,7 +206,7 @@ false
false
println(s"Your weight on $p is ${p.surfaceWeight(mass)}")

19
11
Enum.scala
covtest
EnumTypes$
Expand All @@ -359,7 +223,7 @@ false
false
s"Your weight on $p is ${p.surfaceWeight(mass)}"

20
12
Enum.scala
covtest
EnumTypes$
Expand All @@ -376,7 +240,7 @@ false
false
p.surfaceWeight(mass)

21
13
Enum.scala
covtest
EnumTypes$
Expand All @@ -393,7 +257,7 @@ false
false
def calculateEarthWeightOnPlanets

22
14
Enum.scala
covtest
EnumTypes$
Expand All @@ -410,7 +274,7 @@ false
false
println("Example 2:")

23
15
Enum.scala
covtest
EnumTypes$
Expand All @@ -427,7 +291,7 @@ false
false
calculateEarthWeightOnPlanets(80)

24
16
Enum.scala
covtest
EnumTypes$
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions tests/coverage/pos/EnumJava.scoverage.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Coverage data, format version: 3.0
# Statement data:
# - id
# - source path
# - package name
# - class name
# - class type (Class, Object or Trait)
# - full class name
# - method name
# - start offset
# - end offset
# - line number
# - symbol name
# - tree name
# - is branch
# - invocations count
# - is ignored
# - description (can be multi-line)
# ' ' sign
# ------------------------------------------
Loading

0 comments on commit 7fc537c

Please sign in to comment.