Skip to content

Commit 56f993e

Browse files
committed
Refine simple renaming imports and disallow them for exports
More test cases and fixes for simple renaming imports. Disallow them for exports for now, since the primary use case of a renaming export is not handled yet: lampepfl/dotty-feature-requests#148
1 parent db4f837 commit 56f993e

File tree

7 files changed

+36
-9
lines changed

7 files changed

+36
-9
lines changed

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
179179
case _ => false
180180
}
181181

182-
/** Is this argument node of the form <expr> : _*, or is it a reference to
182+
/** Is this argument node of the form <expr> *, or is it a reference to
183183
* such an argument ? The latter case can happen when an argument is lifted.
184184
*/
185185
def isWildcardStarArg(tree: Tree)(using Context): Boolean = unbind(tree) match {

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,7 @@ object Trees {
10271027
type Template = Trees.Template[T]
10281028
type Import = Trees.Import[T]
10291029
type Export = Trees.Export[T]
1030+
type ImportOrExport = Trees.ImportOrExport[T]
10301031
type PackageDef = Trees.PackageDef[T]
10311032
type Annotated = Trees.Annotated[T]
10321033
type Thicket = Trees.Thicket[T]

compiler/src/dotty/tools/dotc/transform/FirstTransform.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class FirstTransform extends MiniPhase with InfoTransformer { thisPhase =>
150150
}
151151

152152
override def transformOther(tree: Tree)(using Context): Tree = tree match {
153-
case tree: ImportOrExport[_] => EmptyTree
153+
case tree: ImportOrExport => EmptyTree
154154
case tree: NamedArg => transformAllDeep(tree.arg)
155155
case tree => if (tree.isType) toTypeTree(tree) else tree
156156
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,10 @@ class Namer { typer: Typer =>
983983
def exportForwarders(exp: Export): List[tpd.MemberDef] = {
984984
val buf = new mutable.ListBuffer[tpd.MemberDef]
985985
val Export(expr, selectors) = exp
986+
if expr.isEmpty then
987+
report.error(em"Export selector must have prefix and `.`", exp.srcPos)
988+
return Nil
989+
986990
val path = typedAheadExpr(expr, AnySelectionProto)
987991
checkLegalExportPath(path, selectors)
988992
lazy val wildcardBound = importBound(selectors, isGiven = false)

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,29 +2318,32 @@ class Typer extends Namer
23182318
if imp.expr == untpd.EmptyTree then
23192319
assert(imp.selectors.length == 1, imp)
23202320
val from = imp.selectors.head.imported
2321-
val sel = typd(from, WildcardType)
2322-
sel.tpe.dealias match
2323-
case TermRef(prefix: SingletonType, _) =>
2321+
val sel = tryAlternatively
2322+
(typedIdent(from, WildcardType))
2323+
(typedIdent(cpy.Ident(from)(from.name.toTypeName), WildcardType))
2324+
2325+
sel.tpe match
2326+
case TermRef(prefix: SingletonType, _) =>
2327+
singleton(prefix).withSpan(from.span)
2328+
case TypeRef(prefix: SingletonType, _) =>
23242329
singleton(prefix).withSpan(from.span)
23252330
case _ =>
23262331
errorTree(from,
23272332
em"""Illegal import selector: $from
23282333
|The selector is not a member of an object or package.""")
23292334
else typd(imp.expr, AnySelectionProto)
23302335

2331-
def typedImport(imp: untpd.Import, sym: Symbol)(using Context): Import = {
2336+
def typedImport(imp: untpd.Import, sym: Symbol)(using Context): Import =
23322337
val expr1 = typedImportQualifier(imp, typedExpr)
23332338
checkLegalImportPath(expr1)
23342339
val selectors1 = typedSelectors(imp.selectors)
23352340
assignType(cpy.Import(imp)(expr1, selectors1), sym)
2336-
}
23372341

2338-
def typedExport(exp: untpd.Export)(using Context): Export = {
2342+
def typedExport(exp: untpd.Export)(using Context): Export =
23392343
val expr1 = typedExpr(exp.expr, AnySelectionProto)
23402344
// already called `checkLegalExportPath` in Namer
23412345
val selectors1 = typedSelectors(exp.selectors)
23422346
assignType(cpy.Export(exp)(expr1, selectors1))
2343-
}
23442347

23452348
def typedPackageDef(tree: untpd.PackageDef)(using Context): Tree =
23462349
val pid1 = withMode(Mode.InPackageClauseName)(typedExpr(tree.pid, AnySelectionProto))

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class CompilationTests {
6767

6868
aggregateTests(
6969
compileFile("tests/rewrites/rewrites.scala", scala2CompatMode.and("-rewrite", "-indent")),
70+
compileFile("tests/rewrites/rewrites3x.scala", defaultOptions.and("-rewrite", "-source", "3.1-migration")),
7071
compileFile("tests/rewrites/i8982.scala", defaultOptions.and("-indent", "-rewrite")),
7172
compileFile("tests/rewrites/i9632.scala", defaultOptions.and("-indent", "-rewrite"))
7273
).checkRewrites()

tests/pos/renaming-imports.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,21 @@ import c.mutable as mut
77
import mut.ArrayBuffer as Buf
88

99
val y = Buf(1, 2, 3)
10+
11+
object O:
12+
type OString = String
13+
def foo22(x: Int) = x
14+
15+
class C:
16+
import O.*
17+
import foo22 as foo
18+
import OString as OS
19+
import scala.collection.Iterable
20+
println(foo(22))
21+
val s: OS = ""
22+
23+
def test =
24+
import C as CC
25+
println(C().s)
26+
27+

0 commit comments

Comments
 (0)