Skip to content

Commit 9135604

Browse files
committed
Java Select: try typing as both SelectFromTypeTree and Select
1 parent 9bffab2 commit 9135604

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
168168
selectors match {
169169
case Pair(Ident(from), Ident(Name)) :: rest =>
170170
val selName = if (name.isTypeName) from.toTypeName else from
171-
checkUnambiguous(selectionType(site, selName, tree.pos))
171+
checkUnambiguous(selectionType(site, selName, tree.pos)(refctx))
172172
case Ident(Name) :: rest =>
173-
checkUnambiguous(selectionType(site, name, tree.pos))
173+
checkUnambiguous(selectionType(site, name, tree.pos)(refctx))
174174
case _ :: rest =>
175175
namedImportRef(site, rest)
176176
case nil =>
@@ -270,9 +270,40 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
270270
}
271271

272272
def typedSelect(tree: untpd.Select, pt: Type)(implicit ctx: Context): Tree = track("typedSelect") {
273-
val qual1 = typedExpr(tree.qualifier, selectionProto(tree.name, pt, this))
274-
if (tree.name.isTypeName) checkStable(qual1.tpe, qual1.pos)
275-
checkValue(assignType(cpy.Select(tree, qual1, tree.name), qual1), pt)
273+
def asSelect(implicit ctx: Context): Tree = {
274+
val qual1 = typedExpr(tree.qualifier, selectionProto(tree.name, pt, this))
275+
if (tree.name.isTypeName) checkStable(qual1.tpe, qual1.pos)
276+
checkValue(assignType(cpy.Select(tree, qual1, tree.name), qual1), pt)
277+
}
278+
279+
def asJavaSelectFromTypeTree(implicit ctx: Context): Tree = {
280+
// Translate names in Select/Ident nodes to type names.
281+
def convertToTypeName(tree: untpd.Tree): Option[untpd.Tree] = tree match {
282+
case Select(qual, name) => Some(untpd.Select(qual, name.toTypeName))
283+
case Ident(name) => Some(untpd.Ident(name.toTypeName))
284+
case _ => None
285+
}
286+
287+
// Try to convert Select(qual, name) to a SelectFromTypeTree.
288+
def convertToSelectFromType(qual: untpd.Tree, origName: Name): Option[untpd.SelectFromTypeTree] =
289+
convertToTypeName(qual) match {
290+
case Some(qual1) => Some(untpd.SelectFromTypeTree(qual1 withPos qual.pos, origName.toTypeName))
291+
case _ => None
292+
}
293+
294+
convertToSelectFromType(tree.qualifier, tree.name) match {
295+
case Some(sftt) =>
296+
println(s"$tree converted to $sftt")
297+
typedSelectFromTypeTree(sftt, pt)
298+
case _ => ctx.error(d"Could not convert $tree to a SelectFromTypeTree"); EmptyTree
299+
}
300+
}
301+
302+
if(ctx.compilationUnit.isJava && tree.name.isTypeName) {
303+
// SI-3120 Java uses the same syntax, A.B, to express selection from the
304+
// value A and from the type A. We have to try both.
305+
tryEither(tryCtx => asSelect(tryCtx))((_,_) => asJavaSelectFromTypeTree(ctx))
306+
} else asSelect(ctx)
276307
}
277308

278309
def typedSelectFromTypeTree(tree: untpd.SelectFromTypeTree, pt: Type)(implicit ctx: Context): SelectFromTypeTree = track("typedSelectFromTypeTree") {

0 commit comments

Comments
 (0)