Skip to content

Commit

Permalink
Add patch for undefined behavior with object $ (#19705)
Browse files Browse the repository at this point in the history
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.

Closes #19702
  • Loading branch information
bishabosha authored Feb 19, 2024
2 parents 69170d3 + b0b6ec5 commit 898ce33
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/classpath/FileUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/pos/i19702/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions tests/pos/i19702/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@main def hello =
test.$.test()

println("?")

0 comments on commit 898ce33

Please sign in to comment.