Skip to content

Commit d12a9f1

Browse files
committed
add notNull to tree
1 parent a423df5 commit d12a9f1

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ class Definitions {
224224
@tu lazy val Compiletime_constValueOpt: Symbol = CompiletimePackageObject.requiredMethod("constValueOpt")
225225
@tu lazy val Compiletime_code : Symbol = CompiletimePackageObject.requiredMethod("code")
226226
@tu lazy val Compiletime_summonFrom : Symbol = CompiletimePackageObject.requiredMethod("summonFrom")
227+
@tu lazy val Compiletime_notNull : Symbol = CompiletimePackageObject.requiredMethod("$notNull")
227228
@tu lazy val CompiletimeTestingPackageObject: Symbol = ctx.requiredModule("scala.compiletime.testing.package")
228229
@tu lazy val CompiletimeTesting_typeChecks: Symbol = CompiletimeTestingPackageObject.requiredMethod("typeChecks")
229230
@tu lazy val CompiletimeTesting_typeCheckErrors: Symbol = CompiletimeTestingPackageObject.requiredMethod("typeCheckErrors")

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import transform.SymUtils._
4141
import transform.TypeUtils._
4242
import reporting.trace
4343
import Nullables.{NotNullInfo, given}
44+
import NullOpsDecorator._
4445

4546
object Typer {
4647

@@ -417,7 +418,20 @@ class Typer extends Namer
417418
tree.withType(ownType)
418419
}
419420

420-
checkStableIdentPattern(tree1, pt)
421+
val tree2 = ownType match {
422+
case ot: TermRef
423+
if ctx.explicitNulls &&
424+
pt != AssignProto && // Ensure it is not the lhs of Assign
425+
ctx.notNullInfos.impliesNotNull(ot) =>
426+
Apply(
427+
TypeApply(Ident(defn.Compiletime_notNull.namedType), TypeTree(ownType.stripNull) :: Nil),
428+
tree1 :: Nil)
429+
case _ =>
430+
tree1
431+
}
432+
433+
434+
checkStableIdentPattern(tree2, pt)
421435
}
422436

423437
/** Check that a stable identifier pattern is indeed stable (SLS 8.1.5)
@@ -1554,16 +1568,6 @@ class Typer extends Namer
15541568
typed(annot, defn.AnnotationClass.typeRef)
15551569

15561570
def typedValDef(vdef: untpd.ValDef, sym: Symbol)(implicit ctx: Context): Tree = {
1557-
sym.infoOrCompleter match
1558-
case completer: Namer#Completer
1559-
if completer.creationContext.notNullInfos ne ctx.notNullInfos =>
1560-
// The RHS of a val def should know about not null facts established
1561-
// in preceding statements (unless the ValDef is completed ahead of time,
1562-
// then it is impossible).
1563-
vdef.symbol.info = Completer(completer.original)(
1564-
given completer.creationContext.withNotNullInfos(ctx.notNullInfos))
1565-
case _ =>
1566-
15671571
val ValDef(name, tpt, _) = vdef
15681572
completeAnnotations(vdef, sym)
15691573
if (sym.isOneOf(GivenOrImplicit)) checkImplicitConversionDefOK(sym)
@@ -2223,6 +2227,19 @@ class Typer extends Namer
22232227
ctx // all preceding statements will have been executed in this case
22242228
case _ =>
22252229
ctx.withNotNullInfos(initialNotNullInfos)
2230+
// We have to check the Completer of symbol befor typedValDef,
2231+
// otherwise the symbol is already completed using creation context.
2232+
mdef.getAttachment(SymOfTree).map(s => (s, s.infoOrCompleter)) match {
2233+
case Some((sym, completer: Namer#Completer))
2234+
if completer.creationContext.notNullInfos ne defCtx.notNullInfos =>
2235+
// The RHS of a val def should know about not null facts established
2236+
// in preceding statements (unless the ValDef is completed ahead of time,
2237+
// then it is impossible).
2238+
sym.info = Completer(completer.original)(
2239+
given completer.creationContext.withNotNullInfos(defCtx.notNullInfos))
2240+
case _ =>
2241+
}
2242+
22262243
typed(mdef)(given defCtx) match {
22272244
case mdef1: DefDef if !Inliner.bodyToInline(mdef1.symbol).isEmpty =>
22282245
buf += inlineExpansion(mdef1)

library/src/scala/compiletime/package.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,11 @@ package object compiletime {
6363
* }
6464
*/
6565
type S[N <: Int] <: Int
66+
67+
/** Strip the Null type from x
68+
*
69+
* val x: String|Null = ???
70+
* val _:String = $notNull[String](x)
71+
*/
72+
inline def $notNull[A](x: A | Null): x.type & A = x.asInstanceOf
6673
}

tests/explicit-nulls/pos/widen-nullable-union.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Test {
3535
}
3636

3737
locally {
38-
val x: (A | Null) & (Null | B) = ???
38+
val x: (A | Null) & (B | Null) = ???
3939
val y = x
4040
val _: (A & B) | Null = y
4141
}

0 commit comments

Comments
 (0)