From 467cbc7856411667bc4948a739f0f22b2d6ac7d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pa=C5=82ka?= Date: Tue, 24 Aug 2021 15:04:52 +0200 Subject: [PATCH] Disallow inline secondary constructors --- compiler/src/dotty/tools/dotc/typer/Inliner.scala | 2 ++ compiler/src/dotty/tools/dotc/typer/Typer.scala | 2 ++ tests/neg/i12986a/Bar.scala | 2 ++ tests/neg/i12986a/Test.scala | 1 + tests/neg/i12986b.scala | 4 ++++ 5 files changed, 11 insertions(+) create mode 100644 tests/neg/i12986a/Bar.scala create mode 100644 tests/neg/i12986a/Test.scala create mode 100644 tests/neg/i12986b.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Inliner.scala b/compiler/src/dotty/tools/dotc/typer/Inliner.scala index cab9f397d507..7d5b26a7be85 100644 --- a/compiler/src/dotty/tools/dotc/typer/Inliner.scala +++ b/compiler/src/dotty/tools/dotc/typer/Inliner.scala @@ -97,6 +97,8 @@ object Inliner { if tree.symbol.isExperimental then Feature.checkExperimentalDef(tree.symbol, tree) + if tree.symbol.isConstructor then return tree // error already reported for the inline constructor definition + /** Set the position of all trees logically contained in the expansion of * inlined call `call` to the position of `call`. This transform is necessary * when lifting bindings from the expansion to the outside of the call. diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 6c13afa219b8..c04239aafefa 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -2164,6 +2164,8 @@ class Typer extends Namer PrepareInlineable.registerInlineInfo(sym, rhsToInline) if sym.isConstructor then + if sym.is(Inline) then + report.error("constructors cannot be `inline`", ddef) if sym.isPrimaryConstructor then if sym.owner.is(Case) then for diff --git a/tests/neg/i12986a/Bar.scala b/tests/neg/i12986a/Bar.scala new file mode 100644 index 000000000000..fcc12aca6259 --- /dev/null +++ b/tests/neg/i12986a/Bar.scala @@ -0,0 +1,2 @@ +class Bar(i: Int): + inline def this() = this(0) // error diff --git a/tests/neg/i12986a/Test.scala b/tests/neg/i12986a/Test.scala new file mode 100644 index 000000000000..84399837c008 --- /dev/null +++ b/tests/neg/i12986a/Test.scala @@ -0,0 +1 @@ +val bar = new Bar() diff --git a/tests/neg/i12986b.scala b/tests/neg/i12986b.scala new file mode 100644 index 000000000000..d8de9dbfb51c --- /dev/null +++ b/tests/neg/i12986b.scala @@ -0,0 +1,4 @@ +class Bar(i: Int): + transparent inline def this() = this(0) // error + +val bar = Bar()