Skip to content

Commit 11a3f9f

Browse files
author
gorilskij
committed
add test case
1 parent 1ca06a4 commit 11a3f9f

File tree

2 files changed

+174
-7
lines changed

2 files changed

+174
-7
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ object Inliner {
196196
// as its right hand side. The call to the wrapper unapply serves as the signpost for pattern matching.
197197
// After pattern matching, the anonymous class is removed in phase InlinePatterns with a beta reduction step.
198198
//
199-
// An inline unapply `P.unapply` in a plattern `P(x1,x2,...)` is transformed into
199+
// An inline unapply `P.unapply` in a pattern `P(x1,x2,...)` is transformed into
200200
// `{ class $anon { def unapply(t0: T0)(using t1: T1, t2: T2, ...): R = P.unapply(t0)(using t1, t2, ...) }; new $anon }.unapply`
201201
// and the call `P.unapply(x1, x2, ...)` is inlined.
202202
// This serves as a placeholder for the inlined body until the `patternMatcher` phase. After pattern matcher
@@ -211,24 +211,22 @@ object Inliner {
211211
val targs = fun match
212212
case TypeApply(_, targs) => targs
213213
case _ => Nil
214-
215214
val unapplyInfo = sym.info match
216215
case info: PolyType => info.instantiate(targs.map(_.tpe)) match
217216
case MethodTpe(_, _, rt: PolyType) => rt.instantiate(targs.map(_.tpe))
218217
case MethodTpe(_, _, rt) if sym.flags.is(ExtensionMethod) => rt
219218
case info => info
220-
221219
case MethodTpe(_, _, rt: PolyType) => rt.instantiate(targs.map(_.tpe))
222220
case MethodTpe(_, _, rt) if sym.flags.is(ExtensionMethod) => rt
223221
case info => info
224222

225-
val unappplySym = newSymbol(cls, sym.name.toTermName, Synthetic | Method, unapplyInfo, coord = sym.coord).entered
226-
val unapply = DefDef(unappplySym, argss =>
227-
inlineCall(fun.appliedToArgss(argss).withSpan(unapp.span))(using ctx.withOwner(unappplySym))
223+
val unapplySym = newSymbol(cls, sym.name.toTermName, Synthetic | Method, unapplyInfo, coord = sym.coord).entered
224+
val unapply = DefDef(unapplySym, argss =>
225+
inlineCall(fun.appliedToArgss(argss).withSpan(unapp.span))(using ctx.withOwner(unapplySym))
228226
)
229227
val cdef = ClassDef(cls, DefDef(constr), List(unapply))
230228
val newUnapply = Block(cdef :: Nil, New(cls.typeRef, Nil))
231-
val newFun = newUnapply.select(unappplySym).withSpan(unapp.span)
229+
val newFun = newUnapply.select(unapplySym).withSpan(unapp.span)
232230
cpy.UnApply(unapp)(newFun, implicits, patterns)
233231
}
234232

tests/pos/i8577.scala

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
def main: Unit = {
2+
{
3+
object A1:
4+
def unapplySeq(x: Int) = Some(Seq(x))
5+
6+
val A1(x) = 1
7+
println(s"A1: $x")
8+
}
9+
10+
{
11+
object A2:
12+
def unapplySeq[T](x: T) = Some(Seq(x))
13+
14+
val A2(x) = 2
15+
println(s"A2: $x")
16+
}
17+
18+
{
19+
object B1:
20+
inline def unapplySeq(x: Int) = Some(Seq(x))
21+
22+
val B1(x) = 3
23+
println(s"B1: $x")
24+
}
25+
26+
{
27+
object B2:
28+
inline def unapplySeq[T](x: T) = Some(Seq(x))
29+
30+
val B2(x) = 4
31+
println(s"B2: $x")
32+
}
33+
34+
{
35+
object C1:
36+
inline def unapplySeq(inline x: Int) = Some(Seq(x))
37+
38+
val C1(x) = 5
39+
println(s"C1: $x")
40+
}
41+
42+
{
43+
object C2:
44+
inline def unapplySeq[T](inline x: T) = Some(Seq(x))
45+
46+
val C2(x) = 6
47+
println(s"C2: $x")
48+
}
49+
50+
{
51+
object D1
52+
extension (o: D1.type) def unapplySeq(x: Int) = Some(Seq(x))
53+
54+
val D1(x) = 7
55+
println(s"D1: $x")
56+
}
57+
58+
{
59+
object D2
60+
extension (o: D2.type) def unapplySeq[T](x: T) = Some(Seq(x))
61+
62+
val D2(x) = 8
63+
println(s"D2: $x")
64+
}
65+
66+
{
67+
object D3
68+
extension [T] (o: D3.type) def unapplySeq(x: T) = Some(Seq(x))
69+
70+
val D3(x) = 9
71+
println(s"D3: $x")
72+
}
73+
74+
{
75+
object D4
76+
extension [T] (o: D4.type) def unapplySeq[U](x: T | U) = Some(Seq(x))
77+
78+
val D4(x) = 10
79+
println(s"D4: $x")
80+
}
81+
82+
{
83+
object E1
84+
extension (o: E1.type) inline def unapplySeq(x: Int) = Some(Seq(x))
85+
86+
val E1(x) = 11
87+
println(s"E1: $x")
88+
}
89+
90+
{
91+
object E2
92+
extension (o: E2.type) inline def unapplySeq[T](x: T) = Some(Seq(x))
93+
94+
val E2(x) = 12
95+
println(s"E2: $x")
96+
}
97+
98+
{
99+
object E4b
100+
extension [T] (o: E4b.type) inline def unapplySeq[U](x: U) = Some(Seq(x))
101+
102+
val E4b(x) = 14.2
103+
println(s"E4b: $x")
104+
}
105+
106+
{
107+
object F1
108+
extension (o: F1.type) inline def unapplySeq(inline x: Int) = Some(Seq(x))
109+
110+
val F1(x) = 15
111+
println(s"F1: $x")
112+
}
113+
114+
{
115+
object F2
116+
extension (o: F2.type) inline def unapplySeq[T](inline x: T) = Some(Seq(x))
117+
118+
val F2(x) = 16
119+
println(s"F2: $x")
120+
}
121+
122+
{
123+
object F4b
124+
extension [T] (o: F4b.type) inline def unapplySeq[U](inline x: U) = Some(Seq(x))
125+
126+
val F4b(x) = 18.2
127+
println(s"F4b: $x")
128+
}
129+
130+
{
131+
object F4d
132+
extension [T] (o: F4d.type) inline def unapplySeq[U](inline x: (T, U)) = Some(Seq(x))
133+
134+
val F4d(x) = (18.4, 18.5)
135+
println(s"F4d: $x")
136+
}
137+
138+
{
139+
object G1
140+
extension (inline o: G1.type) inline def unapplySeq(x: Int) = Some(Seq(x))
141+
142+
val G1(x) = 19
143+
println(s"G1: $x")
144+
}
145+
146+
{
147+
object G2
148+
extension (inline o: G2.type) inline def unapplySeq[T](x: T) = Some(Seq(x))
149+
150+
val G2(x) = 20
151+
println(s"G2: $x")
152+
}
153+
154+
{
155+
object H1
156+
extension (inline o: H1.type) inline def unapplySeq(inline x: Int) = Some(Seq(x))
157+
158+
val H1(x) = 23
159+
println(s"H1: $x")
160+
}
161+
162+
{
163+
object H2
164+
extension (inline o: H2.type) inline def unapplySeq[T](inline x: T) = Some(Seq(x))
165+
166+
val H2(x) = 24
167+
println(s"H2: $x")
168+
}
169+
}

0 commit comments

Comments
 (0)