-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing cache invalidations for opaque types
Two problems: - Base types for applied opaque types were cached - Denotations for opaque types were cached This allowed knowledge about opaque aliases to leak from companion objects to the outside world.
- Loading branch information
Showing
4 changed files
with
69 additions
and
4 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,15 @@ | ||
object OpaqueEscape{ | ||
opaque type Wrapped = Int | ||
abstract class EscaperBase { | ||
def unwrap(i:Wrapped):Int | ||
def wrap(i:Int):Wrapped | ||
} | ||
class Escaper extends EscaperBase{ | ||
override def unwrap(i:Int):Int = i // error overriding method unwrap | ||
override def wrap(i:Int):Int = i // error overriding method wrap | ||
} | ||
|
||
val e = new Escaper:EscaperBase | ||
val w:Wrapped = e.wrap(1) | ||
val u:Int = e.unwrap(w) | ||
} |
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,48 @@ | ||
object ia { | ||
|
||
import java.util.Arrays | ||
|
||
opaque type IArray[A1] = Array[A1] | ||
|
||
object IArray { | ||
def initialize[A](body: => Array[A]): IArray[A] = body | ||
def size[A](ia: IArray[A]): Int = ia.length | ||
def get[A](ia: IArray[A], i: Int): A = ia(i) | ||
|
||
// return a sorted copy of the array | ||
def sorted[A <: AnyRef : math.Ordering](ia: IArray[A]): IArray[A] = { | ||
val arr = Arrays.copyOf(ia, ia.length) | ||
scala.util.Sorting.quickSort(arr) | ||
arr | ||
} | ||
|
||
// use a standard java method to search a sorted IArray. | ||
// (note that this doesn't mutate the array). | ||
def binarySearch(ia: IArray[Long], elem: Long): Int = | ||
Arrays.binarySearch(ia, elem) | ||
} | ||
|
||
// same as IArray.binarySearch but implemented by-hand. | ||
// | ||
// given a sorted IArray, returns index of `elem`, | ||
// or a negative value if not found. | ||
def binaryIndexOf(ia: IArray[Long], elem: Long): Int = { | ||
var lower: Int = 0 | ||
var upper: Int = IArray.size(ia) | ||
while (lower <= upper) { | ||
val middle = (lower + upper) >>> 1 | ||
val n = IArray.get(ia, middle) | ||
|
||
if (n == elem) return middle | ||
else if (n < elem) lower = middle + 1 | ||
else upper = middle - 1 | ||
} | ||
-lower - 1 | ||
} | ||
|
||
def xs: IArray[Long] = ??? | ||
|
||
xs.length // error: not a member | ||
xs.apply(2) // error: not a member | ||
xs(1) = 0 // error: not a member | ||
} |