Skip to content

Commit 298ff3f

Browse files
Workaround case x @ U() : T => (fix main branch) (#16369)
This kind of patterns were disabled in #16150. Fixes #16367
2 parents a1729b0 + a01248f commit 298ff3f

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

tests/pos-with-compiler-cc/dotc/core/Types.scala

+9-5
Original file line numberDiff line numberDiff line change
@@ -1289,11 +1289,15 @@ object Types {
12891289
* then the top-level union isn't widened. This is needed so that type inference can infer nullable types.
12901290
*/
12911291
def widenUnion(using Context): Type = widen match
1292-
case tp @ OrNull(tp1): OrType =>
1293-
// Don't widen `T|Null`, since otherwise we wouldn't be able to infer nullable unions.
1294-
val tp1Widen = tp1.widenUnionWithoutNull
1295-
if (tp1Widen.isRef(defn.AnyClass)) tp1Widen
1296-
else tp.derivedOrType(tp1Widen, defn.NullType)
1292+
case tp @ OrNull(tp1) =>
1293+
tp match
1294+
case tp: OrType =>
1295+
// Don't widen `T|Null`, since otherwise we wouldn't be able to infer nullable unions.
1296+
val tp1Widen = tp1.widenUnionWithoutNull
1297+
if (tp1Widen.isRef(defn.AnyClass)) tp1Widen
1298+
else tp.derivedOrType(tp1Widen, defn.NullType)
1299+
case _ =>
1300+
tp.widenUnionWithoutNull
12971301
case tp =>
12981302
tp.widenUnionWithoutNull
12991303

tests/pos-with-compiler-cc/dotc/typer/Typer.scala

+11-7
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,17 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
477477
* (x: T | Null) => x.$asInstanceOf$[x.type & T]
478478
*/
479479
def toNotNullTermRef(tree: Tree, pt: Type)(using Context): Tree = tree.tpe match
480-
case ref @ OrNull(tpnn) : TermRef
481-
if pt != AssignProto && // Ensure it is not the lhs of Assign
482-
ctx.notNullInfos.impliesNotNull(ref) &&
483-
// If a reference is in the context, it is already trackable at the point we add it.
484-
// Hence, we don't use isTracked in the next line, because checking use out of order is enough.
485-
!ref.usedOutOfOrder =>
486-
tree.cast(AndType(ref, tpnn))
480+
case tp @ OrNull(tpnn) =>
481+
tp match
482+
case ref: TermRef
483+
if pt != AssignProto && // Ensure it is not the lhs of Assign
484+
ctx.notNullInfos.impliesNotNull(ref) &&
485+
// If a reference is in the context, it is already trackable at the point we add it.
486+
// Hence, we don't use isTracked in the next line, because checking use out of order is enough.
487+
!ref.usedOutOfOrder =>
488+
tree.cast(AndType(ref, tpnn))
489+
case _ =>
490+
tree
487491
case _ =>
488492
tree
489493

0 commit comments

Comments
 (0)