Skip to content

Commit 173899b

Browse files
committed
Introduce Context#isJava
Safer to use than ctx.compilationUnit.isJava since compilationUnit is currently nullable (and ends up being null at least in some of our tests).
1 parent 8f7df67 commit 173899b

File tree

6 files changed

+14
-7
lines changed

6 files changed

+14
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,7 @@ object desugar {
16251625
Apply(Select(Apply(scalaDot(nme.StringContext), strs), id).withSpan(tree.span), elems)
16261626
case PostfixOp(t, op) =>
16271627
if ((ctx.mode is Mode.Type) && !isBackquoted(op) && op.name == tpnme.raw.STAR) {
1628-
if ctx.compilationUnit.isJava then
1628+
if ctx.isJava then
16291629
AppliedTypeTree(ref(defn.RepeatedParamType), t)
16301630
else
16311631
Annotated(

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,13 @@ object Contexts {
363363
/** Does current phase use an erased types interpretation? */
364364
final def erasedTypes = phase.erasedTypes
365365

366+
/** Are we in a Java compilation unit? */
367+
final def isJava: Boolean =
368+
// FIXME: It would be much nicer if compilationUnit was non-nullable,
369+
// perhaps we need to introduce a `NoCompilationUnit` compilation unit
370+
// to be used as a default value.
371+
compilationUnit != null && compilationUnit.isJava
372+
366373
/** Is current phase after FrontEnd? */
367374
final def isAfterTyper = base.isAfterTyper(phase)
368375

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ object Erasure {
542542
private def checkValue(tree: Tree)(using Context): Unit =
543543
val sym = tree.tpe.termSymbol
544544
if (sym is Flags.Package)
545-
|| (sym.isAllOf(Flags.JavaModule) && !ctx.compilationUnit.isJava)
545+
|| (sym.isAllOf(Flags.JavaModule) && !ctx.isJava)
546546
then
547547
report.error(JavaSymbolIsNotAValue(sym), tree.srcPos)
548548

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ trait Checking {
943943

944944
/** Check that `tpt` does not define a higher-kinded type */
945945
def checkSimpleKinded(tpt: Tree)(using Context): Tree =
946-
if (!tpt.tpe.hasSimpleKind && !ctx.compilationUnit.isJava)
946+
if (!tpt.tpe.hasSimpleKind && !ctx.isJava)
947947
// be more lenient with missing type params in Java,
948948
// needed to make pos/java-interop/t1196 work.
949949
errorTree(tpt, MissingTypeParameterFor(tpt.tpe))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ trait TypeAssigner {
110110
if (tpe.isError) tpe
111111
else errorType(ex"$whatCanNot be accessed as a member of $pre$where.$whyNot", pos)
112112
}
113-
else if ctx.compilationUnit != null && ctx.compilationUnit.isJava && tpe.isAnyRef then
113+
else if ctx.isJava && tpe.isAnyRef then
114114
defn.FromJavaObjectType
115115
else
116116
TypeOps.makePackageObjPrefixExplicit(tpe withDenot d)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ class Typer extends Namer
576576
val qual1 = typedType(tree.qualifier, selectionProto(tree.name, pt, this))
577577
assignType(cpy.Select(tree)(qual1, tree.name), qual1)
578578
}
579-
else if (ctx.compilationUnit.isJava && tree.name.isTypeName)
579+
else if (ctx.isJava && tree.name.isTypeName)
580580
// SI-3120 Java uses the same syntax, A.B, to express selection from the
581581
// value A and from the type A. We have to try both.
582582
selectWithFallback(tryJavaSelectOnType) // !!! possibly exponential bcs of qualifier retyping
@@ -1760,7 +1760,7 @@ class Typer extends Namer
17601760
else if (tpt1.symbol == defn.orType)
17611761
checkedArgs = checkedArgs.mapconserve(arg =>
17621762
checkSimpleKinded(checkNoWildcard(arg)))
1763-
else if (ctx.compilationUnit.isJava)
1763+
else if (ctx.isJava)
17641764
if (tpt1.symbol eq defn.ArrayClass) then
17651765
checkedArgs match {
17661766
case List(arg) =>
@@ -3524,7 +3524,7 @@ class Typer extends Namer
35243524
if ((pt eq AnyTypeConstructorProto) || tp.typeParamSymbols.isEmpty) tree
35253525
else {
35263526
val tp1 =
3527-
if (ctx.compilationUnit.isJava)
3527+
if (ctx.isJava)
35283528
// Cook raw type
35293529
AppliedType(tree.tpe, tp.typeParams.map(Function.const(TypeBounds.empty)))
35303530
else

0 commit comments

Comments
 (0)