Skip to content

Commit 88ab4ef

Browse files
committed
Avoid nonsensical error message in processByNameArgs
Transforming the argument can already cause errors coming from TypeAssigne rthat need to be caught instead of being issued.
1 parent d45fea0 commit 88ab4ef

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
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: 4 additions & 3 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

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:9:22 --------------------------------------------
2+
9 | if x != null then 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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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 f(x.fld != null) // error

0 commit comments

Comments
 (0)