Skip to content

Commit 6bdb9f0

Browse files
committed
Add tests
1 parent 77df754 commit 6bdb9f0

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import Constants._
1111
import Names._
1212
import StdNames._
1313
import Contexts._
14-
import Nullables.{CompareNull, TrackedRef}
1514
import NullOpsDecorator._
1615

1716
object ConstFold {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ object Nullables with
104104
/** Is given reference tracked for nullability?
105105
* This is the case if the reference is a path to an immutable val, or if it refers
106106
* to a local mutable variable where all assignments to the variable are _reachable_
107-
* (in the sense of how it is defined in assignmentSpans).
107+
* (in the sense of how it is defined in assignmentSpans). We use this function to decide
108+
* whether we need to compute the NotNullInfo and add it to the context. This requires the
109+
* use of a mutable variable is not out of order.
108110
*
109111
* When dealing with local mutable variables, there are two questions:
110112
*
@@ -149,9 +151,7 @@ object Nullables with
149151
def isTracked(ref: TermRef)(given Context) =
150152
ref.isStable
151153
|| { val sym = ref.symbol
152-
sym.is(Mutable)
153-
&& sym.owner.isTerm
154-
&& !ref.usedOutOfOrder // todo: remove
154+
!ref.usedOutOfOrder
155155
&& sym.span.exists
156156
&& curCtx.compilationUnit != null // could be null under -Ytest-pickler
157157
&& curCtx.compilationUnit.assignmentSpans.contains(sym.span.start)
@@ -204,7 +204,8 @@ object Nullables with
204204

205205
given refOps: extension (ref: TermRef) with
206206

207-
def usedOutOfOrder(given Context) =
207+
/* Is the use of a mutable variable out of order */
208+
def usedOutOfOrder(given Context): Boolean =
208209
val refSym = ref.symbol
209210
val refOwner = refSym.owner
210211

tests/explicit-nulls/neg/var-ref-in-closure.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,24 @@ object VarRef {
149149
x
150150
}
151151
}
152+
153+
locally {
154+
val x: String|Null = ???
155+
if (x != null) {
156+
def f = {
157+
val y: String = x // ok, x is a value definition
158+
y
159+
}
160+
}
161+
}
162+
163+
locally {
164+
var x: String|Null = ???
165+
if (x != null) {
166+
def f = {
167+
val y: String = x // error: the use of x is out of order
168+
y
169+
}
170+
}
171+
}
152172
}

0 commit comments

Comments
 (0)