title: Working around nasty Pattern Matcher bug
body: <p>I am defining a generic tree structure, i.e. one which can be extended so that for example branches and leaves contain additional values (I need this to add a name string for example).</p>
  
  <p>So it looks like this:</p>
  
  <pre><code>trait TreeNodes {
    val Node              = Either
    type Node[+B, +L]     = Either[B, L]
    val IsBranch          = Left
    type IsBranch[+B, +L] = Left[B, L]
    val IsLeaf            = Right
    type IsLeaf[+B, +L]   = Right[B, L]
  }
  
  object TreeLike extends TreeNodes {
    trait BranchLike[Elem, B, L] {
      type N = Node[B, L]
  
      def iterator: Iterator[N]
    }
  
    trait LeafLike[Elem] {
      def value: Elem
    }
  }
  trait TreeLike[Elem, Repr] {
    type Leaf   &lt;: TreeLike.LeafLike[Elem]
    type Branch &lt;: TreeLike.BranchLike[Elem, Branch, Leaf]
  
    def root: Branch
  }
  </code></pre>
  
  <p>Unfortunately, there is a pattern matcher bug:</p>
  
  <pre><code>def test[Elem, T &lt;: TreeLike[Elem, T]](tree: T): Unit = {
    import TreeLike.{IsLeaf, IsBranch}
  
    def printLeaves(b: T#Branch): Unit = b.iterator.foreach {
      case IsLeaf(l)    =&gt; println(l.value)
      case IsBranch(c)  =&gt; printLeaves(c)
    }
  
    printLeaves(tree.root)
  }
  </code></pre>
  
  <p>The error is as follows:</p>
  
  <pre><code>[error]         during phase: patmat
  [error]      library version: version 2.10.3
  [error]     compiler version: version 2.10.3
  ...
  [error]    symbol definition: case val x1: b.N
  [error]                  tpe: b.N
  [error]        symbol owners: value x1
  [error]       context owners: value x0$1 -&gt; value $anonfun -&gt; method printLeaves -&gt; 
    method test -&gt; object Voodoo -&gt; package typerbug
  ...
  [error] no-symbol does not have an owner
  </code></pre>
  
  <p>I suspect patmat has troubles with <code>T#Branch</code> somehow. Any ideas how to work around here?</p>
  
  <p>I am also not 100% happy with wrapping leaves and branches in <code>Either</code>. This was necessary because the stuff got way out of hand when I tried to define a super type of <code>LeafLike</code> and <code>BranchLike</code> and figuring out how to correctly sub type that in implementations, and pattern matching broke done as well because I didn't figure out how to get correct extractors. So perhaps using <code>Either</code> is not that as a bad idea?</p>