Skip to content

Fix more problems detected by fuzzing in #4389 #4411

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

Merged
merged 13 commits into from
Apr 30, 2018
Merged
34 changes: 16 additions & 18 deletions compiler/src/dotty/tools/dotc/ast/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -769,28 +769,26 @@ object Trees {
def genericEmptyTree[T >: Untyped]: Thicket[T] = theEmptyTree.asInstanceOf[Thicket[T]]

def flatten[T >: Untyped](trees: List[Tree[T]]): List[Tree[T]] = {
var buf: ListBuffer[Tree[T]] = null
var xs = trees
while (!xs.isEmpty) {
xs.head match {
case Thicket(elems) =>
if (buf == null) {
buf = new ListBuffer
var ys = trees
while (ys ne xs) {
buf += ys.head
ys = ys.tail
def recur(buf: ListBuffer[Tree[T]], remaining: List[Tree[T]]): ListBuffer[Tree[T]] =
remaining match {
case Thicket(elems) :: remaining1 =>
var buf1 = buf
if (buf1 == null) {
buf1 = new ListBuffer[Tree[T]]
var scanned = trees
while (scanned `ne` remaining) {
buf1 += scanned.head
scanned = scanned.tail
}
}
for (elem <- elems) {
assert(!elem.isInstanceOf[Thicket[_]])
buf += elem
}
case tree =>
recur(recur(buf1, elems), remaining1)
case tree :: remaining1 =>
if (buf != null) buf += tree
recur(buf, remaining1)
case nil =>
buf
}
xs = xs.tail
}
val buf = recur(null, trees)
if (buf != null) buf.toList else trees
}

Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Constants.scala
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ object Constants {
*/
def convertTo(pt: Type)(implicit ctx: Context): Constant = {
def classBound(pt: Type): Type = pt.dealias.stripTypeVar match {
case tref: TypeRef if !tref.symbol.isClass => classBound(tref.info.bounds.lo)
case tref: TypeRef if !tref.symbol.isClass && tref.info.exists =>
classBound(tref.info.bounds.lo)
case param: TypeParamRef =>
ctx.typerState.constraint.entry(param) match {
case TypeBounds(lo, hi) =>
Expand Down
10 changes: 8 additions & 2 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1467,8 +1467,14 @@ object SymDenotations {
onBehalf.signalProvisional()
val builder = new BaseDataBuilder
for (p <- classParents) {
if (p.typeSymbol.isClass) builder.addAll(p.typeSymbol.asClass.baseClasses)
else assert(isRefinementClass || ctx.mode.is(Mode.Interactive), s"$this has non-class parent: $p")
var pcls = p.typeSymbol
if (!pcls.isClass) pcls = p.underlyingClassRef(refinementOK = false).typeSymbol
// This roundabout way is necessary for avoiding cyclic references.
// A test case is CompilationTests.compileMixed
pcls match {
case pcls: ClassSymbol => builder.addAll(pcls.baseClasses)
case _ => assert(isRefinementClass || ctx.mode.is(Mode.Interactive), s"$this has non-class parent: $p")
}
}
(classSymbol :: builder.baseClasses, builder.baseClassSet)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
val accu1 = if (accu exists (_ derivesFrom c)) accu else c :: accu
if (cs == c.baseClasses) accu1 else dominators(rest, accu1)
case Nil => // this case can happen because after erasure we do not have a top class anymore
assert(ctx.erasedTypes)
assert(ctx.erasedTypes || ctx.reporter.errorsReported)
defn.ObjectClass :: Nil
}

Expand Down
11 changes: 6 additions & 5 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -589,13 +589,14 @@ object Types {

def goRefined(tp: RefinedType) = {
val pdenot = go(tp.parent)
val pinfo = pdenot.info
val rinfo = tp.refinedInfo
if (name.isTypeName) { // simplified case that runs more efficiently
if (name.isTypeName && !pinfo.isInstanceOf[ClassInfo]) { // simplified case that runs more efficiently
val jointInfo =
if (rinfo.isAlias) rinfo
else if (pdenot.info.isAlias) pdenot.info
else if (ctx.pendingMemberSearches.contains(name)) pdenot.info safe_& rinfo
else pdenot.info recoverable_& rinfo
else if (pinfo.isAlias) pinfo
else if (ctx.pendingMemberSearches.contains(name)) pinfo safe_& rinfo
else pinfo recoverable_& rinfo
pdenot.asSingleDenotation.derivedSingleDenotation(pdenot.symbol, jointInfo)
} else {
pdenot & (
Expand Down Expand Up @@ -3477,7 +3478,7 @@ object Types {
if (selfTypeCache == null)
selfTypeCache = {
val given = cls.givenSelfType
if (!given.exists) appliedRef
if (!given.isValueType) appliedRef
Copy link
Member

Choose a reason for hiding this comment

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

Might be worth explicitly checking that an error happened like other commits in this PR do, e.g.:

if (!given.isValueType) {
  assert(!given.exists || ctx.reporter.errorsReported)
  appliedRef
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The given type might be higher-kinded (in fact that's what it was in the test) but this would be detected only later.

Copy link
Member

Choose a reason for hiding this comment

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

OK, in that case maybe just add a comment explaining that non-value types self can only happen with erroneous code? Otherwise readers of this code might be puzzled.

else if (cls is Module) given
else if (ctx.erasedTypes) appliedRef
else AndType(given, appliedRef)
Expand Down
14 changes: 10 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,11 @@ object NamerContextOps {
/** Find moduleClass/sourceModule in effective scope */
private def findModuleBuddy(name: Name, scope: Scope)(implicit ctx: Context) = {
val it = scope.lookupAll(name).filter(_ is Module)
assert(it.hasNext, s"no companion $name in $scope")
it.next()
if (it.hasNext) it.next()
else {
assert(ctx.reporter.errorsReported, s"no companion $name in $scope")
NoSymbol
}
}
}

Expand Down Expand Up @@ -1033,8 +1036,11 @@ class Namer { typer: Typer =>
*/
def moduleValSig(sym: Symbol)(implicit ctx: Context): Type = {
val clsName = sym.name.moduleClassName
val cls = ctx.denotNamed(clsName) suchThat (_ is ModuleClass)
ctx.owner.thisType select (clsName, cls)
val cls = ctx.denotNamed(clsName).suchThat(_ is ModuleClass).orElse {
assert(ctx.reporter.errorsReported)
ctx.newStubSymbol(ctx.owner, clsName)
}
ctx.owner.thisType.select(clsName, cls)
}

/** The type signature of a ValDef or DefDef
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,11 @@ class Typer extends Namer
assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
}

def typedClassDef(cdef: untpd.TypeDef, cls: ClassSymbol)(implicit ctx: Context) = track("typedClassDef") {
def typedClassDef(cdef: untpd.TypeDef, cls: ClassSymbol)(implicit ctx: Context): Tree = track("typedClassDef") {
if (!cls.info.isInstanceOf[ClassInfo]) {
assert(ctx.reporter.errorsReported)
return cdef.withType(UnspecifiedErrorType)
Copy link
Member

Choose a reason for hiding this comment

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

I think ideally we should have a way to recursively set the type of the tree children to maintain the invariant that a typed tree only contains typed trees.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, that's not so easy. Sometimes, there's simply no way to proceed. And we cannot just assign an ErrorType, because a lot of trees are not typed trees, so we'd have to replace them with something else.

Copy link
Member

Choose a reason for hiding this comment

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

I guess they could be replaced by ErrorTrees.

Copy link
Member

Choose a reason for hiding this comment

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

In fact, maybe cdef should just be replaced by an ErrorTree since it's unusable anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, yes. Let's do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm wait. What do you mean by ErrorTree? We only have errorTree which gives an error type to an existing tree.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, we can replace it with an EmptyTree instead.

}
val TypeDef(name, impl @ Template(constr, parents, self, _)) = cdef
val superCtx = ctx.superCallContext

Expand Down
6 changes: 6 additions & 0 deletions tests/neg/bad-selftype.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
trait x0[T] { self: x0 => } // error

trait x1[T] { self: (=> String) => } // error

trait x2[T] { self: ([X] => X) => } // error

5 changes: 5 additions & 0 deletions tests/neg/parser-stability-11.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package x0 {
case class x0 // error // error
}
package x0
class x0 // error // error
3 changes: 3 additions & 0 deletions tests/neg/parser-stability-12.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trait x0[] // error
trait x1[x1 <:x0]
extends x1[ // error
4 changes: 4 additions & 0 deletions tests/neg/parser-stability-14.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object x0 {
{
val x1 x0 // error
object x1 // error // error
6 changes: 6 additions & 0 deletions tests/neg/parser-stability-15.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package x0
class x0 {
def x1 =
x2 match // error
] // error
case x3[] => x0 // error // error // error // error
5 changes: 5 additions & 0 deletions tests/neg/parser-stability-16.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class x0[x0] {
val x1 : x0
}
trait x3 extends x0 {
x1 = 0 object // error // error
2 changes: 2 additions & 0 deletions tests/neg/parser-stability-17.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
trait x0[] { x0: x0 => }
class x0[x1] extends x0[x0 x0] x2 x0
4 changes: 4 additions & 0 deletions tests/neg/parser-stability-18.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
trait x0 {
class x1 (x1:x0
{
type x1 <: List[x1 <: // error // error
36 changes: 36 additions & 0 deletions tests/pos/class-refinement.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
object Test {

class C {

class I

}

trait T

val x: C { type I <: T } = ??? // direct refinement of class member

val y: x.I = ???

}

class B {
class C {
type I
}
trait T

type CC <: C

val x: CC { type I <: T } = ???
}

object Test2 extends B {

class CC extends C { class I }

val y: x.I = ??? // indirect refinement of class member


}