Skip to content

Commit 8671e84

Browse files
committed
Fix calleeType
This one will bring back previously fixed regressions that have to be looked at separately.
1 parent ba45875 commit 8671e84

File tree

8 files changed

+118
-7
lines changed

8 files changed

+118
-7
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,17 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
18761876
*/
18771877
var paramIndex = Map[Name, Int]()
18781878

1879+
def containsParamRef(tree: untpd.Tree, params: List[untpd.ValDef]): Boolean =
1880+
import untpd.*
1881+
val acc = new UntypedTreeAccumulator[Boolean]:
1882+
def apply(x: Boolean, t: Tree)(using Context) =
1883+
if x then true
1884+
else t match
1885+
case _: untpd.TypedSplice => false
1886+
case Ident(name) => params.exists(_.name == name)
1887+
case _ => foldOver(x, t)
1888+
acc(false, tree)
1889+
18791890
/** Infer parameter type from the body of the function
18801891
*
18811892
* 1. If function is of the form
@@ -1910,24 +1921,25 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
19101921
for (param <- params; idx <- paramIndices(param, args))
19111922
yield param.name -> idx
19121923
}.toMap
1913-
if (paramIndex.size == params.length)
1924+
if (paramIndex.size == params.length) then
19141925
expr match
19151926
case untpd.TypedSplice(expr1) =>
19161927
expr1.tpe
1917-
case _ =>
1928+
case _ if !containsParamRef(expr, params) =>
19181929
val outerCtx = ctx
19191930
val nestedCtx = outerCtx.fresh.setNewTyperState()
1920-
inContext(nestedCtx) {
1921-
val protoArgs = args.map(_.withType(WildcardType))
1931+
inContext(nestedCtx):
1932+
// try to type expr with fresh unknown arguments.
1933+
val protoArgs = args.map(arg => untpd.Ident(UniqueName.fresh()).withSpan(arg.span))
19221934
val callProto = FunProto(protoArgs, WildcardType)(this, app.applyKind)
19231935
val expr1 = typedExpr(expr, callProto)
19241936
if nestedCtx.reporter.hasErrors then NoType
1925-
else inContext(outerCtx) {
1937+
else inContext(outerCtx):
19261938
nestedCtx.typerState.commit()
19271939
fnBody = cpy.Apply(fnBody)(untpd.TypedSplice(expr1), args)
19281940
expr1.tpe
1929-
}
1930-
}
1941+
case _ =>
1942+
NoType
19311943
else NoType
19321944
case _ =>
19331945
NoType

tests/pos/i12679.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ object Example:
77

88
def example[F[_]](maybeQux: Option[String], bool: Boolean) =
99
maybeQux.fold(foo[F](bool))(foo[F](_))
10+
11+
object Example2:
12+
def foo(qux: String, quux: String = ""): Unit = ???
13+
14+
def foo(qux: Boolean): Unit = ???
15+
16+
def example(maybeQux: Option[String], bool: Boolean) =
17+
maybeQux.fold(foo(bool))(s => foo(s))

tests/pos/i24686.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def Test = Seq.empty[DenseMatrix[Double]].reduce(DenseMatrix.horzcat(_, _))
2+
3+
trait Matrix[T]
4+
trait DenseMatrix[T] extends Matrix[T]
5+
6+
object DenseMatrix:
7+
def horzcat[M, V](matrices: M*)(using OpSet.InPlaceImpl2[DenseMatrix[V], M]): DenseMatrix[V] = ???
8+
9+
object OpSet extends HasOps:
10+
trait InPlaceImpl2[V1, V2]
11+
12+
trait HasOps
13+
object HasOps extends DenseMatrixExpandedOps with DensMatrixLowPriority
14+
15+
trait DenseMatrixExpandedOps:
16+
given OpSet.InPlaceImpl2[DenseMatrix[Double], DenseMatrix[Double]] = ???
17+
18+
trait DensMatrixLowPriority extends LowPriorityDenseMatrix1
19+
trait LowPriorityDenseMatrix1:
20+
given [V]: OpSet.InPlaceImpl2[DenseMatrix[V], Matrix[V]] = ???

tests/pos/i24689a.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.io.*
2+
import java.nio.file.*
3+
4+
abstract class Using[Source, A]:
5+
def apply[R](src: Source)(f: A => R): R = ???
6+
7+
object Using:
8+
val fileInputStream: Using[Path, InputStream] = ???
9+
10+
def transfer(in: Path, out: OutputStream): Unit =
11+
Using.fileInputStream(in)(in => transfer(in, out))
12+
13+
def transfer(in: InputStream, out: OutputStream): Unit = ???

tests/pos/i24689b.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
trait A
2+
trait B
3+
4+
object Test1:
5+
def foo(f: B => Unit) = ???
6+
def transfer(in: A): Unit =
7+
foo(in => transfer(in))
8+
foo(transfer)
9+
foo(transfer(_))
10+
def transfer(in: B): Unit = ???
11+
12+
object Test2:
13+
def foo[T <: (B => Unit)](f: T) = ???
14+
def transfer(in: A): Unit =
15+
foo(in => transfer(in))
16+
foo(transfer)
17+
foo(transfer(_))
18+
def transfer(in: B): Unit = ???

tests/pos/i24694/A_1.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait Show[T]
2+
object Show:
3+
given [A: Show]: Show[List[A]] = ???
4+
5+
trait Eq[A] extends Any, Serializable
6+
object Eq:
7+
def fromUniversalEquals[A]: Eq[A] = ???
8+
9+
object expect:
10+
def same[A](expected: A, found: A)(using
11+
eqA: Eq[A] = Eq.fromUniversalEquals[A],
12+
showA: Show[A]
13+
): Unit = ???
14+
15+
sealed trait XmlEvent
16+
object XmlEvent:
17+
given Show[XmlEvent] = ???

tests/pos/i24694/B_2.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def test(input: Option[List[XmlEvent]]) =
2+
val works =
3+
expect.same(List.empty[XmlEvent], input.get)
4+
val fails = input.map: tokens =>
5+
expect.same(List.empty[XmlEvent], tokens)

tests/pos/i24696.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
trait Schema[A]
2+
object Schema:
3+
final case class Either[A, B](left: Schema[A], right: Schema[B]) extends Schema[scala.util.Either[A, B]]
4+
trait DecodeError
5+
6+
object DynamicValue:
7+
final case class LeftValue(value: DynamicValue) extends DynamicValue
8+
final case class RightValue(value: DynamicValue) extends DynamicValue
9+
10+
sealed trait DynamicValue:
11+
self =>
12+
def toTypedValueLazyError[A](using schema: Schema[A]): Either[DecodeError, A] =
13+
(self, schema) match
14+
case (DynamicValue.LeftValue(value), Schema.Either(schema1, _)) =>
15+
value.toTypedValueLazyError(using schema1).map(Left(_))
16+
case (DynamicValue.RightValue(value), Schema.Either(_, schema1)) =>
17+
value.toTypedValueLazyError(using schema1).map(Right(_))
18+
case _ => ???

0 commit comments

Comments
 (0)