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, minimal fix without refactoring #22325

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

aherlihy
Copy link
Contributor

@aherlihy aherlihy commented Jan 8, 2025

Per the discussion on #22150 (comment) we should do a refactor of how we check for Named Tuples and move the logic into unapply, which is WIP. Not sure if we should just do the refactor right away, or merge the slightly quicker fix that's in this PR first?

@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.

@@ -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

Choose a reason for hiding this comment

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

But selNames.isEmpty will be false

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.

3 participants