Skip to content

Commit 7c589a3

Browse files
authored
Merge pull request #11431 from dotty-staging/fix-11430
Move checkValue to Typer
2 parents e79ca04 + 101e981 commit 7c589a3

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
@@ -500,7 +500,7 @@ class Typer extends Namer
500500
case _ =>
501501
tree.withType(ownType)
502502
val tree2 = toNotNullTermRef(tree1, pt)
503-
checkStableIdentPattern(tree2, pt)
503+
checkLegalValue(tree2, pt)
504504
tree2
505505

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

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

23502353
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)