Skip to content

Commit 101e981

Browse files
committed
Move checkValue to Typer
Move the check to Typer so that we get better error messages, and don't fail subsequent assertions. Fixes #11430
1 parent 0273336 commit 101e981

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import typer.{NoChecking, LiftErased}
2121
import typer.Inliner
2222
import typer.ProtoTypes._
2323
import typer.ErrorReporting.errorTree
24+
import typer.Checking.checkValue
2425
import core.TypeErasure._
2526
import core.Decorators._
2627
import dotty.tools.dotc.ast.{tpd, untpd}
@@ -569,18 +570,6 @@ object Erasure {
569570

570571
/** Check that Java statics and packages can only be used in selections.
571572
*/
572-
private def checkValue(tree: Tree, proto: Type)(using Context): tree.type =
573-
if (!proto.isInstanceOf[SelectionProto] && !proto.isInstanceOf[ApplyingProto]) then
574-
checkValue(tree)
575-
tree
576-
577-
private def checkValue(tree: Tree)(using Context): Unit =
578-
val sym = tree.tpe.termSymbol
579-
if (sym is Flags.Package)
580-
|| (sym.isAllOf(Flags.JavaModule) && !ctx.isJava)
581-
then
582-
report.error(JavaSymbolIsNotAValue(sym), tree.srcPos)
583-
584573
private def checkNotErased(tree: Tree)(using Context): tree.type = {
585574
if (!ctx.mode.is(Mode.Type)) {
586575
if (isErased(tree))
@@ -644,7 +633,7 @@ object Erasure {
644633
super.typedLiteral(tree)
645634

646635
override def typedIdent(tree: untpd.Ident, pt: Type)(using Context): Tree =
647-
checkValue(checkNotErased(super.typedIdent(tree, pt)), pt)
636+
checkNotErased(super.typedIdent(tree, pt))
648637

649638
/** Type check select nodes, applying the following rewritings exhaustively
650639
* on selections `e.m`, where `OT` is the type of the owner of `m` and `ET`
@@ -772,7 +761,7 @@ object Erasure {
772761
}
773762
}
774763

775-
checkValue(checkNotErased(recur(qual1)), pt)
764+
checkNotErased(recur(qual1))
776765
}
777766

778767
override def typedThis(tree: untpd.This)(using Context): Tree =

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,21 @@ object Checking {
653653
|| to.isRef(defn.ObjectClass, skipRefined = false)
654654
then
655655
report.error(em"the result of an implicit conversion must be more specific than $to", pos)
656+
657+
def checkValue(tree: Tree)(using Context): Unit =
658+
val sym = tree.tpe.termSymbol
659+
if sym.is(Flags.Package) || sym.isAllOf(Flags.JavaModule) && !ctx.isJava then
660+
report.error(JavaSymbolIsNotAValue(sym), tree.srcPos)
661+
662+
def checkValue(tree: Tree, proto: Type)(using Context): tree.type =
663+
tree match
664+
case tree: RefTree
665+
if tree.name.isTermName
666+
&& !proto.isInstanceOf[SelectionProto]
667+
&& !proto.isInstanceOf[FunOrPolyProto] =>
668+
checkValue(tree)
669+
case _ =>
670+
tree
656671
}
657672

658673
trait Checking {

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ class Typer extends Namer
499499
case _ =>
500500
tree.withType(ownType)
501501
val tree2 = toNotNullTermRef(tree1, pt)
502-
checkStableIdentPattern(tree2, pt)
502+
checkLegalValue(tree2, pt)
503503
tree2
504504

505505
def isLocalExtensionMethodRef: Boolean = rawType match
@@ -537,9 +537,12 @@ class Typer extends Namer
537537
errorTree(tree, MissingIdent(tree, kind, name))
538538
end typedIdent
539539

540-
/** Check that a stable identifier pattern is indeed stable (SLS 8.1.5)
540+
/** (1) If this reference is neither applied nor selected, check that it does
541+
* not refer to a package or Java companion object.
542+
* (2) Check that a stable identifier pattern is indeed stable (SLS 8.1.5)
541543
*/
542-
private def checkStableIdentPattern(tree: Tree, pt: Type)(using Context): Unit =
544+
private def checkLegalValue(tree: Tree, pt: Type)(using Context): Unit =
545+
checkValue(tree, pt)
543546
if ctx.mode.is(Mode.Pattern)
544547
&& !tree.isType
545548
&& !pt.isInstanceOf[ApplyingProto]
@@ -557,7 +560,7 @@ class Typer extends Namer
557560
if checkedType.exists then
558561
val select = toNotNullTermRef(assignType(tree, checkedType), pt)
559562
if selName.isTypeName then checkStable(qual.tpe, qual.srcPos, "type prefix")
560-
checkStableIdentPattern(select, pt)
563+
checkLegalValue(select, pt)
561564
ConstFold(select)
562565
else if couldInstantiateTypeVar(qual.tpe.widen) then
563566
// try again with more defined qualifier type
@@ -2343,7 +2346,7 @@ class Typer extends Namer
23432346
assert(imp.selectors.length == 1, imp)
23442347
val from = imp.selectors.head.imported
23452348
val sel = tryAlternatively
2346-
(typedIdent(from, WildcardType))
2349+
(typedIdent(from, AnySelectionProto))
23472350
(typedIdent(cpy.Ident(from)(from.name.toTypeName), WildcardType))
23482351

23492352
sel.tpe match

tests/neg/i11430.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def a(o: String) = Seq(o, org) // error: package org is not a value
2+
def a2(o: String) = Seq[String](o, org) // error: package org is not a value
3+
def a3(o: String): Seq[String] = Seq(o, org) // error: package org is not a value
4+
def a4(o: String): Seq[String] = Seq(o, org) // error: package org is not a value

0 commit comments

Comments
 (0)