Skip to content

Commit a4ea8bf

Browse files
committed
Make NamedTupls an experimental feature again
1 parent c3d6c48 commit a4ea8bf

Some content is hidden

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

51 files changed

+150
-91
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/parsing/Parsers.scala

Lines changed: 2 additions & 2 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()
@@ -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/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
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),

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])

library/src/scala/runtime/stdLibPatches/language.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ object language:
9797
* @see [[https://dotty.epfl.ch/docs/reference/experimental/named-tuples]]
9898
*/
9999
@compileTimeOnly("`namedTuples` can only be used at compile time in import statements")
100-
@deprecated("The experimental.namedTuples language import is no longer needed since the feature is now standard", since = "3.6")
101100
object namedTuples
102101

103102
/** Experimental support for new features for better modularity, including

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,8 @@ class CompletionSuite extends BaseCompletionSuite:
19881988

19891989
@Test def `namedTuple completions` =
19901990
check(
1991-
"""|import scala.NamedTuple.*
1991+
"""|import scala.language.experimental.namedTuples
1992+
|import scala.NamedTuple.*
19921993
|
19931994
|val person = (name = "Jamie", city = "Lausanne")
19941995
|
@@ -1999,7 +2000,8 @@ class CompletionSuite extends BaseCompletionSuite:
19992000

20002001
@Test def `Selectable with namedTuple Fields member` =
20012002
check(
2002-
"""|import scala.NamedTuple.*
2003+
"""|import scala.language.experimental.namedTuples
2004+
|import scala.NamedTuple.*
20032005
|
20042006
|class NamedTupleSelectable extends Selectable {
20052007
| type Fields <: AnyNamedTuple

tests/neg/i20517.check

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
-- [E007] Type Mismatch Error: tests/neg/i20517.scala:9:43 -------------------------------------------------------------
2-
9 | def dep(foo: Foo[Any]): From[foo.type] = (elem = "") // error
3-
| ^^^^^^^^^^^
4-
| Found: (elem : String)
5-
| Required: NamedTuple.From[(foo : Foo[Any])]
6-
|
7-
| longer explanation available when compiling with `-explain`
1+
-- [E007] Type Mismatch Error: tests/neg/i20517.scala:10:43 ------------------------------------------------------------
2+
10 | def dep(foo: Foo[Any]): From[foo.type] = (elem = "") // error
3+
| ^^^^^^^^^^^
4+
| Found: (elem : String)
5+
| Required: NamedTuple.From[(foo : Foo[Any])]
6+
|
7+
| longer explanation available when compiling with `-explain`

tests/neg/i20517.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import scala.language.experimental.namedTuples
12
import NamedTuple.From
23

34
case class Foo[+T](elem: T)

tests/neg/infix-named-args.check

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
-- [E134] Type Error: tests/neg/infix-named-args.scala:2:13 ------------------------------------------------------------
2-
2 | def f = 42 + (x = 1) // error // werror
1+
-- [E134] Type Error: tests/neg/infix-named-args.scala:4:13 ------------------------------------------------------------
2+
4 | def f = 42 + (x = 1) // error // werror
33
| ^^^^
44
| None of the overloaded alternatives of method + in class Int with types
55
| (x: Double): Double
@@ -11,29 +11,29 @@
1111
| (x: Byte): Int
1212
| (x: String): String
1313
| match arguments ((x : Int)) (a named tuple)
14-
-- [E204] Syntax Warning: tests/neg/infix-named-args.scala:2:15 --------------------------------------------------------
15-
2 | def f = 42 + (x = 1) // error // werror
14+
-- [E204] Syntax Warning: tests/neg/infix-named-args.scala:4:15 --------------------------------------------------------
15+
4 | def f = 42 + (x = 1) // error // werror
1616
| ^^^^^^^
1717
|Ambigious syntax: this infix call argument list is interpreted as single named tuple argument, not as an named arguments list.
1818
|This can be rewritten automatically under -rewrite -source 3.6-migration.
1919
|
2020
| longer explanation available when compiling with `-explain`
21-
-- [E204] Syntax Warning: tests/neg/infix-named-args.scala:5:26 --------------------------------------------------------
22-
5 | def g = new C() `multi` (x = 42, y = 27) // werror
21+
-- [E204] Syntax Warning: tests/neg/infix-named-args.scala:7:26 --------------------------------------------------------
22+
7 | def g = new C() `multi` (x = 42, y = 27) // werror
2323
| ^^^^^^^^^^^^^^^^
2424
|Ambigious syntax: this infix call argument list is interpreted as single named tuple argument, not as an named arguments list.
2525
|This can be rewritten automatically under -rewrite -source 3.6-migration.
2626
|
2727
| longer explanation available when compiling with `-explain`
28-
-- [E204] Syntax Warning: tests/neg/infix-named-args.scala:6:21 --------------------------------------------------------
29-
6 | def h = new C() ** (x = 42, y = 27) // werror
28+
-- [E204] Syntax Warning: tests/neg/infix-named-args.scala:8:21 --------------------------------------------------------
29+
8 | def h = new C() ** (x = 42, y = 27) // werror
3030
| ^^^^^^^^^^^^^^^^
3131
|Ambigious syntax: this infix call argument list is interpreted as single named tuple argument, not as an named arguments list.
3232
|This can be rewritten automatically under -rewrite -source 3.6-migration.
3333
|
3434
| longer explanation available when compiling with `-explain`
35-
-- [E204] Syntax Warning: tests/neg/infix-named-args.scala:13:18 -------------------------------------------------------
36-
13 | def f = this ** (x = 2) // werror
35+
-- [E204] Syntax Warning: tests/neg/infix-named-args.scala:15:18 -------------------------------------------------------
36+
15 | def f = this ** (x = 2) // werror
3737
| ^^^^^^^
3838
|Ambigious syntax: this infix call argument list is interpreted as single named tuple argument, not as an named arguments list.
3939
|This can be rewritten automatically under -rewrite -source 3.6-migration.

tests/neg/infix-named-args.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import scala.language.experimental.namedTuples
2+
13
class C:
24
def f = 42 + (x = 1) // error // werror
35
def multi(x: Int, y: Int): Int = x + y

tests/neg/named-tuple-selectable.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import scala.language.experimental.namedTuples
2+
13
class FromFields extends Selectable:
24
type Fields = (i: Int)
35
def selectDynamic(key: String) =

tests/neg/named-tuples-2.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
-- Error: tests/neg/named-tuples-2.scala:4:9 ---------------------------------------------------------------------------
2-
4 | case (name, age) => () // error
1+
-- Error: tests/neg/named-tuples-2.scala:5:9 ---------------------------------------------------------------------------
2+
5 | case (name, age) => () // error
33
| ^
44
| this case is unreachable since type (String, Int, Boolean) is not a subclass of class Tuple2
5-
-- Error: tests/neg/named-tuples-2.scala:5:9 ---------------------------------------------------------------------------
6-
5 | case (n, a, m, x) => () // error
5+
-- Error: tests/neg/named-tuples-2.scala:6:9 ---------------------------------------------------------------------------
6+
6 | case (n, a, m, x) => () // error
77
| ^
88
| this case is unreachable since type (String, Int, Boolean) is not a subclass of class Tuple4

tests/neg/named-tuples-2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import language.experimental.namedTuples
12
def Test =
23
val person = (name = "Bob", age = 33, married = true)
34
person match

tests/neg/named-tuples-3.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
-- [E007] Type Mismatch Error: tests/neg/named-tuples-3.scala:5:16 -----------------------------------------------------
2-
5 |val p: Person = f // error
1+
-- [E007] Type Mismatch Error: tests/neg/named-tuples-3.scala:7:16 -----------------------------------------------------
2+
7 |val p: Person = f // error
33
| ^
44
| Found: NamedTuple.NamedTuple[(Int, Any), (Int, String)]
55
| Required: Person

tests/neg/named-tuples-3.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import language.experimental.namedTuples
2+
13
def f: NamedTuple.NamedTuple[(Int, Any), (Int, String)] = ???
24

35
type Person = (name: Int, age: String)

0 commit comments

Comments
 (0)