Skip to content

Commit f8dded5

Browse files
committed
Disallow inline methods and opaque types in same scope
1 parent 6be0b81 commit f8dded5

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,8 @@ class Namer { typer: Typer =>
803803

804804
private def addInlineInfo(sym: Symbol) = original match {
805805
case original: untpd.DefDef if sym.isInlineMethod =>
806+
if (sym.owner.isClass && sym.owner.seesOpaques)
807+
ctx.error(em"Implementation restriction: No inline methods allowed where opaque type aliases are in scope", sym.sourcePos)
806808
PrepareInlineable.registerInlineInfo(
807809
sym,
808810
implicit ctx => typedAheadExpr(original).asInstanceOf[tpd.DefDef].rhs

library/src-bootstrapped/scala/IArray.scala

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,28 @@ implicit object arrayOps {
1515
* @param n the index of the element to select
1616
* @return the element of the array at the given index
1717
*/
18-
inline def (arr: IArray[T]) apply[T] (n: Int): T = arr.asInstanceOf[Array[T]].apply(n)
18+
def (arr: IArray[Byte]) apply (n: Int): Byte = arr.asInstanceOf[Array[Byte]].apply(n)
19+
def (arr: IArray[Short]) apply (n: Int): Short = arr.asInstanceOf[Array[Short]].apply(n)
20+
def (arr: IArray[Char]) apply (n: Int): Char = arr.asInstanceOf[Array[Char]].apply(n)
21+
def (arr: IArray[Int]) apply (n: Int): Int = arr.asInstanceOf[Array[Int]].apply(n)
22+
def (arr: IArray[Long]) apply (n: Int): Long = arr.asInstanceOf[Array[Long]].apply(n)
23+
def (arr: IArray[Float]) apply (n: Int): Float = arr.asInstanceOf[Array[Float]].apply(n)
24+
def (arr: IArray[Double]) apply (n: Int): Double = arr.asInstanceOf[Array[Double]].apply(n)
25+
def (arr: IArray[T]) apply[T <: Object] (n: Int): T = arr.asInstanceOf[Array[T]].apply(n)
26+
def (arr: IArray[T]) apply[T] (n: Int): T = arr.asInstanceOf[Array[T]].apply(n)
1927

2028
/** The number of elements in an immutable array
2129
* @param arr the immutable array
2230
*/
23-
inline def (arr: IArray[T]) length[T] : Int = arr.asInstanceOf[Array[T]].length
31+
def (arr: IArray[Byte]) length: Int = arr.asInstanceOf[Array[Byte]].length
32+
def (arr: IArray[Short]) length: Int = arr.asInstanceOf[Array[Short]].length
33+
def (arr: IArray[Char]) length: Int = arr.asInstanceOf[Array[Char]].length
34+
def (arr: IArray[Int]) length: Int = arr.asInstanceOf[Array[Int]].length
35+
def (arr: IArray[Long]) length: Int = arr.asInstanceOf[Array[Long]].length
36+
def (arr: IArray[Float]) length: Int = arr.asInstanceOf[Array[Float]].length
37+
def (arr: IArray[Double]) length: Int = arr.asInstanceOf[Array[Double]].length
38+
def (arr: IArray[Object]) length: Int = arr.asInstanceOf[Array[Object]].length
39+
def (arr: IArray[T]) length[T] : Int = arr.asInstanceOf[Array[T]].length
2440
}
2541

2642
object IArray {

tests/neg/inline3.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
object K0 {
2+
3+
type T = String
4+
5+
opaque type ProductInstances[F[_], T] = ErasedProductInstances[F[T]]
6+
7+
inline def summonAsArray[F[_], T]: Array[Any] = ??? // error: Implementation restriction: No inline methods allowed
8+
9+
inline def mkProductInstances[F[_], T]: ProductInstances[F, T] = // error: Implementation restriction: No inline methods allowed
10+
new ErasedProductInstances(summonAsArray[F, T]).asInstanceOf[ProductInstances[F, T]]
11+
12+
val x: T = ""
13+
14+
inline def foo(x: T): T = "foo".asInstanceOf[T] // error: Implementation restriction: No inline methods allowed
15+
16+
}
17+
18+
final class ErasedProductInstances[FT](is0: => Array[Any])
19+
20+
trait Monoid[A]
21+
case class ISB(i: Int)
22+
23+
object Test {
24+
//val K0 = new K0
25+
K0.foo(K0.x)
26+
K0.mkProductInstances[Monoid, ISB]
27+
28+
}

tests/run/opaque-immutable-array-xm.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ object Test extends App {
66
opaque type IArray[A1] = Array[A1]
77

88
implicit object IArray {
9-
inline def initialize[A](body: => Array[A]): IArray[A] = body
9+
def initialize[A](body: => Array[A]): IArray[A] = body
1010
def apply[A: ClassTag](xs: A*): IArray[A] = initialize(Array(xs: _*))
1111

1212
// These should be inline but that does not work currently. Try again

0 commit comments

Comments
 (0)