Skip to content

Commit a8136ff

Browse files
committed
Fix #1286: Error on inexistent imports that are not used.
This commit also fixes #1583.
1 parent 6070cce commit a8136ff

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/dotty/tools/dotc/transform/PostTyper.scala

+13
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,19 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisTran
275275
case tpe => tpe
276276
}
277277
)
278+
case Import(expr, selectors) =>
279+
val exprTpe = expr.tpe
280+
def checkIdent(ident: Ident): Unit = {
281+
val name = ident.name.asTermName.encode
282+
if (name != nme.WILDCARD && !exprTpe.member(name).exists && !exprTpe.member(name.toTypeName).exists)
283+
ctx.error(s"${ident.name} is not a member of ${expr.show}", ident.pos)
284+
}
285+
selectors.foreach {
286+
case ident: Ident => checkIdent(ident)
287+
case Thicket((ident: Ident) :: _) => checkIdent(ident)
288+
case _ =>
289+
}
290+
super.transform(tree)
278291
case tree =>
279292
super.transform(tree)
280293
}

tests/neg/i1286.scala

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import scala.idontexist // error
2+
import scala.io.Idontexist // error
3+
4+
import scala.io
5+
import io.Idontexist2 // error
6+
7+
import scala.io.{ AnsiColor, Idontexist3 } // error
8+
9+
import scala.io.{ Idontexist4 => Foo } // error
10+
import scala.io.{ Idontexist5 => _ } // error
11+
12+
import scala.language.dynamics
13+
import scala.language.noAutoTupling
14+
import scala.language.idontexist // error
15+
16+
object Test

tests/repl/imports.check

+10
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,14 @@ scala> buf += xs
1515
|
1616
scala> buf ++= xs
1717
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
18+
scala> import util.foo
19+
-- Error: <console> ----------------------------------------------------------------------------------------------------
20+
8 |import util.foo
21+
| ^^^
22+
| foo is not a member of util
23+
scala> import util.foo.bar
24+
-- [E008] Member Not Found Error: <console> ----------------------------------------------------------------------------
25+
8 |import util.foo.bar
26+
| ^^^^^^^^
27+
| value `foo` is not a member of util.type - did you mean `util.Left`?
1828
scala> :quit

0 commit comments

Comments
 (0)