Skip to content

Commit 521c737

Browse files
committed
Disabllow type parameters in OnlyCapability annotations
1 parent f6ca933 commit 521c737

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

compiler/src/dotty/tools/dotc/cc/CaptureOps.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,12 @@ object OnlyCapability:
922922

923923
def unapply(tree: AnnotatedType)(using Context): Option[(Type, ClassSymbol)] = tree match
924924
case AnnotatedType(parent: Type, ann) if ann.hasSymbol(defn.OnlyCapabilityAnnot) =>
925-
ann.tree.tpe.argTypes.head.classSymbol match
926-
case cls: ClassSymbol => Some((parent, cls))
925+
val arg = ann.tree.tpe.argTypes.head
926+
arg match
927+
case tp: TypeRef if tp.symbol.isClass =>
928+
tp.symbol match
929+
case cls: ClassSymbol => Some((parent, cls))
930+
case _ => None
927931
case _ => None
928932
case _ => None
929933
end OnlyCapability
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Error: tests/neg-custom-args/captures/classified-tp.scala:10:17 -----------------------------------------------------
2+
10 | val c: AnyRef^{any.only[C]} = new AnyRef // error
3+
| ^^^^^^^^^^^
4+
| scala.caps.any.type @onlyCapability[C] is not a legal element of a capture set
5+
|
6+
| where: C is a type in method test with bounds <: B
7+
-- Error: tests/neg-custom-args/captures/classified-tp.scala:11:17 -----------------------------------------------------
8+
11 | val d: AnyRef^{any.only[D]} = new AnyRef // error
9+
| ^^^^^^^^^^^
10+
| scala.caps.any.type @onlyCapability[D] is not a legal element of a capture set
11+
|
12+
| where: D is a type in method test with bounds <: scala.caps.Classifier
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import language.experimental.captureChecking
2+
import caps.*
3+
4+
trait A extends Classifier, SharedCapability
5+
trait B extends A, Classifier
6+
7+
def test[C <: B, D <: Classifier] =
8+
val a: AnyRef^{any.only[A]} = new AnyRef
9+
val b: AnyRef^{any.only[B]} = new AnyRef
10+
val c: AnyRef^{any.only[C]} = new AnyRef // error
11+
val d: AnyRef^{any.only[D]} = new AnyRef // error

0 commit comments

Comments
 (0)