Skip to content

Commit 82bb161

Browse files
KacperFKorbanKordyjan
authored andcommitted
Don't lift the argument of a synchronized block in scoverage
possible fix for lampepfl#16940 [Cherry-picked 17684c5]
1 parent bef8427 commit 82bb161

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
457457
* they shouldn't be lifted.
458458
*/
459459
val sym = fun.symbol
460-
sym.exists && (isShortCircuitedOp(sym) || StringInterpolatorOpt.isCompilerIntrinsic(sym))
460+
sym.exists && (isShortCircuitedOp(sym) || StringInterpolatorOpt.isCompilerIntrinsic(sym) || sym == defn.Object_synchronized)
461461
end
462462

463463
val fun = tree.fun

tests/run/i16940.scala

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// scalac: -coverage-out:coverage
2+
// scalajs: --skip
3+
4+
import concurrent.ExecutionContext.Implicits.global
5+
import scala.concurrent.*
6+
import scala.concurrent.duration.*
7+
8+
var test = 0
9+
10+
def brokenSynchronizedBlock(option: Boolean): Future[Unit] = Future {
11+
if (option) {
12+
Thread.sleep(500)
13+
}
14+
synchronized {
15+
val tmp = test
16+
Thread.sleep(1000)
17+
test = tmp + 1
18+
}
19+
}
20+
21+
object Test extends App {
22+
Await.result(
23+
Future.sequence(Seq(brokenSynchronizedBlock(false), brokenSynchronizedBlock(true)))
24+
.map { result =>
25+
println(test)
26+
assert(test == 2)
27+
},
28+
3.seconds
29+
)
30+
}

0 commit comments

Comments
 (0)