From 4fd0e18e6441e6c94095e936c4a9456bb18464cd Mon Sep 17 00:00:00 2001 From: Joel Wilsson Date: Thu, 29 Aug 2024 18:31:17 +0200 Subject: [PATCH 1/2] Print parens for single method argument only if a direct tuple type A type alias of a tuple type should be printed without parenthesis. --- .../dotty/tools/dotc/core/Definitions.scala | 28 ++++++++---- .../tools/dotc/printing/RefinedPrinter.scala | 2 +- .../pc/tests/completion/CompletionSuite.scala | 44 +++++++++++++++++++ 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Definitions.scala b/compiler/src/dotty/tools/dotc/core/Definitions.scala index 26ddc47c6b89..f084e98e8806 100644 --- a/compiler/src/dotty/tools/dotc/core/Definitions.scala +++ b/compiler/src/dotty/tools/dotc/core/Definitions.scala @@ -1722,18 +1722,30 @@ class Definitions { def isPolymorphicAfterErasure(sym: Symbol): Boolean = (sym eq Any_isInstanceOf) || (sym eq Any_asInstanceOf) || (sym eq Object_synchronized) - /** Is this type a `TupleN` type? + def isTypeTestOrCast(sym: Symbol): Boolean = + (sym eq Any_isInstanceOf) + || (sym eq Any_asInstanceOf) + || (sym eq Any_typeTest) + || (sym eq Any_typeCast) + + /** 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 diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index 4fdfd7cb3e60..286d68319c4e 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -159,7 +159,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) } diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala index 27aa3f505b80..862257d7d1eb 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala @@ -146,6 +146,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( From 665c1f847c8a043e19b7d7c78efb45a7b10321d0 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Tue, 22 Apr 2025 19:28:38 +0200 Subject: [PATCH 2/2] Print parens for single method argument only if a direct tuple type A type alias of a tuple type should be printed without parenthesis. [Cherry-picked 57e2ef022d53d5725b9becc44b6ce9d6dcd4a353][modified]