Skip to content

Commit 7935ffc

Browse files
committed
Drop support of inline unapply
Inline unapply did not inline the call to unapply, it only used its body to refine the type and then kept the unapply. It was a special case for tuples *: unapply to work, but typing the unapply explicitly gives the same result.
1 parent 6d8305d commit 7935ffc

File tree

7 files changed

+17
-28
lines changed

7 files changed

+17
-28
lines changed

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -929,10 +929,7 @@ object SymDenotations {
929929
def isSkolem: Boolean = name == nme.SKOLEM
930930

931931
def isInlineMethod(implicit ctx: Context): Boolean =
932-
isAllOf(InlineMethod, butNot = Accessor) &&
933-
!name.isUnapplyName // unapply methods do not count as inline methods
934-
// we need an inline flag on them only do that
935-
// reduceProjection gets access to their rhs
932+
isAllOf(InlineMethod, butNot = Accessor)
936933

937934
/** Is this a Scala 2 macro */
938935
final def isScala2Macro(implicit ctx: Context): Boolean = is(Macro) && symbol.owner.is(Scala2x)

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,14 +1016,8 @@ trait Checking {
10161016

10171017
/** Check that we are in an inline context (inside an inline method or in inline code) */
10181018
def checkInInlineContext(what: String, posd: Positioned)(implicit ctx: Context): Unit =
1019-
if (!ctx.inInlineMethod && !ctx.isInlineContext) {
1020-
val inInlineUnapply = ctx.owner.ownersIterator.exists(owner =>
1021-
owner.name.isUnapplyName && owner.is(Inline) && owner.is(Method))
1022-
val msg =
1023-
if (inInlineUnapply) "cannot be used in an inline unapply"
1024-
else "can only be used in an inline method"
1025-
ctx.error(em"$what $msg", posd.sourcePos)
1026-
}
1019+
if !ctx.inInlineMethod && !ctx.isInlineContext then
1020+
ctx.error(em"$what can only be used in an inline method", posd.sourcePos)
10271021

10281022
/** 1. Check that all case classes that extend `scala.Enum` are `enum` cases
10291023
* 2. Check that case class `enum` cases do not extend java.lang.Enum.

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -992,12 +992,6 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
992992
yield constToLiteral(reduceProjection(ref(scrut).select(accessor).ensureApplied))
993993
caseAccessors.length == pats.length && reduceSubPatterns(pats, selectors)
994994
}
995-
else if (unapp.symbol.isInlineMethod) { // TODO: Adapt to typed setting
996-
val app = untpd.Apply(untpd.TypedSplice(unapp), untpd.ref(scrut))
997-
val app1 = typer.typedExpr(app)
998-
val args = tupleArgs(app1)
999-
args.nonEmpty && reduceSubPatterns(pats, args)
1000-
}
1001995
else false
1002996
case _ =>
1003997
false

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,10 @@ object PrepareInlineable {
240240
if (inlined.owner.isClass && inlined.owner.seesOpaques)
241241
ctx.error(em"Implementation restriction: No inline methods allowed where opaque type aliases are in scope", inlined.sourcePos)
242242
if (ctx.outer.inInlineMethod)
243-
ctx.error(ex"implementation restriction: nested inline methods are not supported", inlined.sourcePos)
244-
if (inlined.name.isUnapplyName && tupleArgs(body).isEmpty)
245-
ctx.warning(
246-
em"inline unapply method can be rewritten only if its right hand side is a tuple (e1, ..., eN)",
247-
body.sourcePos)
243+
ctx.error(ex"Implementation restriction: nested inline methods are not supported", inlined.sourcePos)
244+
if (inlined.name.isUnapplyName)
245+
ctx.error(em"Implementation restriction: inline ${inlined.name} methods are not supported", inlined.sourcePos)
246+
248247
if (inlined.is(Macro) && !ctx.isAfterTyper) {
249248

250249
def checkMacro(tree: Tree): Unit = tree match {

tests/neg/inine-unnapply.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
object Foo {
3+
inline def unapply(x: Any): Boolean = ??? // error: Implementation restriction: inline unapply methods are not supported
4+
inline def unapplySeq(x: Any): Seq[Any] = ??? // error: Implementation restriction: inline unapplySeq methods are not supported
5+
}

tests/neg/inline-unapply.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ object Test {
22

33
class C(val x: Int, val y: Int)
44

5-
inline def unapply(c: C): Option[(Int, Int)] = Some((c.x, c.y)) // ok
5+
inline def unapply(c: C): Some[(Int, Int)] = Some((c.x, c.y)) // error: Implementation restriction: inline unapply methods are not supported
66

77
}
88
object Test2 {
99

1010
class C(x: Int, y: Int)
1111

12-
inline def unapply(c: C): Option[(Int, Int)] = inline c match { // error: inline match cannot be used in an inline unapply
13-
case x: C => (1, 1)
12+
inline def unapply(c: C): Option[(Int, Int)] = inline c match { // error: Implementation restriction: inline unapply methods are not supported
13+
case x: C => Some((1, 1))
1414
}
1515
}

tests/pos/inline-i1773.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
object Test {
22
implicit class Foo(sc: StringContext) {
33
object q {
4-
inline def unapply(arg: Any): Option[(Any, Any)] =
4+
def unapply(arg: Any): Option[(Any, Any)] =
55
Some((sc.parts(0), sc.parts(1)))
66
}
77
}
88

99
def main(args: Array[String]): Unit = {
10-
val q"class $name extends $parent" = new Object // now ok, was an error that "method unapply is used"
10+
val q"class $name extends $parent" = new Object
1111
println(name)
1212
println(parent)
1313
}

0 commit comments

Comments
 (0)