Skip to content

Commit f2020e1

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 cd5805c commit f2020e1

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
@@ -2343,29 +2343,32 @@ class Typer extends Namer
23432343
if imp.expr == untpd.EmptyTree then
23442344
assert(imp.selectors.length == 1, imp)
23452345
val from = imp.selectors.head.imported
2346-
val sel = typd(from, WildcardType)
2347-
sel.tpe.dealias match
2348-
case TermRef(prefix: SingletonType, _) =>
2346+
val sel = tryAlternatively
2347+
(typedIdent(from, WildcardType))
2348+
(typedIdent(cpy.Ident(from)(from.name.toTypeName), WildcardType))
2349+
2350+
sel.tpe match
2351+
case TermRef(prefix: SingletonType, _) =>
2352+
singleton(prefix).withSpan(from.span)
2353+
case TypeRef(prefix: SingletonType, _) =>
23492354
singleton(prefix).withSpan(from.span)
23502355
case _ =>
23512356
errorTree(from,
23522357
em"""Illegal import selector: $from
23532358
|The selector is not a member of an object or package.""")
23542359
else typd(imp.expr, AnySelectionProto)
23552360

2356-
def typedImport(imp: untpd.Import, sym: Symbol)(using Context): Import = {
2361+
def typedImport(imp: untpd.Import, sym: Symbol)(using Context): Import =
23572362
val expr1 = typedImportQualifier(imp, typedExpr)
23582363
checkLegalImportPath(expr1)
23592364
val selectors1 = typedSelectors(imp.selectors)
23602365
assignType(cpy.Import(imp)(expr1, selectors1), sym)
2361-
}
23622366

2363-
def typedExport(exp: untpd.Export)(using Context): Export = {
2367+
def typedExport(exp: untpd.Export)(using Context): Export =
23642368
val expr1 = typedExpr(exp.expr, AnySelectionProto)
23652369
// already called `checkLegalExportPath` in Namer
23662370
val selectors1 = typedSelectors(exp.selectors)
23672371
assignType(cpy.Export(exp)(expr1, selectors1))
2368-
}
23692372

23702373
def typedPackageDef(tree: untpd.PackageDef)(using Context): Tree =
23712374
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)