Skip to content

Commit 7df2efa

Browse files
committed
Define almost all Predef overwrites directly in Scala 2 library TASTy
The `valueOf` is not implemented yet.
1 parent 27f1a13 commit 7df2efa

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

project/TastyMiMaFilters.scala

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import tastymima.intf._
33

44
object TastyMiMaFilters {
55
val StdlibBootstrapped: java.util.List[ProblemMatcher] = asList(
6+
7+
// FIXME: method overwritten in scala2-library-bootstrapped/src/scala/Predef.scala
8+
ProblemMatcher.make(ProblemKind.IncompatibleTypeChange, "scala.Predef.nn"), // The symbol scala.Predef.nn has an incompatible type in current version: before: [T](x: T)(x.type & T); after: [T](x: scala.|[T, scala.Null])scala.&[x.type, T]
9+
// FIXME: method NOT overwritten yet in scala2-library-bootstrapped/src/scala/Predef.scala
10+
ProblemMatcher.make(ProblemKind.MissingTermMember, "scala.Predef.valueOf"), // The member scala.Predef.valueOf with signature (1):java.lang.Object does not have a correspondant in current version
11+
612
// Probably OK
713
ProblemMatcher.make(ProblemKind.IncompatibleSelfTypeChange, "scala.*"),
814

@@ -55,11 +61,6 @@ object TastyMiMaFilters {
5561
ProblemMatcher.make(ProblemKind.NewAbstractMember, "scala.collection.convert.impl.TableStepperBase.i0_="),
5662
ProblemMatcher.make(ProblemKind.NewAbstractMember, "scala.collection.convert.impl.TableStepperBase.maxLength_="),
5763

58-
// Problem: ???
59-
ProblemMatcher.make(ProblemKind.MissingTermMember, "scala.Predef.nn"), // The member scala.Predef.nn with signature (1,java.lang.Object):java.lang.Object does not have a correspondant in current version
60-
ProblemMatcher.make(ProblemKind.MissingTermMember, "scala.Predef.ne"), // The member scala.Predef.ne with signature (java.lang.Object,java.lang.Object):scala.Boolean does not have a correspondant in current version
61-
ProblemMatcher.make(ProblemKind.MissingTermMember, "scala.Predef.eq"), // The member scala.Predef.eq with signature (java.lang.Object,java.lang.Object):scala.Boolean does not have a correspondant in current version
62-
6364
// Problem: protected lazy val (processThread, (futureThread, futureValue), destroyer) = { ... }
6465
// https://github.com/scala/scala/blob/cff8a9af4da67658d8e1e32f929e1aff03ffa384/src/library/scala/sys/process/ProcessImpl.scala#L99C5-L99C83
6566
ProblemMatcher.make(ProblemKind.IncompatibleKindChange, "scala.sys.process.ProcessImpl.CompoundProcess.destroyer"), // before: lazy val; after: def
@@ -75,10 +76,6 @@ object TastyMiMaFilters {
7576
// https://github.com/scala/scala/blob/2.13.x/src/library/scala/collection/mutable/ArrayBuilder.scala#L504C1-L504C87
7677
ProblemMatcher.make(ProblemKind.MissingTermMember, "scala.collection.mutable.ArrayBuilder.ofUnit.addAll"), // The member scala.collection.mutable.ArrayBuilder.ofUnit.addAll with signature (java.lang.Object,scala.Int,scala.Int):scala.collection.mutable.ArrayBuilder$.ofUnit does not have a correspondant in current version
7778

78-
// Probably OK (TASTy MiMa bug): Patched Predef members
79-
ProblemMatcher.make(ProblemKind.MissingTermMember, "scala.Predef.valueOf"), // The member scala.Predef.valueOf with signature (1):java.lang.Object does not have a correspondant in current version
80-
ProblemMatcher.make(ProblemKind.MissingTermMember, "scala.Predef.summon"), // The member scala.Predef.summon with signature (1,java.lang.Object):java.lang.Object does not have a correspondant in current version
81-
8279
// TASTy-MiMa bugs
8380
ProblemMatcher.make(ProblemKind.InternalError, "scala.collection.SeqView.appendedAll"),
8481
ProblemMatcher.make(ProblemKind.InternalError, "scala.collection.SeqView.concat"),

scala2-library-bootstrapped/src/scala/Predef.scala

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,61 @@ object Predef extends LowPriorityImplicits {
508508
*/
509509
// $ to avoid accidental shadowing (e.g. scala/bug#7788)
510510
implicit def $conforms[A]: A => A = <:<.refl
511+
512+
// TODO Add `valueOf`:
513+
// Issue is that this introduces a cyclic dependency between
514+
// the Scala 2 and 3 library due to the reference to `summonFrom`.
515+
// /**
516+
// * Retrieve the single value of a type with a unique inhabitant.
517+
// *
518+
// * @example {{{
519+
// * object Foo
520+
// * val foo = valueOf[Foo.type]
521+
// * // foo is Foo.type = Foo
522+
// *
523+
// * val bar = valueOf[23]
524+
// * // bar is 23.type = 23
525+
// * }}}
526+
// * @group utilities
527+
// */
528+
// inline def valueOf[T]: T = summonFrom {
529+
// case ev: ValueOf[T] => ev.value
530+
// }
531+
532+
/** Summon a given value of type `T`. Usually, the argument is not passed explicitly.
533+
*
534+
* @tparam T the type of the value to be summoned
535+
* @return the given value typed: the provided type parameter
536+
*/
537+
transparent inline def summon[T](using x: T): x.type = x
538+
539+
// Extension methods for working with explicit nulls
540+
541+
/** Strips away the nullability from a value. Note that `.nn` performs a checked cast,
542+
* so if invoked on a `null` value it will throw an `NullPointerException`.
543+
* @example {{{
544+
* val s1: String | Null = "hello"
545+
* val s2: String = s1.nn
546+
*
547+
* val s3: String | Null = null
548+
* val s4: String = s3.nn // throw NullPointerException
549+
* }}}
550+
*/
551+
extension [T](x: T | Null) inline def nn: x.type & T =
552+
if x.asInstanceOf[Any] == null then scala.runtime.Scala3RunTime.nnFail()
553+
x.asInstanceOf[x.type & T]
554+
555+
extension (inline x: AnyRef | Null)
556+
/** Enables an expression of type `T|Null`, where `T` is a subtype of `AnyRef`, to be checked for `null`
557+
* using `eq` rather than only `==`. This is needed because `Null` no longer has
558+
* `eq` or `ne` methods, only `==` and `!=` inherited from `Any`. */
559+
inline def eq(inline y: AnyRef | Null): Boolean =
560+
x.asInstanceOf[AnyRef] eq y.asInstanceOf[AnyRef]
561+
/** Enables an expression of type `T|Null`, where `T` is a subtype of `AnyRef`, to be checked for `null`
562+
* using `ne` rather than only `!=`. This is needed because `Null` no longer has
563+
* `eq` or `ne` methods, only `==` and `!=` inherited from `Any`. */
564+
inline def ne(inline y: AnyRef | Null): Boolean =
565+
!(x eq y)
511566
}
512567

513568
/** The `LowPriorityImplicits` class provides implicit values that

0 commit comments

Comments
 (0)