Skip to content

Commit 6c32869

Browse files
committed
Disallow type parameters in OnlyCapability annotations
1 parent f6ca933 commit 6c32869

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,11 @@ 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+
ann.tree.tpe.argTypes.head.dealias match
926+
case tp: TypeRef if tp.symbol.isClass =>
927+
tp.symbol match
928+
case cls: ClassSymbol => Some((parent, cls))
929+
case _ => None
927930
case _ => None
928931
case _ => None
929932
end OnlyCapability
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- Error: tests/neg-custom-args/captures/classified-tp.scala:11:17 -----------------------------------------------------
2+
11 | 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:12:17 -----------------------------------------------------
8+
12 | 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
13+
-- Error: tests/neg-custom-args/captures/classified-tp.scala:13:17 -----------------------------------------------------
14+
13 | val e: AnyRef^{any.only[E]} = new AnyRef // error
15+
| ^^^^^^^^^^^
16+
| scala.caps.any.only[E] is not well-formed since trait E is not a classifier class.
17+
| A classifier class is a class extending `caps.Capability` and directly extending `caps.Classifier`.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import language.experimental.captureChecking
2+
import caps.*
3+
4+
trait A extends Classifier, SharedCapability
5+
trait B extends A, Classifier
6+
trait E
7+
8+
def test[C <: B, D <: Classifier] =
9+
val a: AnyRef^{any.only[A]} = new AnyRef
10+
val b: AnyRef^{any.only[B]} = new AnyRef
11+
val c: AnyRef^{any.only[C]} = new AnyRef // error
12+
val d: AnyRef^{any.only[D]} = new AnyRef // error
13+
val e: AnyRef^{any.only[E]} = new AnyRef // error

0 commit comments

Comments
 (0)