Skip to content

Commit 0676450

Browse files
committed
Check that @use annotations only appear for method and class parameters
1 parent 62b2f1c commit 0676450

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
@@ -495,6 +495,7 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
495495
case _ =>
496496
traverseChildren(tree)
497497
postProcess(tree)
498+
checkProperUse(tree)
498499
end traverse
499500

500501
def postProcess(tree: Tree)(using Context): Unit = tree match
@@ -637,6 +638,16 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
637638
case _ =>
638639
case _ =>
639640
end postProcess
641+
642+
def checkProperUse(tree: Tree)(using Context): Unit = tree match
643+
case tree: MemberDef =>
644+
def useAllowed(sym: Symbol) =
645+
(sym.is(Param) || sym.is(ParamAccessor)) && !sym.owner.isAnonymousFunction
646+
for ann <- tree.symbol.annotations do
647+
if ann.symbol == defn.UseAnnot && !useAllowed(tree.symbol) then
648+
report.error(i"Only parameters of methods can have @use annotations", tree.srcPos)
649+
case _ =>
650+
end checkProperUse
640651
end setupTraverser
641652

642653
/** 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)