Skip to content

Commit 62aa47a

Browse files
committed
Support simple renaming imports in Typer
Support imports import a as b where `a` is a simple identifier.
1 parent 73b4604 commit 62aa47a

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
340340

341341
def selectorText(sel: untpd.ImportSelector): Text =
342342
val id: Text =
343-
if sel.isGiven then keywordText("given") else toText(sel.imported)
343+
if sel.isGiven then keywordText("given") else toText(sel.imported.name)
344344
val rename: Text =
345345
if sel.renamed.isEmpty then "" else Str(" => ") ~ toText(sel.renamed)
346346
val bound: Text =

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ class Namer { typer: Typer =>
693693
// make sure testing contexts are not captured by completers
694694
assert(!ictx.reporter.isInstanceOf[ExploringReporter])
695695

696-
protected def typeSig(sym: Symbol): Type = original match {
696+
protected def typeSig(sym: Symbol): Type = original match
697697
case original: ValDef =>
698698
if (sym.is(Module)) moduleValSig(sym)
699699
else valOrDefDefSig(original, sym, Nil, identity)(using localContext(sym).setNewScope)
@@ -702,16 +702,12 @@ class Namer { typer: Typer =>
702702
nestedTyper(sym) = typer1
703703
typer1.defDefSig(original, sym)(using localContext(sym).setTyper(typer1))
704704
case imp: Import =>
705-
try {
706-
val expr1 = typedAheadExpr(imp.expr, AnySelectionProto)
705+
try
706+
val expr1 = typedImportQualifier(imp, typedAheadExpr)
707707
ImportType(expr1)
708-
}
709-
catch {
710-
case ex: CyclicReference =>
711-
typr.println(s"error while completing ${imp.expr}")
712-
throw ex
713-
}
714-
}
708+
catch case ex: CyclicReference =>
709+
typr.println(s"error while completing ${imp.expr}")
710+
throw ex
715711

716712
final override def complete(denot: SymDenotation)(using Context): Unit = {
717713
if (Config.showCompletions && ctx.typerState != creationContext.typerState) {

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2314,8 +2314,22 @@ class Typer extends Namer
23142314
.asInstanceOf[untpd.ImportSelector]
23152315
}
23162316

2317+
def typedImportQualifier(imp: untpd.Import, typd: (untpd.Tree, Type) => Tree)(using Context): Tree =
2318+
if imp.expr == untpd.EmptyTree then
2319+
assert(imp.selectors.length == 1, imp)
2320+
val from = imp.selectors.head.imported
2321+
val sel = typd(from, WildcardType)
2322+
sel.tpe.dealias match
2323+
case TermRef(prefix: SingletonType, _) =>
2324+
singleton(prefix).withSpan(from.span)
2325+
case _ =>
2326+
errorTree(from,
2327+
em"""Illegal import selector: $from
2328+
|The selector is not a member of an object or package.""")
2329+
else typd(imp.expr, AnySelectionProto)
2330+
23172331
def typedImport(imp: untpd.Import, sym: Symbol)(using Context): Import = {
2318-
val expr1 = typedExpr(imp.expr, AnySelectionProto)
2332+
val expr1 = typedImportQualifier(imp, typedExpr)
23192333
checkLegalImportPath(expr1)
23202334
val selectors1 = typedSelectors(imp.selectors)
23212335
assignType(cpy.Import(imp)(expr1, selectors1), sym)

tests/neg/renaming-imports.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def test =
2+
object foo:
3+
var x = 0
4+
import foo as f // error
5+
f.x
6+

tests/pos/renaming-imports.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import java as j
2+
import collection as c
3+
4+
val x: j.io.IOException = ???
5+
6+
import c.mutable as mut
7+
import mut.ArrayBuffer as Buf
8+
9+
val y = Buf(1, 2, 3)

0 commit comments

Comments
 (0)