Skip to content

Commit 39b9011

Browse files
committed
Make widenInferred work correctly for wildcard bounds
1 parent a7ef3e2 commit 39b9011

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,10 @@ trait ConstraintHandling[AbstractContext] {
293293

294294
/** Widen inferred type `inst` with upper `bound`, according to the following rules:
295295
* 1. If `inst` is a singleton type, or a union containing some singleton types,
296-
* widen (all) the singleton type(s), provied the result is a subtype of `bound`
296+
* widen (all) the singleton type(s), provided the result is a subtype of `bound`
297297
* (i.e. `inst.widenSingletons <:< bound` succeeds with satisfiable constraint)
298298
* 2. If `inst` is a union type, approximate the union type from above by an intersection
299-
* of all common base types, provied the result is a subtype of `bound`.
299+
* of all common base types, provided the result is a subtype of `bound`.
300300
*
301301
* Don't do these widenings if `bound` is a subtype of `scala.Singleton`.
302302
* Also, if the result of these widenings is a TypeRef to a module class,
@@ -309,15 +309,17 @@ trait ConstraintHandling[AbstractContext] {
309309
def widenInferred(inst: Type, bound: Type)(implicit actx: AbstractContext): Type = {
310310
def widenOr(tp: Type) = {
311311
val tpw = tp.widenUnion
312-
if ((tpw ne tp) && tpw <:< bound) tpw else tp
312+
if (tpw ne tp) && (tpw <:< bound) then tpw else tp
313313
}
314314
def widenSingle(tp: Type) = {
315315
val tpw = tp.widenSingletons
316-
if ((tpw ne tp) && tpw <:< bound) tpw else tp
316+
if (tpw ne tp) && (tpw <:< bound) then tpw else tp
317317
}
318+
def isSingleton(tp: Type): Boolean = tp match
319+
case WildcardType(optBounds) => optBounds.exists && isSingleton(optBounds.bounds.hi)
320+
case _ => isSubTypeWhenFrozen(tp, defn.SingletonType)
318321
val wideInst =
319-
if (isSubTypeWhenFrozen(bound, defn.SingletonType)) inst
320-
else widenOr(widenSingle(inst))
322+
if isSingleton(bound) then inst else widenOr(widenSingle(inst))
321323
wideInst match
322324
case wideInst: TypeRef if wideInst.symbol.is(Module) =>
323325
TermRef(wideInst.prefix, wideInst.symbol.sourceModule)

0 commit comments

Comments
 (0)