Skip to content

Commit 52b87c2

Browse files
Backport "Avoid stacked thisCall contexts" to LTS (#21130)
Backports #20488 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents d5b8193 + cfb98d9 commit 52b87c2

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ trait Implicits:
10401040
trace(s"search implicit ${pt.show}, arg = ${argument.show}: ${argument.tpe.show}", implicits, show = true) {
10411041
record("inferImplicit")
10421042
assert(ctx.phase.allowsImplicitSearch,
1043-
if (argument.isEmpty) i"missing implicit parameter of type $pt after typer at phase ${ctx.phase.phaseName}"
1043+
if (argument.isEmpty) i"missing implicit parameter of type $pt after typer at phase ${ctx.phase}"
10441044
else i"type error: ${argument.tpe} does not conform to $pt${err.whyNoMatchStr(argument.tpe, pt)}")
10451045

10461046
val usableForInference = pt.exists && !pt.unusableForInference

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

+17-10
Original file line numberDiff line numberDiff line change
@@ -3752,7 +3752,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
37523752

37533753
def dummyArg(tp: Type) = untpd.Ident(nme.???).withTypeUnchecked(tp)
37543754

3755-
def addImplicitArgs(using Context) = {
3755+
val origCtx = ctx
3756+
3757+
def addImplicitArgs(using Context) =
37563758
def hasDefaultParams = methPart(tree).symbol.hasDefaultParams
37573759
def implicitArgs(formals: List[Type], argIndex: Int, pt: Type): List[Tree] = formals match
37583760
case Nil => Nil
@@ -3872,15 +3874,20 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
38723874
case _ => retyped
38733875
else issueErrors(tree, args)
38743876
}
3875-
else tree match {
3876-
case tree: Block =>
3877-
readaptSimplified(tpd.Block(tree.stats, tpd.Apply(tree.expr, args)))
3878-
case tree: NamedArg =>
3879-
readaptSimplified(tpd.NamedArg(tree.name, tpd.Apply(tree.arg, args)))
3880-
case _ =>
3881-
readaptSimplified(tpd.Apply(tree, args))
3882-
}
3883-
}
3877+
else
3878+
inContext(origCtx):
3879+
// Reset context in case it was set to a supercall context before.
3880+
// otherwise the invariant for taking another this or super call context is not met.
3881+
// Test case is i20483.scala
3882+
tree match
3883+
case tree: Block =>
3884+
readaptSimplified(tpd.Block(tree.stats, tpd.Apply(tree.expr, args)))
3885+
case tree: NamedArg =>
3886+
readaptSimplified(tpd.NamedArg(tree.name, tpd.Apply(tree.arg, args)))
3887+
case _ =>
3888+
readaptSimplified(tpd.Apply(tree, args))
3889+
end addImplicitArgs
3890+
38843891
pt.revealIgnored match {
38853892
case pt: FunProto if pt.applyKind == ApplyKind.Using =>
38863893
// We can end up here if extension methods are called with explicit given arguments.

tests/pos/i20483.scala

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
class Foo
3+
(x: Option[String])
4+
(using Boolean)
5+
(using Int)
6+
(using Double):
7+
8+
def this
9+
(x: String)
10+
(using Boolean)
11+
(using Int)
12+
(using Double) =
13+
this(Some(x))

0 commit comments

Comments
 (0)