Skip to content

Commit 1de473b

Browse files
committed
Disallow package names as types
Fixes scala#12432
1 parent f5bfed9 commit 1de473b

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
@@ -253,6 +253,10 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
253253
if tree.symbol.is(ConstructorProxy) then
254254
report.error(em"constructor proxy ${tree.symbol} cannot be used as a value", tree.srcPos)
255255

256+
def checkNotPackage(tree: Tree)(using Context): Unit =
257+
if tree.symbol.is(Package) then
258+
report.error(i"${tree.symbol} cannot be used as a type", tree.srcPos)
259+
256260
override def transform(tree: Tree)(using Context): Tree =
257261
try tree match {
258262
// TODO move CaseDef case lower: keep most probable trees first for performance
@@ -263,21 +267,25 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
263267
case None =>
264268
ctx
265269
super.transform(tree)(using gadtCtx)
266-
case tree: Ident if !tree.isType =>
267-
if tree.symbol.is(Inline) && !Inliner.inInlineMethod then
268-
ctx.compilationUnit.needsInlining = true
269-
checkNoConstructorProxy(tree)
270-
tree.tpe match {
271-
case tpe: ThisType => This(tpe.cls).withSpan(tree.span)
272-
case _ => tree
273-
}
270+
case tree: Ident =>
271+
if tree.isType then
272+
checkNotPackage(tree)
273+
tree
274+
else
275+
if tree.symbol.is(Inline) && !Inliner.inInlineMethod then
276+
ctx.compilationUnit.needsInlining = true
277+
checkNoConstructorProxy(tree)
278+
tree.tpe match {
279+
case tpe: ThisType => This(tpe.cls).withSpan(tree.span)
280+
case _ => tree
281+
}
274282
case tree @ Select(qual, name) =>
275283
if tree.symbol.is(Inline) then
276284
ctx.compilationUnit.needsInlining = true
277-
if (name.isTypeName) {
285+
if name.isTypeName then
286+
checkNotPackage(tree)
278287
Checking.checkRealizable(qual.tpe, qual.srcPos)
279288
withMode(Mode.Type)(super.transform(tree))
280-
}
281289
else
282290
checkNoConstructorProxy(tree)
283291
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)