Skip to content

Named tuples second implementation #19174

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 68 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
9d1f20d
Improvements to tuples: Drop experimental
odersky Dec 13, 2023
2395ece
Improvements to Tuples: New methods
odersky Dec 13, 2023
64a79c9
Improvements to tuples: Allow prefix slice in fromArray
odersky Dec 13, 2023
cb95265
Improvements to tuples: Rearrange types into a more logical order
odersky Dec 13, 2023
6ae8252
Improvements to tuples: more new types and methods
odersky Dec 13, 2023
1f79b87
Add NamedTuple object to library
odersky Dec 2, 2023
b9899b7
Support for named tuples with new representation
odersky Dec 3, 2023
27d6288
Add doc page
odersky Dec 3, 2023
5dd48f9
Make NamedTuple covariant in its value type
odersky Dec 3, 2023
bd9bb8a
Various tweaks
odersky Dec 5, 2023
e0a11cd
Harden NamedTuple handling against ill-formed NamedTuples
odersky Dec 5, 2023
b9d86fe
Fix test
odersky Dec 5, 2023
0fbdb49
Simplify tupleElementTypes unapply handling
odersky Dec 8, 2023
6f53dcd
Fix pattern matching for get matches
odersky Dec 8, 2023
b44f15d
Another fix for named get patterns
odersky Dec 14, 2023
02aa578
Avoid widening into unreducible types when inferring types
odersky Dec 14, 2023
b7115e7
Fix rebase breakage
odersky Jan 12, 2024
5c9bb5f
Make NamedTuples work under new MatchType spec
odersky Jan 12, 2024
4758830
Avoid TypeError exception in RefinedPrinter
odersky Jan 12, 2024
18f600d
Move named-tuples-strawman.ccala to pending
odersky Jan 12, 2024
5513ed6
Update check file
odersky Jan 12, 2024
fb1541a
Better printing of NamedTuple type trees
odersky Jan 14, 2024
cf09b19
Add FieldsOf type
odersky Jan 14, 2024
4baa509
Describe and add tests for source incompabilities
odersky Jan 14, 2024
702dcd5
Rename NamedTuple.FieldsOf --> NamedTuple.From
odersky Jan 14, 2024
1e31d16
Implement Fields as a Selectable type member
odersky Feb 5, 2024
22e6c89
Refactor typedSelect
odersky Feb 5, 2024
d0888f6
Add section on computable field names to reference doc page
odersky Feb 5, 2024
69964b0
Fix fields as a selectable type member
odersky Feb 6, 2024
a3409e0
Make NamedTuple.From work for named tuple arguments
odersky Feb 6, 2024
111674c
Fix NamedArg term/type classification
odersky Feb 7, 2024
1fd5962
Rename inMode to withModeBits
odersky Feb 10, 2024
c0b792f
Revert NamedArg term/type classification
odersky Feb 10, 2024
611861b
Update MimaFilters
odersky Feb 10, 2024
640da16
Fix rebase breakage in Parsers.scala
odersky Feb 16, 2024
67c0af2
Add tyql test case
odersky Feb 16, 2024
2cd5d7e
Introduce auto-conversion from named tuples to tuples
odersky Feb 17, 2024
d8b7595
Update documentation following review suggestions
odersky Feb 18, 2024
e0eb247
Drop TreeInfo's `dropNamedArg`
odersky Apr 24, 2024
4279a58
Print wildcard types in named tuples correctly
odersky Apr 24, 2024
c0bd1e4
Don't abbreviate tuple bindings if right-hand-side is named
odersky Apr 24, 2024
b997f3d
Strip named tuple from scrutinee when testing refutability of a named…
odersky Apr 24, 2024
2206d88
Fix tuple selection in pattern matcher
odersky Apr 24, 2024
ca19f1a
Fixes to NamedTuple
odersky Apr 25, 2024
984fe62
More tests
odersky Apr 25, 2024
21bcfef
Mention restriction against `_1`, `_2`, ... as named tuple labels
odersky Apr 25, 2024
92d22c9
Address review comments on Tuple.scala
odersky Apr 26, 2024
1613ee1
Reject recursive dynamicSelect corner case
odersky Apr 27, 2024
a04d3a7
Update compiler/src/dotty/tools/dotc/typer/Typer.scala
odersky Apr 27, 2024
37b1bd2
Update tests/neg/unselectable-fields.check
odersky Apr 27, 2024
3f8f6c6
Use consistent naming and drop remaining braces in tuple type ops
EugeneFlesselle Apr 29, 2024
9627c08
Add an infix shorthand for `Append[X, Y]`
EugeneFlesselle Apr 29, 2024
8a4162f
Drop unreachable case from `type Zip`
EugeneFlesselle Apr 29, 2024
0ab9e7b
Document `Concat` covariance in 2nd parameter
EugeneFlesselle Apr 29, 2024
075b7d1
Refine bounds of `type Filter` predicate
EugeneFlesselle Apr 29, 2024
c58c8c2
Do `contains` runtime operation based on term equality
EugeneFlesselle Apr 29, 2024
57b17ac
Do `indexOf` runtime operation based on term equality
EugeneFlesselle Apr 29, 2024
f427ec9
Do `filter` runtime operation based on a term level predicate
EugeneFlesselle Apr 29, 2024
94b7c1f
Mark `type Append` 2nd argument as covariant
EugeneFlesselle Apr 29, 2024
5df2120
Move `NonEmptyTuple` methods into `Tuple`
EugeneFlesselle Apr 29, 2024
40c6138
Reorder operations to be same between term and type level
EugeneFlesselle Apr 29, 2024
03509b8
Drop braces remaining at term level
EugeneFlesselle Apr 29, 2024
9047ac3
Fix `def indexOf` to return the size instead of -1
EugeneFlesselle May 1, 2024
579e14a
Revert doing tuple runtime operations based on a term level predicates
EugeneFlesselle May 1, 2024
8d6fa37
Make named-tuples-strawman-2.scala a run test
EugeneFlesselle May 1, 2024
1e29c4f
import language.experimental.namedTuples in pos/fieldsOf.scala
EugeneFlesselle May 2, 2024
0c89c92
Undo all unnecessary changes to Tuples.
odersky May 7, 2024
f80a8dd
Revert the addition of `type Fields` in `Selectable`.
sjrd May 6, 2024
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
Prev Previous commit
Next Next commit
Simplify tupleElementTypes unapply handling
  • Loading branch information
