diff --git a/core/src/main/scala/shapeless/default.scala b/core/src/main/scala/shapeless/default.scala index e6ddfe2a1..862475759 100644 --- a/core/src/main/scala/shapeless/default.scala +++ b/core/src/main/scala/shapeless/default.scala @@ -243,15 +243,18 @@ class DefaultMacros(val c: whitebox.Context) extends CaseClassMacros { alt => alt.isMethod && hasDefaultParams(alt.asMethod) } - def defaultsFor(fields: List[(TermName, Type)]) = for { - ((_, argTpe), i) <- fields.zipWithIndex - default = tpe.companion.member(TermName(s"apply$$default$$${i + 1}")) orElse - altCompanion.member(TermName(s"$$lessinit$$greater$$default$$${i + 1}")) - } yield if (default.isTerm) { - val defaultTpe = appliedType(someTpe, devarargify(argTpe)) - val defaultVal = some(q"$companion.$default") - (defaultTpe, defaultVal) - } else (noneTpe, none) + def defaultsFor(fields: List[(TermName, Type)]) = { + lazy val enclosing = ownerChain(c.internal.enclosingOwner) + for (((_, argTpe), i) <- fields.zipWithIndex) yield { + val default = tpe.companion.member(TermName(s"apply$$default$$${i + 1}")) orElse + altCompanion.member(TermName(s"$$lessinit$$greater$$default$$${i + 1}")) + if (default.isTerm) { + val owner = default.owner + val qualifier = if (!owner.isStatic && enclosing.contains(owner)) This(owner) else companion + (appliedType(someTpe, devarargify(argTpe)), some(q"$qualifier.$default")) + } else (noneTpe, none) + } + } def mkDefault(defaults: List[(Type, Tree)]) = { val (types, values) = defaults.unzip diff --git a/core/src/test/scala/shapeless/default.scala b/core/src/test/scala/shapeless/default.scala index fc5ac3fce..4c641fd8f 100644 --- a/core/src/test/scala/shapeless/default.scala +++ b/core/src/test/scala/shapeless/default.scala @@ -224,4 +224,14 @@ class DefaultTests { assertTypedEquals[None.type :: HNil](None :: HNil, default2()) assertTypedEquals[HNil](HNil, default3()) } + + @Test + def testInCompanion: Unit = + assertEquals(InCompanion.default, Some(9) :: HNil) + + // Note: this has to be inside a class, not an object + case class InCompanion(a: Int = 9) + object InCompanion { + val default = Default[InCompanion].apply() + } }