Skip to content

Commit 9a3b024

Browse files
committed
not constrain if formal contains instantiated type vars
1 parent 111e4a0 commit 9a3b024

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ object Inferencing {
2727
* but only if the overall result of `isFullyDefined` is `true`.
2828
* Variables that are successfully minimized do not count as uninstantiated.
2929
*/
30-
def isFullyDefined(tp: Type, force: ForceDegree.Value, ifProto: Boolean = false)(using Context): Boolean =
31-
withFreshTyperState(new IsFullyDefinedAccumulator(force, ifProto = ifProto).process(tp), x => x)
30+
def isFullyDefined(tp: Type, force: ForceDegree.Value)(using Context): Boolean =
31+
withFreshTyperState(new IsFullyDefinedAccumulator(force).process(tp), x => x)
3232

3333
/** Try to fully define `tp`. Return whether constraint has changed.
3434
* Any changed constraint is kept.
@@ -161,7 +161,7 @@ object Inferencing {
161161
* Instance types can be improved by replacing covariant occurrences of Nothing
162162
* with fresh type variables, if `force` allows this in its `canImprove` implementation.
163163
*/
164-
private class IsFullyDefinedAccumulator(force: ForceDegree.Value, minimizeSelected: Boolean = false, ifProto: Boolean = false)
164+
private class IsFullyDefinedAccumulator(force: ForceDegree.Value, minimizeSelected: Boolean = false)
165165
(using Context) extends TypeAccumulator[Boolean] {
166166

167167
/** Replace toplevel-covariant occurrences (i.e. covariant without double flips)
@@ -233,10 +233,8 @@ object Inferencing {
233233
val tpd = tp.dealias
234234
if tpd ne tp then apply(x, tpd)
235235
else tp match
236-
case _: WildcardType =>
236+
case _: WildcardType | _: ProtoType =>
237237
false
238-
case tp: ProtoType =>
239-
ifProto && foldOver(x, tp)
240238
case tvar: TypeVar if !tvar.isInstantiated =>
241239
force.appliesTo(tvar)
242240
&& ctx.typerState.constraint.contains(tvar)

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import transform.CheckUnused.OriginalName
5454
import scala.annotation.{unchecked as _, *}
5555
import dotty.tools.dotc.util.chaining.*
5656
import dotty.tools.dotc.ast.untpd.Mod
57-
import dotty.tools.dotc.reporting.Reporter.NoReporter
5857

5958
object Typer {
6059

@@ -4358,33 +4357,25 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
43584357
else formals1
43594358
implicitArgs(formals2, argIndex + 1, pt)
43604359

4361-
4362-
def doesntContainsWildcards = {
4363-
val newCtx = ctx.fresh.setNewScope.setReporter(new reporting.ThrowingReporter(NoReporter))
4364-
val substCtxMap = new TypeMap():
4365-
def apply(tp: Type): Type = tp match
4366-
case tp: FunProto => mapOver(
4367-
tp.derivedFunProto(resultType = tp.resultType(using newCtx)).withContext(newCtx)
4368-
)
4369-
case tp => mapOver(tp)
4370-
val pt1 = substCtxMap(pt.deepenProtoTrans(using newCtx))
4371-
try {
4372-
!pt1.containsWildcardTypes(using newCtx)
4373-
} catch {
4374-
case _: UnhandledError => false
4375-
}
4376-
}
43774360
val pt1 = pt.deepenProtoTrans
4361+
val approxPt = withMode(Mode.TypevarsMissContext):
4362+
wildApprox(pt1)
4363+
var formalConstrained = false
4364+
val tm = new TypeMap:
4365+
def apply(t: Type): Type = t match
4366+
case tvar: TypeVar =>
4367+
formalConstrained |= ctx.typerState.constraint.contains(tvar) || tvar.instanceOpt.isInstanceOf[TypeVar]
4368+
tvar
4369+
case _ =>
4370+
if formalConstrained then t
4371+
else mapOver(t)
4372+
tm(formal)
43784373
if (pt1 `ne` pt)
43794374
&& (pt1 ne sharpenedPt)
4380-
&& formal.typeSymbol != defn.ClassTagClass
4375+
&& (AvoidWildcardsMap()(approxPt) `eq` approxPt)
43814376
&& !isFullyDefined(formal, ForceDegree.none)
4382-
&& !formal.existsPart(ty => {
4383-
val dty = ty.dealias
4384-
(dty ne ty) && ty.isInstanceOf[TypeVar] && dty.isInstanceOf[TypeVar]
4385-
}, StopAt.Static, forceLazy = false)
4386-
&& doesntContainsWildcards then
4387-
withoutMode(Mode.ImplicitsEnabled)(constrainResult(tree.symbol, wtp, wildApprox(pt1)))
4377+
&& !formalConstrained then
4378+
constrainResult(tree.symbol, wtp, pt1)
43884379
val arg = inferImplicitArg(formal, tree.span.endPos)
43894380

43904381
def canProfitFromMoreConstraints =

0 commit comments

Comments
 (0)