Skip to content

Commit e36ab9d

Browse files
authored
Merge pull request #72 from armanbilge/feature/threadunsafe
Add `@threadUnsafe3`
2 parents a635501 + d156561 commit e36ab9d

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed

annotation/src/main/scala-2.12/org/typelevel/scalaccompat/annotation/package.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ package object annotation {
2929

3030
type targetName3 = targetNameIgnored
3131

32+
type threadUnsafe3 = threadUnsafeIgnored
33+
3234
type uncheckedVariance = scala.annotation.unchecked.uncheckedVariance
3335
type uncheckedVariance2 = uncheckedVariance
3436
type uncheckedVariance212 = uncheckedVariance

annotation/src/main/scala-2.13/org/typelevel/scalaccompat/annotation/package.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ package object annotation {
2929

3030
type targetName3 = targetNameIgnored
3131

32+
type threadUnsafe3 = threadUnsafeIgnored
33+
3234
type uncheckedVariance = scala.annotation.unchecked.uncheckedVariance
3335
type uncheckedVariance2 = uncheckedVariance
3436
type uncheckedVariance212 = uncheckedVarianceIgnored

annotation/src/main/scala-3/org/typelevel/scalaccompat/annotation/package.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ package object annotation {
2929

3030
type targetName3 = scala.annotation.targetName
3131

32+
type threadUnsafe3 = scala.annotation.threadUnsafe
33+
3234
type uncheckedVariance = scala.annotation.unchecked.uncheckedVariance
3335
type uncheckedVariance2 = uncheckedVarianceIgnored
3436
type uncheckedVariance212 = uncheckedVarianceIgnored
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2022 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.typelevel.scalaccompat.annotation
18+
package internal
19+
20+
private[annotation] class threadUnsafeIgnored extends scala.annotation.Annotation
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2022 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.typelevel.scalaccompat.annotation
18+
19+
object CustomThreadUnsafeHelper {
20+
final val isScala3 = false
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2022 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.typelevel.scalaccompat.annotation
18+
19+
object CustomThreadUnsafeHelper {
20+
final val isScala3 = true
21+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2022 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.typelevel.scalaccompat.annotation
18+
19+
import munit.FunSuite
20+
21+
import java.util.concurrent.atomic.AtomicInteger
22+
import scala.concurrent.ExecutionContext.Implicits._
23+
import scala.concurrent.Future
24+
25+
class CustomThreadUnsafeSuite extends FunSuite {
26+
27+
val counter = new AtomicInteger
28+
29+
@threadUnsafe3
30+
lazy val foo = {
31+
counter.incrementAndGet()
32+
Thread.sleep(100)
33+
()
34+
}
35+
36+
test("threadUnsafe3 respected on Scala 3 only") {
37+
Future.sequence(List(Future(foo), Future(foo))).map { _ =>
38+
if (CustomThreadUnsafeHelper.isScala3) {
39+
assertEquals(counter.get(), 2)
40+
} else { // Scala 2
41+
assertEquals(counter.get(), 1)
42+
}
43+
}
44+
}
45+
46+
}

0 commit comments

Comments
 (0)