From 20edaf60ac510b2b7ba39bc7979b1b905b2ecde2 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 8 Apr 2021 11:40:34 +0200 Subject: [PATCH] Remove call to `constValue` if it fails to expand This avoids subsequet failures on this call. Fixes #11985 --- .../src/dotty/tools/dotc/typer/Inliner.scala | 7 ++++-- tests/neg/i11985.scala | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/neg/i11985.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Inliner.scala b/compiler/src/dotty/tools/dotc/typer/Inliner.scala index 408c8ad9de65..d5893f44f631 100644 --- a/compiler/src/dotty/tools/dotc/typer/Inliner.scala +++ b/compiler/src/dotty/tools/dotc/typer/Inliner.scala @@ -684,8 +684,11 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) { if (callTypeArgs.length == 1) if (inlinedMethod == defn.Compiletime_constValue) { val constVal = tryConstValue - if (!constVal.isEmpty) return constVal - report.error(em"not a constant type: ${callTypeArgs.head}; cannot take constValue", call.srcPos) + if constVal.isEmpty then + val msg = em"not a constant type: ${callTypeArgs.head}; cannot take constValue" + return ref(defn.Predef_undefined).withSpan(call.span).withType(ErrorType(msg)) + else + return constVal } else if (inlinedMethod == defn.Compiletime_constValueOpt) { val constVal = tryConstValue diff --git a/tests/neg/i11985.scala b/tests/neg/i11985.scala new file mode 100644 index 000000000000..fee056594974 --- /dev/null +++ b/tests/neg/i11985.scala @@ -0,0 +1,24 @@ +import compiletime._ +import compiletime.ops.int._ + +object Test { + type TupleTypeIndex[T <: Tuple, C] <: Int = T match { + case C *: t => 0 + case h *: t => S[TupleTypeIndex[t, C]] + } + + trait TupleExtractor[TT <: Tuple, C] { + def get(t: TT): C + } + + given [T <: Tuple, C, EV <: TupleTypeIndex[T, C]]: TupleExtractor[T, C] with { + def get(t: T): C = t.toArray.apply(toIntC[TupleTypeIndex[T, C]]).asInstanceOf[C] // error + } + + + transparent inline def toIntC[N <: Int]: Int = + inline constValue[N] match + case 0 => 0 + case _: S[n1] => 1 + toIntC[n1] + +}