Skip to content

Commit 8d73b15

Browse files
committed
Avoid widening into unreducible types when inferring types
This is a general improvement, independent of named tuples.
1 parent 052a5bc commit 8d73b15

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2360,7 +2360,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
23602360
* @param canConstrain If true, new constraints might be added to simplify the lub.
23612361
* @param isSoft If the lub is a union, this determines whether it's a soft union.
23622362
*/
2363-
def lub(tp1: Type, tp2: Type, canConstrain: Boolean = false, isSoft: Boolean = true): Type = /*>|>*/ trace(s"lub(${tp1.show}, ${tp2.show}, canConstrain=$canConstrain, isSoft=$isSoft)", subtyping, show = true) /*<|<*/ {
2363+
def lub(tp1: Type, tp2: Type, canConstrain: Boolean = false, isSoft: Boolean = true): Type = trace(s"lub(${tp1.show}, ${tp2.show}, canConstrain=$canConstrain, isSoft=$isSoft)", subtyping, show = true) {
23642364
if (tp1 eq tp2) tp1
23652365
else if !tp1.exists || (tp2 eq WildcardType) then tp1
23662366
else if !tp2.exists || (tp1 eq WildcardType) then tp2

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,12 @@ object TypeOps:
390390
(tp.tp1.dealias, tp.tp2.dealias) match
391391
case (tp1 @ AppliedType(tycon1, args1), tp2 @ AppliedType(tycon2, args2))
392392
if tycon1.typeSymbol == tycon2.typeSymbol && (tycon1 =:= tycon2) =>
393-
mergeRefinedOrApplied(tp1, tp2)
393+
mergeRefinedOrApplied(tp1, tp2) match
394+
case tp: AppliedType if tp.isUnreducibleWild =>
395+
// fall back to or-dominators rather tahn inferring a type that would
396+
// caue an unreducible type error later.
397+
approximateOr(tp1, tp2)
398+
case tp => tp
394399
case (tp1, tp2) =>
395400
approximateOr(tp1, tp2)
396401
case _ =>

tests/pos/named-tuple-widen.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import language.experimental.namedTuples
2+
3+
class A
4+
class B
5+
val y1: (a1: A, b1: B) = ???
6+
val y2: (a2: A, b2: B) = ???
7+
var z1 = if ??? then y1 else y2 // -- what is the type of z2
8+
var z2: NamedTuple.AnyNamedTuple = z1
9+
val _ = z1 = z2

0 commit comments

Comments
 (0)