Skip to content

Commit 2317c0a

Browse files
committed
Disallow package names as types
Fixes #12432
1 parent 8d3083b commit 2317c0a

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
255255
if tree.symbol.is(ConstructorProxy) then
256256
report.error(em"constructor proxy ${tree.symbol} cannot be used as a value", tree.srcPos)
257257

258+
def checkNotPackage(tree: Tree)(using Context): Unit =
259+
if tree.symbol.is(Package) then
260+
report.error(i"${tree.symbol} cannot be used as a type", tree.srcPos)
261+
258262
override def transform(tree: Tree)(using Context): Tree =
259263
try tree match {
260264
// TODO move CaseDef case lower: keep most probable trees first for performance
@@ -265,21 +269,25 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
265269
case None =>
266270
ctx
267271
super.transform(tree)(using gadtCtx)
268-
case tree: Ident if !tree.isType =>
269-
if tree.symbol.is(Inline) && !Inliner.inInlineMethod then
270-
ctx.compilationUnit.needsInlining = true
271-
checkNoConstructorProxy(tree)
272-
tree.tpe match {
273-
case tpe: ThisType => This(tpe.cls).withSpan(tree.span)
274-
case _ => tree
275-
}
272+
case tree: Ident =>
273+
if tree.isType then
274+
checkNotPackage(tree)
275+
tree
276+
else
277+
if tree.symbol.is(Inline) && !Inliner.inInlineMethod then
278+
ctx.compilationUnit.needsInlining = true
279+
checkNoConstructorProxy(tree)
280+
tree.tpe match {
281+
case tpe: ThisType => This(tpe.cls).withSpan(tree.span)
282+
case _ => tree
283+
}
276284
case tree @ Select(qual, name) =>
277285
if tree.symbol.is(Inline) then
278286
ctx.compilationUnit.needsInlining = true
279-
if (name.isTypeName) {
287+
if name.isTypeName then
288+
checkNotPackage(tree)
280289
Checking.checkRealizable(qual.tpe, qual.srcPos)
281290
withMode(Mode.Type)(super.transform(tree))
282-
}
283291
else
284292
checkNoConstructorProxy(tree)
285293
transformSelect(tree, Nil)

tests/neg/i12432.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package dotty.tools.dotc.typer
2+
3+
class i1 ( i1 : annotation ) // error
4+
class i2 ( a : scala.annotation ) // error

0 commit comments

Comments
 (0)