Skip to content
Next Next commit
initial works
  • Loading branch information
liufengyun committed Jul 5, 2016
commit 5cdd42f3fd4cb6faa8670b53ead6a1e7fce062f1
10 changes: 10 additions & 0 deletions src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,17 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
private var _id = 0 // left for debuging

override def transformMatch(tree: Match)(implicit ctx: Context, info: TransformerInfo): Tree = {
val Match(sel, cases) = tree

val translated = new Translator()(ctx).translator.translateMatch(tree)

// check exhaustivity and unreachability
val engine = new SpaceEngine
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PatternMatcher now has a 🌠SpaceEngine🌠 and can do interstellar travels. Looking forward to battle space invaders.
👾👾👾👾

if (!engine.skipCheck(sel.tpe)) {
engine.exhaustivity(tree)
engine.redundancy(tree)
}

translated.ensureConforms(tree.tpe)
}

Expand Down Expand Up @@ -1275,6 +1284,7 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
case _ => (cases, None)
}


// checkMatchVariablePatterns(nonSyntheticCases) // only used for warnings

// we don't transform after uncurry
Expand Down
9 changes: 9 additions & 0 deletions src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import Symbols._, TypeUtils._
*
* (9) Adds SourceFile annotations to all top-level classes and objects
*
* (10) Adds Child annotations to all sealed classes
*
* The reason for making this a macro transform is that some functions (in particular
* super and protected accessors and instantiation checks) are naturally top-down and
* don't lend themselves to the bottom-up approach of a mini phase. The other two functions
Expand Down Expand Up @@ -231,6 +233,13 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisTran
ctx.compilationUnit.source.exists &&
sym != defn.SourceFileAnnot)
sym.addAnnotation(Annotation.makeSourceFile(ctx.compilationUnit.source.file.path))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're unlucky, this will conflict with #1343.

if (!sym.isAnonymousClass) // ignore anonymous class
for (parent <- sym.asClass.classInfo.classParents) {
val pclazz = parent.classSymbol
if (pclazz.is(Sealed)) pclazz.addAnnotation(Annotation.makeChild(sym))
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anonymous classes caused pickling-related error for tests/run/t8611b.scala:

https://gist.github.com/liufengyun/f4aca0853e06f96c9130a0a05bfa0d4f

If I ignore anonymous classes, then it works:

              if (!sym.isAnonymousClass) // ignore anonymous class
                for (parent <- sym.asClass.classInfo.classParents) {
                  val pclazz = parent.classSymbol
                  if (pclazz.is(Sealed)) pclazz.addAnnotation(Annotation.makeChild(sym))
                }

The problem is that ignore anonymous classes would make the algorithm silent in case it should report a warning:

sealed trait A
case class B() extends A
case class C() extends A

object M {
  val x = new A {}

  x match {
     case _: B => true
     case _: C => true
  }
}

Any ideas, @odersky @smarter ?

Copy link
Member

@smarter smarter May 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a sealed trait has direct anonymous subclasses then it's impossible to pattern match on all subclasses. I think the simplest solution is to disallow direct anonymous subclasses of sealed traits.

tree
}
else {
Expand Down
Loading