Skip to content

Commit d3cb769

Browse files
committed
chore: allow to compile AnyVal with Scala 3
1 parent b10f71b commit d3cb769

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

@@ -2232,7 +2226,6 @@ class Definitions {
22322226
orType,
22332227
RepeatedParamClass,
22342228
ByNameParamClass2x,
2235-
AnyValClass,
22362229
NullClass,
22372230
NothingClass,
22382231
SingletonClass,
@@ -2538,51 +2531,6 @@ class Definitions {
25382531
| */
25392532
""".stripMargin)
25402533

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

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
@@ -3465,13 +3465,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34653465

34663466
/** Turn a parent type into a constructor call where needed. This is the case where
34673467
* - we are in a Scala class or module (not a Java class, nor a trait), and
3468+
* - the symbol is not `scala.AnyVal` (must extend `scala.Any` which doesn't have a constructor)
34683469
* - the parent symbol is a non-trait class, or
34693470
* - the parent symbol is a trait that takes at least one (explicit or implicit) parameter
34703471
* and the parent symbol is directly extended by the current class (i.e. not
34713472
* extended by the superclass).
34723473
*/
34733474
def ensureConstrCall(cls: ClassSymbol, parent: Tree, psym: Symbol)(using Context): Tree =
3474-
if parent.isType && !cls.is(Trait) && !cls.is(JavaDefined) && psym.isClass
3475+
if parent.isType && !cls.is(Trait) && !cls.is(JavaDefined) && psym.isClass && cls != defn.AnyValClass
34753476
// Annotations are represented as traits with constructors, but should
34763477
// never be called as such outside of annotation trees.
34773478
&& !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
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)