Skip to content

Commit 1de5b3e

Browse files
KacperFKorbanWojciechMazur
authored andcommitted
Add a run test for poly context bounds; cleanup typer changes
[Cherry-picked f9db9fa]
1 parent 44577f6 commit 1de5b3e

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

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

Lines changed: 0 additions & 1 deletion
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

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

@@ -3578,17 +3578,17 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
35783578
case xtree => typedUnnamed(xtree)
35793579

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

35903590
/** Interpolate and simplify the type of the given tree. */
3591-
protected def simplify(tree: Tree, pt: Type, locked: TypeVars)(using Context): Tree =
3591+
protected def simplify(tree: Tree, pt: Type, locked: TypeVars)(using Context): tree.type =
35923592
if !tree.denot.isOverloaded then // for overloaded trees: resolve overloading before simplifying
35933593
if !tree.tpe.widen.isInstanceOf[MethodOrPoly] // wait with simplifying until method is fully applied
35943594
|| tree.isDef // ... unless tree is a definition
Lines changed: 6 additions & 0 deletions
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
Lines changed: 30 additions & 0 deletions
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)