Skip to content

Commit 9997371

Browse files
committed
Allow qualified types with implicit argument name in patterns
1 parent 9850097 commit 9997371

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ object desugar {
234234
else vdef1
235235
end valDef
236236

237+
def caseDef(cdef: CaseDef)(using Context): CaseDef =
238+
if Feature.qualifiedTypesEnabled then
239+
val CaseDef(pat, guard, body) = cdef
240+
val pat1 = DesugarQualifiedTypesInPatternMap().transform(pat)
241+
cpy.CaseDef(cdef)(pat1, guard, body)
242+
else
243+
cdef
244+
237245
def mapParamss(paramss: List[ParamClause])
238246
(mapTypeParam: TypeDef => TypeDef)
239247
(mapTermParam: ValDef => ValDef)(using Context): List[ParamClause] =
@@ -2487,6 +2495,14 @@ object desugar {
24872495
else
24882496
tpt
24892497

2498+
private class DesugarQualifiedTypesInPatternMap extends UntypedTreeMap:
2499+
override def transform(tree: Tree)(using Context): Tree =
2500+
tree match
2501+
case Typed(ident @ Ident(name: TermName), tpt) =>
2502+
cpy.Typed(tree)(ident, desugarQualifiedTypes(tpt, name))
2503+
case _ =>
2504+
super.transform(tree)
2505+
24902506
/** Returns the annotated type used to represent the qualified type with the
24912507
* given components:
24922508
* `parent @qualified[parent]((paramName: parent) => qualifier)`.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2267,9 +2267,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
22672267
}
22682268

22692269
/** Type a case. */
2270-
def typedCase(tree: untpd.CaseDef, sel: Tree, wideSelType: Type, pt: Type)(using Context): CaseDef = {
2270+
def typedCase(tree0: untpd.CaseDef, sel: Tree, wideSelType: Type, pt: Type)(using Context): CaseDef = {
22712271
val originalCtx = ctx
22722272
val gadtCtx: Context = ctx.fresh.setFreshGADTBounds
2273+
val tree = desugar.caseDef(tree0)
22732274

22742275
def caseRest(pat: Tree)(using Context) = {
22752276
val pt1 = instantiateMatchTypeProto(pat, pt) match {

tests/printing/qualified-types.check

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,32 @@ package example {
7979
Int @qualified[Int]((x8: Int) => x8 > 0)]((
8080
x8: Int @qualified[Int]((x8: Int) => x8 > 0)) => x7 < 10)
8181
= ???
82-
()
82+
val x9: Any = 42
83+
x9 match
84+
{
85+
case y @ _:Int @qualified[Int]((y: Int) => y > 0) =>
86+
println(
87+
_root_.scala.StringContext.apply(
88+
[""," is Int with x > 0" : String]*).s([y : Any]*)
89+
)
90+
case _ =>
91+
()
92+
}
93+
x9 match
94+
{
95+
case
96+
Tuple2.unapply[Any, Any](
97+
y @ _:Int @qualified[Int]((y: Int) => y > 0),
98+
z @ _:Int @qualified[Int]((z: Int) => z > 0)):(Any, Any)
99+
=>
100+
println(
101+
_root_.scala.StringContext.apply(
102+
[""," is Int with y > 0 and "," is Int with z > 0" : String]*)
103+
.s([y,z : Any]*)
104+
)
105+
case _ =>
106+
()
107+
}
83108
}
84109
def bar(x: Int @qualified[Int]((x: Int) => x > 0)): Nothing = ???
85110
def secondGreater1(x: Int, y: Int)(z: Int @qualified[Int]((w: Int) => x > y)

tests/printing/qualified-types.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ def implicitArgumentName() =
4949
val x7: (Int with x7 > 0) with x6 < 10 = ???
5050
val x8: ((Int with x8 > 0) with x7 < 10) = ???
5151

52+
val x9: Any = 42
53+
x9 match
54+
case y: Int with y > 0 =>
55+
println(s"$y is Int with x > 0")
56+
case _ => ()
57+
58+
x9 match
59+
case (y: Int with y > 0, z: Int with z > 0) =>
60+
println(s"$y is Int with y > 0 and $z is Int with z > 0")
61+
case _ => ()
62+
5263
def bar(x: Int with x > 0) = ???
5364
def secondGreater1(x: Int, y: Int)(z: {w: Int with x > y}) = ???
5465
def secondGreater2(x: Int, y: Int)(z: Int with x > y) = ???

0 commit comments

Comments
 (0)