Skip to content

Commit d2a174d

Browse files
committed
Merge pull request scala#4449 from retronym/ticket/9273
SI-9273 Avoid unpositioned error for bare classOf
2 parents 029cce7 + 4ff3abb commit d2a174d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4881,10 +4881,11 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
48814881
(// this -> Foo.this
48824882
if (sym.isThisSym)
48834883
typed1(This(sym.owner) setPos tree.pos, mode, pt)
4884-
// Inferring classOf type parameter from expected type. Otherwise an
4885-
// actual call to the stubbed classOf method is generated, returning null.
4886-
else if (isPredefClassOf(sym) && pt.typeSymbol == ClassClass && pt.typeArgs.nonEmpty)
4887-
typedClassOf(tree, TypeTree(pt.typeArgs.head))
4884+
else if (isPredefClassOf(sym) && pt.typeSymbol == ClassClass && pt.typeArgs.nonEmpty) {
4885+
// Inferring classOf type parameter from expected type. Otherwise an
4886+
// actual call to the stubbed classOf method is generated, returning null.
4887+
typedClassOf(tree, TypeTree(pt.typeArgs.head).setPos(tree.pos.focus))
4888+
}
48884889
else {
48894890
val pre1 = if (sym.isTopLevel) sym.owner.thisType else if (qual == EmptyTree) NoPrefix else qual.tpe
48904891
val tree1 = if (qual == EmptyTree) tree else atPos(tree.pos)(Select(atPos(tree.pos.focusStart)(qual), name))

test/files/neg/t9273.check

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
t9273.scala:2: error: class type required but ? found
2+
val foo: Class[_] = classOf // error without position, line or file
3+
^
4+
t9273.scala:3: error: not found: type X
5+
val foo1: Class[_] = classOf[X] // good error, all info contained
6+
^
7+
t9273.scala:7: error: not found: type X
8+
val foo4: Class[_] = Predef.classOf[X] // good error, all info contained
9+
^
10+
three errors found

test/files/neg/t9273.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class MissingLineNumbers {
2+
val foo: Class[_] = classOf // error without position, line or file
3+
val foo1: Class[_] = classOf[X] // good error, all info contained
4+
val foo2 = classOf // Infers T=Nothing
5+
6+
val foo3: Class[_] = Predef.classOf // Infers T=Nothing. Irregular wrt typedIdent.
7+
val foo4: Class[_] = Predef.classOf[X] // good error, all info contained
8+
val foo5 = Predef.classOf // Infers T=Nothing
9+
}

0 commit comments

Comments
 (0)