Skip to content

Commit

Permalink
Introduce Context#isJava
Browse files Browse the repository at this point in the history
Safer to use than ctx.compilationUnit.isJava since compilationUnit is
currently nullable (and ends up being null at least in some of our tests).
  • Loading branch information
smarter committed Aug 21, 2020
1 parent 8f7df67 commit 173899b
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/Erasure.scala
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Checking.scala
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Expand Up @@ -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
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 173899b

Please sign in to comment.