Skip to content

Commit

Permalink
Detail "not a constant type" message (#17626)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand authored Jun 1, 2023
2 parents fa22c9c + 8fff722 commit 365e271
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/inlines/Inlines.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import transform.{PostTyper, Inlining, CrossVersionChecks}
import staging.StagingLevel

import collection.mutable
import reporting.trace
import reporting.{NotConstant, trace}
import util.Spans.Span

/** Support for querying inlineable methods and for inlining calls to such methods */
Expand Down Expand Up @@ -413,7 +413,7 @@ object Inlines:
if (inlinedMethod == defn.Compiletime_constValue) {
val constVal = tryConstValue
if constVal.isEmpty then
val msg = em"not a constant type: ${callTypeArgs.head}; cannot take constValue"
val msg = NotConstant("cannot take constValue", callTypeArgs.head.tpe)
return ref(defn.Predef_undefined).withSpan(call.span).withType(ErrorType(msg))
else
return constVal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
case MatchTypeScrutineeCannotBeHigherKindedID // errorNumber: 179
case AmbiguousExtensionMethodID // errorNumber 180
case UnqualifiedCallToAnyRefMethodID // errorNumber: 181
case NotConstantID // errorNumber: 182

def errorNumber = ordinal - 1

Expand Down
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2624,6 +2624,13 @@ extends TypeMsg(NotClassTypeID), ShowMatchTrace(tp):
def msg(using Context) = i"$tp is not a class type"
def explain(using Context) = ""

class NotConstant(suffix: String, tp: Type)(using Context)
extends TypeMsg(NotConstantID), ShowMatchTrace(tp):
def msg(using Context) =
i"$tp is not a constant type"
+ (if suffix.isEmpty then "" else i"; $suffix")
def explain(using Context) = ""

class MissingImplicitArgument(
arg: tpd.Tree,
pt: Type,
Expand Down
14 changes: 14 additions & 0 deletions tests/neg/17211.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- [E182] Type Error: tests/neg/17211.scala:14:12 ----------------------------------------------------------------------
14 | constValue[IsInt[Foo.Foo]] // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| IsInt[Foo.Foo] is not a constant type; cannot take constValue
|
| Note: a match type could not be fully reduced:
|
| trying to reduce IsInt[Foo.Foo]
| failed since selector Foo.Foo
| does not match case Int => (true : Boolean)
| and cannot be shown to be disjoint from it either.
| Therefore, reduction cannot advance to the remaining case
|
| case _ => (false : Boolean)
14 changes: 14 additions & 0 deletions tests/neg/17211.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import scala.compiletime.constValue

type IsInt[A] = A match
case Int => true
case _ => false

def test =
val x = constValue[IsInt[Int]] // val res2: Boolean = true; works
val y = constValue[IsInt[String]] // val res3: Boolean = false; works

object Foo:
opaque type Foo = Int

constValue[IsInt[Foo.Foo]] // error

0 comments on commit 365e271

Please sign in to comment.