Skip to content

Commit

Permalink
small tree attachment refactoring: firstAttachment
Browse files Browse the repository at this point in the history
  • Loading branch information
adriaanm committed May 2, 2012
1 parent 90d2bee commit f004e99
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/compiler/scala/tools/nsc/typechecker/Typers.scala
Expand Up @@ -2209,7 +2209,8 @@ trait Typers extends Modes with Adaptations with Taggings with PatMatVirtualiser
def adaptCase(cdef: CaseDef, mode: Int, tpe: Type): CaseDef = deriveCaseDef(cdef)(adapt(_, mode, tpe))

// takes untyped sub-trees of a match and type checks them
def typedMatch(selector0: Tree, cases: List[CaseDef], mode: Int, resTp: Type) = {
def typedMatch(selector0: Tree, cases: List[CaseDef], mode: Int, resTp: Type): Match = {
// strip off the annotation as it won't type check
val (selector, doTranslation) = selector0 match {
case Annotated(Ident(nme.synthSwitch), selector) => (selector, false)
case s => (s, true)
Expand Down
11 changes: 5 additions & 6 deletions src/library/scala/reflect/api/Trees.scala
Expand Up @@ -110,13 +110,12 @@ trait Trees { self: Universe =>
def withoutAttachment(att: Any): this.type = { detach(att); this }
def attachment[T: ClassTag]: T = attachmentOpt[T] getOrElse { throw new Error("no attachment of type %s".format(classTag[T].erasure)) }
def attachmentOpt[T: ClassTag]: Option[T] =
firstAttachment { case attachment if attachment.getClass == classTag[T].erasure => attachment.asInstanceOf[T] }

def firstAttachment[T](p: PartialFunction[Any, T]): Option[T] =
rawatt match {
case NontrivialAttachment(pos, payload) =>
val index = payload.indexWhere(p => p.getClass == classTag[T].erasure)
if (index != -1) Some(payload(index).asInstanceOf[T])
else None
case _ =>
None
case NontrivialAttachment(pos, payload) => payload.collectFirst(p)
case _ => None
}

private[this] var rawtpe: Type = _
Expand Down

0 comments on commit f004e99

Please sign in to comment.