Skip to content
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

Handle TypeProxy of Named Tuples in unapply #22325

Merged
merged 6 commits into from
Jan 13, 2025
Merged

Conversation

aherlihy
Copy link
Contributor

@aherlihy aherlihy commented Jan 8, 2025

Fixes #22150.
Previously, there were several ways to check if something was a Named Tuple (derivesFromNamedTuple, isNamedTupleType and NamedTuple.unapply), this PR moves everything into NamedTuple.unapply. namedTupleElementTypes now takes an argument derived that when false will skip unapply (to avoid infinite recursion, used in desugaring and RefinedPrinter where trees can have invalid cycles).

@aherlihy aherlihy marked this pull request as draft January 8, 2025 23:37
(if normalize then self.normalized else self).dealias match
case defn.NamedTuple(nmes, vals) =>
val names = nmes.tupleElementTypesUpTo(bound, normalize).getOrElse(Nil).map(_.dealias).map:
case ConstantType(Constant(str: String)) => str.toTermName
case t => throw TypeError(em"Malformed NamedTuple: names must be string types, but $t was found.")
val values = vals.tupleElementTypesUpTo(bound, normalize).getOrElse(Nil)
names.zip(values)
case tp: TypeProxy =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this. In principle it looks OK, but TypeProxy#superType goes through normalize AFAIK, so it is as if the normalize argument has no effect anymore. Does that break things somewhere? What do the tests say?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All compilation tests are passing, haven't finished the full suite yet. From what I could tell the normalize argument is just used for pretty-printing?

The only unrelated tests that failed with this change were i12456 and i9328 which were fixed with the bounds check. I'm really not sure this is the right use of bounds, but I noticed it wasn't used anywhere in this method but was used in the regular tuple method to limit recursion. Given the docstring, this is a bit of a misuse, but for the moment I couldn’t find a nice way to check for cycles / don’t know if that’s done anywhere else I can reuse?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, should we predicate TypeProxy and following cases on normalize = false then?

Copy link
Contributor Author

@aherlihy aherlihy Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the printer need to print trees with invalid cycles? It's used in error messages so I assume it's possible. We can either keep using the original namedTupleElementTypes in the printer, which does not call TypeProxy.supertype (so no need to update it to respect the normalization flag). If the printer does have to deal with cyclic structures but also if it uses the derived namedTupleElementTypes, then we can update TypeProxy to respect the normalization flag but then we'd need some way to check there aren't cycles.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was resolved by avoiding recurring on TypeProxy when called from the printer.

@@ -128,6 +128,7 @@ class TypeUtils:
case None => throw new AssertionError("not a tuple")

def namedTupleElementTypesUpTo(bound: Int, normalize: Boolean = true)(using Context): List[(TermName, Type)] =
if bound < 0 then Nil else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a bit too coarse. You recurse 2^31 times in the normal case. It's a bit tricky to limit recursion in the face of illegal recursive aliases, because we do the namedTupleElementTypesUpTo before Typer in desugaring. There we have not yet checked and ruled out such illegal cycles.

One idea would be to leave namedTupleElementTypesUpTo as it is for desugaring, but have another version that follows superTypes of TypeProxies in Typer and Completions. Or have another Boolean flag that says whether we should follow proxies and &/I.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One idea would be to leave namedTupleElementTypesUpTo as it is for desugaring, but have another version that follows superTypes of TypeProxies in Typer and Completions.

It's not clear to me why desugaring should be treated specially, in

var selNames = pt.namedTupleElementTypes.map(_(0))
if selNames.isEmpty && pt.classSymbol.is(CaseClass) then
immediately after calling pt.namedTupleElementTypes we call pt.classSymbol and classSymbol also recurses on TypeProxy:
case tp: TypeProxy =>
tp.superType.classSymbol

Copy link
Contributor Author

@aherlihy aherlihy Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure, but it's true having desugar recur doesn't cause any of the existing invalid cycle tests to fail. If we do want desugar to recur then we can collapse derived and normalize into one flag.

@aherlihy aherlihy changed the title Handle TypeProxy of Named Tuples, minimal fix without refactoring Handle TypeProxy of Named Tuples in unapply Jan 10, 2025
@aherlihy aherlihy force-pushed the i22150 branch 3 times, most recently from c1997ce to 1a5a8b3 Compare January 10, 2025 21:56
@aherlihy aherlihy marked this pull request as ready for review January 10, 2025 22:12
@aherlihy aherlihy requested a review from odersky January 10, 2025 22:12
@aherlihy aherlihy assigned aherlihy and odersky and unassigned aherlihy Jan 13, 2025
Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic looks correct. One can probably streamline this a bit more. But this can be dine later.

@@ -1744,7 +1744,7 @@ object desugar {
def adaptPatternArgs(elems: List[Tree], pt: Type)(using Context): List[Tree] =

def reorderedNamedArgs(wildcardSpan: Span): List[untpd.Tree] =
var selNames = pt.namedTupleElementTypes.map(_(0))
var selNames = pt.namedTupleElementTypes(false).map(_(0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var selNames = pt.namedTupleElementTypes(false).map(_(0))
var selNames = pt.namedTupleElementTypes(derived = false).map(_(0))

We have a rule to always pass a booleans as named arguments.

@@ -110,7 +110,7 @@ object Applications {
}

def namedTupleOrProductTypes(tp: Type)(using Context): List[Type] =
if tp.isNamedTupleType then tp.namedTupleElementTypes.map(_(1))
if tp.isNamedTupleType then tp.namedTupleElementTypes(true).map(_(1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if tp.isNamedTupleType then tp.namedTupleElementTypes(true).map(_(1))
if tp.isNamedTupleType then tp.namedTupleElementTypes(derived = true).map(_(1))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the same for the other boolean arguments.

(if normalize then self.normalized else self).dealias match
// for desugaring and printer, ignore derived types to avoid infinite recursion in NamedTuple.unapply
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid the duplication with the extractor? Maybe have two extractors NamedTuple.unapply and NamedTupleDirect.unapply that share the same logic?

@odersky odersky merged commit c10def4 into scala:main Jan 13, 2025
29 checks passed
aherlihy added a commit to aherlihy/scala3 that referenced this pull request Jan 16, 2025
@WojciechMazur WojciechMazur added this to the 3.6.4 milestone Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Named Tuple binding from IArray.unapplySeq pattern can't select fields.
4 participants