Skip to content

Commit 211ceaf

Browse files
Backport "Make named tuples an experimental feature again" to 3.6 (#22048)
Backports #22045 to the 3.6.2. PR submitted by the release tooling.
2 parents 7debedf + 1298d1f commit 211ceaf

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
@@ -651,7 +651,7 @@ object Parsers {
651651
else leading :: Nil
652652

653653
def maybeNamed(op: () => Tree): () => Tree = () =>
654-
if isIdent && in.lookahead.token == EQUALS && sourceVersion.isAtLeast(`3.6`) then
654+
if isIdent && in.lookahead.token == EQUALS && in.featureEnabled(Feature.namedTuples) then
655655
atSpan(in.offset):
656656
val name = ident()
657657
in.nextToken()
@@ -1133,8 +1133,8 @@ object Parsers {
11331133
if isType then infixOp
11341134
else infixOp.right match
11351135
case Tuple(args) if args.exists(_.isInstanceOf[NamedArg]) && !isNamedTupleOperator =>
1136-
report.errorOrMigrationWarning(AmbiguousNamedTupleInfixApply(), infixOp.right.srcPos, MigrationVersion.AmbiguousNamedTupleInfixApply)
1137-
if MigrationVersion.AmbiguousNamedTupleInfixApply.needsPatch then
1136+
report.errorOrMigrationWarning(DeprecatedInfixNamedArgumentSyntax(), infixOp.right.srcPos, MigrationVersion.AmbiguousNamedTupleSyntax)
1137+
if MigrationVersion.AmbiguousNamedTupleSyntax.needsPatch then
11381138
val asApply = cpy.Apply(infixOp)(Select(opInfo.operand, opInfo.operator.name), args)
11391139
patch(source, infixOp.span, asApply.show(using ctx.withoutColors))
11401140
asApply // allow to use pre-3.6 syntax in migration mode
@@ -2166,7 +2166,7 @@ object Parsers {
21662166

21672167
if namedOK && isIdent && in.lookahead.token == EQUALS then
21682168
commaSeparated(() => namedArgType())
2169-
else if tupleOK && isIdent && in.lookahead.isColon && sourceVersion.isAtLeast(`3.6`) then
2169+
else if tupleOK && isIdent && in.lookahead.isColon && in.featureEnabled(Feature.namedTuples) then
21702170
commaSeparated(() => namedElem())
21712171
else
21722172
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
@@ -789,7 +789,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
789789
def tryNamedTupleSelection() =
790790
val namedTupleElems = qual.tpe.widenDealias.namedTupleElementTypes
791791
val nameIdx = namedTupleElems.indexWhere(_._1 == selName)
792-
if nameIdx >= 0 && sourceVersion.isAtLeast(`3.6`) then
792+
if nameIdx >= 0 && Feature.enabled(Feature.namedTuples) then
793793
typed(
794794
untpd.Apply(
795795
untpd.Select(untpd.TypedSplice(qual), nme.apply),
@@ -3394,7 +3394,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
33943394
/** Translate tuples of all arities */
33953395
def typedTuple(tree: untpd.Tuple, pt: Type)(using Context): Tree =
33963396
val tree1 = desugar.tuple(tree, pt)
3397-
checkAmbiguousNamedTupleAssignment(tree)
3397+
checkDeprecatedAssignmentSyntax(tree)
33983398
if tree1 ne tree then typed(tree1, pt)
33993399
else
34003400
val arity = tree.trees.length
@@ -3423,15 +3423,18 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34233423
/** Checks if `tree` is a named tuple with one element that could be
34243424
* interpreted as an assignment, such as `(x = 1)`. If so, issues a warning.
34253425
*/
3426-
def checkAmbiguousNamedTupleAssignment(tree: untpd.Tuple)(using Context): Unit =
3426+
def checkDeprecatedAssignmentSyntax(tree: untpd.Tuple)(using Context): Unit =
34273427
tree.trees match
34283428
case List(NamedArg(name, value)) =>
34293429
val tmpCtx = ctx.fresh.setNewTyperState()
34303430
typedAssign(untpd.Assign(untpd.Ident(name), value), WildcardType)(using tmpCtx)
34313431
if !tmpCtx.reporter.hasErrors then
34323432
// If there are no errors typing the above, then the named tuple is
34333433
// ambiguous and we issue a warning.
3434-
report.migrationWarning(AmbiguousNamedTupleAssignment(name, value), tree.srcPos)
3434+
report.migrationWarning(DeprecatedAssignmentSyntax(name, value), tree.srcPos)
3435+
if MigrationVersion.AmbiguousNamedTupleSyntax.needsPatch then
3436+
patch(tree.source, Span(tree.span.start, tree.span.start + 1), "{")
3437+
patch(tree.source, Span(tree.span.end - 1, tree.span.end), "}")
34353438
case _ => ()
34363439

34373440
/** 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-bootstrapped/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)