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 #11061: stop accidentally supported auto-tupling for case classes #10987

Merged
merged 1 commit into from
Jan 18, 2021
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
28 changes: 15 additions & 13 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,23 @@ object Applications {
}

def productSelectorTypes(tp: Type, errorPos: SrcPos)(using Context): List[Type] = {
def tupleSelectors(n: Int, tp: Type): List[Type] = {
val sel = extractorMemberType(tp, nme.selectorName(n), errorPos)
// extractorMemberType will return NoType if this is the tail of tuple with an unknown tail
// such as `Int *: T` where `T <: Tuple`.
if (sel.exists) sel :: tupleSelectors(n + 1, tp) else Nil
}
def genTupleSelectors(n: Int, tp: Type): List[Type] = tp match {
case tp: AppliedType if !defn.isTupleClass(tp.tycon.typeSymbol) && tp.derivesFrom(defn.PairClass) =>
val List(head, tail) = tp.args
head :: genTupleSelectors(n, tail)
case _ => tupleSelectors(n, tp)
}
genTupleSelectors(0, tp)
val sels = for (n <- Iterator.from(0)) yield extractorMemberType(tp, nme.selectorName(n), errorPos)
sels.takeWhile(_.exists).toList
}

def tupleComponentTypes(tp: Type)(using Context): List[Type] =
tp.widenExpr.dealias match
case tp: AppliedType =>
if defn.isTupleClass(tp.tycon.typeSymbol) then
tp.args
else if tp.tycon.derivesFrom(defn.PairClass) then
val List(head, tail) = tp.args
head :: tupleComponentTypes(tail)
else
Nil
case _ =>
Nil

def productArity(tp: Type, errorPos: SrcPos = NoSourcePosition)(using Context): Int =
if (defn.isProductSubType(tp)) productSelectorTypes(tp, errorPos).size else -1

Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import TypeComparer.CompareResult
import util.Spans._
import util.common._
import util.{Property, SimpleIdentityMap, SrcPos}
import Applications.{productSelectorTypes, wrapDefs, defaultArgument}
import Applications.{tupleComponentTypes, wrapDefs, defaultArgument}

import collection.mutable
import annotation.tailrec
Expand Down Expand Up @@ -1273,8 +1273,8 @@ class Typer extends Namer
/** Is `formal` a product type which is elementwise compatible with `params`? */
def ptIsCorrectProduct(formal: Type) =
isFullyDefined(formal, ForceDegree.flipBottom) &&
(defn.isProductSubType(formal) || formal.derivesFrom(defn.PairClass)) &&
productSelectorTypes(formal, tree.srcPos).corresponds(params) {
defn.isProductSubType(formal) &&
tupleComponentTypes(formal).corresponds(params) {
(argType, param) =>
param.tpt.isEmpty || argType.widenExpr <:< typedAheadType(param.tpt).tpe
}
Expand Down
2 changes: 1 addition & 1 deletion scala3doc/src/dotty/renderers/MemberRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MemberRenderer(signatureRenderer: SignatureRenderer, buildNode: ContentNod
case Origin.Overrides(defs) =>
def renderDef(d: Overriden): Seq[TagArg] =
Seq(" -> ", signatureRenderer.renderLink(d.name, d.dri))
val headNode = m.inheritedFrom.map(signatureRenderer.renderLink(_, _))
val headNode = m.inheritedFrom.map(form => signatureRenderer.renderLink(form.name, form.dri))
val tailNodes = defs.flatMap(renderDef)
val nodes = headNode.fold(tailNodes.drop(1))(_ +: tailNodes)
tableRow("Definition Classes", div(nodes:_*))
Expand Down
7 changes: 7 additions & 0 deletions tests/neg/i11061.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
case class Foo(a: Int, b: Int)

object Test {
def foo(x: Foo) = List(x).map(_ + _) // error

def main(args: Array[String]): Unit = println(foo(Foo(3, 4)))
}
2 changes: 1 addition & 1 deletion tests/pos/i6199a.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ case class ValueWithEncoder[T](value: T, encoder: Encoder[T])

object Test {
val a: Seq[ValueWithEncoder[_]] = Seq.empty
val b = a.map((value, encoder) => encoder.encode(value))
val b = a.map(ve => ve.encoder.encode(ve.value))
val c: Seq[String] = b
}