diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 527e5d4057ef..90210e12dd04 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -2048,14 +2048,16 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer untpd.Block(makeCanThrow(capabilityProof), expr) def typedTry(tree: untpd.Try, pt: Type)(using Context): Try = { - // We want to type check tree.expr first to comput NotNullInfo, but `addCanThrowCapabilities` - // uses the types of patterns in `tree.cases` to determine the capabilities. - // Hence, we create a copy of cases with empty body and type check that first, then type check - // the rest of the tree in order. - val casesEmptyBody1 = tree.cases.mapconserve(cpy.CaseDef(_)(body = EmptyTree)) - val casesEmptyBody2 = typedCases(casesEmptyBody1, EmptyTree, defn.ThrowableType, WildcardType) - val expr2 :: cases2x = harmonic(harmonize, pt) { + // We want to type check tree.expr first to comput NotNullInfo, but `addCanThrowCapabilities` + // uses the types of patterns in `tree.cases` to determine the capabilities. + // Hence, we create a copy of cases with empty body and type check that first, then type check + // the rest of the tree in order. + // It may seem that invalid references can be created if the type of the pattern contains + // type binds, but this is not a valid `CanThrow` capability (checked by `addCanThrowCapabilities`), + // so it is not a problem. + val casesEmptyBody1 = tree.cases.mapconserve(cpy.CaseDef(_)(body = EmptyTree)) + val casesEmptyBody2 = typedCases(casesEmptyBody1, EmptyTree, defn.ThrowableType, WildcardType) val expr1 = typed(addCanThrowCapabilities(tree.expr, casesEmptyBody2), pt.dropIfProto) val casesCtx = ctx.addNotNullInfo(expr1.notNullInfo.retractedInfo) val cases1 = typedCases(tree.cases, EmptyTree, defn.ThrowableType, pt.dropIfProto)(using casesCtx) diff --git a/tests/explicit-nulls/neg/i21380b.scala b/tests/explicit-nulls/neg/i21380b.scala index 83e23053547c..bd28bc63bd26 100644 --- a/tests/explicit-nulls/neg/i21380b.scala +++ b/tests/explicit-nulls/neg/i21380b.scala @@ -3,7 +3,7 @@ def test1 = x = "" 1 match case 1 => x = null - case _ => x = x.trim() // ok + case _ => x = x.trim() // error // LTS specific x.replace("", "") // error def test2(i: Int) = @@ -11,11 +11,11 @@ def test2(i: Int) = i match case 1 => x = "1" case _ => x = " " - x.replace("", "") // ok + x.replace("", "") // error // LTS specific def test3(i: Int) = var x: String | Null = null i match case 1 if x != null => () case _ => x = " " - x.trim() // ok \ No newline at end of file + x.trim() // error // LTS specific \ No newline at end of file diff --git a/tests/explicit-nulls/neg/i21380c.scala b/tests/explicit-nulls/neg/i21380c.scala index f86a5638e4c8..c5758743d784 100644 --- a/tests/explicit-nulls/neg/i21380c.scala +++ b/tests/explicit-nulls/neg/i21380c.scala @@ -3,7 +3,7 @@ def test1(i: Int): Int = if i == 0 then x = "" else x = "" try - x = x.replace(" ", "") // ok + x = x.replace(" ", "") // error // LTS specific throw new Exception() catch case e: Exception => @@ -15,7 +15,7 @@ def test2: Int = var x: String | Null = null try throw new Exception() finally x = "" - x.length // ok + x.length // error // LTS specific def test3 = var x: String | Null = ""