Skip to content

Fix #10951: update typedDynamicSelect #10952

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 3 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/untpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,12 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
def derivedTree(originalSym: Symbol)(using Context): tpd.Tree
}

/** A type tree that memoizes the result of a typed tree. Used when
* typechecking selectDynamic calls to prevent typechecking type arguments
* multiple types.
*/
final class MemoizedTypeTree(val result: tpd.Tree)(implicit @constructorOnly src: SourceFile) extends TypeTree

/** Property key containing TypeTrees whose type is computed
* from the symbol in this type. These type trees have marker trees
* TypeRefOfSym or InfoOfSym as their originals.
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ trait Applications extends Compatibility {
case _ =>
}
def tryDynamicTypeApply(): Tree = typedFn match {
case typedFn: Select if !pt.isInstanceOf[FunProto] => typedDynamicSelect(typedFn, typedArgs, pt)
case typedFn: Select if !pt.isInstanceOf[FunProto] => typedDynamicSelect(typedFn, typedArgs.map(untpd.MemoizedTypeTree(_)), pt)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's see whether we find a way to avoid MemoizedTypeTree. Why did it not work with typedArgs in the first place? What issue gets fixed by wrapping the arguments in MemoizedTypeTree?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the issue with typedArgs was that after
tree.args: List(Tuple(List(Ident(Int), Ident(Int))))

gets typed to
typedArgs: List(AppliedTypeTree(TypeTree[TypeRef(...,class Tuple2)],List(Ident(Int), Ident(Int))))

it fails when it tries to type check the AppliedTypeTree again because typedAppliedTypeTree sets pt to AnyTypeConstructorProto
https://github.com/lampepfl/dotty/blob/04ea25cfe9c77f612964ba306f070ba058ee695c/compiler/src/dotty/tools/dotc/typer/Typer.scala#L1700-L1703

So I wrapped typedArgs with MemoizedTypeTree so that typedTypeTree could know that the args were already type checked and return the cached results instead of an error type. doing a hasType check + cast worked as well, but having an explicit memoize type tree seemed like a safer solution.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for explaining! We actually already have an analogous memoization mechanism in place. It's untpd.TypedSplice. Can you try to simply replace MemoizedTypeTree with untpd.TypedSplice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for the suggestion! i've updated the PR

case _ => tree.withType(TryDynamicCallType)
}
if (typedFn.tpe eq TryDynamicCallType) tryDynamicTypeApply()
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Dynamic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ trait Dynamic {
* Note: inner part of translation foo.bar(baz) = quux ~~> foo.selectDynamic(bar).update(baz, quux) is achieved
* through an existing transformation of in typedAssign [foo.bar(baz) = quux ~~> foo.bar.update(baz, quux)].
*/
def typedDynamicSelect(tree: untpd.Select, targs: List[Tree], pt: Type)(using Context): Tree =
def typedDynamicSelect(tree: untpd.Select, targs: List[untpd.Tree], pt: Type)(using Context): Tree =
typedApply(coreDynamic(tree.qualifier, nme.selectDynamic, tree.name, tree.span, targs), pt)

/** Translate selection that does not typecheck according to the normal rules into a updateDynamic.
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,7 @@ class Typer extends Namer
case None =>
errorTree(tree, "Something's wrong: missing original symbol for type tree")
}
case tree: untpd.MemoizedTypeTree => tree.result
case _ =>
tree.withType(
if (isFullyDefined(pt, ForceDegree.flipBottom)) pt
Expand Down
8 changes: 8 additions & 0 deletions tests/pos/i10951.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.language.dynamics

object Dyn extends Dynamic:
def selectDynamic[T](name: String): Option[T] = None

val a: Option[(Int, Int)] = Dyn.asdf[Tuple2[Int, Int]]
val b: Option[(Int, Int)] = Dyn.selectDynamic[(Int, Int)]("asdf")
val c: Option[(Int, Int)] = Dyn.asdf[(Int, Int)]