Skip to content

Commit 3f5116d

Browse files
committed
1 parent 74d484c commit 3f5116d

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

airframe-surface/src/main/scala-3/org/opengrabeso/airframe/surface/CompileTimeSurfaceFactory.scala

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,14 +568,22 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
568568
// https://github.com/lampepfl/dotty-macro-examples/blob/aed51833db652f67741089721765ad5a349f7383/defaultParamsInference/src/macro.scala
569569
val defaultValue: Expr[Option[Any]] = field.defaultValueGetter match
570570
case Some(m) =>
571-
val companion = Ref(t.typeSymbol.companionModule)
572-
// Populate method type parameters with Any type
573-
val dummyTypeList: List[TypeRepr] = m.paramSymss.flatten.map { tp => TypeRepr.of[Any] }.toList
574-
val dv: Term = companion.select(m).appliedToTypes(dummyTypeList)
575-
'{ Some(${ dv.asExprOf[Any] }) }
571+
// Check if this is a case class in a trait, which can cause companion reference issues
572+
val isInTrait = t.typeSymbol.owner.flags.is(Flags.Trait)
573+
if isInTrait then
574+
// Skip default values for case classes defined in traits to avoid erasure issues
575+
'{ None }
576+
else
577+
val companion = Ref(t.typeSymbol.companionModule)
578+
// Populate method type parameters with Any type
579+
val dummyTypeList: List[TypeRepr] = m.paramSymss.flatten.map { tp => TypeRepr.of[Any] }.toList
580+
val dv: Term = companion.select(m).appliedToTypes(dummyTypeList)
581+
'{ Some(${ dv.asExprOf[Any] }) }
582+
576583
case _ => '{ None }
577584

578585
// Generate a field accessor { (x:Any) => x.asInstanceOf[A].(field name) }
586+
/*
579587
val paramIsAccessible =
580588
t.typeSymbol.fieldMember(paramName) match
581589
case nt if nt == Symbol.noSymbol => false
@@ -585,6 +593,7 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
585593
case m if m.privateWithin.nonEmpty => false
586594
case _ => true
587595
// println(s"${paramName} ${paramIsAccessible}")
596+
*/
588597

589598
// Using StaticMethodParameter when supportin Scala.js in Scala 3.
590599
// TODO: Deprecate RuntimeMethodParameter
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.opengrabeso.airframe.surface
15+
16+
import org.scalatest.funsuite.AnyFunSuite
17+
import org.scalatest.matchers.should
18+
19+
trait MT:
20+
case class B(min: Int = 0, max: Int = 0)
21+
22+
object MT extends MT
23+
24+
/**
25+
* Test and fix ported from https://github.com/wvlet/airframe/pull/3941
26+
* */
27+
class TraitInheritedClassTest extends AnyFunSuite with should.Matchers {
28+
test("reproduce the crash with trait-inherited case class") {
29+
// This should not crash the compiler during the erasure phase
30+
val schema = Surface.of[MT.B]
31+
schema should not be null
32+
schema.name shouldBe "B"
33+
schema.fullName shouldBe "org.opengrabeso.airframe.surface.MT.B"
34+
schema.params.length shouldBe 2
35+
schema.params(0).name shouldBe "min"
36+
schema.params(1).name shouldBe "max"
37+
}
38+
39+
test("original issue scenario should work") {
40+
// This reproduces the exact scenario from the issue
41+
val schema = Surface.of[MT.B]
42+
43+
// Should be able to print the schema without crashing
44+
val schemaString = schema.toString
45+
schemaString.contains("B") shouldBe true
46+
47+
// Should have proper parameters with default values (though they may be None due to trait limitation)
48+
schema.params.foreach { param =>
49+
param.surface should not be null
50+
param.surface.name shouldBe "Int"
51+
}
52+
}
53+
}

airframe-surface/src/test/scala/org/opengrabeso/airframe/surface/i3439.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ class i3439 extends AnyFunSuite with should.Matchers {
3131
test("Handle inherited inner class") {
3232
//val mm = Surface.methodsOf[OuterType.type]
3333
val m = Surface.methodsOf[OuterType.InnerType]
34-
//m.map(_.name) shouldContain "compare"
34+
m.map(_.name) should contain ("compare")
3535
}
3636
}

0 commit comments

Comments
 (0)