Skip to content

Commit 0dd57f2

Browse files
committed
Improve printing @use and @consume
1 parent 531d165 commit 0dd57f2

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ class PlainPrinter(_ctx: Context) extends Printer {
134134

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

381+
/** Print the annotation that are meant to be on the parameter symbol but was moved
382+
* to parameter types. Examples are `@use` and `@consume`. */
383+
protected def specialAnnotText(sym: ClassSymbol, tp: Type): Text =
384+
Str(s"@${sym.name} ").provided(tp.hasAnnotation(sym))
385+
379386
protected def paramsText(lam: LambdaType): Text = {
380387
def paramText(ref: ParamRef) =
381388
val erased = ref.underlying.hasAnnotation(defn.ErasedParamAnnot)
382-
def maybeAnnotsText(sym: ClassSymbol): Text =
383-
Str(s"@${sym.name} ").provided(ref.underlying.hasAnnotation(sym))
384389
keywordText("erased ").provided(erased)
385-
~ maybeAnnotsText(defn.UseAnnot) ~ maybeAnnotsText(defn.ConsumeAnnot)
390+
~ specialAnnotText(defn.UseAnnot, ref.underlying)
391+
~ specialAnnotText(defn.ConsumeAnnot, ref.underlying)
386392
~ ParamRefNameString(ref) ~ hashStr(lam) ~ toTextRHS(ref.underlying, isParameter = true)
387393
Text(lam.paramRefs.map(paramText), ", ")
388394
}

tests/neg-custom-args/captures/leak-problem-unboxed.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ def useBoxedAsync1(@use x: Box[Async^]): Unit = x.get.read() // ok
1919
def test(): Unit =
2020

2121
val f: Box[Async^] => Unit = (x: Box[Async^]) => useBoxedAsync(x) // error
22-
val _: Box[Async^] => Unit = useBoxedAsync(_) // error
23-
val _: Box[Async^] => Unit = useBoxedAsync // error
24-
val _ = useBoxedAsync(_) // error
25-
val _ = useBoxedAsync // error
22+
val t1: Box[Async^] => Unit = useBoxedAsync(_) // error
23+
val t2: Box[Async^] => Unit = useBoxedAsync // error
24+
val t3 = useBoxedAsync(_) // was error, now ok
25+
val t4 = useBoxedAsync // was error, now ok
2626

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

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

32-
leaked() // scope violation
32+
leaked() // scope violation

0 commit comments

Comments
 (0)