Skip to content

Commit 10bb2e6

Browse files
Make named tuples an experimental feature again (#22045)
Resolves #22042 * Reverts most of the stabilization changes introduced in #21680 excluding bugfixes introduced when stabilizing the name tuples * Adapts #21823 and #21949 warnings to make them both syntax deprecations instead of ambiguous syntax. * Adds automatic rewrite to #21823 to replace `(foo = bar)` into `{foo = bar}`
2 parents bcacaee + 24c3e7f commit 10bb2e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+225
-125
lines changed

compiler/src/dotty/tools/dotc/config/Feature.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ object Feature:
3434
val pureFunctions = experimental("pureFunctions")
3535
val captureChecking = experimental("captureChecking")
3636
val into = experimental("into")
37+
val namedTuples = experimental("namedTuples")
3738
val modularity = experimental("modularity")
3839
val betterMatchTypeExtractors = experimental("betterMatchTypeExtractors")
3940
val quotedPatternsWithPolymorphicFunctions = experimental("quotedPatternsWithPolymorphicFunctions")
@@ -65,6 +66,7 @@ object Feature:
6566
(pureFunctions, "Enable pure functions for capture checking"),
6667
(captureChecking, "Enable experimental capture checking"),
6768
(into, "Allow into modifier on parameter types"),
69+
(namedTuples, "Allow named tuples"),
6870
(modularity, "Enable experimental modularity features"),
6971
(betterMatchTypeExtractors, "Enable better match type extractors"),
7072
(betterFors, "Enable improvements in `for` comprehensions")

compiler/src/dotty/tools/dotc/config/MigrationVersion.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ enum MigrationVersion(val warnFrom: SourceVersion, val errorFrom: SourceVersion)
2626
case WithOperator extends MigrationVersion(`3.4`, future)
2727
case FunctionUnderscore extends MigrationVersion(`3.4`, future)
2828
case NonNamedArgumentInJavaAnnotation extends MigrationVersion(`3.6`, `3.6`)
29-
case AmbiguousNamedTupleInfixApply extends MigrationVersion(`3.6`, never)
29+
case AmbiguousNamedTupleSyntax extends MigrationVersion(`3.6`, future)
3030
case ImportWildcard extends MigrationVersion(future, future)
3131
case ImportRename extends MigrationVersion(future, future)
3232
case ParameterEnclosedByParenthesis extends MigrationVersion(future, future)

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ object Parsers {
667667
else leading :: Nil
668668

669669
def maybeNamed(op: () => Tree): () => Tree = () =>
670-
if isIdent && in.lookahead.token == EQUALS && sourceVersion.isAtLeast(`3.6`) then
670+
if isIdent && in.lookahead.token == EQUALS && in.featureEnabled(Feature.namedTuples) then
671671
atSpan(in.offset):
672672
val name = ident()
673673
in.nextToken()
@@ -1149,8 +1149,8 @@ object Parsers {
11491149
if isType then infixOp
11501150
else infixOp.right match
11511151
case Tuple(args) if args.exists(_.isInstanceOf[NamedArg]) && !isNamedTupleOperator =>
1152-
report.errorOrMigrationWarning(AmbiguousNamedTupleInfixApply(), infixOp.right.srcPos, MigrationVersion.AmbiguousNamedTupleInfixApply)
1153-
if MigrationVersion.AmbiguousNamedTupleInfixApply.needsPatch then
1152+
report.errorOrMigrationWarning(DeprecatedInfixNamedArgumentSyntax(), infixOp.right.srcPos, MigrationVersion.AmbiguousNamedTupleSyntax)
1153+
if MigrationVersion.AmbiguousNamedTupleSyntax.needsPatch then
11541154
val asApply = cpy.Apply(infixOp)(Select(opInfo.operand, opInfo.operator.name), args)
11551155
patch(source, infixOp.span, asApply.show(using ctx.withoutColors))
11561156
asApply // allow to use pre-3.6 syntax in migration mode
@@ -2172,7 +2172,7 @@ object Parsers {
21722172

21732173
if namedOK && isIdent && in.lookahead.token == EQUALS then
21742174
commaSeparated(() => namedArgType())
2175-
else if tupleOK && isIdent && in.lookahead.isColon && sourceVersion.isAtLeast(`3.6`) then
2175+
else if tupleOK && isIdent && in.lookahead.isColon && in.featureEnabled(Feature.namedTuples) then
21762176
commaSeparated(() => namedElem())
21772177
else
21782178
commaSeparated(() => argType())

compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
216216
case FinalLocalDefID // errorNumber: 200
217217
case NonNamedArgumentInJavaAnnotationID // errorNumber: 201
218218
case QuotedTypeMissingID // errorNumber: 202
219-
case AmbiguousNamedTupleAssignmentID // errorNumber: 203
220-
case AmbiguousNamedTupleInfixApplyID // errorNumber: 204
219+
case DeprecatedAssignmentSyntaxID // errorNumber: 203
220+
case DeprecatedInfixNamedArgumentSyntaxID // errorNumber: 204
221221

222222
def errorNumber = ordinal - 1
223223

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,21 +3344,20 @@ final class QuotedTypeMissing(tpe: Type)(using Context) extends StagingMessage(Q
33443344

33453345
end QuotedTypeMissing
33463346

3347-
final class AmbiguousNamedTupleAssignment(key: Name, value: untpd.Tree)(using Context) extends SyntaxMsg(AmbiguousNamedTupleAssignmentID):
3347+
final class DeprecatedAssignmentSyntax(key: Name, value: untpd.Tree)(using Context) extends SyntaxMsg(DeprecatedAssignmentSyntaxID):
33483348
override protected def msg(using Context): String =
3349-
i"""Ambiguous syntax: this is interpreted as a named tuple with one element,
3349+
i"""Deprecated syntax: in the future it would be interpreted as a named tuple with one element,
33503350
|not as an assignment.
33513351
|
33523352
|To assign a value, use curly braces: `{${key} = ${value}}`."""
3353-
3353+
+ Message.rewriteNotice("This", version = SourceVersion.`3.6-migration`)
3354+
33543355
override protected def explain(using Context): String = ""
33553356

3356-
class AmbiguousNamedTupleInfixApply()(using Context) extends SyntaxMsg(AmbiguousNamedTupleInfixApplyID):
3357+
class DeprecatedInfixNamedArgumentSyntax()(using Context) extends SyntaxMsg(DeprecatedInfixNamedArgumentSyntaxID):
33573358
def msg(using Context) =
3358-
"Ambigious syntax: this infix call argument list is interpreted as single named tuple argument, not as an named arguments list."
3359-
+ Message.rewriteNotice("This", version = SourceVersion.`3.6-migration`)
3359+
i"""Deprecated syntax: infix named arguments lists are deprecated; in the future it would be interpreted as a single name tuple argument.
3360+
|To avoid this warning, either remove the argument names or use dotted selection."""
3361+
+ Message.rewriteNotice("This", version = SourceVersion.`3.6-migration`)
33603362

3361-
def explain(using Context) =
3362-
i"""Starting with Scala 3.6 infix named arguments are interpretted as Named Tuple.
3363-
|
3364-
|To avoid this warning, either remove the argument names or use dotted selection."""
3363+
def explain(using Context) = ""

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
795795
def tryNamedTupleSelection() =
796796
val namedTupleElems = qual.tpe.widenDealias.namedTupleElementTypes
797797
val nameIdx = namedTupleElems.indexWhere(_._1 == selName)
798-
if nameIdx >= 0 && sourceVersion.isAtLeast(`3.6`) then
798+
if nameIdx >= 0 && Feature.enabled(Feature.namedTuples) then
799799
typed(
800800
untpd.Apply(
801801
untpd.Select(untpd.TypedSplice(qual), nme.apply),
@@ -3404,7 +3404,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34043404
/** Translate tuples of all arities */
34053405
def typedTuple(tree: untpd.Tuple, pt: Type)(using Context): Tree =
34063406
val tree1 = desugar.tuple(tree, pt)
3407-
checkAmbiguousNamedTupleAssignment(tree)
3407+
checkDeprecatedAssignmentSyntax(tree)
34083408
if tree1 ne tree then typed(tree1, pt)
34093409
else
34103410
val arity = tree.trees.length
@@ -3433,15 +3433,18 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34333433
/** Checks if `tree` is a named tuple with one element that could be
34343434
* interpreted as an assignment, such as `(x = 1)`. If so, issues a warning.
34353435
*/
3436-
def checkAmbiguousNamedTupleAssignment(tree: untpd.Tuple)(using Context): Unit =
3436+
def checkDeprecatedAssignmentSyntax(tree: untpd.Tuple)(using Context): Unit =
34373437
tree.trees match
34383438
case List(NamedArg(name, value)) =>
34393439
val tmpCtx = ctx.fresh.setNewTyperState()
34403440
typedAssign(untpd.Assign(untpd.Ident(name), value), WildcardType)(using tmpCtx)
34413441
if !tmpCtx.reporter.hasErrors then
34423442
// If there are no errors typing the above, then the named tuple is
34433443
// ambiguous and we issue a warning.
3444-
report.migrationWarning(AmbiguousNamedTupleAssignment(name, value), tree.srcPos)
3444+
report.migrationWarning(DeprecatedAssignmentSyntax(name, value), tree.srcPos)
3445+
if MigrationVersion.AmbiguousNamedTupleSyntax.needsPatch then
3446+
patch(tree.source, Span(tree.span.start, tree.span.start + 1), "{")
3447+
patch(tree.source, Span(tree.span.end - 1, tree.span.end), "}")
34453448
case _ => ()
34463449

34473450
/** Retrieve symbol attached to given tree */

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class CompilationTests {
8080
compileDir("tests/rewrites/annotation-named-pararamters", defaultOptions.and("-rewrite", "-source:3.6-migration")),
8181
compileFile("tests/rewrites/i21418.scala", unindentOptions.and("-rewrite", "-source:3.5-migration")),
8282
compileFile("tests/rewrites/infix-named-args.scala", defaultOptions.and("-rewrite", "-source:3.6-migration")),
83+
compileFile("tests/rewrites/ambigious-named-tuple-assignment.scala", defaultOptions.and("-rewrite", "-source:3.6-migration")),
8384
).checkRewrites()
8485
}
8586

docs/_docs/reference/other-new-features/named-tuples.md renamed to docs/_docs/reference/experimental/named-tuples.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
layout: doc-page
33
title: "Named Tuples"
4-
nightlyOf: https://docs.scala-lang.org/scala3/reference/other-new-features/named-tuples.html
4+
nightlyOf: https://docs.scala-lang.org/scala3/reference/experimental/named-tuples.html
55
---
66

7-
Starting in Scala 3.6, the elements of a tuple can be named. Example:
7+
The elements of a tuple can now be named. Example:
88
```scala
99
type Person = (name: String, age: Int)
1010
val Bob: Person = (name = "Bob", age = 33)

docs/sidebar.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ subsection:
7272
- page: reference/other-new-features/export.md
7373
- page: reference/other-new-features/opaques.md
7474
- page: reference/other-new-features/opaques-details.md
75-
- page: reference/other-new-features/named-tuples.md
7675
- page: reference/other-new-features/open-classes.md
7776
- page: reference/other-new-features/parameter-untupling.md
7877
- page: reference/other-new-features/parameter-untupling-spec.md
@@ -159,6 +158,7 @@ subsection:
159158
- page: reference/experimental/cc.md
160159
- page: reference/experimental/purefuns.md
161160
- page: reference/experimental/tupled-function.md
161+
- page: reference/experimental/named-tuples.md
162162
- page: reference/experimental/modularity.md
163163
- page: reference/experimental/typeclasses.md
164164
- page: reference/experimental/runtimeChecked.md

library/src/scala/NamedTuple.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package scala
2+
import annotation.experimental
23
import compiletime.ops.boolean.*
34

5+
@experimental
46
object NamedTuple:
57

68
/** The type to which named tuples get mapped to. For instance,
@@ -131,6 +133,7 @@ object NamedTuple:
131133
end NamedTuple
132134

133135
/** Separate from NamedTuple object so that we can match on the opaque type NamedTuple. */
136+
@experimental
134137
object NamedTupleDecomposition:
135138
import NamedTuple.*
136139
extension [N <: Tuple, V <: Tuple](x: NamedTuple[N, V])

0 commit comments

Comments
 (0)