diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index 321639456124..fdf49447713f 100644 --- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala +++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala @@ -1625,7 +1625,7 @@ object desugar { Apply(Select(Apply(scalaDot(nme.StringContext), strs), id).withSpan(tree.span), elems) case PostfixOp(t, op) => if ((ctx.mode is Mode.Type) && !isBackquoted(op) && op.name == tpnme.raw.STAR) { - if ctx.compilationUnit.isJava then + if ctx.isJava then AppliedTypeTree(ref(defn.RepeatedParamType), t) else Annotated( diff --git a/compiler/src/dotty/tools/dotc/core/Contexts.scala b/compiler/src/dotty/tools/dotc/core/Contexts.scala index ce2b2fa508e7..95875bfedcc1 100644 --- a/compiler/src/dotty/tools/dotc/core/Contexts.scala +++ b/compiler/src/dotty/tools/dotc/core/Contexts.scala @@ -363,6 +363,13 @@ object Contexts { /** Does current phase use an erased types interpretation? */ final def erasedTypes = phase.erasedTypes + /** Are we in a Java compilation unit? */ + final def isJava: Boolean = + // FIXME: It would be much nicer if compilationUnit was non-nullable, + // perhaps we need to introduce a `NoCompilationUnit` compilation unit + // to be used as a default value. + compilationUnit != null && compilationUnit.isJava + /** Is current phase after FrontEnd? */ final def isAfterTyper = base.isAfterTyper(phase) diff --git a/compiler/src/dotty/tools/dotc/transform/Erasure.scala b/compiler/src/dotty/tools/dotc/transform/Erasure.scala index 5516346dc09f..fe215b369d5f 100644 --- a/compiler/src/dotty/tools/dotc/transform/Erasure.scala +++ b/compiler/src/dotty/tools/dotc/transform/Erasure.scala @@ -542,7 +542,7 @@ object Erasure { private def checkValue(tree: Tree)(using Context): Unit = val sym = tree.tpe.termSymbol if (sym is Flags.Package) - || (sym.isAllOf(Flags.JavaModule) && !ctx.compilationUnit.isJava) + || (sym.isAllOf(Flags.JavaModule) && !ctx.isJava) then report.error(JavaSymbolIsNotAValue(sym), tree.srcPos) diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 2b8987103556..a81386ec2209 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -943,7 +943,7 @@ trait Checking { /** Check that `tpt` does not define a higher-kinded type */ def checkSimpleKinded(tpt: Tree)(using Context): Tree = - if (!tpt.tpe.hasSimpleKind && !ctx.compilationUnit.isJava) + if (!tpt.tpe.hasSimpleKind && !ctx.isJava) // be more lenient with missing type params in Java, // needed to make pos/java-interop/t1196 work. errorTree(tpt, MissingTypeParameterFor(tpt.tpe)) diff --git a/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala b/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala index 071378013d58..144ca853c056 100644 --- a/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala +++ b/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala @@ -110,7 +110,7 @@ trait TypeAssigner { if (tpe.isError) tpe else errorType(ex"$whatCanNot be accessed as a member of $pre$where.$whyNot", pos) } - else if ctx.compilationUnit != null && ctx.compilationUnit.isJava && tpe.isAnyRef then + else if ctx.isJava && tpe.isAnyRef then defn.FromJavaObjectType else TypeOps.makePackageObjPrefixExplicit(tpe withDenot d) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 31e8fac005dc..adfd6713abe6 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -576,7 +576,7 @@ class Typer extends Namer val qual1 = typedType(tree.qualifier, selectionProto(tree.name, pt, this)) assignType(cpy.Select(tree)(qual1, tree.name), qual1) } - else if (ctx.compilationUnit.isJava && tree.name.isTypeName) + else if (ctx.isJava && tree.name.isTypeName) // SI-3120 Java uses the same syntax, A.B, to express selection from the // value A and from the type A. We have to try both. selectWithFallback(tryJavaSelectOnType) // !!! possibly exponential bcs of qualifier retyping @@ -1760,7 +1760,7 @@ class Typer extends Namer else if (tpt1.symbol == defn.orType) checkedArgs = checkedArgs.mapconserve(arg => checkSimpleKinded(checkNoWildcard(arg))) - else if (ctx.compilationUnit.isJava) + else if (ctx.isJava) if (tpt1.symbol eq defn.ArrayClass) then checkedArgs match { case List(arg) => @@ -3524,7 +3524,7 @@ class Typer extends Namer if ((pt eq AnyTypeConstructorProto) || tp.typeParamSymbols.isEmpty) tree else { val tp1 = - if (ctx.compilationUnit.isJava) + if (ctx.isJava) // Cook raw type AppliedType(tree.tpe, tp.typeParams.map(Function.const(TypeBounds.empty))) else