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

Surface - Method type exploration #3440

Draft
wants to merge 4 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 @@ -574,6 +574,7 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
val methodName = method.name
val methodArgs = methodArgsOf(t, method).flatten
val argClasses = methodArgs.map { arg =>
// check if type is referencing t, which is something.InnerType, but via a base class, like Base.this.InnerType
clsOf(arg.tpe.dealias)
}
val isConstructor = t.typeSymbol.primaryConstructor == method
Expand Down Expand Up @@ -678,6 +679,7 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
methodArgAccessor = ${ methodArgAccessor }
)
}
println(paramExprs.map(_.show).mkString("\n"))
Expr.ofSeq(paramExprs)

private def getTree(e: Expr[?]): Tree =
Expand Down Expand Up @@ -745,7 +747,7 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
methodsOfInternal(t).asTerm
).asExprOf[Seq[MethodSurface]]

// println(s"=== methodOf: ${t.typeSymbol.fullName} => \n${expr.show}")
println(s"=== methodOf: ${t.typeSymbol.fullName} => \n${expr.show}")
expr

private val seenMethodParent = scala.collection.mutable.Set[TypeRepr]()
Expand Down Expand Up @@ -863,19 +865,28 @@ 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 = {
val params = methodParametersOf(t, m)
val args = methodArgsOf(t, m).flatten
println(s"m $m ${args.map(_.tpe.show).mkString(",")} ${params.show}")
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
35 changes: 35 additions & 0 deletions airframe-surface/src/test/scala/wvlet/airframe/surface/i3439.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 i3439 extends AirSpec {

trait Base {
class InnerType {
def compare(that: InnerType): Int = 0
}
}

object OuterType extends Base {
def create: InnerType = new InnerType
}

test("Handle inherited inner class") {
//val mm = Surface.methodsOf[OuterType.type]
val m = Surface.methodsOf[OuterType.InnerType]
//m.map(_.name) shouldContain "compare"
}
}
Loading