-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix checking ctx to carry correct modes #15350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c48bf46
0988a8d
f37acc8
f5c01c1
6095a12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1059,7 +1059,18 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer | |
val (stats1, exprCtx) = withoutMode(Mode.Pattern) { | ||
typedBlockStats(tree.stats) | ||
} | ||
val expr1 = typedExpr(tree.expr, pt.dropIfProto)(using exprCtx) | ||
var expr1 = typedExpr(tree.expr, pt.dropIfProto)(using exprCtx) | ||
|
||
// If unsafe nulls is enabled inside a block but not enabled outside | ||
olhotak marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment does not correspond to the code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the comment is correct: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The confusion is that the mode is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, my bad. I misread the code. |
||
// and the type does not conform the expected type without unsafe nulls, | ||
// we will cast the last expression to the expected type. | ||
// See: tests/explicit-nulls/pos/unsafe-block.scala | ||
if ctx.mode.is(Mode.SafeNulls) | ||
&& !exprCtx.mode.is(Mode.SafeNulls) | ||
&& pt.isValueType | ||
&& !inContext(exprCtx.addMode(Mode.SafeNulls))(expr1.tpe <:< pt) then | ||
expr1 = expr1.cast(pt) | ||
|
||
ensureNoLocalRefs( | ||
cpy.Block(tree)(stats1, expr1) | ||
.withType(expr1.tpe) | ||
|
@@ -2602,7 +2613,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer | |
|The selector is not a member of an object or package.""") | ||
else typd(imp.expr, AnySelectionProto) | ||
|
||
def typedImport(imp: untpd.Import, sym: Symbol)(using Context): Tree = | ||
def typedImport(imp: untpd.Import)(using Context): Tree = | ||
val sym = retrieveSym(imp) | ||
val expr1 = typedImportQualifier(imp, typedExpr(_, _)(using ctx.withOwner(sym))) | ||
checkLegalImportPath(expr1) | ||
val selectors1 = typedSelectors(imp.selectors) | ||
|
@@ -2868,7 +2880,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer | |
case tree: untpd.If => typedIf(tree, pt) | ||
case tree: untpd.Function => typedFunction(tree, pt) | ||
case tree: untpd.Closure => typedClosure(tree, pt) | ||
case tree: untpd.Import => typedImport(tree, retrieveSym(tree)) | ||
case tree: untpd.Import => typedImport(tree) | ||
case tree: untpd.Export => typedExport(tree) | ||
case tree: untpd.Match => typedMatch(tree, pt) | ||
case tree: untpd.Return => typedReturn(tree) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMessageID]: | ||
|
||
case NoExplanationID // errorNumber: -1 | ||
case EmptyCatchOrFinallyBlockID extends ErrorMessageID(isActive = false) // errorNumber: 0 | ||
|
||
def errorNumber = ordinal - 1 | ||
|
||
enum Color(val rgb: Int): | ||
case Red extends Color(0xFF0000) | ||
case Green extends Color(0x00FF00) | ||
case Blue extends Color(0x0000FF) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// testNotNull can be inserted during PatternMatcher | ||
def f(xs: List[String]) = | ||
xs.zipWithIndex.collect { | ||
case (arg, idx) => idx | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
def trim(x: String | Null): String = | ||
import scala.language.unsafeNulls | ||
// The type of `x.trim()` is `String | Null`. | ||
// Although `String | Null` conforms the expected type `String`, | ||
// we still need to cast the expression to the expected type here, | ||
// because outside the scope we don't have `unsafeNulls` anymore. | ||
x.trim() | ||
|
||
class TestDefs: | ||
|
||
def f1: String | Null = null | ||
def f2: Array[String | Null] | Null = null | ||
def f3: Array[String] | Null = null | ||
|
||
def h1a: String = | ||
import scala.language.unsafeNulls | ||
f1 | ||
|
||
def h1b: String | Null = | ||
import scala.language.unsafeNulls | ||
f1 | ||
|
||
def h2a: Array[String] = | ||
import scala.language.unsafeNulls | ||
f2 | ||
|
||
def h2b: Array[String | Null] = | ||
import scala.language.unsafeNulls | ||
f2 | ||
|
||
def h3a: Array[String] = | ||
import scala.language.unsafeNulls | ||
f3 | ||
|
||
def h3b: Array[String | Null] = | ||
import scala.language.unsafeNulls | ||
f3 | ||
|
||
class TestVals: | ||
|
||
val f1: String | Null = null | ||
val f2: Array[String | Null] | Null = null | ||
val f3: Array[String] | Null = null | ||
|
||
val h1a: String = | ||
import scala.language.unsafeNulls | ||
f1 | ||
|
||
val h1b: String | Null = | ||
import scala.language.unsafeNulls | ||
f1 | ||
|
||
val h2a: Array[String] = | ||
import scala.language.unsafeNulls | ||
f2 | ||
|
||
val h2b: Array[String | Null] = | ||
import scala.language.unsafeNulls | ||
f2 | ||
|
||
val h3a: Array[String] = | ||
import scala.language.unsafeNulls | ||
f3 | ||
|
||
val h3b: Array[String | Null] = | ||
import scala.language.unsafeNulls | ||
f3 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
import java.nio.file.FileSystems | ||
import java.util.ArrayList | ||
|
||
def directorySeparator: String = | ||
import scala.language.unsafeNulls | ||
FileSystems.getDefault().getSeparator() | ||
class A: | ||
|
||
def directorySeparator: String = | ||
import scala.language.unsafeNulls | ||
FileSystems.getDefault().getSeparator() | ||
|
||
def getFirstOfFirst(xs: ArrayList[ArrayList[ArrayList[String]]]): String = | ||
import scala.language.unsafeNulls | ||
xs.get(0).get(0).get(0) | ||
|
||
def getFirstOfFirst(xs: ArrayList[ArrayList[ArrayList[String]]]): String = | ||
class B: | ||
import scala.language.unsafeNulls | ||
xs.get(0).get(0).get(0) | ||
|
||
def directorySeparator: String = | ||
FileSystems.getDefault().getSeparator() | ||
|
||
def getFirstOfFirst(xs: ArrayList[ArrayList[ArrayList[String]]]): String = | ||
xs.get(0).get(0).get(0) |
Uh oh!
There was an error while loading. Please reload this page.