Skip to content

Commit 5807fef

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 0cdbff1 commit 5807fef

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
@@ -5053,6 +5053,11 @@ object Types {
50535053

50545054
/** Wildcard type, possibly with bounds */
50555055
abstract case class WildcardType(optBounds: Type) extends CachedGroundType with TermType {
5056+
5057+
def effectiveBounds(using Context): TypeBounds = optBounds match
5058+
case bounds: TypeBounds => bounds
5059+
case _ => TypeBounds.empty
5060+
50565061
def derivedWildcardType(optBounds: Type)(using Context): WildcardType =
50575062
if (optBounds eq this.optBounds) this
50585063
else if (!optBounds.exists) WildcardType

0 commit comments

Comments
 (0)