Skip to content

Commit 103ef96

Browse files
committed
Elide capabilities implied by Capability subtypes when printing
When printing a type `C^` where `C` extends `Capability`, don't show the `^`. This is overridden under -Yprint-debug.
1 parent c9eaa14 commit 103ef96

File tree

7 files changed

+19
-14
lines changed

7 files changed

+19
-14
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,16 @@ class PlainPrinter(_ctx: Context) extends Printer {
177177
* capturing function types.
178178
*/
179179
protected def toTextCapturing(parent: Type, refsText: Text, boxText: Text): Text =
180-
changePrec(InfixPrec):
181-
boxText ~ toTextLocal(parent) ~ "^"
182-
~ (refsText provided refsText != rootSetText)
180+
def coreText = boxText ~ toTextLocal(parent)
181+
if parent.derivesFrom(defn.Caps_Capability)
182+
&& refsText == impliedByCapabilitySetText
183+
&& !printDebug
184+
then coreText
185+
else changePrec(InfixPrec):
186+
coreText~ "^" ~ (refsText provided refsText != rootSetText)
183187

184188
final protected def rootSetText = Str("{cap}") // TODO Use disambiguation
189+
final protected def impliedByCapabilitySetText = Str("{cap}")
185190

186191
def toText(tp: Type): Text = controlled {
187192
homogenize(tp) match {

tests/neg-custom-args/captures/byname.check

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
-- Error: tests/neg-custom-args/captures/byname.scala:19:5 -------------------------------------------------------------
99
19 | h(g()) // error
1010
| ^^^
11-
| reference (cap2 : Cap^) is not included in the allowed capture set {cap1}
11+
| reference (cap2 : Cap) is not included in the allowed capture set {cap1}
1212
| of an enclosing function literal with expected type () ?->{cap1} I
1313
-- Error: tests/neg-custom-args/captures/byname.scala:22:12 ------------------------------------------------------------
1414
22 | h2(() => g())() // error
1515
| ^^^
16-
| reference (cap2 : Cap^) is not included in the allowed capture set {cap1}
16+
| reference (cap2 : Cap) is not included in the allowed capture set {cap1}
1717
| of an enclosing function literal with expected type () ->{cap1} I

tests/neg-custom-args/captures/cc-this5.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Error: tests/neg-custom-args/captures/cc-this5.scala:16:20 ----------------------------------------------------------
22
16 | def f = println(c) // error
33
| ^
4-
| reference (c : Cap^) is not included in the allowed capture set {}
4+
| reference (c : Cap) is not included in the allowed capture set {}
55
| of the enclosing class A
66
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/cc-this5.scala:21:15 -------------------------------------
77
21 | val x: A = this // error

tests/neg-custom-args/captures/effect-swaps.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
-- Error: tests/neg-custom-args/captures/effect-swaps.scala:66:15 ------------------------------------------------------
2626
66 | Result.make: // error: local reference leaks
2727
| ^^^^^^^^^^^
28-
|local reference contextual$9 from (using contextual$9: boundary.Label[Result[box Future[box T^?]^{fr, contextual$9}, box E^?]]^):
28+
|local reference contextual$9 from (using contextual$9: boundary.Label[Result[box Future[box T^?]^{fr, contextual$9}, box E^?]]):
2929
| box Future[box T^?]^{fr, contextual$9} leaks into outer capture set of type parameter T of method make in object Result
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/explain-under-approx.scala:12:10 -------------------------
22
12 | col.add(Future(() => 25)) // error
33
| ^^^^^^^^^^^^^^^^
4-
| Found: Future[Int]{val a: (async : Async^)}^{async}
4+
| Found: Future[Int]{val a: (async : Async)}^{async}
55
| Required: Future[Int]^{col.futs*}
66
|
77
| longer explanation available when compiling with `-explain`
88
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/explain-under-approx.scala:15:11 -------------------------
99
15 | col1.add(Future(() => 25)) // error
1010
| ^^^^^^^^^^^^^^^^
11-
| Found: Future[Int]{val a: (async : Async^)}^{async}
11+
| Found: Future[Int]{val a: (async : Async)}^{async}
1212
| Required: Future[Int]^{col1.futs*}
1313
|
1414
| longer explanation available when compiling with `-explain`
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/extending-cap-classes.scala:7:15 -------------------------
22
7 | val x2: C1 = new C2 // error
33
| ^^^^^^
4-
| Found: C2^
4+
| Found: C2
55
| Required: C1
66
|
77
| longer explanation available when compiling with `-explain`
88
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/extending-cap-classes.scala:8:15 -------------------------
99
8 | val x3: C1 = new C3 // error
1010
| ^^^^^^
11-
| Found: C3^
11+
| Found: C3
1212
| Required: C1
1313
|
1414
| longer explanation available when compiling with `-explain`
1515
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/extending-cap-classes.scala:13:15 ------------------------
1616
13 | val z2: C1 = y2 // error
1717
| ^^
18-
| Found: (y2 : C2^)
18+
| Found: (y2 : C2)
1919
| Required: C1
2020
|
2121
| longer explanation available when compiling with `-explain`

tests/neg-custom-args/captures/i21614.check

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i21614.scala:12:33 ---------------------------------------
22
12 | files.map((f: F) => new Logger(f)) // error, Q: can we make this pass (see #19076)?
33
| ^
4-
| Found: (f : F^)
5-
| Required: File^
4+
| Found: (f : F)
5+
| Required: File
66
|
77
| longer explanation available when compiling with `-explain`
88
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i21614.scala:15:12 ---------------------------------------

0 commit comments

Comments
 (0)