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 (internal): Use .typeMember instead of .tree to get parameter types #3466

Merged
merged 4 commits into from
Mar 22, 2024
Merged
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 @@ -306,23 +306,16 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
}

private def typeMappingTable(t: TypeRepr, method: Symbol): Map[String, TypeRepr] =
val classTypeParams: List[TypeRepr] = t match
val classTypeParams = t.typeSymbol.typeMembers.filter(_.isTypeParam)
val classTypeArgs: List[TypeRepr] = t match
case a: AppliedType => a.args
case _ => List.empty[TypeRepr]

// Build a table for resolving type parameters, e.g., class MyClass[A, B] -> Map("A" -> TypeRepr, "B" -> TypeRepr)
method.paramSymss match
// tpeArgs for case fields, methodArgs for method arguments
case tpeArgs :: tail if t.typeSymbol.typeMembers.nonEmpty =>
val typeArgTable = tpeArgs
.map(_.tree).zipWithIndex.collect {
case (td: TypeDef, i: Int) if i < classTypeParams.size =>
td.name -> classTypeParams(i)
}.toMap[String, TypeRepr]
// pri ntln(s"type args: ${typeArgTable}")
typeArgTable
case _ =>
Map.empty
(classTypeParams zip classTypeArgs)
.map { (paramType, argType) =>
paramType.name -> argType
}.toMap[String, TypeRepr]

// Get a constructor with its generic types are resolved
private def getResolvedConstructorOf(t: TypeRepr): Option[Term] =
Expand All @@ -337,8 +330,8 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
else
val lookupTable = typeMappingTable(t, pc)
// println(s"--- ${lookupTable}")
val typeArgs = pc.paramSymss.headOption.getOrElse(List.empty).map(_.tree).collect { case t: TypeDef =>
lookupTable.getOrElse(t.name, TypeRepr.of[AnyRef])
val typeArgs = pc.paramSymss.headOption.getOrElse(List.empty).filter(_.isTypeParam).map { p =>
lookupTable.getOrElse(p.name, TypeRepr.of[AnyRef])
}
Some(cstr.appliedToTypes(typeArgs))

Expand Down Expand Up @@ -518,8 +511,8 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):

paramss.map { params =>
params.zipWithIndex
.map((x, i) => (x, i + 1, x.tree))
.collect { case (s: Symbol, i: Int, v: ValDef) =>
.map((x, i) => (x, i + 1, t.memberType(x)))
.collect { case (s: Symbol, i: Int, v: TypeRepr) =>
// E.g. case class Foo(a: String)(implicit b: Int)
// println(s"=== ${v.show} ${s.flags.show} ${s.flags.is(Flags.Implicit)}")
// Substitute type param to actual types
Expand All @@ -543,7 +536,7 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
case other =>
other

val resolved: TypeRepr = resolveType(v.tpt.tpe)
val resolved: TypeRepr = resolveType(v)

val isSecret = hasSecretAnnotation(s)
val isRequired = hasRequiredAnnotation(s)
Expand All @@ -556,7 +549,7 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
// println(s"=== target: ${m.name}, ${m.owner.name}")
m.name == targetMethodName
}
MethodArg(v.name, resolved, defaultValueGetter, defaultMethodArgGetter, isImplicit, isRequired, isSecret)
MethodArg(s.name, resolved, defaultValueGetter, defaultMethodArgGetter, isImplicit, isRequired, isSecret)
}
}

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

/**
* note: this is a regression test for a non-deterministic issue, see https://github.com/scala/scala3/issues/19795
*/

object i3417 extends AirSpec {
trait MyOption[T]

class Wrap(val option: MyOption[Int])
class MultiWrap(option: MyOption[Int], seq: Seq[(Double, MyOption[String])], map: Map[MyOption[Double], Double])

test("handle generic types on parameters") {
val s = Surface.of[Wrap]
s.params.size shouldBe 1
s.params.head.surface.name shouldBe "MyOption[Int]"

val m = Surface.of[MultiWrap]
m.params.size shouldBe 3
m.params(0).surface.name shouldBe "MyOption[Int]"
m.params(1).surface.name shouldBe "Seq[Tuple2[Double,MyOption[String]]]"
m.params(2).surface.name shouldBe "Map[MyOption[Double],Double]"
}

}
Loading