Skip to content

Recover the denotation of constant-folded selections #11373

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
Feb 11, 2021
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
9 changes: 8 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package ast

import core._
import Types._, Names._, NameOps._, Flags._, util.Spans._, Contexts._, Constants._
import typer.ProtoTypes
import typer.{ ConstFold, ProtoTypes }
import SymDenotations._, Symbols._, Denotations._, StdNames._, Comments._
import language.higherKinds
import collection.mutable.ListBuffer
Expand Down Expand Up @@ -408,6 +408,13 @@ object Trees {
case class Select[-T >: Untyped] private[ast] (qualifier: Tree[T], name: Name)(implicit @constructorOnly src: SourceFile)
extends RefTree[T] {
type ThisTree[-T >: Untyped] = Select[T]

override def denot(using Context): Denotation = typeOpt match
case ConstantType(_) if ConstFold.foldedUnops.contains(name) =>
// Recover the denotation of a constant-folded selection
qualifier.typeOpt.member(name).atSignature(Signature.NotAMethod, name)
case _ =>
super.denot
}

class SelectWithSig[-T >: Untyped] private[ast] (qualifier: Tree[T], name: Name, val sig: Signature)(implicit @constructorOnly src: SourceFile)
Expand Down
14 changes: 13 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/TreeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,25 @@ class TreeChecker extends Phase with SymTransformer {
override def typedSelect(tree: untpd.Select, pt: Type)(using Context): Tree = {
assert(tree.isTerm || !ctx.isAfterTyper, tree.show + " at " + ctx.phase)
val tpe = tree.typeOpt

// Polymorphic apply methods stay structural until Erasure
val isPolyFunctionApply = (tree.name eq nme.apply) && (tree.qualifier.typeOpt <:< defn.PolyFunctionType)
// Outer selects are pickled specially so don't require a symbol
val isOuterSelect = tree.name.is(OuterSelectName)
val isPrimitiveArrayOp = ctx.erasedTypes && nme.isPrimitiveName(tree.name)
if !(tree.isType || isPolyFunctionApply || isOuterSelect || isPrimitiveArrayOp) then
val denot = tree.denot
assert(denot.exists, i"Selection $tree with type $tpe does not have a denotation")
assert(denot.symbol.exists, i"Denotation $denot of selection $tree with type $tpe does not have a symbol")

val sym = tree.symbol
val symIsFixed = tpe match {
case tpe: TermRef => ctx.erasedTypes || !tpe.isMemberRef
case _ => false
}
if (sym.exists && !sym.is(Private) &&
!symIsFixed &&
!tree.name.is(OuterSelectName)) { // outer selects have effectively fixed symbols
!isOuterSelect) { // outer selects have effectively fixed symbols
val qualTpe = tree.qualifier.typeOpt
val member =
if (sym.is(Private)) qualTpe.member(tree.name)
Expand All @@ -403,6 +414,7 @@ class TreeChecker extends Phase with SymTransformer {
|qualifier type : ${tree.qualifier.typeOpt}
|tree type : ${tree.typeOpt} of class ${tree.typeOpt.getClass}""")
}

checkNotRepeated(super.typedSelect(tree, pt))
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/ConstFold.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object ConstFold:
nme.LT, nme.GT, nme.LE, nme.GE, nme.LSL, nme.LSR, nme.ASR,
nme.ADD, nme.SUB, nme.MUL, nme.DIV, nme.MOD)

private val foldedUnops = Set[Name](
val foldedUnops = Set[Name](
nme.UNARY_!, nme.UNARY_~, nme.UNARY_+, nme.UNARY_-)

def Apply[T <: Apply](tree: T)(using Context): T =
Expand Down