odersky committed May 6, 2024
commit 0fbdb497c40aee5b3e17c22bf9030f55ddcc3bca
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/TypeUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Names.{Name, TermName}
import Constants.Constant

class TypeUtils {

/** A decorator that provides methods on types
* that are needed in the transformer pipeline.
*/
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,9 @@ object PatternMatcher {
val arity = productArity(unappType, unapp.srcPos)
unapplyProductSeqPlan(unappResult, args, arity)
else if unappResult.info <:< defn.NonEmptyTupleTypeRef then
val components = (0 until foldApplyTupleType(unappResult.denot.info).length).toList.map(tupleApp(_, ref(unappResult)))
val components =
(0 until unappResult.denot.info.tupleElementTypes.getOrElse(Nil).length)
.toList.map(tupleApp(_, ref(unappResult)))
matchArgsPlan(components, args, onSuccess)
else {
assert(isGetMatch(unappType))
Expand Down
44 changes: 17 additions & 27 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ object Applications {
(0 until argsNum).map(i => if (i < arity - 1) selTps(i) else elemTp).toList
}

def unapplyArgs(unapplyResult: Type, unapplyFn: Tree, args: List[untpd.Tree], pos: SrcPos)(using Context): List[Type] = {
def unapplyArgs(unapplyResult: Type, unapplyFn: Tree, args: List[untpd.Tree], pos: SrcPos)(using Context): List[Type] =
def getName(fn: Tree): Name =
fn match
case TypeApply(fn, _) => getName(fn)
Expand All @@ -165,46 +165,36 @@ object Applications {
Nil
}

def unapplySeq(tp: Type)(fallback: => List[Type]): List[Type] = {
def unapplySeq(tp: Type)(fallback: => List[Type]): List[Type] =
val elemTp = unapplySeqTypeElemTp(tp)
if (elemTp.exists) args.map(Function.const(elemTp))
else if (isProductSeqMatch(tp, args.length, pos)) productSeqSelectors(tp, args.length, pos)
else if tp.derivesFrom(defn.NonEmptyTupleClass) then foldApplyTupleType(tp)
if elemTp.exists then
args.map(Function.const(elemTp))
else if isProductSeqMatch(tp, args.length, pos) then
productSeqSelectors(tp, args.length, pos)
else if tp.derivesFrom(defn.NonEmptyTupleClass) then
tp.tupleElementTypes.getOrElse(Nil)
else fallback
}

if (unapplyName == nme.unapplySeq)
unapplySeq(unapplyResult) {
if unapplyName == nme.unapplySeq then
unapplySeq(unapplyResult):
if (isGetMatch(unapplyResult, pos)) unapplySeq(getTp)(fail)
else fail
}
else {
else
assert(unapplyName == nme.unapply)
if (isProductMatch(unapplyResult, args.length, pos))
if isProductMatch(unapplyResult, args.length, pos) then
productSelectorTypes(unapplyResult, pos)
else if (isGetMatch(unapplyResult, pos))
else if isGetMatch(unapplyResult, pos) then
getUnapplySelectors(getTp, args, pos)
else if (unapplyResult.widenSingleton isRef defn.BooleanClass)
else if unapplyResult.derivesFrom(defn.BooleanClass) then
Nil
else if (defn.isProductSubType(unapplyResult) && productArity(unapplyResult, pos) != 0)
else if defn.isProductSubType(unapplyResult) && productArity(unapplyResult, pos) != 0 then
productSelectorTypes(unapplyResult, pos)
// this will cause a "wrong number of arguments in pattern" error later on,
// which is better than the message in `fail`.
else if unapplyResult.derivesFrom(defn.NonEmptyTupleClass) then
foldApplyTupleType(unapplyResult)
unapplyResult.tupleElementTypes.getOrElse(Nil)
else fail
}
}

def foldApplyTupleType(tp: Type)(using Context): List[Type] =
object tupleFold extends TypeAccumulator[List[Type]]:
override def apply(accum: List[Type], t: Type): List[Type] =
t match
case AppliedType(tycon, x :: x2 :: Nil) if tycon.typeSymbol == defn.PairClass =>
apply(x :: accum, x2)
case x => foldOver(accum, x)
end tupleFold
tupleFold(Nil, tp).reverse
end unapplyArgs

def wrapDefs(defs: mutable.ListBuffer[Tree] | Null, tree: Tree)(using Context): Tree =
if (defs != null && defs.nonEmpty) tpd.Block(defs.toList, tree) else tree
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2460,6 +2460,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
|| body1.isInstanceOf[Literal]
val symTp =
if isStableIdentifierOrLiteral || pt.isNamedTupleType then pt
// need to combine tuple element types with expected named type
else if isWildcardStarArg(body1)
|| pt == defn.ImplicitScrutineeTypeRef
|| body1.tpe <:< pt // There is some strange interaction with gadt matching.
Expand Down