Skip to content

Commit 28fd23b

Browse files
authored
Merge pull request #257 from scala/backport-lts-3.3-21510
Backport "Print parens for single method argument only if a direct tuple type" to 3.3 LTS
2 parents f50e66a + 665c1f8 commit 28fd23b

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

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

+20-8
Original file line numberDiff line numberDiff line change
@@ -1722,18 +1722,30 @@ class Definitions {
17221722
def isPolymorphicAfterErasure(sym: Symbol): Boolean =
17231723
(sym eq Any_isInstanceOf) || (sym eq Any_asInstanceOf) || (sym eq Object_synchronized)
17241724

1725-
/** Is this type a `TupleN` type?
1725+
def isTypeTestOrCast(sym: Symbol): Boolean =
1726+
(sym eq Any_isInstanceOf)
1727+
|| (sym eq Any_asInstanceOf)
1728+
|| (sym eq Any_typeTest)
1729+
|| (sym eq Any_typeCast)
1730+
1731+
/** Is `tp` a `TupleN` type?
1732+
*
1733+
* @return true if the type of `tp` is `TupleN[T1, T2, ..., Tn]`
1734+
*/
1735+
def isDirectTupleNType(tp: Type)(using Context): Boolean =
1736+
val arity = tp.argInfos.length
1737+
arity <= MaxTupleArity && {
1738+
val tupletp = TupleType(arity)
1739+
tupletp != null && tp.isRef(tupletp.symbol)
1740+
}
1741+
1742+
/** Is `tp` (an alias of) a `TupleN` type?
17261743
*
17271744
* @return true if the dealiased type of `tp` is `TupleN[T1, T2, ..., Tn]`
17281745
*/
1729-
def isTupleNType(tp: Type)(using Context): Boolean = {
1746+
def isTupleNType(tp: Type)(using Context): Boolean =
17301747
val tp1 = tp.dealias
1731-
val arity = tp1.argInfos.length
1732-
arity <= MaxTupleArity && {
1733-
val tupletp = TupleType(arity)
1734-
tupletp != null && tp1.isRef(tupletp.symbol)
1735-
}
1736-
}
1748+
isDirectTupleNType(tp1)
17371749

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
159159
changePrec(GlobalPrec) {
160160
val argStr: Text =
161161
if args.length == 2
162-
&& !defn.isTupleNType(args.head)
162+
&& !defn.isDirectTupleNType(args.head)
163163
&& !isGiven
164164
then
165165
atPrec(InfixPrec) { argText(args.head) }

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

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

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

0 commit comments

Comments
 (0)