Skip to content

Commit acd07c1

Browse files
committed
Simplify and fix avoid logic
The previous formulation broke for named parameters. Test case in flowops1.scala.
1 parent 3f231ab commit acd07c1

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -841,14 +841,6 @@ object Types {
841841
case _ => this
842842
}
843843

844-
/** If this is a refinement type, the unrefined parent,
845-
* else the type itself.
846-
*/
847-
final def unrefine(implicit ctx: Context): Type = stripTypeVar match {
848-
case tp @ RefinedType(tycon, _) => tycon.unrefine
849-
case _ => this
850-
}
851-
852844
/** If this is a (possibly aliased, annotated, and/or parameterized) reference to
853845
* a class, the class type ref, otherwise NoType.
854846
* @param refinementOK If `true` we also skip non-parameter refinements.

src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import core._
66
import ast._
77
import Scopes._, Contexts._, Constants._, Types._, Symbols._, Names._, Flags._, Decorators._
88
import ErrorReporting._, Annotations._, Denotations._, SymDenotations._, StdNames._, TypeErasure._
9+
import TypeApplications.AppliedType
910
import util.Positions._
1011
import config.Printers._
1112
import ast.Trees._
@@ -93,27 +94,17 @@ trait TypeAssigner {
9394
case _ =>
9495
mapOver(tp)
9596
}
97+
case tp @ AppliedType(tycon, args) if toAvoid(tycon) =>
98+
val base = apply(tycon)
99+
apply(base.appliedTo(tp.baseArgInfos(base.typeSymbol)))
96100
case tp @ RefinedType(parent, name) if variance > 0 =>
97-
// The naive approach here would be to first approximate the parent,
98-
// but if the base type of the approximated parent is different from
99-
// the current base type, then the current refinement won't be valid
100-
// if it's a type parameter refinement.
101-
// Therefore we first approximate the base type, then use `baseArgInfos`
102-
// to get correct refinements for the approximated base type, then
103-
// recursively approximate the resulting type.
104-
val base = tp.unrefine
105-
if (toAvoid(base)) {
106-
val base1 = apply(base)
107-
apply(base1.appliedTo(tp.baseArgInfos(base1.typeSymbol)))
101+
val parent1 = apply(tp.parent)
102+
val refinedInfo1 = apply(tp.refinedInfo)
103+
if (toAvoid(refinedInfo1)) {
104+
typr.println(s"dropping refinement from $tp")
105+
parent1
108106
} else {
109-
val parent1 = apply(tp.parent)
110-
val refinedInfo1 = apply(tp.refinedInfo)
111-
if (toAvoid(refinedInfo1)) {
112-
typr.println(s"dropping refinement from $tp")
113-
parent1
114-
} else {
115-
tp.derivedRefinedType(parent1, name, refinedInfo1)
116-
}
107+
tp.derivedRefinedType(parent1, name, refinedInfo1)
117108
}
118109
case tp: TypeVar if ctx.typerState.constraint.contains(tp) =>
119110
val lo = ctx.typerState.constraint.fullLowerBound(tp.origin)

tests/pos/flowops1.scala

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
object Test {
2+
class NotUsed
3+
4+
trait FO[type +Out, type +Mat] { self =>
5+
type Repr <: FO[Mat = self.Mat] {
6+
type Repr = self.Repr
7+
}
8+
def map[T](f: Out => T): Repr[Out = T] = ???
9+
}
10+
11+
class Source[type +Out, type +Mat] extends FO[Out, Mat] {
12+
type Repr <: Source[Out, Mat]
13+
}
14+
15+
class Flow[type -In, type +Out, type +Mat] extends FO[Out, Mat] {
16+
type Repr <: Flow[In, Out, Mat]
17+
}
18+
19+
implicit class x[O, M, F <: FO](val f: F[Out = O, Mat = M]) extends AnyVal {
20+
def xx(i: Int): f.Repr[Out = O] = f.map(identity)
21+
}
22+
23+
val s1 = new Source[Int, NotUsed].xx(12)
24+
val s2: Source[Int, NotUsed] = s1
25+
val f1 = x[Int, NotUsed, Flow[In = Int]](new Flow[Int, Int, NotUsed]).xx(12)
26+
val f2: Flow[Int, Int, NotUsed] = f1
27+
28+
29+
val f3 = x(new Flow[Int, Int, NotUsed]).xx(12)
30+
val f4: Flow[Int, Int, NotUsed] = f3
31+
val f5 = new Flow[Int, Int, NotUsed].xx(12)
32+
val f6: Flow[Int, Int, NotUsed] = f5
33+
}

0 commit comments

Comments
 (0)