Skip to content

Commit 48b9ef0

Browse files
oderskymichelou
authored andcommitted
Disallow package names as types
Fixes scala#12432
1 parent 6615913 commit 48b9ef0

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
@@ -263,6 +263,10 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
263263
case Select(qual, _) => check(qual) // simple select _n
264264
case Apply(TypeApply(Select(qual, _), _), _) => check(qual) // generic select .apply[T](n)
265265

266+
def checkNotPackage(tree: Tree)(using Context): Unit =
267+
if tree.symbol.is(Package) then
268+
report.error(i"${tree.symbol} cannot be used as a type", tree.srcPos)
269+
266270
override def transform(tree: Tree)(using Context): Tree =
267271
try tree match {
268272
// TODO move CaseDef case lower: keep most probable trees first for performance
@@ -273,21 +277,25 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
273277
case None =>
274278
ctx
275279
super.transform(tree)(using gadtCtx)
276-
case tree: Ident if !tree.isType =>
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-
}
280+
case tree: Ident =>
281+
if tree.isType then
282+
checkNotPackage(tree)
283+
tree
284+
else
285+
if tree.symbol.is(Inline) && !Inliner.inInlineMethod then
286+
ctx.compilationUnit.needsInlining = true
287+
checkNoConstructorProxy(tree)
288+
tree.tpe match {
289+
case tpe: ThisType => This(tpe.cls).withSpan(tree.span)
290+
case _ => tree
291+
}
284292
case tree @ Select(qual, name) =>
285293
if tree.symbol.is(Inline) then
286294
ctx.compilationUnit.needsInlining = true
287-
if (name.isTypeName) {
295+
if name.isTypeName then
296+
checkNotPackage(tree)
288297
Checking.checkRealizable(qual.tpe, qual.srcPos)
289298
withMode(Mode.Type)(super.transform(tree))
290-
}
291299
else
292300
checkNoConstructorProxy(tree)
293301
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)