From 1f6fd88cd758e8c149dfb3ecc650898d89005702 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Wed, 20 Dec 2023 07:06:41 +0000 Subject: [PATCH] Add `@static3` --- .../scalaccompat/annotation/package.scala | 2 + .../scalaccompat/annotation/package.scala | 2 + .../scalaccompat/annotation/package.scala | 2 + .../annotation/internal/staticIgnored.scala | 20 ++++++++++ .../annotation/CustomStaticHelper.scala | 21 ++++++++++ .../annotation/CustomStaticHelper.scala | 21 ++++++++++ .../annotation/CustomStaticSuite.scala | 38 +++++++++++++++++++ 7 files changed, 106 insertions(+) create mode 100644 annotation/src/main/scala/org/typelevel/scalaccompat/annotation/internal/staticIgnored.scala create mode 100644 annotation/src/test/scala-2/org/typelevel/scalaccompat/annotation/CustomStaticHelper.scala create mode 100644 annotation/src/test/scala-3/org/typelevel/scalaccompat/annotation/CustomStaticHelper.scala create mode 100644 annotation/src/test/scala/org/typelevel/scalaccompat/annotation/CustomStaticSuite.scala diff --git a/annotation/src/main/scala-2.12/org/typelevel/scalaccompat/annotation/package.scala b/annotation/src/main/scala-2.12/org/typelevel/scalaccompat/annotation/package.scala index 2589801..0ceb46e 100644 --- a/annotation/src/main/scala-2.12/org/typelevel/scalaccompat/annotation/package.scala +++ b/annotation/src/main/scala-2.12/org/typelevel/scalaccompat/annotation/package.scala @@ -27,6 +27,8 @@ package object annotation { type nowarn213 = nowarnIgnored type nowarn3 = nowarnIgnored + type static3 = staticIgnored + type targetName3 = targetNameIgnored type threadUnsafe3 = threadUnsafeIgnored diff --git a/annotation/src/main/scala-2.13/org/typelevel/scalaccompat/annotation/package.scala b/annotation/src/main/scala-2.13/org/typelevel/scalaccompat/annotation/package.scala index ef3e54e..2704b3c 100644 --- a/annotation/src/main/scala-2.13/org/typelevel/scalaccompat/annotation/package.scala +++ b/annotation/src/main/scala-2.13/org/typelevel/scalaccompat/annotation/package.scala @@ -27,6 +27,8 @@ package object annotation { type nowarn213 = nowarn type nowarn3 = nowarnIgnored + type static3 = staticIgnored + type targetName3 = targetNameIgnored type threadUnsafe3 = threadUnsafeIgnored diff --git a/annotation/src/main/scala-3/org/typelevel/scalaccompat/annotation/package.scala b/annotation/src/main/scala-3/org/typelevel/scalaccompat/annotation/package.scala index ff52f40..1d332e4 100644 --- a/annotation/src/main/scala-3/org/typelevel/scalaccompat/annotation/package.scala +++ b/annotation/src/main/scala-3/org/typelevel/scalaccompat/annotation/package.scala @@ -27,6 +27,8 @@ package object annotation { type nowarn213 = nowarnIgnored type nowarn3 = nowarn + type static3 = scala.annotation.static + type targetName3 = scala.annotation.targetName type threadUnsafe3 = scala.annotation.threadUnsafe diff --git a/annotation/src/main/scala/org/typelevel/scalaccompat/annotation/internal/staticIgnored.scala b/annotation/src/main/scala/org/typelevel/scalaccompat/annotation/internal/staticIgnored.scala new file mode 100644 index 0000000..cb967a6 --- /dev/null +++ b/annotation/src/main/scala/org/typelevel/scalaccompat/annotation/internal/staticIgnored.scala @@ -0,0 +1,20 @@ +/* + * Copyright 2022 Typelevel + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.typelevel.scalaccompat.annotation +package internal + +private[annotation] class staticIgnored extends scala.annotation.Annotation diff --git a/annotation/src/test/scala-2/org/typelevel/scalaccompat/annotation/CustomStaticHelper.scala b/annotation/src/test/scala-2/org/typelevel/scalaccompat/annotation/CustomStaticHelper.scala new file mode 100644 index 0000000..9f7fb91 --- /dev/null +++ b/annotation/src/test/scala-2/org/typelevel/scalaccompat/annotation/CustomStaticHelper.scala @@ -0,0 +1,21 @@ +/* + * Copyright 2022 Typelevel + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.typelevel.scalaccompat.annotation + +object CustomStaticHelper { + final val isScala3 = false +} diff --git a/annotation/src/test/scala-3/org/typelevel/scalaccompat/annotation/CustomStaticHelper.scala b/annotation/src/test/scala-3/org/typelevel/scalaccompat/annotation/CustomStaticHelper.scala new file mode 100644 index 0000000..3851526 --- /dev/null +++ b/annotation/src/test/scala-3/org/typelevel/scalaccompat/annotation/CustomStaticHelper.scala @@ -0,0 +1,21 @@ +/* + * Copyright 2022 Typelevel + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.typelevel.scalaccompat.annotation + +object CustomStaticHelper { + final val isScala3 = true +} diff --git a/annotation/src/test/scala/org/typelevel/scalaccompat/annotation/CustomStaticSuite.scala b/annotation/src/test/scala/org/typelevel/scalaccompat/annotation/CustomStaticSuite.scala new file mode 100644 index 0000000..b4fc830 --- /dev/null +++ b/annotation/src/test/scala/org/typelevel/scalaccompat/annotation/CustomStaticSuite.scala @@ -0,0 +1,38 @@ +/* + * Copyright 2022 Typelevel + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.typelevel.scalaccompat.annotation + +import munit.FunSuite + +class CustomStaticDemo +object CustomStaticDemo { + @static3 val foo = 42 +} + +class CustomStaticSuite extends FunSuite { + + test("static3 respected on Scala 3 only") { + val fields = classOf[CustomStaticDemo].getFields().map(_.getName).toList + + if (CustomStaticHelper.isScala3) { + assertEquals(fields, List("foo")) + } else { // Scala 2 + assertEquals(fields, Nil) + } + } + +}