Skip to content

Only allow sound inline parameters in overrides #8785

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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/config/Printers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object Printers {
}

object noPrinter extends Printer {
inline override def println(inline msg: => String): Unit = ()
inline override def println(msg: => String): Unit = ()
}

val default = new Printer
Expand Down
17 changes: 16 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ object Checking {
|| sym.is(TermParam) && !sym.owner.isInlineMethod
))
fail(ParamsNoInline(sym.owner))

if sym.isInlineMethod && !sym.is(Deferred) && sym.allOverriddenSymbols.nonEmpty then
checkInlineOverrideParameters(sym)
if (sym.isOneOf(GivenOrImplicit)) {
if (sym.owner.is(Package))
fail(TopLevelCantBeImplicit(sym))
Expand Down Expand Up @@ -646,6 +647,20 @@ object Checking {
val enumCls = enumCase.owner.linkedClass
if !cls.info.parents.exists(_.typeSymbol == enumCls) then
ctx.error(i"enum case does not extend its enum $enumCls", enumCase.sourcePos)

/** Check the inline override methods only use inline parameters if they override an inline parameter. */
def checkInlineOverrideParameters(sym: Symbol)(using Context): Unit =
lazy val params = sym.paramSymss.flatten
for
sym2 <- sym.allOverriddenSymbols
(p1, p2) <- sym.paramSymss.flatten.lazyZip(sym2.paramSymss.flatten)
if p1.is(Inline) != p2.is(Inline)
do
ctx.error(
if p2.is(Inline) then "Cannot override inline parameter with a non-inline parameter"
else "Cannot override non-inline parameter with an inline parameter",
p1.sourcePos)

}

trait Checking {
Expand Down
35 changes: 35 additions & 0 deletions tests/neg/inline-parameter-override.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@


abstract class Logger {
def log1(msg: String): Unit
inline def log2(msg: String): Unit
inline def log3(inline msg: String): Unit
}

class Logger1 extends Logger {
inline def log1(msg: String): Unit = ()
inline def log2(msg: String): Unit = ()
inline def log3(msg: String): Unit = () // error: Cannot override inline parameter with a non-inline parameter
}

class Logger2 extends Logger {
inline def log1(inline msg: String): Unit = () // error: Cannot override non-inline parameter with an inline parameter
inline def log2(inline msg: String): Unit = () // error: Cannot override non-inline parameter with an inline parameter
inline def log3(inline msg: String): Unit = ()
}

trait A {
inline def f(inline a: Int): Int
}

trait B {
def f(a: Int): Int
}

class C extends A, B {
inline def f(inline a: Int): Int = 3 // error: Cannot override non-inline parameter with and inline parameter
}

class D extends B, A {
inline def f(inline a: Int): Int = 3 // error: Cannot override non-inline parameter with and inline parameter
}
2 changes: 1 addition & 1 deletion tests/run/inline-override-num.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ trait Num[T] {

object Num {
class IntNum extends Num[Int] {
inline def plus(inline x: Int, inline y: Int): Int = x + y
inline def plus(x: Int, y: Int): Int = x + y
}
given IntNum

Expand Down
4 changes: 2 additions & 2 deletions tests/run/typeclass-derivation2b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object TypeLevel {

abstract class GenericSum[S] extends Generic[S] {
def ordinal(x: S): Int
inline def alternative(n: Int): GenericProduct[_ <: S]
inline def alternative(inline n: Int): GenericProduct[_ <: S]
}

abstract class GenericProduct[P] extends Generic[P] {
Expand All @@ -49,7 +49,7 @@ object Lst {
case x: Cons[_] => 0
case Nil => 1
}
inline override def alternative(inline n: Int) <: GenericProduct[_ <: Lst[T]] =
inline def alternative(inline n: Int) <: GenericProduct[_ <: Lst[T]] =
inline n match {
case 0 => Cons.GenericCons[T]
case 1 => Nil.GenericNil
Expand Down