Skip to content

Commit bb7c8e2

Browse files
committed
When simplifying match types, ensure fully defined before reducing
Fixes #11977 Previously the match types were more often fully defined because inlining at Typer caused more situations where type variables were instantiated. Now that inlining is done later, we need to compensate. I believe it's a good idea anyway since it means we can reduce more match types.
1 parent f01c14d commit bb7c8e2

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ object TypeOps:
139139
case tp1 => tp1
140140
}
141141
case tp: AppliedType =>
142+
tp.tycon match
143+
case tycon: TypeRef if tycon.info.isInstanceOf[MatchAlias] =>
144+
isFullyDefined(tp, ForceDegree.all)
145+
case _ =>
142146
val normed = tp.tryNormalize
143147
if normed.exists then normed else tp.map(simplify(_, theMap))
144148
case tp: TypeParamRef =>

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,10 +1816,12 @@ object Types {
18161816
/** A simplified version of this type which is equivalent wrt =:= to this type.
18171817
* This applies a typemap to the type which (as all typemaps) follows type
18181818
* variable instances and reduces typerefs over refined types. It also
1819-
* re-evaluates all occurrences of And/OrType with &/| because
1820-
* what was a union or intersection of type variables might be a simpler type
1821-
* after the type variables are instantiated. Finally, it
1822-
* maps poly params in the current constraint set back to their type vars.
1819+
*
1820+
* - re-evaluates all occurrences of And/OrType with &/| because
1821+
* what was a union or intersection of type variables might be a simpler type
1822+
* after the type variables are instantiated.
1823+
* - maps poly params in the current constraint set back to their type vars.
1824+
* - forces match types to be fully defined and tries to normalize them.
18231825
*
18241826
* NOTE: Simplifying an intersection type might change its erasure (for
18251827
* example, the Java erasure of `Object & Serializable` is `Object`,

tests/pos/i11977.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Test:
2+
3+
def add0(a : (Int, String), b : (Int, String)): Int =
4+
val x = 3
5+
x + b(0)
6+
7+
def add(a : (Int, String), b : (Int, String)) : (Int, String) = (a(0) + b(0), a(1) + b(1))

0 commit comments

Comments
 (0)