Skip to content

Commit cc4b45e

Browse files
committed
Disallow attempts to instantiate packages
Fixes #12430
1 parent f5bfed9 commit cc4b45e

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,9 @@ class Typer extends Namer
778778
typed(cpy.Block(tree)(clsDef :: Nil, New(Ident(x), Nil)), pt)
779779
case _ =>
780780
var tpt1 = typedType(tree.tpt)
781+
val tsym = tpt1.tpe.underlyingClassRef(refinementOK = false).typeSymbol
782+
if tsym.is(Package) then
783+
report error(em"$tsym cannot be instantiated", tpt1.srcPos)
781784
tpt1 = tpt1.withType(ensureAccessible(tpt1.tpe, superAccess = false, tpt1.srcPos))
782785
tpt1 match {
783786
case AppliedTypeTree(_, targs) =>

tests/neg/i12430.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class i0 {
2+
new collection // error
3+
new io // error
4+
}

0 commit comments

Comments
 (0)