Skip to content

Commit a87418b

Browse files
committed
Treat & and | as *left*-associative
While the operators should be associative, they aren't actually guaranteed to be, so correct pretty-printing will help users. * PrinterTests: Add first test for TypeTree printing. * PrinterTests: disable colors, following ErrorMessagesTest.scala.
1 parent 3257bfb commit a87418b

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ class PlainPrinter(_ctx: Context) extends Printer {
161161
}
162162
finally openRecs = openRecs.tail
163163
case AndType(tp1, tp2) =>
164-
changePrec(AndTypePrec) { toText(tp1) ~ " & " ~ toText(tp2) }
164+
changePrec(AndTypePrec) { toText(tp1) ~ " & " ~ atPrec(AndTypePrec + 1) { toText(tp2) } }
165165
case OrType(tp1, tp2) =>
166-
changePrec(OrTypePrec) { toText(tp1) ~ " | " ~ toText(tp2) }
166+
changePrec(OrTypePrec) { toText(tp1) ~ " | " ~ atPrec(OrTypePrec + 1) { toText(tp2) } }
167167
case tp: ErrorType =>
168168
s"<error ${tp.msg.msg}>"
169169
case tp: WildcardType =>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
375375
case SingletonTypeTree(ref) =>
376376
toTextLocal(ref) ~ "." ~ keywordStr("type")
377377
case AndTypeTree(l, r) =>
378-
changePrec(AndTypePrec) { toText(l) ~ " & " ~ toText(r) }
378+
changePrec(AndTypePrec) { toText(l) ~ " & " ~ atPrec(AndTypePrec + 1) { toText(r) } }
379379
case OrTypeTree(l, r) =>
380-
changePrec(OrTypePrec) { toText(l) ~ " | " ~ toText(r) }
380+
changePrec(OrTypePrec) { toText(l) ~ " | " ~ atPrec(OrTypePrec + 1) { toText(r) } }
381381
case RefinedTypeTree(tpt, refines) =>
382382
toTextLocal(tpt) ~ " " ~ blockText(refines)
383383
case AppliedTypeTree(tpt, args) =>

compiler/test/dotty/tools/dotc/printing/PrinterTests.scala

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package dotty.tools.dotc.printing
22

33
import dotty.tools.DottyTest
4-
import dotty.tools.dotc.ast.tpd
4+
import dotty.tools.dotc.ast.{Trees,tpd}
55
import dotty.tools.dotc.core.Names._
66
import dotty.tools.dotc.core.Symbols._
7+
import dotty.tools.dotc.core.Decorators._
78
import org.junit.Assert.assertEquals
89
import org.junit.Test
910

1011
class PrinterTests extends DottyTest {
12+
13+
private def newContext = {
14+
initialCtx.setSetting(ctx.settings.color, "never")
15+
}
16+
ctx = newContext
17+
1118
import tpd._
1219

1320
@Test
@@ -24,4 +31,21 @@ class PrinterTests extends DottyTest {
2431
assertEquals("package object foo", bar.symbol.owner.show)
2532
}
2633
}
34+
35+
@Test
36+
def tpTreeInfixOps: Unit = {
37+
val source = """
38+
|class &&[T,U]
39+
|object Foo {
40+
| def bar1: Int && (Boolean | String) = ???
41+
| def bar2: Int & (Boolean | String) = ???
42+
|}
43+
""".stripMargin
44+
45+
checkCompile("frontend", source) { (tree, context) =>
46+
implicit val ctx = context
47+
val bar @ Trees.DefDef(_, _, _, _, _) = tree.find(tree => tree.symbol.name == termName("bar2")).get
48+
assertEquals("Int & (Boolean | String)", bar.tpt.show)
49+
}
50+
}
2751
}

tests/patmat/andtype-opentype-interaction.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
31: Pattern Match Exhaustivity: _: SealedTrait & OpenClass, _: Trait & OpenClass
44
35: Pattern Match Exhaustivity: _: SealedTrait & OpenTrait & OpenClass, _: Trait & OpenTrait & OpenClass
55
43: Pattern Match Exhaustivity: _: SealedTrait & OpenAbstractClass, _: Trait & OpenAbstractClass
6-
47: Pattern Match Exhaustivity: _: SealedTrait & OpenClass & OpenTrait & OpenClassSubclass, _: Trait & OpenClass & OpenTrait & OpenClassSubclass
6+
47: Pattern Match Exhaustivity: _: SealedTrait & OpenClass & (OpenTrait & OpenClassSubclass), _: Trait & OpenClass & (OpenTrait & OpenClassSubclass)

tests/patmat/andtype-refinedtype-interaction.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
32: Pattern Match Exhaustivity: _: Trait & C1{x: Int}
2-
48: Pattern Match Exhaustivity: _: Clazz & (C1 | C2 | T1){x: Int} & (C3 | C4 | T2){x: Int}, _: Trait & (C1 | C2 | T1){x: Int} & (C3 | C4 | T2){x: Int}
3-
54: Pattern Match Exhaustivity: _: Trait & (C1 | C2 | T1){x: Int} & C3{x: Int}
2+
48: Pattern Match Exhaustivity: _: Clazz & (C1 | (C2 | T1)){x: Int} & (C3 | (C4 | T2)){x: Int}, _: Trait & (C1 | (C2 | T1)){x: Int} & (C3 | (C4 | T2)){x: Int}
3+
54: Pattern Match Exhaustivity: _: Trait & (C1 | (C2 | T1)){x: Int} & C3{x: Int}
44
65: Pattern Match Exhaustivity: _: Trait & (C1 | C2){x: Int} & (C3 | SubC1){x: Int}
55
72: Pattern Match Exhaustivity: _: Trait & (T1 & (C1 | SubC2)){x: Int} & (T2 & (C2 | C3 | SubC1)){x: Int} &
66
SubSubC1{x: Int}

0 commit comments

Comments
 (0)