-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Special rule for {this} in capture sets of class members
Consider the lazylists.scala test in pos-custom-args/captures: ```scala class CC type Cap = {*} CC trait LazyList[+A]: this: ({*} LazyList[A]) => def isEmpty: Boolean def head: A def tail: {this} LazyList[A] object LazyNil extends LazyList[Nothing]: def isEmpty: Boolean = true def head = ??? def tail = ??? extension [A](xs: {*} LazyList[A]) def map[B](f: {*} A => B): {xs, f} LazyList[B] = class Mapped extends LazyList[B]: this: ({xs, f} Mapped) => def isEmpty = false def head: B = f(xs.head) def tail: {this} LazyList[B] = xs.tail.map(f) // OK new Mapped ``` Without this commit, the second to last line is an error since the right hand side has capture set `{xs, f}` but the required capture set is `this`. To fix this, we widen the expected type of the rhs `xs.tail.map(f)` from `{this}` to `{this, f, xs}`. That is, we add the declared captures of the self type to the expected type. The soundness argument for doing this is as follows: Since `tail` does not have parameters, the only thing it could capture are references that the receiver `this` captures as well. So `xs` and `f` must come via `this`. For instance, if the receiver `xs` of `xs.tail` happens to be pure, then `xs.tail` is pure as well. On the other hand, in the neg test `lazylists1.scala` we add the following line to `Mapped`: ```scala def concat(other: {f} LazyList[A]): {this} LazyList[A] = ??? : ({xs, f} LazyList[A]) // error ``` Here, we cannot widen the expected type from `{this}` to `{this, xs, f}` since the result of concat refers to `f` independently of `this`, namely through its parameter `other`. Hence, if `ys: {f} LazyList[String]` then ``` LazyNil.concat(ys) ``` still refers to `f` even though `LazyNil` is pure. But if we would accept the definition of `concat` above, the type of `LazyNil.concat(ys)` would be `LazyList[String]`, which is unsound. The current implementation widens the expected type of class members if the class member does not have tracked parameters. We could potentially refine this to say we widen with all references in the expected type that are not subsumed by one of the parameter types.
- Loading branch information
Showing
8 changed files
with
204 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazylists1.scala:25:63 ----------------------------------- | ||
25 | def concat(other: {f} LazyList[A]): {this} LazyList[A] = ??? : ({xs, f} LazyList[A]) // error | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| Found: {xs, f} LazyList[A] | ||
| Required: {Mapped.this} LazyList[A] | ||
|
||
longer explanation available when compiling with `-explain` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class CC | ||
type Cap = {*} CC | ||
|
||
trait LazyList[+A]: | ||
this: ({*} LazyList[A]) => | ||
|
||
def isEmpty: Boolean | ||
def head: A | ||
def tail: {this} LazyList[A] | ||
|
||
object LazyNil extends LazyList[Nothing]: | ||
def isEmpty: Boolean = true | ||
def head = ??? | ||
def tail = ??? | ||
|
||
extension [A](xs: {*} LazyList[A]) | ||
def map[B](f: {*} A => B): {xs, f} LazyList[B] = | ||
class Mapped extends LazyList[B]: | ||
this: ({xs, f} Mapped) => | ||
|
||
def isEmpty = false | ||
def head: B = f(xs.head) | ||
def tail: {this} LazyList[B] = xs.tail.map(f) // OK | ||
def drop(n: Int): {this} LazyList[B] = ??? : ({xs, f} LazyList[B]) // OK | ||
def concat(other: {f} LazyList[A]): {this} LazyList[A] = ??? : ({xs, f} LazyList[A]) // error | ||
new Mapped | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
-- [E163] Declaration Error: tests/neg-custom-args/captures/lazylists2.scala:50:10 ------------------------------------- | ||
50 | def tail: {xs, f} LazyList[B] = xs.tail.map(f) // error | ||
| ^ | ||
| error overriding method tail in trait LazyList of type => {Mapped.this} LazyList[B]; | ||
| method tail of type => {xs, f} LazyList[B] has incompatible type | ||
|
||
longer explanation available when compiling with `-explain` | ||
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazylists2.scala:18:4 ------------------------------------ | ||
18 | class Mapped extends LazyList[B]: // error | ||
| ^ | ||
| Found: {f, xs} LazyList[B] | ||
| Required: {f} LazyList[B] | ||
19 | this: ({xs, f} Mapped) => | ||
20 | def isEmpty = false | ||
21 | def head: B = f(xs.head) | ||
22 | def tail: {this} LazyList[B] = xs.tail.map(f) | ||
23 | new Mapped | ||
|
||
longer explanation available when compiling with `-explain` | ||
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazylists2.scala:27:4 ------------------------------------ | ||
27 | class Mapped extends LazyList[B]: // error | ||
| ^ | ||
| Found: {f, xs} LazyList[B] | ||
| Required: {xs} LazyList[B] | ||
28 | this: ({xs, f} Mapped) => | ||
29 | def isEmpty = false | ||
30 | def head: B = f(xs.head) | ||
31 | def tail: {this} LazyList[B] = xs.tail.map(f) | ||
32 | new Mapped | ||
|
||
longer explanation available when compiling with `-explain` | ||
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazylists2.scala:41:48 ----------------------------------- | ||
41 | def tail: {this} LazyList[B] = xs.tail.map(f) // error | ||
| ^^^^^^^^^^^^^^ | ||
| Found: {f} LazyList[B] | ||
| Required: {xs} LazyList[B] | ||
|
||
longer explanation available when compiling with `-explain` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class CC | ||
type Cap = {*} CC | ||
|
||
trait LazyList[+A]: | ||
this: ({*} LazyList[A]) => | ||
|
||
def isEmpty: Boolean | ||
def head: A | ||
def tail: {this} LazyList[A] | ||
|
||
object LazyNil extends LazyList[Nothing]: | ||
def isEmpty: Boolean = true | ||
def head = ??? | ||
def tail = ??? | ||
|
||
extension [A](xs: {*} LazyList[A]) | ||
def map[B](f: {*} A => B): {f} LazyList[B] = | ||
class Mapped extends LazyList[B]: // error | ||
this: ({xs, f} Mapped) => | ||
|
||
def isEmpty = false | ||
def head: B = f(xs.head) | ||
def tail: {this} LazyList[B] = xs.tail.map(f) | ||
new Mapped | ||
|
||
def map2[B](f: {*} A => B): {xs} LazyList[B] = | ||
class Mapped extends LazyList[B]: // error | ||
this: ({xs, f} Mapped) => | ||
|
||
def isEmpty = false | ||
def head: B = f(xs.head) | ||
def tail: {this} LazyList[B] = xs.tail.map(f) | ||
new Mapped | ||
|
||
def map3[B](f: {*} A => B): {xs} LazyList[B] = | ||
class Mapped extends LazyList[B]: | ||
this: ({xs} Mapped) => | ||
|
||
def isEmpty = false | ||
def head: B = f(xs.head) | ||
def tail: {this} LazyList[B] = xs.tail.map(f) // error | ||
new Mapped | ||
|
||
def map4[B](f: {*} A => B): {xs} LazyList[B] = | ||
class Mapped extends LazyList[B]: | ||
this: ({xs, f} Mapped) => | ||
|
||
def isEmpty = false | ||
def head: B = f(xs.head) | ||
def tail: {xs, f} LazyList[B] = xs.tail.map(f) // error | ||
new Mapped | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class CC | ||
type Cap = {*} CC | ||
|
||
//------------------------------------------------- | ||
|
||
def test(E: Cap) = | ||
|
||
trait LazyList[+A]: | ||
protected def contents: {E} () => (A, {E} LazyList[A]) | ||
def isEmpty: Boolean | ||
def head: A = contents()._1 | ||
def tail: {E} LazyList[A] = contents()._2 | ||
|
||
class LazyCons[+A](override val contents: {E} () => (A, {E} LazyList[A])) | ||
extends LazyList[A]: | ||
def isEmpty: Boolean = false | ||
|
||
object LazyNil extends LazyList[Nothing]: | ||
def contents: {E} () => (Nothing, LazyList[Nothing]) = ??? | ||
def isEmpty: Boolean = true | ||
|
||
extension [A](xs: {E} LazyList[A]) | ||
def map[B](f: {E} A => B): {E} LazyList[B] = | ||
if xs.isEmpty then LazyNil | ||
else | ||
val cons = () => (f(xs.head), xs.tail.map(f)) | ||
LazyCons(cons) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class CC | ||
type Cap = {*} CC | ||
|
||
trait LazyList[+A]: | ||
this: ({*} LazyList[A]) => | ||
|
||
def isEmpty: Boolean | ||
def head: A | ||
def tail: {this} LazyList[A] | ||
|
||
object LazyNil extends LazyList[Nothing]: | ||
def isEmpty: Boolean = true | ||
def head = ??? | ||
def tail = ??? | ||
|
||
extension [A](xs: {*} LazyList[A]) | ||
def map[B](f: {*} A => B): {xs, f} LazyList[B] = | ||
class Mapped extends LazyList[B]: | ||
this: ({xs, f} Mapped) => | ||
|
||
def isEmpty = false | ||
def head: B = f(xs.head) | ||
def tail: {this} LazyList[B] = xs.tail.map(f) // OK | ||
new Mapped | ||
|
||
def test(cap1: Cap, cap2: Cap) = | ||
def f(x: String): String = if cap1 == cap1 then "" else "a" | ||
def g(x: String): String = if cap2 == cap2 then "" else "a" | ||
|
||
val xs = | ||
class Initial extends LazyList[String]: | ||
this: ({cap1} Initial) => | ||
|
||
def isEmpty = false | ||
def head = f("") | ||
def tail = LazyNil | ||
new Initial |