Skip to content

Commit 938cdd2

Browse files
committed
#90 Fixed unwanted instrumenting of tuple wrapping in functions
1 parent d0c9d07 commit 938cdd2

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

scalac-scoverage-plugin/src/main/scala/scoverage/plugin.scala

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class ScoverageOptions {
5050
var dataDir: String = File.createTempFile("scoverage_datadir_not_defined", ".tmp").getParent
5151
}
5252

53-
5453
class ScoverageInstrumentationComponent(val global: Global)
5554
extends PluginComponent
5655
with TypingTransformers
@@ -324,6 +323,11 @@ class ScoverageInstrumentationComponent(val global: Global)
324323
// ignore macro definitions in 2.11
325324
case DefDef(mods, _, _, _, _, _) if mods.isMacro => tree
326325

326+
case d: DefDef =>
327+
println(d)
328+
val e = d
329+
d
330+
327331
// this will catch methods defined as macros, eg def test = macro testImpl
328332
// it will not catch macro implemenations
329333
case d: DefDef if d.symbol != null
@@ -431,7 +435,10 @@ class ScoverageInstrumentationComponent(val global: Global)
431435
treeCopy
432436
.Match(tree, instrument(selector, selector), transformCases(cases.dropRight(1)) ++ cases.takeRight(1))
433437
} else {
434-
treeCopy.Match(tree, process(selector), transformCases(cases))
438+
if (selector.symbol.isSynthetic)
439+
treeCopy.Match(tree, selector, transformCases(cases))
440+
else
441+
treeCopy.Match(tree, process(selector), transformCases(cases))
435442
}
436443

437444
// a synthetic object is a generated object, such as case class companion
@@ -534,18 +541,24 @@ class ScoverageInstrumentationComponent(val global: Global)
534541
/**
535542
* We can ignore lazy val defs as they are implemented by a generated defdef
536543
*/
537-
case v: ValDef if v.symbol.isLazy => tree
544+
case v: ValDef if v.symbol.isLazy =>
545+
val w = v
546+
tree
538547

539548
/**
540549
* <synthetic> val default: A1 => B1 =
541550
* <synthetic> val x1: Any = _
542551
*/
543-
case v: ValDef if v.symbol.isSynthetic => tree
552+
case v: ValDef if v.symbol.isSynthetic =>
553+
val w = v
554+
tree
544555

545556
/**
546557
* Vals declared in case constructors
547558
*/
548-
case v: ValDef if v.symbol.isParamAccessor && v.symbol.isCaseAccessor => tree
559+
case v: ValDef if v.symbol.isParamAccessor && v.symbol.isCaseAccessor =>
560+
val w = v
561+
tree
549562

550563
// we need to remove the final mod so that we keep the code in order to check its invoked
551564
case v: ValDef if v.mods.isFinal =>

scalac-scoverage-plugin/src/test/scala/scoverage/PluginASTSupportTest.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ class PluginASTSupportTest
6262
assert(!compiler.reporter.hasErrors)
6363
}
6464

65-
66-
// https://github.com/scoverage/scalac-scoverage-plugin/issues/32
65+
// https://github.com/scoverage/scalac-scoverage-plugin/issues/32
6766
test("exhaustive warnings should not be generated for @unchecked") {
6867
val compiler = ScoverageCompiler.default
6968
compiler.compileCodeSnippet( """object PartialMatchObject {

scalac-scoverage-plugin/src/test/scala/scoverage/PluginCoverageTest.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,30 @@ class PluginCoverageTest
159159
compiler.assertNMeasuredStatements(1)
160160
}
161161

162+
test("scoverage should not instrument function tuple wrapping") {
163+
val compiler = ScoverageCompiler.default
164+
compiler.compileCodeSnippet(
165+
"""
166+
| sealed trait Foo
167+
| case class Bar(s: String) extends Foo
168+
| case object Baz extends Foo
169+
|
170+
| object Foo {
171+
| implicit val fooOrdering: Ordering[Foo] = Ordering.fromLessThan {
172+
| case (Bar(_), Baz) => true
173+
| case (Bar(a), Bar(b)) => a < b
174+
| case (_, _) => false
175+
| }
176+
| }
177+
""".stripMargin)
178+
179+
assert(!compiler.reporter.hasErrors)
180+
assert(!compiler.reporter.hasWarnings)
181+
// should have 4 profiled statements: the outer apply, the true, the a < b, the false
182+
// we are testing that we don't instrument the tuple2 call used here
183+
compiler.assertNMeasuredStatements(4)
184+
}
185+
162186
test("scoverage should instrument all case statements in an explicit match") {
163187
val compiler = ScoverageCompiler.default
164188
compiler.compileCodeSnippet( """ trait A {

scalac-scoverage-runtime/src/main/scala/scoverage/Invoker.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ object Invoker {
4040
var files = threadFiles.get()
4141
if (files == null)
4242
files = TrieMap.empty[String, FileWriter]
43-
threadFiles.set(files)
43+
threadFiles.set(files)
4444

4545
val writer = files.getOrElseUpdate(dataDir, new FileWriter(measurementFile(dataDir), true))
4646
writer.append(id.toString + '\n').flush()

0 commit comments

Comments
 (0)