Skip to content

Commit 3408f4e

Browse files
Merge pull request #9443 from dotty-staging/fix-#9321
Fix #9321: Make not-null detection more robust
2 parents 0655346 + fc43fe5 commit 3408f4e

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,11 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
907907
def unapply(tree: tpd.TypeApply)(using Context): Option[tpd.Tree] = tree match
908908
case TypeApply(Select(qual: RefTree, nme.asInstanceOfPM), arg :: Nil) =>
909909
arg.tpe match
910-
case AndType(ref, _) if qual.tpe eq ref => Some(qual)
910+
case AndType(ref, nn1) if qual.tpe eq ref =>
911+
qual.tpe.widen match
912+
case OrNull(nn2) if nn1 eq nn2 =>
913+
Some(qual)
914+
case _ => None
911915
case _ => None
912916
case _ => None
913917
end AssertNotNull

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)