Skip to content

Commit 57e2ef0

Browse files
committed
Print parens for single method argument only if a direct tuple type
A type alias of a tuple type should be printed without parenthesis.
1 parent 5e83606 commit 57e2ef0

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

+14-8
Original file line numberDiff line numberDiff line change
@@ -1781,18 +1781,24 @@ class Definitions {
17811781
|| (sym eq Any_typeTest)
17821782
|| (sym eq Any_typeCast)
17831783

1784-
/** Is this type a `TupleN` type?
1784+
/** Is `tp` a `TupleN` type?
1785+
*
1786+
* @return true if the type of `tp` is `TupleN[T1, T2, ..., Tn]`
1787+
*/
1788+
def isDirectTupleNType(tp: Type)(using Context): Boolean =
1789+
val arity = tp.argInfos.length
1790+
arity <= MaxTupleArity && {
1791+
val tupletp = TupleType(arity)
1792+
tupletp != null && tp.isRef(tupletp.symbol)
1793+
}
1794+
1795+
/** Is `tp` (an alias of) a `TupleN` type?
17851796
*
17861797
* @return true if the dealiased type of `tp` is `TupleN[T1, T2, ..., Tn]`
17871798
*/
1788-
def isTupleNType(tp: Type)(using Context): Boolean = {
1799+
def isTupleNType(tp: Type)(using Context): Boolean =
17891800
val tp1 = tp.dealias
1790-
val arity = tp1.argInfos.length
1791-
arity <= MaxTupleArity && {
1792-
val tupletp = TupleType(arity)
1793-
tupletp != null && tp1.isRef(tupletp.symbol)
1794-
}
1795-
}
1801+
isDirectTupleNType(tp1)
17961802

17971803
def tupleType(elems: List[Type]): Type = {
17981804
val arity = elems.length

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
165165
changePrec(GlobalPrec) {
166166
val argStr: Text =
167167
if args.length == 2
168-
&& !defn.isTupleNType(args.head)
168+
&& !defn.isDirectTupleNType(args.head)
169169
&& !isGiven
170170
then
171171
atPrec(InfixPrec) { argText(args.head) }

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala

+44
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,50 @@ class CompletionSuite extends BaseCompletionSuite:
147147
"XtensionMethod(a: Int): XtensionMethod"
148148
)
149149

150+
@Test def tupleDirect =
151+
check(
152+
"""
153+
|trait Foo {
154+
| def setup: List[(String, String)]
155+
|}
156+
|object Foo {
157+
| val foo: Foo = ???
158+
| foo.setup.exist@@
159+
|}""".stripMargin,
160+
"""|exists(p: ((String, String)) => Boolean): Boolean
161+
|""".stripMargin
162+
)
163+
164+
@Test def tupleAlias =
165+
check(
166+
"""
167+
|trait Foo {
168+
| def setup: List[Foo.TupleAliasResult]
169+
|}
170+
|object Foo {
171+
| type TupleAliasResult = (String, String)
172+
| val foo: Foo = ???
173+
| foo.setup.exist@@
174+
|}""".stripMargin,
175+
"""|exists(p: TupleAliasResult => Boolean): Boolean
176+
|""".stripMargin
177+
)
178+
179+
@Test def listAlias =
180+
check(
181+
"""
182+
|trait Foo {
183+
| def setup: List[Foo.ListAliasResult]
184+
|}
185+
|object Foo {
186+
| type ListAliasResult = List[String]
187+
| val foo: Foo = ???
188+
| foo.setup.exist@@
189+
|}""".stripMargin,
190+
"""|exists(p: ListAliasResult => Boolean): Boolean
191+
|""".stripMargin
192+
)
193+
150194
@Ignore("This test should be handled by compiler fuzzy search")
151195
@Test def fuzzy =
152196
check(

0 commit comments

Comments
 (0)