Skip to content

Commit 7b4fd8b

Browse files
committed
Avoid wildcard types in constraints
Ther semantics is fishy. Instead, use avoidance in necessarySubType and in TypeVarsMiss context mode, and use fresh type variables elsehwere.
1 parent df94b5b commit 7b4fd8b

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Flags._
1010
import config.Config
1111
import config.Printers.typr
1212
import reporting.trace
13+
import typer.ProtoTypes.newTypeVar
1314
import StdNames.tpnme
1415

1516
/** Methods for adding constraints and solving them.
@@ -102,10 +103,12 @@ trait ConstraintHandling {
102103
val dropWildcards = new ApproximatingTypeMap:
103104
if !isUpper then variance = -1
104105
def apply(t: Type): Type = t match
105-
case t: WildcardType if !allowWildcards =>
106-
t.optBounds match
107-
case TypeBounds(lo, hi) => range(lo, hi)
108-
case _ => range(defn.NothingType, defn.AnyType)
106+
case t: WildcardType =>
107+
if !allowWildcards || ctx.mode.is(Mode.TypevarsMissContext) then
108+
val bounds = t.effectiveBounds
109+
range(bounds.lo, bounds.hi)
110+
else
111+
newTypeVar(t.effectiveBounds)
109112
case _ =>
110113
mapOver(t)
111114
// Narrow one of the bounds of type parameter `param`

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5051,6 +5051,11 @@ object Types {
50515051

50525052
/** Wildcard type, possibly with bounds */
50535053
abstract case class WildcardType(optBounds: Type) extends CachedGroundType with TermType {
5054+
5055+
def effectiveBounds(using Context): TypeBounds = optBounds match
5056+
case bounds: TypeBounds => bounds
5057+
case _ => TypeBounds.empty
5058+
50545059
def derivedWildcardType(optBounds: Type)(using Context): WildcardType =
50555060
if (optBounds eq this.optBounds) this
50565061
else if (!optBounds.exists) WildcardType

0 commit comments

Comments
 (0)