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

Fix .toTuple insertion #22028

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions compiler/src/dotty/tools/dotc/core/TypeUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ class TypeUtils:
case defn.NamedTuple(_, _) => true
case _ => false

def derivesFromNamedTuple(using Context): Boolean = self match
case defn.NamedTuple(_, _) => true
case tp: MatchType =>
tp.bound.derivesFromNamedTuple || tp.reduced.derivesFromNamedTuple
case tp: TypeProxy => tp.superType.derivesFromNamedTuple
case tp: AndType => tp.tp1.derivesFromNamedTuple || tp.tp2.derivesFromNamedTuple
case tp: OrType => tp.tp1.derivesFromNamedTuple && tp.tp2.derivesFromNamedTuple
case _ => false

/** Drop all named elements in tuple type */
def stripNamedTuple(using Context): Type = self.normalized.dealias match
case defn.NamedTuple(_, vals) =>
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/interactive/Completion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ object Completion:
.groupByName

val qualTpe = qual.typeOpt
if qualTpe.isNamedTupleType then
if qualTpe.derivesFromNamedTuple then
namedTupleCompletionsFromType(qualTpe)
else if qualTpe.derivesFrom(defn.SelectableClass) then
val pre = if !TypeOps.isLegalPrefix(qualTpe) then Types.SkolemType(qualTpe) else qualTpe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ object PatternMatcher {
// TODO: Drop Case once we use everywhere else `isPatmatGenerated`.

private def dropNamedTuple(tree: Tree): Tree =
val tpe = tree.tpe.widen
val tpe = tree.tpe.widenDealias
if tpe.isNamedTupleType then tree.cast(tpe.stripNamedTuple) else tree

/** The plan `let x = rhs in body(x)` where `x` is a fresh variable */
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ trait Implicits:
|| inferView(dummyTreeOfType(from), to)
(using ctx.fresh.addMode(Mode.ImplicitExploration).setExploreTyperState()).isSuccess
// TODO: investigate why we can't TyperState#test here
|| from.widen.isNamedTupleType && to.derivesFrom(defn.TupleClass)
|| from.widen.derivesFromNamedTuple && to.derivesFrom(defn.TupleClass)
&& from.widen.stripNamedTuple <:< to
)

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2706,7 +2706,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
body1.isInstanceOf[RefTree] && !isWildcardArg(body1)
|| body1.isInstanceOf[Literal]
val symTp =
if isStableIdentifierOrLiteral || pt.isNamedTupleType then pt
if isStableIdentifierOrLiteral || pt.dealias.isNamedTupleType then pt
// need to combine tuple element types with expected named type
else if isWildcardStarArg(body1)
|| pt == defn.ImplicitScrutineeTypeRef
Expand Down Expand Up @@ -4641,7 +4641,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
case _: SelectionProto =>
tree // adaptations for selections are handled in typedSelect
case _ if ctx.mode.is(Mode.ImplicitsEnabled) && tree.tpe.isValueType =>
if tree.tpe.widen.isNamedTupleType && pt.derivesFrom(defn.TupleClass) then
if tree.tpe.derivesFromNamedTuple && pt.derivesFrom(defn.TupleClass) then
readapt(typed(untpd.Select(untpd.TypedSplice(tree), nme.toTuple)))
else if pt.isRef(defn.AnyValClass, skipRefined = false)
|| pt.isRef(defn.ObjectClass, skipRefined = false)
Expand Down
20 changes: 20 additions & 0 deletions tests/pos/named-tuple-downcast.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type Person = (name: String, age: Int)

val Bob: Person = (name = "Bob", age = 33)

type SI = (String, Int)

def id[X](x: X): X = x
val x: (String, Int) = Bob
val y: SI = id(Bob)
val and: Person & String = ???
val _: SI = and
val or: Person | (name: "Bob", age: 33) = ???
val _: SI = or

class C[P <: Person](p: P):
val x: (String, Int) = p




Loading