Skip to content

Commit 86db91c

Browse files
authored
Merge pull request #1147 from joroKr21/static-ctors
Shortcut for static constructors
2 parents 1211c9b + 2748313 commit 86db91c

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

core/src/main/scala/shapeless/generic.scala

+36-36
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,15 @@ trait CaseClassMacros extends ReprTypes with CaseClassMacrosVersionSpecifics {
439439
fn1 < fn2 || (fn1 == fn2 && isLess(s1, s2))
440440
}
441441

442-
val ctorSyms = collectCtors(baseSym).sortWith(orderSyms)
443-
val ctors =
444-
ctorSyms flatMap { sym =>
445-
import c.internal._
442+
val ctors = collectCtors(baseSym).sortWith(orderSyms).flatMap { sym =>
443+
import c.internal._
446444

445+
val owner = sym.owner
446+
val isNamed = !isAnonOrRefinement(sym)
447+
448+
// Construct a stable prefix from the path.
449+
val pre = if (sym.isStatic) prefix(sym.toType) else {
447450
// Look for a path from the macro call site to the subtype.
448-
val owner = sym.owner
449-
val isNamed = !isAnonOrRefinement(sym)
450451
val owners = ownerChain(if (isNamed) owner else owner.owner)
451452
val prePaths = for (pre <- Iterator.iterate(basePre)(prefix).takeWhile(_ != NoPrefix))
452453
yield (pre, owners.iterator.dropWhile(pre.baseType(_) == NoType))
@@ -458,44 +459,43 @@ trait CaseClassMacros extends ReprTypes with CaseClassMacrosVersionSpecifics {
458459
(NoPrefix, if (common < 0) Iterator.empty else owners.iterator drop common - 1)
459460
}
460461

461-
// Construct a stable prefix from the path.
462-
val pre = path.drop(1).foldLeft(pre0) { (pre1, part) =>
462+
path.drop(1).foldLeft(pre0) { (pre1, part) =>
463463
if (part.isType) part.asType.toTypeIn(pre1)
464464
else abort(s"$tpe has a subtype $sym with unstable prefix")
465465
}
466+
}
466467

467-
val ctor = if (isNamed) {
468-
if (sym.isModuleClass) {
469-
sym.toTypeIn(pre)
470-
} else {
471-
val subst = thisType(sym).baseType(baseSym).typeArgs
472-
val params = sym.typeParams
473-
val (args, free) = params.foldRight((List.empty[Type], false)) {
474-
case (param, (args, free)) =>
475-
val i = subst.indexWhere(_.typeSymbol == param)
476-
val arg = if (i >= 0) baseArgs(i) else param.asType.toType
477-
(arg :: args, free || i < 0)
478-
}
479-
480-
val ref = typeRef(pre, sym, args)
481-
if (free) existentialAbstraction(params, ref) else ref
482-
}
468+
val ctor = if (isNamed) {
469+
if (sym.isModuleClass) {
470+
sym.toTypeIn(pre)
483471
} else {
484-
def ownerIsSubType = owner.typeSignatureIn(pre) <:< baseTpe
485-
if (owner.isTerm && owner.asTerm.isVal && ownerIsSubType) singleType(pre, owner)
486-
else abort(s"$tpe has a subtype $sym with unstable prefix")
487-
}
472+
val subst = thisType(sym).baseType(baseSym).typeArgs.map(_.typeSymbol)
473+
val params = sym.typeParams
474+
val free = params.exists(!subst.contains(_))
475+
val args = for (param <- params) yield {
476+
val i = subst.indexOf(param)
477+
if (i >= 0) baseArgs(i) else param.asType.toType
478+
}
488479

489-
if(!isAccessible(ctor))
490-
abort(s"$tpe has an inaccessible subtype $ctor")
491-
if(ctor <:< baseTpe) Some(ctor) else None
480+
val ref = typeRef(pre, sym, args)
481+
if (free) existentialAbstraction(params, ref) else ref
482+
}
483+
} else {
484+
def ownerIsSubType = owner.typeSignatureIn(pre) <:< baseTpe
485+
if (owner.isTerm && owner.asTerm.isVal && ownerIsSubType) singleType(pre, owner)
486+
else abort(s"$tpe has a subtype $sym with unstable prefix")
492487
}
493-
if (ctors.isEmpty)
494-
abort(s"Sealed trait $tpe has no case class subtypes")
495-
ctors
496-
}
497-
else
488+
489+
if (!isAccessible(ctor)) abort(s"$tpe has an inaccessible subtype $ctor")
490+
else if (ctor <:< baseTpe) Some(ctor)
491+
else None
492+
}
493+
494+
if (ctors.isEmpty) abort(s"Sealed trait $tpe has no case class subtypes")
495+
else ctors
496+
} else {
498497
abort(s"$tpe is not a case class, case class-like, a sealed trait or Unit")
498+
}
499499
}
500500

501501
def nameAsString(name: Name): String = name.decodedName.toString.trim

0 commit comments

Comments
 (0)