-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Make reach refinement shallow #19171
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,6 +236,22 @@ extension (tp: Type) | |
* (2) all covariant occurrences of cap replaced by `x*`, provided there | ||
* are no occurrences in `T` at other variances. (1) is standard, whereas | ||
* (2) is new. | ||
* | ||
* For (2), multiple-flipped covariant occurrences of cap won't be replaced. | ||
* In other words, | ||
* | ||
* - For xs: List[File^] ==> List[File^{xs*}], the cap is replaced; | ||
* - while f: [R] -> (op: File^ => R) -> R remains unchanged. | ||
* | ||
* Without this restriction, the signature of functions like withFile: | ||
* | ||
* (path: String) -> [R] -> (op: File^ => R) -> R | ||
* | ||
* could be refined to | ||
* | ||
* (path: String) -> [R] -> (op: File^{withFile*} => R) -> R | ||
* | ||
* which is clearly unsound. | ||
* | ||
* Why is this sound? Covariant occurrences of cap must represent capabilities | ||
* that are reachable from `x`, so they are included in the meaning of `{x*}`. | ||
|
@@ -245,18 +261,27 @@ extension (tp: Type) | |
def withReachCaptures(ref: Type)(using Context): Type = | ||
object narrowCaps extends TypeMap: | ||
var ok = true | ||
def apply(t: Type) = t.dealias match | ||
case t1 @ CapturingType(p, cs) if cs.isUniversal => | ||
if variance > 0 then | ||
t1.derivedCapturingType(apply(p), ref.reach.singletonCaptureSet) | ||
else | ||
ok = false | ||
t | ||
case _ => t match | ||
case t @ CapturingType(p, cs) => | ||
t.derivedCapturingType(apply(p), cs) // don't map capture set variables | ||
case t => | ||
mapOver(t) | ||
|
||
/** Has the variance been flipped at this point? */ | ||
private var isFlipped: Boolean = false | ||
|
||
def apply(t: Type) = | ||
val saved = isFlipped | ||
try | ||
if variance <= 0 then isFlipped = true | ||
t.dealias match | ||
case t1 @ CapturingType(p, cs) if cs.isUniversal => | ||
if variance > 0 then | ||
t1.derivedCapturingType(apply(p), if isFlipped then cs else ref.reach.singletonCaptureSet) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then you can leave things as they were here. |
||
else | ||
ok = false | ||
t | ||
case _ => t match | ||
case t @ CapturingType(p, cs) => | ||
t.derivedCapturingType(apply(p), cs) // don't map capture set variables | ||
case t => | ||
mapOver(t) | ||
finally isFlipped = saved | ||
ref match | ||
case ref: CaptureRef if ref.isTrackableRef => | ||
val tp1 = narrowCaps(tp) | ||
|
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,18 @@ | ||
import language.experimental.captureChecking | ||
trait IO | ||
def test1(): Unit = | ||
val f: IO^ => IO^ = x => x | ||
val g: IO^ => IO^{f*} = f // error | ||
def test2(): Unit = | ||
val f: [R] -> (IO^ => R) -> R = ??? | ||
val g: [R] -> (IO^{f*} => R) -> R = f // error | ||
def test3(): Unit = | ||
val f: [R] -> (IO^ -> R) -> R = ??? | ||
val g: [R] -> (IO^{f*} -> R) -> R = f // error | ||
def test4(): Unit = | ||
val xs: List[IO^] = ??? | ||
val ys: List[IO^{xs*}] = xs // ok | ||
def test5(): Unit = | ||
val f: [R] -> (IO^ -> R) -> IO^ = ??? | ||
val g: [R] -> (IO^ -> R) -> IO^{f*} = f // ok | ||
val h: [R] -> (IO^{f*} -> R) -> IO^ = f // error |
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,8 @@ | ||
import language.experimental.captureChecking | ||
|
||
trait File | ||
val useFile: [R] -> (path: String) -> (op: File^ -> R) -> R = ??? | ||
def main(): Unit = | ||
val f: [R] -> (path: String) -> (op: File^ -> R) -> R = useFile | ||
val g: [R] -> (path: String) -> (op: File^{f*} -> R) -> R = f // error | ||
val leaked = g[File^{f*}]("test")(f => f) // boom |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the logic can be simplified a bit by this: