Skip to content

Commit 365e271

Browse files
authored
Detail "not a constant type" message (#17626)
2 parents fa22c9c + 8fff722 commit 365e271

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

compiler/src/dotty/tools/dotc/inlines/Inlines.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import transform.{PostTyper, Inlining, CrossVersionChecks}
1717
import staging.StagingLevel
1818

1919
import collection.mutable
20-
import reporting.trace
20+
import reporting.{NotConstant, trace}
2121
import util.Spans.Span
2222

2323
/** Support for querying inlineable methods and for inlining calls to such methods */
@@ -413,7 +413,7 @@ object Inlines:
413413
if (inlinedMethod == defn.Compiletime_constValue) {
414414
val constVal = tryConstValue
415415
if constVal.isEmpty then
416-
val msg = em"not a constant type: ${callTypeArgs.head}; cannot take constValue"
416+
val msg = NotConstant("cannot take constValue", callTypeArgs.head.tpe)
417417
return ref(defn.Predef_undefined).withSpan(call.span).withType(ErrorType(msg))
418418
else
419419
return constVal

compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
195195
case MatchTypeScrutineeCannotBeHigherKindedID // errorNumber: 179
196196
case AmbiguousExtensionMethodID // errorNumber 180
197197
case UnqualifiedCallToAnyRefMethodID // errorNumber: 181
198+
case NotConstantID // errorNumber: 182
198199

199200
def errorNumber = ordinal - 1
200201

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,6 +2624,13 @@ extends TypeMsg(NotClassTypeID), ShowMatchTrace(tp):
26242624
def msg(using Context) = i"$tp is not a class type"
26252625
def explain(using Context) = ""
26262626

2627+
class NotConstant(suffix: String, tp: Type)(using Context)
2628+
extends TypeMsg(NotConstantID), ShowMatchTrace(tp):
2629+
def msg(using Context) =
2630+
i"$tp is not a constant type"
2631+
+ (if suffix.isEmpty then "" else i"; $suffix")
2632+
def explain(using Context) = ""
2633+
26272634
class MissingImplicitArgument(
26282635
arg: tpd.Tree,
26292636
pt: Type,

tests/neg/17211.check

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- [E182] Type Error: tests/neg/17211.scala:14:12 ----------------------------------------------------------------------
2+
14 | constValue[IsInt[Foo.Foo]] // error
3+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
| IsInt[Foo.Foo] is not a constant type; cannot take constValue
5+
|
6+
| Note: a match type could not be fully reduced:
7+
|
8+
| trying to reduce IsInt[Foo.Foo]
9+
| failed since selector Foo.Foo
10+
| does not match case Int => (true : Boolean)
11+
| and cannot be shown to be disjoint from it either.
12+
| Therefore, reduction cannot advance to the remaining case
13+
|
14+
| case _ => (false : Boolean)

tests/neg/17211.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.compiletime.constValue
2+
3+
type IsInt[A] = A match
4+
case Int => true
5+
case _ => false
6+
7+
def test =
8+
val x = constValue[IsInt[Int]] // val res2: Boolean = true; works
9+
val y = constValue[IsInt[String]] // val res3: Boolean = false; works
10+
11+
object Foo:
12+
opaque type Foo = Int
13+
14+
constValue[IsInt[Foo.Foo]] // error

0 commit comments

Comments
 (0)