Skip to content

Experiment with Capless-like Scheme for Capture Checking #23291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve printing @use and @consume
  • Loading branch information
Linyxus committed Jun 7, 2025
commit 0dd57f21de060a4efe9b368356cf7c63a21db3ac
12 changes: 9 additions & 3 deletions compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ class PlainPrinter(_ctx: Context) extends Printer {

protected def argText(arg: Type, isErased: Boolean = false): Text =
keywordText("erased ").provided(isErased)
~ specialAnnotText(defn.UseAnnot, arg)
~ specialAnnotText(defn.ConsumeAnnot, arg)
~ homogenizeArg(arg).match
case arg: TypeBounds => "?" ~ toText(arg)
case arg => toText(arg)
Expand Down Expand Up @@ -376,13 +378,17 @@ class PlainPrinter(_ctx: Context) extends Printer {
try "(" ~ toTextRef(tp) ~ " : " ~ toTextGlobal(tp.underlying) ~ ")"
finally elideCapabilityCaps = saved

/** Print the annotation that are meant to be on the parameter symbol but was moved
* to parameter types. Examples are `@use` and `@consume`. */
protected def specialAnnotText(sym: ClassSymbol, tp: Type): Text =
Str(s"@${sym.name} ").provided(tp.hasAnnotation(sym))

protected def paramsText(lam: LambdaType): Text = {
def paramText(ref: ParamRef) =
val erased = ref.underlying.hasAnnotation(defn.ErasedParamAnnot)
def maybeAnnotsText(sym: ClassSymbol): Text =
Str(s"@${sym.name} ").provided(ref.underlying.hasAnnotation(sym))
keywordText("erased ").provided(erased)
~ maybeAnnotsText(defn.UseAnnot) ~ maybeAnnotsText(defn.ConsumeAnnot)
~ specialAnnotText(defn.UseAnnot, ref.underlying)
~ specialAnnotText(defn.ConsumeAnnot, ref.underlying)
~ ParamRefNameString(ref) ~ hashStr(lam) ~ toTextRHS(ref.underlying, isParameter = true)
Text(lam.paramRefs.map(paramText), ", ")
}
Expand Down
10 changes: 5 additions & 5 deletions tests/neg-custom-args/captures/leak-problem-unboxed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def useBoxedAsync1(@use x: Box[Async^]): Unit = x.get.read() // ok
def test(): Unit =

val f: Box[Async^] => Unit = (x: Box[Async^]) => useBoxedAsync(x) // error
val _: Box[Async^] => Unit = useBoxedAsync(_) // error
val _: Box[Async^] => Unit = useBoxedAsync // error
val _ = useBoxedAsync(_) // error
val _ = useBoxedAsync // error
val t1: Box[Async^] => Unit = useBoxedAsync(_) // error
val t2: Box[Async^] => Unit = useBoxedAsync // error
val t3 = useBoxedAsync(_) // was error, now ok
val t4 = useBoxedAsync // was error, now ok

def boom(x: Async^): () ->{f} Unit =
() => f(Box(x))

val leaked = usingAsync[() ->{f} Unit](boom)

leaked() // scope violation
leaked() // scope violation
Loading