Skip to content

Commit f9db9fa

Browse files
committed
Add a run test for poly context bounds; cleanup typer changes
1 parent f292ac5 commit f9db9fa

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import parsing.Parsers
2222

2323
import scala.annotation.internal.sharable
2424
import scala.annotation.threadUnsafe
25-
import dotty.tools.dotc.quoted.QuoteUtils.treeOwner
2625

2726
object desugar {
2827
import untpd.*

compiler/src/dotty/tools/dotc/typer/Typer.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -1945,15 +1945,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
19451945
val resultTpt =
19461946
untpd.InLambdaTypeTree(isResult = true, (tsyms, vsyms) =>
19471947
mt.resultType.substParams(mt, vsyms.map(_.termRef)).substParams(poly, tsyms.map(_.typeRef)))
1948-
val desugared @ Block(List(defdef), _) = desugar.makeClosure(tparams, inferredVParams, body, resultTpt, tree.span)
1948+
val desugared = desugar.makeClosure(tparams, inferredVParams, body, resultTpt, tree.span)
19491949
typed(desugared, pt)
19501950
else
19511951
val msg =
19521952
em"""|Provided polymorphic function value doesn't match the expected type $dpt.
19531953
|Expected type should be a polymorphic function with the same number of type and value parameters."""
19541954
errorTree(EmptyTree, msg, tree.srcPos)
19551955
case _ =>
1956-
val desugared @ Block(List(defdef), _) = desugar.makeClosure(tparams, vparams, body, untpd.TypeTree(), tree.span)
1956+
val desugared = desugar.makeClosure(tparams, vparams, body, untpd.TypeTree(), tree.span)
19571957
typed(desugared, pt)
19581958
end typedPolyFunctionValue
19591959

@@ -3581,17 +3581,17 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
35813581
case xtree => typedUnnamed(xtree)
35823582

35833583
val unsimplifiedType = result.tpe
3584-
val result1 = simplify(result, pt, locked)
3585-
result1.tpe.stripTypeVar match
3584+
simplify(result, pt, locked)
3585+
result.tpe.stripTypeVar match
35863586
case e: ErrorType if !unsimplifiedType.isErroneous => errorTree(xtree, e.msg, xtree.srcPos)
3587-
case _ => result1
3587+
case _ => result
35883588
catch case ex: TypeError =>
35893589
handleTypeError(ex)
35903590
}
35913591
}
35923592

35933593
/** Interpolate and simplify the type of the given tree. */
3594-
protected def simplify(tree: Tree, pt: Type, locked: TypeVars)(using Context): Tree =
3594+
protected def simplify(tree: Tree, pt: Type, locked: TypeVars)(using Context): tree.type =
35953595
if !tree.denot.isOverloaded then // for overloaded trees: resolve overloading before simplifying
35963596
if !tree.tpe.widen.isInstanceOf[MethodOrPoly] // wait with simplifying until method is fully applied
35973597
|| tree.isDef // ... unless tree is a definition
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
42
2+
a string
3+
Kate is 27 years old
4+
42 and a string
5+
a string and Kate is 27 years old
6+
Kate is 27 years old and 42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import scala.language.experimental.modularity
2+
import scala.language.future
3+
4+
trait Show[X]:
5+
def show(x: X): String
6+
7+
given Show[Int] with
8+
def show(x: Int) = x.toString
9+
10+
given Show[String] with
11+
def show(x: String) = x
12+
13+
case class Person(name: String, age: Int)
14+
15+
given Show[Person] with
16+
def show(x: Person) = s"${x.name} is ${x.age} years old"
17+
18+
type Shower = [X: Show] => X => String
19+
val shower: Shower = [X: {Show as show}] => (x: X) => show.show(x)
20+
21+
type DoubleShower = [X: Show] => X => [Y: Show] => Y => String
22+
val doubleShower: DoubleShower = [X: {Show as show1}] => (x: X) => [Y: {Show as show2}] => (y: Y) => s"${show1.show(x)} and ${show2.show(y)}"
23+
24+
object Test extends App:
25+
println(shower(42))
26+
println(shower("a string"))
27+
println(shower(Person("Kate", 27)))
28+
println(doubleShower(42)("a string"))
29+
println(doubleShower("a string")(Person("Kate", 27)))
30+
println(doubleShower(Person("Kate", 27))(42))

0 commit comments

Comments
 (0)