Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3154,6 +3154,21 @@ object Types extends TypeUtils {
}
}

/** Extractor that matches a type that contains a single atom and returns that
* atom.
*
* See also [[Atoms]].
*/
object SingleAtom:
def unapply(tp: Type)(using Context): Option[Type] =
tp.atoms match
case Atoms.Range(lo, hi) =>
if lo.size == 1 then Some(lo.head)
else if hi.size == 1 then Some(hi.head)
else None
case _ =>
None

// `refFn` can be null only if `computed` is true.
case class LazyRef(private var refFn: (Context => (Type | Null)) | Null) extends UncachedProxyType with ValueType {
private var myRef: Type | Null = null
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/ConstFold.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ object ConstFold:
case Literal(c) if c.tag == Constants.NullTag => Some(c)
case _ =>
tree.tpe.widenTermRefExpr.normalized.simplified match
case ConstantType(c) => Some(c)
case SingleAtom(ConstantType(c)) => Some(c)
case _ => None

extension [T <: Tree](tree: T)(using Context)
Expand Down
23 changes: 23 additions & 0 deletions tests/pos/constfold-atoms.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class annot() extends scala.annotation.StaticAnnotation
class refiningAnnot() extends scala.annotation.RefiningAnnotation

type Foo = 4

@main def main =
val v1: 4 = 2 + 2
val v2: 8 = v1 + v1

val v3: 4 | v1.type = 4
val v4: 8 = v3 + v3

val v5: 4 & v1.type = 4
val v6: 8 = v5 + v5

val v7: 4 @annot = 4
val v8: 8 = v7 + v7

val v9: 4 @refiningAnnot = ???
val v10: 8 = v9 + v9

val v11: Foo = 4
val v12: 8 = v11 + v11
Loading