From cc4b45e238c92c64e473a4d71b0c3474f75fa6da Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 12 May 2021 10:26:39 +0200 Subject: [PATCH] Disallow attempts to instantiate packages Fixes #12430 --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 3 +++ tests/neg/i12430.scala | 4 ++++ 2 files changed, 7 insertions(+) create mode 100644 tests/neg/i12430.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 0c7867a03bdc..597afa096cce 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -778,6 +778,9 @@ class Typer extends Namer typed(cpy.Block(tree)(clsDef :: Nil, New(Ident(x), Nil)), pt) case _ => var tpt1 = typedType(tree.tpt) + val tsym = tpt1.tpe.underlyingClassRef(refinementOK = false).typeSymbol + if tsym.is(Package) then + report error(em"$tsym cannot be instantiated", tpt1.srcPos) tpt1 = tpt1.withType(ensureAccessible(tpt1.tpe, superAccess = false, tpt1.srcPos)) tpt1 match { case AppliedTypeTree(_, targs) => diff --git a/tests/neg/i12430.scala b/tests/neg/i12430.scala new file mode 100644 index 000000000000..075c3bba4f90 --- /dev/null +++ b/tests/neg/i12430.scala @@ -0,0 +1,4 @@ +class i0 { + new collection // error + new io // error +} \ No newline at end of file