Skip to content

Detail "not a constant type" message #17626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2023
Merged
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
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