-
Notifications
You must be signed in to change notification settings - Fork 531
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
Add mkAttributedRef specialized for SingleType #994
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,7 +299,6 @@ trait CaseClassMacros extends ReprTypes with CaseClassMacrosVersionSpecifics { | |
val c: blackbox.Context | ||
|
||
import c.universe._ | ||
import internal.constantType | ||
|
||
def abort(msg: String) = | ||
c.abort(c.enclosingPosition, msg) | ||
|
@@ -347,17 +346,6 @@ trait CaseClassMacros extends ReprTypes with CaseClassMacrosVersionSpecifics { | |
loop(sym, Nil) | ||
} | ||
|
||
def mkDependentRef(prefix: Type, path: List[Name]): (Type, Symbol) = { | ||
val (_, pre, sym) = | ||
path.foldLeft((prefix, NoType, NoSymbol)) { | ||
case ((pre, _, sym), nme) => | ||
val sym0 = pre.member(nme) | ||
val pre0 = sym0.typeSignature | ||
(pre0, pre, sym0) | ||
} | ||
(pre, sym) | ||
} | ||
|
||
def isAnonOrRefinement(sym: Symbol): Boolean = { | ||
val nameStr = sym.name.toString | ||
nameStr.contains("$anon") || nameStr == "<refinement>" | ||
|
@@ -510,13 +498,6 @@ trait CaseClassMacros extends ReprTypes with CaseClassMacrosVersionSpecifics { | |
case (tpe, acc) => appliedType(cons, List(devarargify(tpe), acc)) | ||
} | ||
|
||
def mkLabelTpe(name: Name): Type = | ||
appliedType(atatTpe, List(typeOf[scala.Symbol], constantType(nameAsValue(name)))) | ||
|
||
def mkFieldTpe(name: Name, valueTpe: Type): Type = { | ||
appliedType(fieldTypeTpe, List(mkLabelTpe(name), valueTpe)) | ||
} | ||
|
||
def mkHListTpe(items: List[Type]): Type = | ||
mkCompoundTpe(hnilTpe, hconsTpe, items) | ||
|
||
|
@@ -557,100 +538,45 @@ trait CaseClassMacros extends ReprTypes with CaseClassMacrosVersionSpecifics { | |
} | ||
} | ||
|
||
def unpackFieldType(tpe: Type): (Type, Type) = | ||
FieldType.unapply(tpe).getOrElse(abort(s"$tpe is not a field type")) | ||
|
||
def findField(lTpe: Type, kTpe: Type): Option[(Type, Int)] = | ||
unpackHListTpe(lTpe).zipWithIndex.collectFirst { | ||
case (FieldType(k, v), i) if k =:= kTpe => (v, i) | ||
} | ||
|
||
def mkTypTree(tpe: Type): Tree = { | ||
tpe match { | ||
case SingleType(pre @ SingleType(_, _), sym) => | ||
SingletonTypeTree(mkAttributedRef(pre, sym)) | ||
|
||
case TypeRef(pre, _, args) if isVararg(tpe) => | ||
val argTrees = args.map(mkTypTree) | ||
AppliedTypeTree(varargTpt, argTrees) | ||
|
||
case t => tq"$t" | ||
} | ||
} | ||
|
||
def appliedTypTree1(tpe: Type, param: Type, arg: TypeName): Tree = { | ||
tpe match { | ||
case t if t =:= param => | ||
Ident(arg) | ||
case PolyType(params, body) if params.head.asType.toType =:= param => | ||
appliedTypTree1(body, param, arg) | ||
case TypeRef(pre, sym, List()) => | ||
case TypeRef(pre, sym, Nil) => | ||
mkAttributedRef(pre, sym) | ||
case TypeRef(pre, sym, args) => | ||
val argTrees = args.map(appliedTypTree1(_, param, arg)) | ||
AppliedTypeTree(mkAttributedRef(pre, sym), argTrees) | ||
case t => | ||
tq"$tpe" | ||
case other => | ||
tq"$other" | ||
} | ||
} | ||
|
||
def mkCompoundTypTree(nil: Type, cons: Type, items: List[Type]): Tree = | ||
items.foldRight(mkAttributedRef(nil): Tree) { case (tpe, acc) => | ||
AppliedTypeTree(mkAttributedRef(cons), List(mkTypTree(tpe), acc)) | ||
} | ||
|
||
def mkCompoundTypTree1(nil: Type, cons: Type, items: List[Type], param: Type, arg: TypeName): Tree = | ||
items.foldRight(mkAttributedRef(nil): Tree) { case (tpe, acc) => | ||
AppliedTypeTree(mkAttributedRef(cons), List(appliedTypTree1(tpe, param, arg), acc)) | ||
} | ||
|
||
def mkHListTypTree(items: List[Type]): Tree = | ||
mkCompoundTypTree(hnilTpe, hconsTpe, items) | ||
|
||
def mkHListTypTree1(items: List[Type], param: Type, arg: TypeName): Tree = | ||
mkCompoundTypTree1(hnilTpe, hconsTpe, items, param, arg) | ||
|
||
def mkCoproductTypTree(items: List[Type]): Tree = | ||
mkCompoundTypTree(cnilTpe, cconsTpe, items) | ||
|
||
def mkCoproductTypTree1(items: List[Type], param: Type, arg: TypeName): Tree = | ||
mkCompoundTypTree1(cnilTpe, cconsTpe, items, param, arg) | ||
|
||
def unfoldCompoundTpe(compoundTpe: Type, nil: Type, cons: Type): List[Type] = { | ||
@tailrec | ||
def loop(tpe: Type, acc: List[Type]): List[Type] = | ||
tpe.dealias match { | ||
case TypeRef(_, consSym, List(hd, tl)) | ||
if consSym.asType.toType.typeConstructor =:= cons => loop(tl, hd :: acc) | ||
case `nil` => acc | ||
case other => abort(s"Bad compound type $compoundTpe") | ||
} | ||
loop(compoundTpe, Nil).reverse | ||
} | ||
|
||
def hlistElements(tpe: Type): List[Type] = | ||
unfoldCompoundTpe(tpe, hnilTpe, hconsTpe) | ||
|
||
def coproductElements(tpe: Type): List[Type] = | ||
unfoldCompoundTpe(tpe, cnilTpe, cconsTpe) | ||
|
||
def reprTpe(tpe: Type): Type = { | ||
if(isProduct(tpe)) mkHListTpe(fieldsOf(tpe).map(_._2)) | ||
else mkCoproductTpe(ctorsOf(tpe)) | ||
} | ||
|
||
def param1(tpe: Type): Type = | ||
tpe match { | ||
case t if tpe.takesTypeArgs => t.typeParams.head.asType.toType | ||
case TypeRef(_, _, List(arg)) => arg | ||
case _ => NoType | ||
} | ||
|
||
def reprTypTree(tpe: Type): Tree = { | ||
if(isProduct(tpe)) mkHListTypTree(fieldsOf(tpe).map(_._2)) | ||
else mkCoproductTypTree(ctorsOf(tpe)) | ||
} | ||
|
||
def reprTypTree1(tpe: Type, arg: TypeName): Tree = { | ||
val param = param1(tpe) | ||
if(isProduct1(tpe)) mkHListTypTree1(fieldsOf(tpe).map(_._2), param, arg) | ||
|
@@ -677,19 +603,6 @@ trait CaseClassMacros extends ReprTypes with CaseClassMacrosVersionSpecifics { | |
isGetter && !isNonGeneric(sym) | ||
} | ||
|
||
def isSealedHierarchyClassSymbol(symbol: ClassSymbol): Boolean = { | ||
def helper(classSym: ClassSymbol): Boolean = { | ||
classSym.knownDirectSubclasses.toList forall { child0 => | ||
val child = child0.asClass | ||
child.typeSignature // Workaround for <https://issues.scala-lang.org/browse/SI-7755> | ||
|
||
isCaseClassLike(child) || (child.isSealed && helper(child)) | ||
} | ||
} | ||
|
||
symbol.isSealed && helper(symbol) | ||
} | ||
|
||
def classSym(tpe: Type): ClassSymbol = { | ||
val sym = tpe.typeSymbol | ||
if (!sym.isClass) | ||
|
@@ -795,6 +708,12 @@ trait CaseClassMacros extends ReprTypes with CaseClassMacrosVersionSpecifics { | |
global.gen.mkAttributedRef(gPre, gSym).asInstanceOf[Tree] | ||
} | ||
|
||
def mkAttributedRef(singleton: SingleType): Tree = { | ||
val SingleType(pre, sym) = singleton | ||
val getter = sym.asTerm.getter.orElse(sym) | ||
mkAttributedRef(pre, getter) | ||
} | ||
|
||
def isNonGeneric(sym: Symbol): Boolean = { | ||
def check(sym: Symbol): Boolean = { | ||
// See https://issues.scala-lang.org/browse/SI-7424 | ||
|
@@ -1017,7 +936,7 @@ class GenericMacros(val c: whitebox.Context) extends CaseClassMacros { | |
} | ||
|
||
def mkProductGeneric(tpe: Type): Tree = { | ||
val repr = mkHListTypTree(fieldsOf(tpe).map(_._2)) | ||
val repr = mkHListTpe(fieldsOf(tpe).map(_._2)) | ||
val ctorDtor = CtorDtor(tpe) | ||
val (p, ts) = ctorDtor.binding | ||
val to = cq"$p => ${mkHListValue(ts)}.asInstanceOf[$repr]" | ||
|
@@ -1029,18 +948,16 @@ class GenericMacros(val c: whitebox.Context) extends CaseClassMacros { | |
def mkCoproductGeneric(tpe: Type): Tree = { | ||
def mkCoproductCases(tpe0: Type, index: Int): Tree = tpe0 match { | ||
case TypeRef(pre, sym, Nil) if sym.isModuleClass => | ||
val singleton = mkAttributedRef(pre, sym.asClass.module) | ||
cq"p if p eq $singleton => $index" | ||
case SingleType(pre, sym) => | ||
val singleton = mkAttributedRef(pre, sym) | ||
cq"p if p eq $singleton => $index" | ||
cq"p if p eq ${mkAttributedRef(pre, sym.asClass.module)} => $index" | ||
case singleton: SingleType => | ||
cq"p if p eq ${mkAttributedRef(singleton)} => $index" | ||
case _ => | ||
cq"_: $tpe0 => $index" | ||
} | ||
|
||
val coproduct = objectRef[Coproduct.type] | ||
val ctors = ctorsOf(tpe) | ||
val repr = mkCoproductTypTree(ctors) | ||
val repr = mkCoproductTpe(ctors) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
val toCases = ctors.zipWithIndex.map((mkCoproductCases _).tupled) | ||
val to = q"$coproduct.unsafeMkCoproduct((p: @_root_.scala.unchecked) match { case ..$toCases }, p).asInstanceOf[$repr]" | ||
q"$generic.instance[$tpe, $repr]((p: $tpe) => $to, $coproduct.unsafeGet(_).asInstanceOf[$tpe])" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now
mkHListTypTree
is not used in shapeless anymore.But it might be wise to keep it because people might be using it in their own macros.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine removing it for 2.4.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok it turns out there is a lot of dead code already 😄