From a8136fff2cc2f7c75f0a6253db79c3e339f56c98 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 26 Aug 2016 10:07:19 +0200 Subject: Fix #1286: Error on inexistent imports that are not used. This commit also fixes #1583. --- src/dotty/tools/dotc/transform/PostTyper.scala | 13 +++++++++++++ tests/neg/i1286.scala | 16 ++++++++++++++++ tests/repl/imports.check | 10 ++++++++++ 3 files changed, 39 insertions(+) create mode 100644 tests/neg/i1286.scala diff --git a/src/dotty/tools/dotc/transform/PostTyper.scala b/src/dotty/tools/dotc/transform/PostTyper.scala index 51851a589..12d48d98e 100644 --- a/src/dotty/tools/dotc/transform/PostTyper.scala +++ b/src/dotty/tools/dotc/transform/PostTyper.scala @@ -275,6 +275,19 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisTran case tpe => tpe } ) + case Import(expr, selectors) => + val exprTpe = expr.tpe + def checkIdent(ident: Ident): Unit = { + val name = ident.name.asTermName.encode + if (name != nme.WILDCARD && !exprTpe.member(name).exists && !exprTpe.member(name.toTypeName).exists) + ctx.error(s"${ident.name} is not a member of ${expr.show}", ident.pos) + } + selectors.foreach { + case ident: Ident => checkIdent(ident) + case Thicket((ident: Ident) :: _) => checkIdent(ident) + case _ => + } + super.transform(tree) case tree => super.transform(tree) } diff --git a/tests/neg/i1286.scala b/tests/neg/i1286.scala new file mode 100644 index 000000000..40db9ab1d --- /dev/null +++ b/tests/neg/i1286.scala @@ -0,0 +1,16 @@ +import scala.idontexist // error +import scala.io.Idontexist // error + +import scala.io +import io.Idontexist2 // error + +import scala.io.{ AnsiColor, Idontexist3 } // error + +import scala.io.{ Idontexist4 => Foo } // error +import scala.io.{ Idontexist5 => _ } // error + +import scala.language.dynamics +import scala.language.noAutoTupling +import scala.language.idontexist // error + +object Test diff --git a/tests/repl/imports.check b/tests/repl/imports.check index 26b725637..b6d9ae8a7 100644 --- a/tests/repl/imports.check +++ b/tests/repl/imports.check @@ -15,4 +15,14 @@ scala> buf += xs | scala> buf ++= xs res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3) +scala> import util.foo +-- Error: ---------------------------------------------------------------------------------------------------- +8 |import util.foo + | ^^^ + | foo is not a member of util +scala> import util.foo.bar +-- [E008] Member Not Found Error: ---------------------------------------------------------------------------- +8 |import util.foo.bar + | ^^^^^^^^ + | value `foo` is not a member of util.type - did you mean `util.Left`? scala> :quit -- cgit v1.2.3