Skip to content

Allow refinements that refine already refined types. #244

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 1 commit into from
Nov 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -834,17 +834,45 @@ object desugar {
}
}.withPos(tree.pos)

/** Create a class definition with the same info as this refined type.
/** Create a class definition with the same info as the refined type given by `parent`
* and `refinements`.
*
* parent { refinements }
* ==>
* trait <refinement> extends parent { refinements }
* trait <refinement> extends core { this: self => refinements }
*
* Here, `core` is the (possibly parameterized) class part of `parent`.
* If `parent` is the same as `core`, self is empty. Otherwise `self` is `parent`.
*
* Example: Given
*
* class C
* type T1 extends C { type T <: A }
*
* the refined type
*
* If the parent is missing, Object is assumed.
* The result is used for validity checking, is thrown away afterwards.
* T1 { type T <: B }
*
* is expanded to
*
* trait <refinement> extends C { this: T1 => type T <: A }
*
* The result of this method is used for validity checking, is thrown away afterwards.
* @param parentType The type of `parent`
*/
def refinedTypeToClass(tree: RefinedTypeTree)(implicit ctx: Context): TypeDef = {
val parent = if (tree.tpt.isEmpty) TypeTree(defn.ObjectType) else tree.tpt
val impl = Template(emptyConstructor, parent :: Nil, EmptyValDef, tree.refinements)
def refinedTypeToClass(parent: tpd.Tree, refinements: List[Tree])(implicit ctx: Context): TypeDef = {
def stripToCore(tp: Type): Type = tp match {
case tp: RefinedType if tp.argInfos.nonEmpty => tp // parameterized class type
case tp: TypeRef if tp.symbol.isClass => tp // monomorphic class type
case tp: TypeProxy => stripToCore(tp.underlying)
case _ => defn.AnyType
Copy link
Member

Choose a reason for hiding this comment

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

Was this changed intentionally from Object to Any? If so, why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that was intentional. It seems that Any is the more natural parent type for a purely structural type.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed. I remember this coming up as a rough edge between value classes and structural types:

scala> trait T extends Any { def t }
defined trait T

scala> def foo(c: { def t} ) = c.t
warning: there was one feature warning; re-run with -feature for details
foo: (c: AnyRef{def t: Unit})Unit

scala> def test(t: T) = foo(t)
<console>:9: error: type mismatch;
 found   : t.type (with underlying type T)
 required: AnyRef{def t: Unit}
       def test(t: T) = foo(t)
                            ^

It would be interesting to experiment with changing this in scalac and running the community build to find out how compatible it is.

}
val parentCore = stripToCore(parent.tpe)
val untpdParent = TypedSplice(parent)
val (classParent, self) =
if (parent.tpe eq parentCore) (untpdParent, EmptyValDef)
else (TypeTree(parentCore), ValDef(nme.WILDCARD, untpdParent, EmptyTree))
val impl = Template(emptyConstructor, classParent :: Nil, self, refinements)
TypeDef(tpnme.REFINE_CLASS, impl).withFlags(Trait)
}

Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit

def typedRefinedTypeTree(tree: untpd.RefinedTypeTree)(implicit ctx: Context): RefinedTypeTree = track("typedRefinedTypeTree") {
val tpt1 = if (tree.tpt.isEmpty) TypeTree(defn.ObjectType) else typedAheadType(tree.tpt)
val refineClsDef = desugar.refinedTypeToClass(tree)
val refineClsDef = desugar.refinedTypeToClass(tpt1, tree.refinements)
val refineCls = createSymbol(refineClsDef).asClass
val TypeDef(_, Template(_, _, _, refinements1)) = typed(refineClsDef)
assert(tree.refinements.length == refinements1.length, s"${tree.refinements} != $refinements1")
Expand Down
1 change: 1 addition & 0 deletions test/dotc/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class tests extends CompilerTest {
@Test def neg_t1569_failedAvoid = compileFile(negDir, "t1569-failedAvoid", xerrors = 1)
@Test def neg_cycles = compileFile(negDir, "cycles", xerrors = 8)
@Test def neg_boundspropagation = compileFile(negDir, "boundspropagation", xerrors = 4)
@Test def neg_refinedSubtyping = compileFile(negDir, "refinedSubtyping", xerrors = 2)

@Test def dotc = compileDir(dotcDir + "tools/dotc", twice)(allowDeepSubtypes)
@Test def dotc_ast = compileDir(dotcDir + "tools/dotc/ast", twice)
Expand Down
23 changes: 23 additions & 0 deletions tests/neg/refinedSubtyping.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// tests that a refinement subtype satisfies all constraint
// of its refinemen supertype
class Test3 {

trait A
trait B

class C { type T }

type T1 = C { type T <: A }
type T2 = T1 { type T <: B }

type U1 = C { type T <: B }
type U2 = C { type T <: A }

var x: T2 = _
val y1: U1 = ???
val y2: U2 = ???

x = y1 // error
x = y2 // error

}
43 changes: 43 additions & 0 deletions tests/pos/refinedSubtyping.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,46 @@ class Test {
y = x

}

class Test2 {

trait A
trait B

class C { type T }

type T1 = C { type T <: A } { type T <: B }

type U1 = C { type T <: B } { type T <: A }

var x: T1 = _
var y: U1 = _

x = y
y = x
}


class Test3 {

trait A
trait B

class C { type T }

type T1 = C { type T <: A }
type T2 = T1 { type T <: B }

type U1 = C { type T <: B }
type U2 = U1 { type T <: A }

var x: T2 = _
var y: U2 = _

val x1 = x
val y1 = y

x = y
y = x

}