Skip to content

Commit 70c6d1f

Browse files
authored
Merge pull request #14888 from dotty-staging/fix-12432
Disallow package names as types
2 parents f3cca47 + 02a8b70 commit 70c6d1f

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import printing.Printer
1111
import printing.Texts.Text
1212
import util.{Stats, Attachment, Property, SourceFile, NoSource, SrcPos, SourcePosition}
1313
import config.Config
14+
import config.Printers.overload
1415
import annotation.internal.sharable
1516
import annotation.unchecked.uncheckedVariance
1617
import annotation.constructorOnly
@@ -1729,7 +1730,9 @@ object Trees {
17291730
val typer = ctx.typer
17301731
val proto = FunProto(args, expectedType)
17311732
val denot = receiver.tpe.member(method)
1732-
assert(denot.exists, i"no member $receiver . $method, members = ${receiver.tpe.decls}")
1733+
if !denot.exists then
1734+
overload.println(i"members = ${receiver.tpe.decls}")
1735+
report.error(i"no member $receiver . $method", receiver.srcPos)
17331736
val selected =
17341737
if (denot.isOverloaded) {
17351738
def typeParamCount(tp: Type) = tp.widen match {

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import core._
77
import dotty.tools.dotc.typer.Checking
88
import dotty.tools.dotc.typer.Inliner
99
import dotty.tools.dotc.typer.VarianceChecker
10+
import typer.ErrorReporting.errorTree
1011
import Types._, Contexts._, Names._, Flags._, DenotTransformers._, Phases._
1112
import SymDenotations._, StdNames._, Annotations._, Trees._, Scopes._
1213
import Decorators._
@@ -263,6 +264,10 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
263264
case Select(qual, _) => check(qual) // simple select _n
264265
case Apply(TypeApply(Select(qual, _), _), _) => check(qual) // generic select .apply[T](n)
265266

267+
def checkNotPackage(tree: Tree)(using Context): Tree =
268+
if !tree.symbol.is(Package) then tree
269+
else errorTree(tree, i"${tree.symbol} cannot be used as a type")
270+
266271
override def transform(tree: Tree)(using Context): Tree =
267272
try tree match {
268273
// TODO move CaseDef case lower: keep most probable trees first for performance
@@ -273,21 +278,23 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
273278
case None =>
274279
ctx
275280
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-
}
281+
case tree: Ident =>
282+
if tree.isType then
283+
checkNotPackage(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
288296
Checking.checkRealizable(qual.tpe, qual.srcPos)
289-
withMode(Mode.Type)(super.transform(tree))
290-
}
297+
withMode(Mode.Type)(super.transform(checkNotPackage(tree)))
291298
else
292299
checkNoConstructorProxy(tree)
293300
transformSelect(tree, Nil)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
262262
// compare primitive fields first, slow equality checks of non-primitive fields can be skipped when primitives differ
263263
val sortedAccessors = accessors.sortBy(accessor => if (accessor.info.typeSymbol.isPrimitiveValueClass) 0 else 1)
264264
val comparisons = sortedAccessors.map { accessor =>
265-
This(clazz).select(accessor).equal(ref(thatAsClazz).select(accessor)) }
265+
This(clazz).withSpan(ctx.owner.span.focus).select(accessor).equal(ref(thatAsClazz).select(accessor)) }
266266
var rhs = // this.x == this$0.x && this.y == x$0.y && that.canEqual(this)
267267
if comparisons.isEmpty then Literal(Constant(true)) else comparisons.reduceLeft(_ and _)
268268
val canEqualMeth = existingDef(defn.Product_canEqual, clazz)

tests/neg/i12432.scala

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

0 commit comments

Comments
 (0)