From b0b6ec52768699c1394558ef57f50114c91c113c Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 16 Feb 2024 09:02:24 +0100 Subject: [PATCH] Add patch for undefined behavior with `object $` Add an ad-hoc patch to make it possible to use the broken `object $` definitions in https://github.com/com-lihaoyi/Ammonite/blob/main/amm/interp/api/src/main/scala/ammonite/Stubs.scala. This is a temporary patch while these definitions get deprecated. There is not guarantee that the semantics of these objects are correct. There is also no way to adapt the spec to allow there definition without breaking the language. For example consider `object $` and `object $$`, how would we know if the `$$.class` is the class of `$$` or the module class of `$`. We need to know this before we read the file. --- compiler/src/dotty/tools/dotc/classpath/FileUtils.scala | 4 ++++ tests/pos/i19702/Macro_1.scala | 9 +++++++++ tests/pos/i19702/Test_2.scala | 4 ++++ 3 files changed, 17 insertions(+) create mode 100644 tests/pos/i19702/Macro_1.scala create mode 100644 tests/pos/i19702/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/classpath/FileUtils.scala b/compiler/src/dotty/tools/dotc/classpath/FileUtils.scala index 8c31faa43186..b8cb9a2155dc 100644 --- a/compiler/src/dotty/tools/dotc/classpath/FileUtils.scala +++ b/compiler/src/dotty/tools/dotc/classpath/FileUtils.scala @@ -114,6 +114,10 @@ object FileUtils { if classOrModuleName.endsWith("$") && classOrModuleName != "Null$" // scala.runtime.Null$ && classOrModuleName != "Nothing$" // scala.runtime.Nothing$ + // Special case for `object $` in Amonite. + // This is an ad-hoc workaround for Amonite `object $`. See issue #19702 + // This definition is not valid Scala. + && classOrModuleName != "$" then classOrModuleName.stripSuffix("$") else classOrModuleName className + SUFFIX_TASTY diff --git a/tests/pos/i19702/Macro_1.scala b/tests/pos/i19702/Macro_1.scala new file mode 100644 index 000000000000..79aab856dfda --- /dev/null +++ b/tests/pos/i19702/Macro_1.scala @@ -0,0 +1,9 @@ +package test + +// IMPORTANT: this object has undefined behavior due to its illegal use of a $ in an identifier +// This test is only to check that the ad-hoc workaround for Amonite `object $` is working. +// If at some point it becomes impossible to support this test, we can drop it. +// Ideally Amonite will have deprecated the `object $` by then. +object $ { + def test(): Int = 1 +} diff --git a/tests/pos/i19702/Test_2.scala b/tests/pos/i19702/Test_2.scala new file mode 100644 index 000000000000..c8bcd4287e0d --- /dev/null +++ b/tests/pos/i19702/Test_2.scala @@ -0,0 +1,4 @@ +@main def hello = + test.$.test() + + println("?")