Skip to content

Commit 69f4aa2

Browse files
committed
Improve sanitize algorithm
The new algorithm systematically avoids references to pattern-generated variables. This leads to nicer types and fewer casts in practice. It also avoids the problem that a type refers to a variable that has been eliminated.
1 parent 2a73eee commit 69f4aa2

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,20 @@ object PatternMatcher {
9898
sym.is(Synthetic) &&
9999
(sym.is(Label) || sym.name.is(PatMatStdBinderName))
100100

101+
private val refersToInternal = new TypeAccumulator[Boolean] {
102+
def apply(x: Boolean, tp: Type) =
103+
x || {
104+
tp match {
105+
case tp: TermRef => isPatmatGenerated(tp.symbol)
106+
case _ => false
107+
}
108+
} || foldOver(x, tp)
109+
}
110+
101111
/** A type map that eliminates all patternmatcher-generated termrefs that
102112
* can be replaced by a source-level alias.
103113
*/
104-
private val sanitize = new TypeMap {
114+
private val sanitize2 = new TypeMap {
105115
def apply(t: Type): Type = t.widenExpr match {
106116
case t: TermRef if isPatmatGenerated(t.symbol) =>
107117
t.info.widenExpr match {
@@ -112,6 +122,11 @@ object PatternMatcher {
112122
}
113123
}
114124

125+
private def sanitize(tp: Type): Type = tp.widenExpr match {
126+
case tp: TermRef if refersToInternal(false, tp) => sanitize(tp.underlying)
127+
case tp => tp
128+
}
129+
115130
// ------- Plan and test types ------------------------
116131

117132
/** Counter to display plans nicely, for debugging */

0 commit comments

Comments
 (0)