Skip to content

Commit 572bb37

Browse files
hamzaremmalWojciechMazur
authored andcommitted
do not patch the implicit scope of scala.Unit in Scala.js
[Cherry-picked 6f526ef]
1 parent b2f7eef commit 572bb37

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

compiler/src/dotty/tools/backend/sjs/JSDefinitions.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ final class JSDefinitions()(using Context) {
6969
@threadUnsafe lazy val PseudoUnion_fromTypeConstructorR = PseudoUnionModule.requiredMethodRef("fromTypeConstructor")
7070
def PseudoUnion_fromTypeConstructor(using Context) = PseudoUnion_fromTypeConstructorR.symbol
7171

72-
@threadUnsafe lazy val UnionOpsModuleRef = requiredModuleRef("scala.scalajs.js.internal.UnitOps")
73-
7472
@threadUnsafe lazy val JSArrayType: TypeRef = requiredClassRef("scala.scalajs.js.Array")
7573
def JSArrayClass(using Context) = JSArrayType.symbol.asClass
7674
@threadUnsafe lazy val JSDynamicType: TypeRef = requiredClassRef("scala.scalajs.js.Dynamic")

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -733,13 +733,6 @@ trait ImplicitRunInfo:
733733
.suchThat(companion => companion.is(Module) && companion.owner == sym.owner)
734734
.symbol)
735735

736-
// The companion of `js.|` defines an implicit conversions from
737-
// `A | Unit` to `js.UndefOrOps[A]`. To keep this conversion in scope
738-
// in Scala 3, where we re-interpret `js.|` as a real union, we inject
739-
// it in the scope of `Unit`.
740-
if t.isRef(defn.UnitClass) && ctx.settings.scalajs.value then
741-
companions += JSDefinitions.jsdefn.UnionOpsModuleRef
742-
743736
if sym.isClass then
744737
for p <- t.parents do companions ++= iscopeRefs(p)
745738
else

library-js/src/scala/Unit.scala

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc. dba Akka
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
// DO NOT EDIT, CHANGES WILL BE LOST
14+
// This auto-generated code can be modified in "project/GenerateAnyVals.scala".
15+
// Afterwards, running "sbt generateSources" regenerates this source file.
16+
17+
package scala
18+
19+
import scala.language.`2.13`
20+
import scala.scalajs.js
21+
import scala.annotation.nowarn
22+
23+
/** `Unit` is a subtype of [[scala.AnyVal]]. There is only one value of type
24+
* `Unit`, `()`, and it is not represented by any object in the underlying
25+
* runtime system. A method with return type `Unit` is analogous to a Java
26+
* method which is declared `void`.
27+
*/
28+
final abstract class Unit private extends AnyVal {
29+
// Provide a more specific return type for Scaladoc
30+
override def getClass(): Class[Unit] = ???
31+
}
32+
33+
@scala.annotation.compileTimeOnly("`Unit` companion object is not allowed in source; instead, use `()` for the unit value")
34+
object Unit extends AnyValCompanion {
35+
36+
/** Transforms a value type into a boxed reference type.
37+
*
38+
* This method is not intended for use in source code.
39+
* The runtime representation of this value is platform specific.
40+
*
41+
* @param x the Unit to be boxed
42+
* @return a scala.runtime.BoxedUnit offering `x` as its underlying value.
43+
*/
44+
def box(x: Unit): scala.runtime.BoxedUnit = scala.runtime.BoxedUnit.UNIT
45+
46+
/** Transforms a boxed type into a value type. Note that this
47+
* method is not typesafe: it accepts any Object, but will throw
48+
* an exception if the argument is not a scala.runtime.BoxedUnit.
49+
*
50+
* This method is not intended for use in source code.
51+
* The result of successfully unboxing a value is `()`.
52+
*
53+
* @param x the scala.runtime.BoxedUnit to be unboxed.
54+
* @throws ClassCastException if the argument is not a scala.runtime.BoxedUnit
55+
* @return the Unit value ()
56+
*/
57+
def unbox(x: java.lang.Object): Unit = x.asInstanceOf[scala.runtime.BoxedUnit]
58+
59+
/** The String representation of the scala.Unit companion object. */
60+
override def toString = "object scala.Unit"
61+
62+
// The companion of `js.|` defines an implicit conversions from
63+
// `A | Unit` to `js.UndefOrOps[A]`. To keep this conversion in scope
64+
// in Scala 3, where we re-interpret `js.|` as a real union, we inject
65+
// it in the scope of `Unit`.
66+
@nowarn("cat=deprecation")
67+
implicit def unitOrOps[A](x: A | Unit): js.UndefOrOps[A] =
68+
js.internal.UnitOps.unitOrOps[A](x)
69+
70+
}
71+

library-js/src/scala/scalajs/js/internal/UnitOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package scala.scalajs.js.internal
22

33
import scala.scalajs.js
44

5-
/** Under -scalajs, this object is part of the implicit scope of `scala.Unit` */
5+
@deprecated(message = "The implicit conversion was moved to the companion object of `scala.Unit` in Scala.js", since = "3.8.0")
66
object UnitOps:
77
implicit def unitOrOps[A](x: A | Unit): js.UndefOrOps[A] =
88
new js.UndefOrOps(x)

0 commit comments

Comments
 (0)