Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for Enumeration support (#3411) #3429

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -863,19 +863,24 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
name != "<init>"
}

allMethods.filter(m => isOwnedByTargetClass(m, t))
allMethods.filter(m => isOwnedByTargetClass(m, t) && !enumerationWorkaround(m, t))

private def nonObject(x: Symbol): Boolean =
!x.flags.is(Flags.Synthetic) &&
!x.flags.is(Flags.Artifact) &&
x.fullName != "scala.Any" &&
x.fullName != "java.lang.Object" &&
// Exclude caes class methods
// Exclude case class methods
x.fullName != "scala.Product"

private def isOwnedByTargetClass(m: Symbol, t: TypeRepr): Boolean =
m.owner == t.typeSymbol || t.baseClasses.filter(nonObject).exists(_ == m.owner)

// workaround https://github.com/lampepfl/dotty/issues/19825 - surface of enumeration value methods fails
private def enumerationWorkaround(m: Symbol, t: TypeRepr): Boolean =
t.baseClasses.exists(_.fullName.startsWith("scala.Enumeration.")) // this will match both Value and ValueSet
// note: it would be possible to let id, hashCode and equals pass through if desired

private def modifierBitMaskOf(m: Symbol): Int =
var mod = 0

Expand Down
36 changes: 36 additions & 0 deletions airframe-surface/src/test/scala/wvlet/airframe/surface/i3411.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 wvlet.airframe.surface

import wvlet.airspec.AirSpec

object i3411 extends AirSpec {

object SomeEnum extends Enumeration {
type SomeEnum = Value

val A, B, C = Value
}

import SomeEnum.SomeEnum

test("Handle a Scala 2 enumeration") {
val s = Surface.of[SomeEnum] // just check there is no error - no expected properties
val m = Surface.methodsOf[SomeEnum]
debug(s.params)
// enumeration type (value) usually contains at least the compare method
// note: we are unable to handle compare at the moment and some other methods inherited from Value, see https://github.com/lampepfl/dotty/issues/19825
// m.map(_.name) shouldContain "compare"
}
}
Loading