-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
(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 => |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
There was a problem hiding this comment.
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
scala3/compiler/src/dotty/tools/dotc/ast/Desugar.scala
Lines 1747 to 1748 in 0677702
var selNames = pt.namedTupleElementTypes.map(_(0)) | |
if selNames.isEmpty && pt.classSymbol.is(CaseClass) then |
pt.namedTupleElementTypes
we call pt.classSymbol
and classSymbol
also recurses on TypeProxy: scala3/compiler/src/dotty/tools/dotc/core/Types.scala
Lines 580 to 581 in 0677702
case tp: TypeProxy => | |
tp.superType.classSymbol |
There was a problem hiding this comment.
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.
c1997ce
to
1a5a8b3
Compare
…down This reverts commit 2f03f86.
There was a problem hiding this 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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if tp.isNamedTupleType then tp.namedTupleElementTypes(true).map(_(1)) | |
if tp.isNamedTupleType then tp.namedTupleElementTypes(derived = true).map(_(1)) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
Fixes #22150.
Previously, there were several ways to check if something was a Named Tuple (
derivesFromNamedTuple
,isNamedTupleType
andNamedTuple.unapply
), this PR moves everything intoNamedTuple.unapply
.namedTupleElementTypes
now takes an argumentderived
that when false will skipunapply
(to avoid infinite recursion, used in desugaring and RefinedPrinter where trees can have invalid cycles).