Skip to content

Disallow case classes defined in inline methods #11165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
InvalidReferenceInImplicitNotFoundAnnotationID,
TraitMayNotDefineNativeMethodID,
JavaEnumParentArgsID,
AlreadyDefinedID
AlreadyDefinedID,
CaseClassInInlinedCodeID

def errorNumber = ordinal - 2
}
11 changes: 11 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2500,3 +2500,14 @@ import transform.SymUtils._
|""".stripMargin
def explain = ""
}

class CaseClassInInlinedCode(tree: tpd.Tree)(using Context)
extends SyntaxMsg(CaseClassInInlinedCodeID) {

def defKind = if tree.symbol.is(Module) then "object" else "class"
def msg = s"Case $defKind definitions are not allowed in inline methods or quoted code. Use a normal $defKind instead."
def explain =
em"""Case class/object definitions generate a considerable fooprint in code size.
|Inlining such definition would multiply this footprint for each call site.
|""".stripMargin
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
checkAnnotations(tree)
healInfo(tree, tree.srcPos)
super.transform(tree)

case tree: TypeDef if tree.symbol.is(Case) && level > 0 =>
report.error(reporting.CaseClassInInlinedCode(tree), tree)
super.transform(tree)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case classes are just desugarings. Maybe generalize it to all TypeDef?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by generalizing? Do you mean not allowing these ones?

'{ class Bar extends Foo { def foo = ... }; ... }
'{ type T = ...;  }

Those are necessary.

Copy link
Contributor

@liufengyun liufengyun Jan 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't generalize, the rule seems arbitrary and will not do the job. If a programmer just hand-crafted the desugaring of a case class, doesn't it have the same effect as a case class?

case _ =>
super.transform(tree)
}
Expand Down
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/PrepareInlineable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ object PrepareInlineable {
// myAccessors += TypeDef(accessor).withPos(tree.pos.focus)
// ref(accessor).withSpan(tree.span)
//
case _ => tree
case _: TypeDef if tree.symbol.is(Case) =>
report.error(reporting.CaseClassInInlinedCode(tree), tree)
tree
case _ =>
tree
}
}

Expand Down Expand Up @@ -251,7 +255,7 @@ object PrepareInlineable {
}
}

def checkInlineMethod(inlined: Symbol, body: Tree)(using Context): body.type = {
private def checkInlineMethod(inlined: Symbol, body: Tree)(using Context): body.type = {
if (inlined.owner.isClass && inlined.owner.seesOpaques)
report.error(em"Implementation restriction: No inline methods allowed where opaque type aliases are in scope", inlined.srcPos)
if Inliner.inInlineMethod(using ctx.outer) then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import scala.quoted._
def test(using Quotes) = {
'{ case class Foo() }
'{ case class Foo() } // error
}
9 changes: 9 additions & 0 deletions tests/neg-macros/i7068-c.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def species(using quoted.Quotes) = '{
case object Bar // error
case class FooT() // error
${
case object Baz // ok
???
}
FooT()
}
2 changes: 1 addition & 1 deletion tests/pos/i7068-a.scala → tests/neg/i7068-a.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ sealed trait Foo[T] {
}

inline def species[T](t: T) = {
case class FooT(x: T) extends Foo[T] {
case class FooT(x: T) extends Foo[T] { // error
def foo(s: Foo[T]) = s match {
case FooT(x) => ???
}
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/i7068-b.scala → tests/neg/i7068-b.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
inline def species() = {
case class FooT()
case class FooT() // error
FooT()
}
val foo = species()