Skip to content

Commit 41cc3c1

Browse files
hamzaremmalWojciechMazur
authored andcommitted
chore: allow to compile AnyVal with Scala 3
[Cherry-picked c081e16]
1 parent dcba8ae commit 41cc3c1

File tree

5 files changed

+6
-58
lines changed

5 files changed

+6
-58
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,7 @@ class Definitions {
283283
def AnyType: TypeRef = AnyClass.typeRef
284284
@tu lazy val MatchableClass: ClassSymbol = completeClass(enterCompleteClassSymbol(ScalaPackageClass, tpnme.Matchable, Trait | TransparentType, AnyType :: Nil), ensureCtor = false)
285285
def MatchableType: TypeRef = MatchableClass.typeRef
286-
@tu lazy val AnyValClass: ClassSymbol =
287-
val res = completeClass(enterCompleteClassSymbol(ScalaPackageClass, tpnme.AnyVal, Abstract | TransparentType, List(AnyType, MatchableType)))
288-
// Mark companion as absent, so that class does not get re-completed
289-
val companion = ScalaPackageVal.info.decl(nme.AnyVal).symbol
290-
companion.moduleClass.markAbsent()
291-
companion.markAbsent()
292-
res
286+
@tu lazy val AnyValClass: ClassSymbol = requiredClass("scala.AnyVal")
293287

294288
def AnyValType: TypeRef = AnyValClass.typeRef
295289

@@ -2231,7 +2225,6 @@ class Definitions {
22312225
orType,
22322226
RepeatedParamClass,
22332227
ByNameParamClass2x,
2234-
AnyValClass,
22352228
NullClass,
22362229
NothingClass,
22372230
SingletonClass,
@@ -2537,51 +2530,6 @@ class Definitions {
25372530
| */
25382531
""".stripMargin)
25392532

2540-
add(AnyValClass,
2541-
"""/** `AnyVal` is the root class of all ''value types'', which describe values
2542-
| * not implemented as objects in the underlying host system. Value classes
2543-
| * are specified in Scala Language Specification, section 12.2.
2544-
| *
2545-
| * The standard implementation includes nine `AnyVal` subtypes:
2546-
| *
2547-
| * [[scala.Double]], [[scala.Float]], [[scala.Long]], [[scala.Int]], [[scala.Char]],
2548-
| * [[scala.Short]], and [[scala.Byte]] are the ''numeric value types''.
2549-
| *
2550-
| * [[scala.Unit]] and [[scala.Boolean]] are the ''non-numeric value types''.
2551-
| *
2552-
| * Other groupings:
2553-
| *
2554-
| * - The ''subrange types'' are [[scala.Byte]], [[scala.Short]], and [[scala.Char]].
2555-
| * - The ''integer types'' include the subrange types as well as [[scala.Int]] and [[scala.Long]].
2556-
| * - The ''floating point types'' are [[scala.Float]] and [[scala.Double]].
2557-
| *
2558-
| * Prior to Scala 2.10, `AnyVal` was a sealed trait. Beginning with Scala 2.10,
2559-
| * however, it is possible to define a subclass of `AnyVal` called a ''user-defined value class''
2560-
| * which is treated specially by the compiler. Properly-defined user value classes provide a way
2561-
| * to improve performance on user-defined types by avoiding object allocation at runtime, and by
2562-
| * replacing virtual method invocations with static method invocations.
2563-
| *
2564-
| * User-defined value classes which avoid object allocation...
2565-
| *
2566-
| * - must have a single `val` parameter that is the underlying runtime representation.
2567-
| * - can define `def`s, but no `val`s, `var`s, or nested `traits`s, `class`es or `object`s.
2568-
| * - typically extend no other trait apart from `AnyVal`.
2569-
| * - cannot be used in type tests or pattern matching.
2570-
| * - may not override `equals` or `hashCode` methods.
2571-
| *
2572-
| * A minimal example:
2573-
| * {{{
2574-
| * class Wrapper(val underlying: Int) extends AnyVal {
2575-
| * def foo: Wrapper = new Wrapper(underlying * 19)
2576-
| * }
2577-
| * }}}
2578-
| *
2579-
| * It's important to note that user-defined value classes are limited, and in some circumstances,
2580-
| * still must allocate a value class instance at runtime. These limitations and circumstances are
2581-
| * explained in greater detail in the [[https://docs.scala-lang.org/overviews/core/value-classes.html Value Classes and Universal Traits]].
2582-
| */
2583-
""".stripMargin)
2584-
25852533
add(NullClass,
25862534
"""/** `Null` is - together with [[scala.Nothing]] - at the bottom of the Scala type hierarchy.
25872535
| *

compiler/src/dotty/tools/dotc/transform/Erasure.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ class Erasure extends Phase with DenotTransformer {
187187
tp.typeSymbol == cls && ctx.compilationUnit.source.file.name == sourceName
188188
assert(
189189
isErasedType(tp)
190+
|| isAllowed(defn.AnyValClass, "AnyVal.scala")
190191
|| isAllowed(defn.ArrayClass, "Array.scala")
191192
|| isAllowed(defn.TupleClass, "Tuple.scala")
192193
|| isAllowed(defn.NonEmptyTupleClass, "Tuple.scala")

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3411,13 +3411,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34113411

34123412
/** Turn a parent type into a constructor call where needed. This is the case where
34133413
* - we are in a Scala class or module (not a Java class, nor a trait), and
3414+
* - the symbol is not `scala.AnyVal` (must extend `scala.Any` which doesn't have a constructor)
34143415
* - the parent symbol is a non-trait class, or
34153416
* - the parent symbol is a trait that takes at least one (explicit or implicit) parameter
34163417
* and the parent symbol is directly extended by the current class (i.e. not
34173418
* extended by the superclass).
34183419
*/
34193420
def ensureConstrCall(cls: ClassSymbol, parent: Tree, psym: Symbol)(using Context): Tree =
3420-
if parent.isType && !cls.is(Trait) && !cls.is(JavaDefined) && psym.isClass
3421+
if parent.isType && !cls.is(Trait) && !cls.is(JavaDefined) && psym.isClass && cls != defn.AnyValClass
34213422
// Annotations are represented as traits with constructors, but should
34223423
// never be called as such outside of annotation trees.
34233424
&& !psym.is(JavaAnnotation)

library/src/scala/AnyVal.scala.ignore renamed to library/src-bootstrapped/scala/AnyVal.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ import scala.language.`2.13`
5555
* still must allocate a value class instance at runtime. These limitations and circumstances are
5656
* explained in greater detail in the [[https://docs.scala-lang.org/overviews/core/value-classes.html Value Classes and Universal Traits]].
5757
*/
58-
transparent abstract class AnyVal extends Any {
59-
def getClass(): Class[_ <: AnyVal] = null
58+
transparent abstract class AnyVal extends Any, Matchable {
59+
def getClass(): Class[? <: AnyVal] = null.asInstanceOf[Class[? <: AnyVal]]
6060
}

tests/neg/i1688.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@ package scala
22
sealed trait Null // error
33

44
object Null // error
5-
6-
class AnyVal // error

0 commit comments

Comments
 (0)