Skip to content

Commit 77ec689

Browse files
authored
Merge pull request #7830 from dotty-staging/fix-flow
Avoid nonsensical error message in processByNameArgs
2 parents d45fea0 + a5f5101 commit 77ec689

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ object ErrorReporting {
8989
if (tree.tpe.widen.exists)
9090
i"${exprStr(tree)} does not take ${kind}parameters"
9191
else {
92-
if (ctx.settings.Ydebug.value) new FatalError("").printStackTrace()
9392
i"undefined: $tree # ${tree.uniqueId}: ${tree.tpe.toString} at ${ctx.phase}"
9493
}
9594

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ object Nullables with
464464
*/
465465
def postProcessByNameArgs(fn: TermRef, app: Tree)(given ctx: Context): Tree =
466466
fn.widen match
467-
case mt: MethodType if mt.paramInfos.exists(_.isInstanceOf[ExprType]) =>
467+
case mt: MethodType
468+
if mt.paramInfos.exists(_.isInstanceOf[ExprType]) && !fn.symbol.is(Inline) =>
468469
app match
469470
case Apply(fn, args) =>
470471
val dropNotNull = new TreeMap with
@@ -487,10 +488,10 @@ object Nullables with
487488
case _ => super.typedUnadapted(t, pt, locked)
488489

489490
def postProcess(formal: Type, arg: Tree): Tree =
490-
val arg1 = dropNotNull.transform(arg)
491+
val nestedCtx = ctx.fresh.setNewTyperState()
492+
val arg1 = dropNotNull.transform(arg)(given nestedCtx)
491493
if arg1 eq arg then arg
492494
else
493-
val nestedCtx = ctx.fresh.setNewTyperState()
494495
val arg2 = retyper.typed(arg1, formal)(given nestedCtx)
495496
if nestedCtx.reporter.hasErrors || !(arg2.tpe <:< formal) then
496497
ctx.error(em"""This argument was typed using flow assumptions about mutable variables
@@ -506,7 +507,10 @@ object Nullables with
506507

507508
def recur(formals: List[Type], args: List[Tree]): List[Tree] = (formals, args) match
508509
case (formal :: formalsRest, arg :: argsRest) =>
509-
val arg1 = postProcess(formal.widenExpr.repeatedToSingle, arg)
510+
val arg1 =
511+
if formal.isInstanceOf[ExprType]
512+
then postProcess(formal.widenExpr.repeatedToSingle, arg)
513+
else arg
510514
val argsRest1 = recur(
511515
if formal.isRepeatedParam then formals else formalsRest,
512516
argsRest)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ trait TypeAssigner {
411411
else
412412
errorType(i"wrong number of arguments at ${ctx.phase.prev} for $fntpe: ${fn.tpe}, expected: ${fntpe.paramInfos.length}, found: ${args.length}", tree.sourcePos)
413413
case t =>
414+
if (ctx.settings.Ydebug.value) new FatalError("").printStackTrace()
414415
errorType(err.takesNoParamsStr(fn, ""), tree.sourcePos)
415416
}
416417
ConstFold(tree.withType(ownType))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Error: tests/neg-custom-args/explicit-nulls/byname-nullables1.scala:10:6 --------------------------------------------
2+
10 | f(x.fld != null) // error
3+
| ^^^^^^^^^^^^^
4+
| This argument was typed using flow assumptions about mutable variables
5+
| but it is passed to a by-name parameter where such flow assumptions are unsound.
6+
| Wrapping the argument in `byName(...)` fixes the problem by disabling the flow assumptions.
7+
|
8+
| `byName` needs to be imported from the `scala.compiletime` package.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def f(op: => Boolean): Unit = ()
2+
def f(op: Int): Unit = ()
3+
4+
class C with
5+
var fld: String | Null = null
6+
7+
def test() =
8+
var x: C | Null = C()
9+
if x != null then
10+
f(x.fld != null) // error
11+
require(x.fld != null, "error") // ok

0 commit comments

Comments
 (0)