Skip to content

Fix #8843: Normalize type when constant folding #8852

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
May 2, 2020
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
*/
def constToLiteral(tree: Tree)(implicit ctx: Context): Tree = {
val tree1 = ConstFold(tree)
tree1.tpe.widenTermRefExpr.dealias match {
tree1.tpe.widenTermRefExpr.dealias.normalized match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tree1.tpe.widenTermRefExpr.dealias.normalized match {
tree1.tpe.widenTermRefExpr.normalized match {

I'm wondering if it will make a difference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not work without the dealiasing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should refine normalized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OlivierBlanvillain should normalized dealias?

case ConstantType(value) =>
if (isIdempotentExpr(tree1)) Literal(value).withSpan(tree.span)
else tree1 match {
Expand Down
7 changes: 1 addition & 6 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1002,14 +1002,9 @@ object SymDenotations {
/** Is this a Scala 2 macro */
final def isScala2Macro(implicit ctx: Context): Boolean = is(Macro) && symbol.owner.is(Scala2x)

/** An erased value or an inline method.
*/
/** An erased value or an erased inline method or field */
def isEffectivelyErased(implicit ctx: Context): Boolean =
is(Erased) || is(Inline) && !isRetainedInline && !hasAnnotation(defn.ScalaStaticAnnot)
// Do not mark local inline vals as erased. Currently some inline val references do not get
// fully inlined and then would fail the erased check.
// TODO: remove this condition when #8842 and #8843 are fixed
&& (owner.isClass || is(Method))

/** ()T and => T types should be treated as equivalent for this symbol.
* Note: For the moment, we treat Scala-2 compiled symbols as loose matching,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class PruneErasedDefs extends MiniPhase with SymTransformer { thisTransform =>
else tree

private def trivialErasedTree(tree: Tree)(using Context): Tree =
tree.tpe.widenTermRefExpr.dealias match
tree.tpe.widenTermRefExpr.dealias.normalized match
case ConstantType(c) => Literal(c)
case _ => ref(defn.Predef_undefined)

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ trait Checking {
if sym.is(Inline, butNot = DeferredOrTermParamOrAccessor) && !ctx.erasedTypes && !ctx.inInlineMethod then
// final vals can be marked inline even if they're not pure, see Typer#patchFinalVals
val purityLevel = if (sym.is(Final)) Idempotent else Pure
tpt.tpe.widenTermRefExpr.dealias match
tpt.tpe.widenTermRefExpr.dealias.normalized match
case tp: ConstantType =>
if !(exprPurity(tree) >= purityLevel) then
ctx.error(em"inline value must be pure", tree.sourcePos)
Expand Down
11 changes: 11 additions & 0 deletions tests/pos/i8843.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class C:
type X <: Tuple

inline def f(c: C): Unit = {
inline val size = compiletime.constValue[Tuple.Size[c.X]]
val n = size
val m: Int = n
???
}

def test: Unit = f(??? : C { type X = Tuple1[Any] })