Skip to content

Commit d16c0f8

Browse files
committed
Change printing to new syntax
1 parent d7b3f09 commit d16c0f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+305
-306
lines changed

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

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,14 @@ class PlainPrinter(_ctx: Context) extends Printer {
149149
+ defn.ObjectClass
150150
+ defn.FromJavaObjectSymbol
151151

152-
def toText(cs: CaptureSet): Text =
153-
"{" ~ Text(cs.elems.toList.map(toTextCaptureRef), ", ") ~ "}"
152+
def toTextCaptureSet(cs: CaptureSet): Text =
153+
if printDebug && !cs.isConst then cs.toString
154+
else if ctx.settings.YccDebug.value then cs.show
155+
else if !cs.isConst && cs.elems.isEmpty then "?"
156+
else "{" ~ Text(cs.elems.toList.map(toTextCaptureRef), ", ") ~ "}"
157+
158+
protected def toTextCapturing(parent: Type, refsText: Text, boxText: Text): Text =
159+
changePrec(InfixPrec)(boxText ~ toTextLocal(parent) ~ "^" ~ refsText)
154160

155161
def toText(tp: Type): Text = controlled {
156162
homogenize(tp) match {
@@ -207,20 +213,8 @@ class PlainPrinter(_ctx: Context) extends Printer {
207213
(" <: " ~ toText(bound) provided !bound.isAny)
208214
}.close
209215
case tp @ EventuallyCapturingType(parent, refs) =>
210-
def box =
211-
Str("box ") provided tp.isBoxed //&& ctx.settings.YccDebug.value
212-
def printRegular(refsText: Text) =
213-
changePrec(GlobalPrec)(box ~ refsText ~ " " ~ toText(parent))
214-
if printDebug && !refs.isConst then
215-
printRegular(refs.toString)
216-
else if ctx.settings.YccDebug.value then
217-
printRegular(refs.show)
218-
else if !refs.isConst && refs.elems.isEmpty then
219-
printRegular("?")
220-
else if Config.printCaptureSetsAsPrefix then
221-
printRegular(toText(refs))
222-
else
223-
changePrec(InfixPrec)(box ~ toText(parent) ~ " @retains(" ~ toText(refs.elems.toList, ",") ~ ")")
216+
val boxText: Text = Str("box ") provided tp.isBoxed //&& ctx.settings.YccDebug.value
217+
toTextCapturing(parent, toTextCaptureSet(refs), boxText)
224218
case tp: PreviousErrorType if ctx.settings.XprintTypes.value =>
225219
"<error>" // do not print previously reported error message because they may try to print this error type again recuresevely
226220
case tp: ErrorType =>
@@ -241,14 +235,13 @@ class PlainPrinter(_ctx: Context) extends Printer {
241235
~ (Str(": ") provided !tp.resultType.isInstanceOf[MethodOrPoly])
242236
~ toText(tp.resultType)
243237
}
244-
case ExprType(ct @ EventuallyCapturingType(parent, refs))
245-
if ct.annot.symbol == defn.RetainsByNameAnnot =>
246-
if refs.isUniversal then changePrec(GlobalPrec) { "=> " ~ toText(parent) }
247-
else toText(CapturingType(ExprType(parent), refs))
248238
case ExprType(restp) =>
249-
changePrec(GlobalPrec) {
250-
(if Feature.pureFunsEnabled then "-> " else "=> ") ~ toText(restp)
251-
}
239+
def arrowText: Text = restp match
240+
case ct @ EventuallyCapturingType(parent, refs) if ct.annot.symbol == defn.RetainsByNameAnnot =>
241+
if refs.isUniversal then Str("=>") else Str("->") ~ toTextCaptureSet(refs)
242+
case _ =>
243+
if Feature.pureFunsEnabled then "->" else "=>"
244+
changePrec(GlobalPrec)(arrowText ~ " " ~ toText(restp))
252245
case tp: HKTypeLambda =>
253246
changePrec(GlobalPrec) {
254247
"[" ~ paramsText(tp) ~ "]" ~ lambdaHash(tp) ~ Str(" =>> ") ~ toTextGlobal(tp.resultType)

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

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -144,44 +144,49 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
144144
private def arrow(isGiven: Boolean, isPure: Boolean): String =
145145
(if isGiven then "?" else "") + (if isPure then "->" else "=>")
146146

147-
override def toText(tp: Type): Text = controlled {
148-
def toTextTuple(args: List[Type]): Text =
149-
"(" ~ argsText(args) ~ ")"
147+
private def toTextFunction(tp: AppliedType, refs: Text = Str("")): Text =
148+
val AppliedType(tycon, args) = (tp: @unchecked)
149+
val tsym = tycon.typeSymbol
150+
val isGiven = tsym.name.isContextFunction
151+
val isPure = Feature.pureFunsEnabled && !tsym.name.isImpureFunction
152+
changePrec(GlobalPrec) {
153+
val argStr: Text =
154+
if args.length == 2
155+
&& !defn.isTupleNType(args.head)
156+
&& !isGiven
157+
then
158+
atPrec(InfixPrec) { argText(args.head) }
159+
else
160+
"("
161+
~ argsText(args.init)
162+
~ ")"
163+
argStr ~ " " ~ arrow(isGiven, isPure) ~ refs ~ " " ~ argText(args.last)
164+
}
150165

151-
def toTextFunction(args: List[Type], isGiven: Boolean, isPure: Boolean): Text =
166+
private def toTextMethodAsFunction(info: Type, isPure: Boolean, refs: Text = Str("")): Text = info match
167+
case info: MethodType =>
152168
changePrec(GlobalPrec) {
153-
val argStr: Text =
154-
if args.length == 2
155-
&& !defn.isTupleNType(args.head)
156-
&& !isGiven
157-
then
158-
atPrec(InfixPrec) { argText(args.head) }
159-
else
160-
"("
161-
~ argsText(args.init)
162-
~ ")"
163-
argStr ~ " " ~ arrow(isGiven, isPure) ~ " " ~ argText(args.last)
169+
"("
170+
~ paramsText(info)
171+
~ ") "
172+
~ arrow(info.isImplicitMethod, isPure)
173+
~ refs
174+
~ " "
175+
~ toTextMethodAsFunction(info.resultType, isPure)
176+
}
177+
case info: PolyType =>
178+
changePrec(GlobalPrec) {
179+
"["
180+
~ paramsText(info)
181+
~ "] => "
182+
~ toTextMethodAsFunction(info.resultType, isPure)
164183
}
184+
case _ =>
185+
toText(info)
165186

166-
def toTextMethodAsFunction(info: Type, isPure: Boolean): Text = info match
167-
case info: MethodType =>
168-
changePrec(GlobalPrec) {
169-
"("
170-
~ paramsText(info)
171-
~ ") "
172-
~ arrow(info.isImplicitMethod, isPure)
173-
~ " "
174-
~ toTextMethodAsFunction(info.resultType, isPure)
175-
}
176-
case info: PolyType =>
177-
changePrec(GlobalPrec) {
178-
"["
179-
~ paramsText(info)
180-
~ "] => "
181-
~ toTextMethodAsFunction(info.resultType, isPure)
182-
}
183-
case _ =>
184-
toText(info)
187+
override def toText(tp: Type): Text = controlled {
188+
def toTextTuple(args: List[Type]): Text =
189+
"(" ~ argsText(args) ~ ")"
185190

186191
def isInfixType(tp: Type): Boolean = tp match
187192
case AppliedType(tycon, args) =>
@@ -223,9 +228,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
223228
val tsym = tycon.typeSymbol
224229
if tycon.isRepeatedParam then toTextLocal(args.head) ~ "*"
225230
else if tp.isConvertibleParam then "into " ~ toText(args.head)
226-
else if defn.isFunctionSymbol(tsym) then
227-
toTextFunction(args, tsym.name.isContextFunction,
228-
isPure = Feature.pureFunsEnabled && !tsym.name.isImpureFunction)
231+
else if defn.isFunctionSymbol(tsym) then toTextFunction(tp)
229232
else if isInfixType(tp) then
230233
val l :: r :: Nil = args: @unchecked
231234
val opName = tyconName(tycon)
@@ -618,7 +621,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
618621
def toTextAnnot =
619622
toTextLocal(arg) ~~ annotText(annot.symbol.enclosingClass, annot)
620623
def toTextRetainsAnnot =
621-
try changePrec(GlobalPrec)(toText(captureSet) ~ " " ~ toText(arg))
624+
try changePrec(GlobalPrec)(toTextCaptureSet(captureSet) ~ " " ~ toText(arg))
622625
catch case ex: IllegalCaptureRef => toTextAnnot
623626
if annot.symbol.maybeOwner == defn.RetainsAnnot
624627
&& Feature.ccEnabled && Config.printCaptureSetsAsPrefix && !printDebug
@@ -737,12 +740,21 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
737740
case ImpureByNameTypeTree(bntpt) =>
738741
"=> " ~ toTextLocal(bntpt)
739742
case _ =>
740-
changePrec(GlobalPrec)("{" ~ Text(refs.map(toText), ", ") ~ "} " ~ toText(parent))
743+
toText(parent) ~ "^"
744+
~ changePrec(GlobalPrec)("{" ~ Text(refs.map(toText), ", ") ~ "}")
741745
case _ =>
742746
tree.fallbackToText(this)
743747
}
744748
}
745749

750+
override protected def toTextCapturing(tp: Type, refsText: Text, boxText: Text): Text = tp match
751+
case tp: AppliedType if defn.isFunctionSymbol(tp.typeSymbol) && !printDebug =>
752+
boxText ~ toTextFunction(tp, refsText)
753+
case tp: RefinedType if defn.isFunctionOrPolyType(tp) && !printDebug =>
754+
boxText ~ toTextMethodAsFunction(tp.refinedInfo, isPure = !tp.typeSymbol.name.isImpureFunction, refsText)
755+
case _ =>
756+
super.toTextCapturing(tp, refsText, boxText)
757+
746758
override def toText[T <: Untyped](tree: Tree[T]): Text = controlled {
747759
import untpd._
748760

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
-- Warning: tests/neg-custom-args/captures/byname.scala:17:18 ----------------------------------------------------------
2-
17 | def h(x: {cap1} -> I) = x // warning
3-
| ^
4-
| Style: by-name `->` should immediately follow closing `}` of capture set
5-
| to avoid confusion with function type.
6-
| That is, `{c}-> T` instead of `{c} -> T`.
71
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/byname.scala:10:6 ----------------------------------------
82
10 | h(f2()) // error
93
| ^^^^
10-
| Found: {cap1} (x$0: Int) -> Int
11-
| Required: {cap2} (x$0: Int) -> Int
4+
| Found: (x$0: Int) ->{cap1} Int
5+
| Required: (x$0: Int) ->{cap2} Int
126
|
137
| longer explanation available when compiling with `-explain`
148
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/byname.scala:19:5 ----------------------------------------
159
19 | h(g()) // error
1610
| ^^^
17-
| Found: {cap2} () ?-> I
18-
| Required: {cap1} () ?-> I
11+
| Found: () ?->{cap2} I
12+
| Required: () ?->{cap1} I
1913
|
2014
| longer explanation available when compiling with `-explain`

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ def test(cap1: Cap, cap2: Cap) =
55
def g(x: Int) = if cap2 == cap2 then 1 else x
66
def g2(x: Int) = if cap1 == cap1 then 1 else x
77
def f2() = if cap1 == cap1 then g2 else g2
8-
def h(ff: => {cap2} Int -> Int) = ff
8+
def h(ff: => Int ->{cap2} Int) = ff
99
h(f()) // ok
1010
h(f2()) // error
1111

1212
class I
1313

14-
def test2(cap1: Cap, cap2: Cap): {cap1} I =
14+
def test2(cap1: Cap, cap2: Cap): I^{cap1} =
1515
def f() = if cap1 == cap1 then I() else I()
1616
def g() = if cap2 == cap2 then I() else I()
17-
def h(x: {cap1} -> I) = x // warning
17+
def h(x: ->{cap1} I) = x // ok
1818
h(f()) // OK
1919
h(g()) // error
2020

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/capt1.scala:4:2 ------------------------------------------
22
4 | () => if x == null then y else y // error
33
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4-
| Found: {x} () -> ? C
4+
| Found: () ->{x} C^?
55
| Required: () -> C
66
|
77
| longer explanation available when compiling with `-explain`
88
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/capt1.scala:7:2 ------------------------------------------
99
7 | () => if x == null then y else y // error
1010
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11-
| Found: {x} () -> ? C
11+
| Found: () ->{x} C^?
1212
| Required: Matchable
1313
|
1414
| longer explanation available when compiling with `-explain`
1515
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/capt1.scala:14:2 -----------------------------------------
1616
14 | def f(y: Int) = if x == null then y else y // error
1717
| ^
18-
| Found: {x} Int -> Int
18+
| Found: Int ->{x} Int
1919
| Required: Matchable
2020
15 | f
2121
|
2222
| longer explanation available when compiling with `-explain`
2323
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/capt1.scala:21:2 -----------------------------------------
2424
21 | class F(y: Int) extends A: // error
2525
| ^
26-
| Found: {x} A
26+
| Found: A^{x}
2727
| Required: A
2828
22 | def m() = if x == null then y else y
2929
23 | F(22)
@@ -32,22 +32,22 @@
3232
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/capt1.scala:26:2 -----------------------------------------
3333
26 | new A: // error
3434
| ^
35-
| Found: {x} A
35+
| Found: A^{x}
3636
| Required: A
3737
27 | def m() = if x == null then y else y
3838
|
3939
| longer explanation available when compiling with `-explain`
4040
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/capt1.scala:32:24 ----------------------------------------
4141
32 | val z2 = h[() -> Cap](() => x) // error
4242
| ^^^^^^^
43-
| Found: {x} () -> Cap
44-
| Required: () -> box {*} C
43+
| Found: () ->{x} Cap
44+
| Required: () -> box C^{*}
4545
|
4646
| longer explanation available when compiling with `-explain`
4747
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/capt1.scala:33:5 -----------------------------------------
4848
33 | (() => C()) // error
4949
| ^^^^^^^^^
50-
| Found: ? () -> Cap
51-
| Required: () -> box {*} C
50+
| Found: () ->? Cap
51+
| Required: () -> box C^{*}
5252
|
5353
| longer explanation available when compiling with `-explain`

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/cc-this.scala:8:15 ---------------------------------------
22
8 | val y: C = this // error
33
| ^^^^
4-
| Found: (C.this : {C.this.x} C)
4+
| Found: (C.this : C^{C.this.x})
55
| Required: C
66
|
77
| longer explanation available when compiling with `-explain`
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
-- [E058] Type Mismatch Error: tests/neg-custom-args/captures/cc-this3.scala:8:6 ---------------------------------------
22
8 |class B extends A: // error
33
| ^
4-
| illegal inheritance: self type {*} B of class B does not conform to self type {} A
4+
| illegal inheritance: self type B^{*} of class B does not conform to self type A^{}
55
| of parent class A
66
|
77
| longer explanation available when compiling with `-explain`
88
-- [E058] Type Mismatch Error: tests/neg-custom-args/captures/cc-this3.scala:11:6 --------------------------------------
99
11 |class C(val f: () => Int) extends A // error
1010
| ^
11-
| illegal inheritance: self type {C.this.f} C of class C does not conform to self type {} A
11+
| illegal inheritance: self type C^{C.this.f} of class C does not conform to self type A^{}
1212
| of parent class A
1313
|
1414
| longer explanation available when compiling with `-explain`

tests/neg-custom-args/captures/cc-this3.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ class A:
66
val x: A = this
77

88
class B extends A: // error
9-
this: {*} B =>
9+
this: B^{*} =>
1010

1111
class C(val f: () => Int) extends A // error
1212

1313
class A2
1414

1515
class B2 extends A2: // ok
16-
this: {*} B2 =>
16+
this: B2^{*} =>
1717

1818
class C2(val f: () => Int) extends A2 // ok

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
1 |open class C: // error
33
| ^
44
| class C needs an explicitly declared self type since its
5-
| inferred self type {} C
5+
| inferred self type C^{}
66
| is not visible in other compilation units that define subclasses.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/cc-this5.scala:21:15 -------------------------------------
66
21 | val x: A = this // error
77
| ^^^^
8-
| Found: (A.this : {c} A)
8+
| Found: (A.this : A^{c})
99
| Required: A
1010
|
1111
| longer explanation available when compiling with `-explain`
1212
-- [E058] Type Mismatch Error: tests/neg-custom-args/captures/cc-this5.scala:7:9 ---------------------------------------
1313
7 | object D extends C: // error
1414
| ^
15-
| illegal inheritance: self type {c} D.type of object D does not conform to self type {} C
15+
| illegal inheritance: self type D.type^{c} of object D does not conform to self type C^{}
1616
| of parent class C
1717
|
1818
| longer explanation available when compiling with `-explain`
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/class-contra.scala:12:39 ---------------------------------
2-
12 | def fun(x: K{val f: {a} T}) = x.setf(a) // error
2+
12 | def fun(x: K{val f: T^{a}}) = x.setf(a) // error
33
| ^
4-
| Found: (a : {x, y} T)
4+
| Found: (a : T^{x, y})
55
| Required: T
66
|
77
| longer explanation available when compiling with `-explain`
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

22
class C
3-
type Cap = {*} C
3+
type Cap = C^
44

5-
class K(val f: {*} T):
6-
def setf(x: {f} T) = ???
5+
class K(val f: T^):
6+
def setf(x: T^{f}) = ???
77

88
class T
99

1010
def test(x: Cap, y: Cap) =
11-
val a: {x, y} T = ???
12-
def fun(x: K{val f: {a} T}) = x.setf(a) // error
11+
val a: T^{x, y} = ???
12+
def fun(x: K{val f: T^{a}}) = x.setf(a) // error
1313
()

0 commit comments

Comments
 (0)