Skip to content

Commit c8a56d0

Browse files
committed
Re-apply nullable to Option.apply
1 parent 0cce3c6 commit c8a56d0

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object NullOpsDecorator:
4242
}
4343
if tpStripped ne tpWiden then tpStripped else tp
4444

45-
if ctx.explicitNulls then strip(self) else self
45+
strip(self)
4646
}
4747

4848
/** Is self (after widening and dealiasing) a type of the form `T | Null`? */

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,12 @@ object GenericSignatures {
304304
builder.append(')')
305305
methodResultSig(rte)
306306

307+
case OrNull(tp1) if !tp1.derivesFrom(defn.AnyValClass) =>
308+
// Special case for nullable union types whose underlying type is not a value class.
309+
// For example, `T | Null` where `T` is a type parameter becomes `T` in the signature;
310+
// `Int | Null` still becomes `Object`.
311+
jsig1(tp1)
312+
307313
case tp: AndType =>
308314
// Only intersections appearing as the upper-bound of a type parameter
309315
// can be preserved in generic signatures and those are already
@@ -455,15 +461,15 @@ object GenericSignatures {
455461
else x
456462
}
457463

458-
private def collectMethodParams(mtd: MethodOrPoly)(using Context): (List[TypeParamInfo], List[Type], Type) =
464+
private def collectMethodParams(mtd: MethodOrPoly)(using Context): (List[TypeParamInfo], List[Type], Type) =
459465
val tparams = ListBuffer.empty[TypeParamInfo]
460466
val vparams = ListBuffer.empty[Type]
461467

462468
@tailrec def recur(tpe: Type): Type = tpe match
463469
case mtd: MethodType =>
464470
vparams ++= mtd.paramInfos.filterNot(_.hasAnnotation(defn.ErasedParamAnnot))
465471
recur(mtd.resType)
466-
case PolyType(tps, tpe) =>
472+
case PolyType(tps, tpe) =>
467473
tparams ++= tps
468474
recur(tpe)
469475
case _ =>

library/src/scala/Option.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object Option {
2828
* @param x the value
2929
* @return Some(value) if value != null, None if value == null
3030
*/
31-
def apply[A](x: A): Option[A] = if (x == null) None else Some(x)
31+
def apply[A](x: A | Null): Option[A] = if (x == null) None else Some(x)
3232

3333
/** An Option factory which returns `None` in a manner consistent with
3434
* the collections hierarchy.

library/src/scala/quoted/FromExpr.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ object FromExpr {
104104
*/
105105
given OptionFromExpr[T](using Type[T], FromExpr[T]): FromExpr[Option[T]] with {
106106
def unapply(x: Expr[Option[T]])(using Quotes) = x match {
107-
case '{ Option[T](${Expr(y)}) } => Some(Option(y))
107+
case '{ Option[T](${Expr(y)}: T) } => Some(Option(y))
108108
case '{ None } => Some(None)
109109
case '{ ${Expr(opt)} : Some[T] } => Some(opt)
110110
case _ => None

sbt-bridge/src/xsbt/ConsoleInterface.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import java.util.ArrayList;
77

8-
import scala.Some;
8+
import scala.Option;
99

1010
import xsbti.Logger;
1111

@@ -40,7 +40,7 @@ public void run(
4040
completeArgsList.add(classpathString);
4141
String[] completeArgs = completeArgsList.toArray(args);
4242

43-
ReplDriver driver = new ReplDriver(completeArgs, System.out, Some.apply(loader), ReplDriver.pprintImport());
43+
ReplDriver driver = new ReplDriver(completeArgs, System.out, Option.apply(loader), ReplDriver.pprintImport());
4444

4545
State state = driver.initialState();
4646
assert bindNames.length == bindValues.length;

0 commit comments

Comments
 (0)