Skip to content

Commit f5229fd

Browse files
committed
Check that @use annotations only appear for method and class parameters
1 parent 735506b commit f5229fd

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
483483
case _ =>
484484
traverseChildren(tree)
485485
postProcess(tree)
486+
checkProperUse(tree)
486487
end traverse
487488

488489
def postProcess(tree: Tree)(using Context): Unit = tree match
@@ -625,6 +626,16 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
625626
case _ =>
626627
case _ =>
627628
end postProcess
629+
630+
def checkProperUse(tree: Tree)(using Context): Unit = tree match
631+
case tree: MemberDef =>
632+
def useAllowed(sym: Symbol) =
633+
(sym.is(Param) || sym.is(ParamAccessor)) && !sym.owner.isAnonymousFunction
634+
for ann <- tree.symbol.annotations do
635+
if ann.symbol == defn.UseAnnot && !useAllowed(tree.symbol) then
636+
report.error(i"Only parameters of methods can have @use annotations", tree.srcPos)
637+
case _ =>
638+
end checkProperUse
628639
end setupTraverser
629640

630641
/** Checks whether an abstract type could be impure. See also: [[needsVariable]]. */
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import caps.use
2+
class Test:
3+
@use def F = ??? // error
4+
@use val x = ??? // error
5+
@use type T // error
6+
def foo(@use c: Test): Unit = ??? // OK
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import caps.use
2+
class Test:
3+
val bar = (@use c: Test) => () // error

tests/neg-custom-args/captures/i21646.scala

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import language.experimental.namedTuples
2+
3+
class A:
4+
type T
5+
6+
class B extends A
7+
8+
val f: (x: A) => x.T = ???
9+
val g: (x: B) => x.T = f // OK
10+
val h: (x: A) => x.T = g // error

tests/pos-custom-args/captures/i21646.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ class Resource[T <: Capability](gen: T):
99

1010
@main def run =
1111
val myFile: File = ???
12-
val r = Resource(myFile) // error
12+
val r = Resource(myFile) // now ok, was error
1313
()

0 commit comments

Comments
 (0)