Skip to content

Commit de3fbe2

Browse files
authored
Merge pull request #10354 from dotty-staging/opaquify-assert
Make `assert` work as a plain inline function
2 parents cc3da61 + a483d4a commit de3fbe2

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

compiler/src/dotty/tools/dotc/transform/Erasure.scala

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -588,16 +588,17 @@ object Erasure {
588588
tree.withType(erasure(tree.tpe))
589589

590590
/** This override is only needed to semi-erase type ascriptions */
591-
override def typedTyped(tree: untpd.Typed, pt: Type)(using Context): Tree = {
591+
override def typedTyped(tree: untpd.Typed, pt: Type)(using Context): Tree =
592592
val Typed(expr, tpt) = tree
593-
val tpt1 = tpt match {
594-
case Block(_, tpt) => tpt // erase type aliases (statements) from type block
595-
case tpt => tpt
596-
}
597-
val tpt2 = typedType(tpt1)
598-
val expr1 = typed(expr, tpt2.tpe)
599-
assignType(untpd.cpy.Typed(tree)(expr1, tpt2), tpt2)
600-
}
593+
if tpt.typeOpt.typeSymbol == defn.UnitClass then
594+
typed(expr, defn.UnitType)
595+
else
596+
val tpt1 = tpt match
597+
case Block(_, tpt) => tpt // erase type aliases (statements) from type block
598+
case tpt => tpt
599+
val tpt2 = typedType(tpt1)
600+
val expr1 = typed(expr, tpt2.tpe)
601+
assignType(untpd.cpy.Typed(tree)(expr1, tpt2), tpt2)
601602

602603
override def typedLiteral(tree: untpd.Literal)(using Context): Tree =
603604
if (tree.typeOpt.isRef(defn.UnitClass))

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import ast.{tpd, untpd, Trees}
1212
import Trees._
1313
import scala.util.control.NonFatal
1414
import util.Spans.Span
15+
import Nullables._
1516

1617
/** A version of Typer that keeps all symbols defined and referenced in a
1718
* previously typed tree.
@@ -53,13 +54,15 @@ class ReTyper extends Typer with ReChecking {
5354

5455
override def typedTyped(tree: untpd.Typed, pt: Type)(using Context): Tree = {
5556
assertTyped(tree)
57+
5658
val tpt1 = checkSimpleKinded(typedType(tree.tpt))
5759
val expr1 = tree.expr match {
5860
case id: untpd.Ident if (ctx.mode is Mode.Pattern) && untpd.isVarPattern(id) && (id.name == nme.WILDCARD || id.name == nme.WILDCARD_STAR) =>
5961
tree.expr.withType(tpt1.tpe)
6062
case _ => typed(tree.expr)
6163
}
62-
untpd.cpy.Typed(tree)(expr1, tpt1).withType(tree.typeOpt)
64+
val result = untpd.cpy.Typed(tree)(expr1, tpt1).withType(tree.typeOpt)
65+
if ctx.mode.isExpr then result.withNotNullInfo(expr1.notNullInfo) else result
6366
}
6467

6568
override def typedTypeTree(tree: untpd.TypeTree, pt: Type)(using Context): TypeTree =

library/src/dotty/DottyPredef.scala

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ package dotty
33
object DottyPredef {
44
import compiletime.summonFrom
55

6-
inline final def assert(inline assertion: Boolean, inline message: => Any): Unit = {
7-
if (!assertion)
8-
scala.runtime.Scala3RunTime.assertFailed(message)
9-
}
6+
inline def assert(inline assertion: Boolean, inline message: => Any): Unit =
7+
if !assertion then scala.runtime.Scala3RunTime.assertFailed(message)
108

11-
transparent inline final def assert(inline assertion: Boolean): Unit = {
12-
if (!assertion)
13-
scala.runtime.Scala3RunTime.assertFailed()
14-
}
9+
inline def assert(inline assertion: Boolean): Unit =
10+
if !assertion then scala.runtime.Scala3RunTime.assertFailed()
1511

1612
/**
1713
* Retrieve the single value of a type with a unique inhabitant.

tests/run/nullAsInstanceOf.check

-2 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)