Skip to content

Commit f5dde7f

Browse files
oderskynicolasstucki
authored andcommitted
Fix #9321: Make not-null detection more robust
We can't rely on a test whether a tree transform yields the same tree, since typed tree copiers do not always map equal sub-trees to equal results.
1 parent 0655346 commit f5dde7f

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,12 @@ object Nullables:
480480
if mt.paramInfos.exists(_.isInstanceOf[ExprType]) && !fn.symbol.is(Inline) =>
481481
app match
482482
case Apply(fn, args) =>
483-
val dropNotNull = new TreeMap:
483+
object dropNotNull extends TreeMap:
484+
var dropped: Boolean = false
484485
override def transform(t: Tree)(using Context) = t match
485486
case AssertNotNull(t0) if t0.symbol.is(Mutable) =>
486487
nullables.println(i"dropping $t")
488+
dropped = true
487489
transform(t0)
488490
case t: ValDef if !t.symbol.is(Lazy) => super.transform(t)
489491
case t: MemberDef =>
@@ -502,7 +504,7 @@ object Nullables:
502504
def postProcess(formal: Type, arg: Tree): Tree =
503505
val nestedCtx = ctx.fresh.setNewTyperState()
504506
val arg1 = dropNotNull.transform(arg)(using nestedCtx)
505-
if arg1 eq arg then arg
507+
if !dropNotNull.dropped then arg
506508
else
507509
val arg2 = retyper.typed(arg1, formal)(using nestedCtx)
508510
if nestedCtx.reporter.hasErrors || !(arg2.tpe <:< formal) then

tests/pos-macros/i9321/macros.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import scala.quoted._
2+
3+
type Foo
4+
type F[X]
5+
def varargsFunc(funcs0: Foo*) = ???
6+
7+
inline def mcr1: F[String] = ${ mcr1Impl }
8+
def mcr1Impl(using QuoteContext): Expr[F[String]] = '{???}
9+
10+
inline def mcr2: Unit = ${mcr2Impl}
11+
def mcr2Impl(using ctx: QuoteContext): Expr[Unit] =
12+
val func: Expr[Seq[Foo] => Unit] =
13+
'{ (esx: Seq[Foo]) => varargsFunc(esx: _*) }
14+
val trees: Expr[Seq[Foo]] =
15+
'{Nil}
16+
Expr.betaReduce(func)(trees)

tests/pos-macros/i9321/test.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def f(x: => Any) = ???
2+
def g = f {
3+
mcr1
4+
mcr2
5+
}

0 commit comments

Comments
 (0)