Skip to content

Print parens for single method argument only if a direct tuple type #21510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1781,18 +1781,24 @@ class Definitions {
|| (sym eq Any_typeTest)
|| (sym eq Any_typeCast)

/** Is this type a `TupleN` type?
/** Is `tp` a `TupleN` type?
*
* @return true if the type of `tp` is `TupleN[T1, T2, ..., Tn]`
*/
def isDirectTupleNType(tp: Type)(using Context): Boolean =
val arity = tp.argInfos.length
arity <= MaxTupleArity && {
val tupletp = TupleType(arity)
tupletp != null && tp.isRef(tupletp.symbol)
}

/** Is `tp` (an alias of) a `TupleN` type?
*
* @return true if the dealiased type of `tp` is `TupleN[T1, T2, ..., Tn]`
*/
def isTupleNType(tp: Type)(using Context): Boolean = {
def isTupleNType(tp: Type)(using Context): Boolean =
val tp1 = tp.dealias
val arity = tp1.argInfos.length
arity <= MaxTupleArity && {
val tupletp = TupleType(arity)
tupletp != null && tp1.isRef(tupletp.symbol)
}
}
isDirectTupleNType(tp1)

def tupleType(elems: List[Type]): Type = {
val arity = elems.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
changePrec(GlobalPrec) {
val argStr: Text =
if args.length == 2
&& !defn.isTupleNType(args.head)
&& !defn.isDirectTupleNType(args.head)
&& !isGiven
then
atPrec(InfixPrec) { argText(args.head) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,50 @@ class CompletionSuite extends BaseCompletionSuite:
"XtensionMethod(a: Int): XtensionMethod"
)

@Test def tupleDirect =
check(
"""
|trait Foo {
| def setup: List[(String, String)]
|}
|object Foo {
| val foo: Foo = ???
| foo.setup.exist@@
|}""".stripMargin,
"""|exists(p: ((String, String)) => Boolean): Boolean
|""".stripMargin
)

@Test def tupleAlias =
check(
"""
|trait Foo {
| def setup: List[Foo.TupleAliasResult]
|}
|object Foo {
| type TupleAliasResult = (String, String)
| val foo: Foo = ???
| foo.setup.exist@@
|}""".stripMargin,
"""|exists(p: TupleAliasResult => Boolean): Boolean
|""".stripMargin
)

@Test def listAlias =
check(
"""
|trait Foo {
| def setup: List[Foo.ListAliasResult]
|}
|object Foo {
| type ListAliasResult = List[String]
| val foo: Foo = ???
| foo.setup.exist@@
|}""".stripMargin,
"""|exists(p: ListAliasResult => Boolean): Boolean
|""".stripMargin
)

@Ignore("This test should be handled by compiler fuzzy search")
@Test def fuzzy =
check(
Expand Down
Loading